UpgradeCommand.cs (10491B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="UpgradeCommand.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace AppInstallerCLIE2ETests 8 { 9 using System.IO; 10 using AppInstallerCLIE2ETests.Helpers; 11 using NUnit.Framework; 12 13 /// <summary> 14 /// Test upgrade command. 15 /// </summary> 16 public class UpgradeCommand : BaseCommand 17 { 18 private static readonly string DenyUpgradePackage = "AppInstallerTest.TestUpgradeDeny"; 19 20 /// <summary> 21 /// Tear down. 22 /// </summary> 23 [TearDown] 24 public void TearDown() 25 { 26 // Due to its properties, this being present is problematic. 27 TestCommon.RunAICLICommand("uninstall", DenyUpgradePackage); 28 } 29 30 /// <summary> 31 /// Test upgrade portable package. 32 /// </summary> 33 [Test] 34 public void UpgradePortable() 35 { 36 string installDir = TestCommon.GetPortablePackagesDirectory(); 37 string packageId, commandAlias, fileName, packageDirName, productCode; 38 packageId = "AppInstallerTest.TestPortableExe"; 39 packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier; 40 commandAlias = fileName = "AppInstallerTestExeInstaller.exe"; 41 42 var result = TestCommon.RunAICLICommand("install", "AppInstallerTest.TestPortableExe -v 1.0.0.0"); 43 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 44 Assert.True(result.StdOut.Contains("Successfully installed")); 45 46 var result2 = TestCommon.RunAICLICommand("upgrade", $"{packageId} -v 2.0.0.0"); 47 Assert.AreEqual(Constants.ErrorCode.S_OK, result2.ExitCode); 48 Assert.True(result2.StdOut.Contains("Successfully installed")); 49 TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, true); 50 } 51 52 /// <summary> 53 /// Test upgrade portable package with arp mismatch. 54 /// </summary> 55 [Test] 56 public void UpgradePortableARPMismatch() 57 { 58 string packageId = "AppInstallerTest.TestPortableExe"; 59 string productCode = packageId + "_" + Constants.TestSourceIdentifier; 60 61 var installResult = TestCommon.RunAICLICommand("install", "AppInstallerTest.TestPortableExe -v 1.0.0.0"); 62 Assert.AreEqual(Constants.ErrorCode.S_OK, installResult.ExitCode); 63 Assert.True(installResult.StdOut.Contains("Successfully installed")); 64 65 // Modify packageId to cause mismatch. 66 TestCommon.ModifyPortableARPEntryValue(productCode, Constants.WinGetPackageIdentifier, "testPackageId"); 67 68 var upgradeResult = TestCommon.RunAICLICommand("upgrade", $"{packageId} -v 2.0.0.0"); 69 70 // Reset and perform uninstall cleanup 71 TestCommon.ModifyPortableARPEntryValue(productCode, Constants.WinGetPackageIdentifier, packageId); 72 TestCommon.RunAICLICommand("uninstall", $"--product-code {productCode}"); 73 74 Assert.AreNotEqual(Constants.ErrorCode.S_OK, upgradeResult.ExitCode); 75 Assert.True(upgradeResult.StdOut.Contains("Portable package from a different source already exists")); 76 } 77 78 /// <summary> 79 /// Test upgrade portable package force override. 80 /// </summary> 81 [Test] 82 public void UpgradePortableForcedOverride() 83 { 84 string installDir = TestCommon.GetPortablePackagesDirectory(); 85 string packageId, commandAlias, fileName, packageDirName, productCode; 86 packageId = "AppInstallerTest.TestPortableExe"; 87 packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier; 88 commandAlias = fileName = "AppInstallerTestExeInstaller.exe"; 89 90 var installResult = TestCommon.RunAICLICommand("install", "AppInstallerTest.TestPortableExe -v 1.0.0.0"); 91 Assert.AreEqual(Constants.ErrorCode.S_OK, installResult.ExitCode); 92 Assert.True(installResult.StdOut.Contains("Successfully installed")); 93 94 // Modify packageId and sourceId to cause mismatch. 95 TestCommon.ModifyPortableARPEntryValue(productCode, Constants.WinGetPackageIdentifier, "testPackageId"); 96 TestCommon.ModifyPortableARPEntryValue(productCode, Constants.WinGetSourceIdentifier, "testSourceId"); 97 98 var upgradeResult = TestCommon.RunAICLICommand("upgrade", $"{packageId} -v 2.0.0.0 --force"); 99 Assert.AreEqual(Constants.ErrorCode.S_OK, upgradeResult.ExitCode); 100 Assert.True(upgradeResult.StdOut.Contains("Successfully installed")); 101 TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, true); 102 } 103 104 /// <summary> 105 /// Test upgrade portable package uninstall previous version. 106 /// </summary> 107 [Test] 108 public void UpgradePortableUninstallPrevious() 109 { 110 string installDir = TestCommon.GetPortablePackagesDirectory(); 111 string packageId, commandAlias, fileName, packageDirName, productCode; 112 packageId = "AppInstallerTest.TestPortableExe"; 113 packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier; 114 commandAlias = fileName = "AppInstallerTestExeInstaller.exe"; 115 116 var result = TestCommon.RunAICLICommand("install", $"{packageId} -v 1.0.0.0"); 117 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 118 Assert.True(result.StdOut.Contains("Successfully installed")); 119 120 var result2 = TestCommon.RunAICLICommand("upgrade", $"{packageId} -v 3.0.0.0"); 121 Assert.AreEqual(Constants.ErrorCode.S_OK, result2.ExitCode); 122 Assert.True(result2.StdOut.Contains("Successfully installed")); 123 TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, true); 124 } 125 126 /// <summary> 127 /// Test upgrade with deny behavior. 128 /// </summary> 129 [Test] 130 public void UpgradeBehaviorDeny() 131 { 132 string packageId = DenyUpgradePackage; 133 134 var result = TestCommon.RunAICLICommand("install", $"{packageId} -v 1.0.0.0"); 135 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 136 Assert.True(result.StdOut.Contains("Successfully installed")); 137 138 var result2 = TestCommon.RunAICLICommand("upgrade", $"{packageId} -v 2.0.0.0"); 139 Assert.AreEqual(Constants.ErrorCode.APPINSTALLER_CLI_ERROR_INSTALL_UPGRADE_NOT_SUPPORTED, result2.ExitCode); 140 Assert.True(result2.StdOut.Contains("package cannot be upgraded using winget")); 141 } 142 143 /// <summary> 144 /// Test upgrade portable package machine scope. 145 /// </summary> 146 [Test] 147 public void UpgradePortableMachineScope() 148 { 149 string installDir = TestCommon.GetRandomTestDir(); 150 WinGetSettingsHelper.ConfigureInstallBehavior(Constants.PortablePackageMachineRoot, installDir); 151 152 string packageId, commandAlias, fileName, packageDirName, productCode; 153 packageId = "AppInstallerTest.TestPortableExe"; 154 packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier; 155 commandAlias = fileName = "AppInstallerTestExeInstaller.exe"; 156 157 var result = TestCommon.RunAICLICommand("install", $"{packageId} -v 1.0.0.0 --scope machine"); 158 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 159 Assert.True(result.StdOut.Contains("Successfully installed")); 160 161 var result2 = TestCommon.RunAICLICommand("upgrade", $"{packageId} -v 2.0.0.0"); 162 WinGetSettingsHelper.ConfigureInstallBehavior(Constants.PortablePackageMachineRoot, string.Empty); 163 Assert.AreEqual(Constants.ErrorCode.S_OK, result2.ExitCode); 164 Assert.True(result2.StdOut.Contains("Successfully installed")); 165 TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, true, TestCommon.Scope.Machine); 166 } 167 168 /// <summary> 169 /// Test upgrade zip portable package. 170 /// </summary> 171 [Test] 172 public void UpgradeZip_Portable() 173 { 174 string installDir = TestCommon.GetPortablePackagesDirectory(); 175 string packageId, commandAlias, fileName, packageDirName, productCode; 176 packageId = "AppInstallerTest.TestZipInstallerWithPortable"; 177 packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier; 178 commandAlias = "TestPortable.exe"; 179 fileName = "AppInstallerTestExeInstaller.exe"; 180 181 var result = TestCommon.RunAICLICommand("install", $"AppInstallerTest.TestZipInstallerWithPortable -v 1.0.0.0"); 182 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 183 Assert.True(result.StdOut.Contains("Successfully installed")); 184 185 var result2 = TestCommon.RunAICLICommand("upgrade", $"{packageId} -v 2.0.0.0"); 186 Assert.AreEqual(Constants.ErrorCode.S_OK, result2.ExitCode); 187 Assert.True(result2.StdOut.Contains("Successfully installed")); 188 TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, true, TestCommon.Scope.User); 189 } 190 191 /// <summary> 192 /// Test upgrade when a new dependency is added that is not installed. 193 /// </summary> 194 [Test] 195 public void UpgradeAddsDependency() 196 { 197 var result = TestCommon.RunAICLICommand("install", $"AppInstallerTest.TestUpgradeAddsDependency -v 1.0 --verbose"); 198 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 199 200 result = TestCommon.RunAICLICommand("upgrade", $"AppInstallerTest.TestUpgradeAddsDependency"); 201 Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode); 202 } 203 } 204 }