winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

UninstallCommand.cs (11013B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="UninstallCommand.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 uninstall command.
     15     /// </summary>
     16     public class UninstallCommand : BaseCommand
     17     {
     18         // Custom product code for overriding the default in the test exe
     19         private const string CustomProductCode = "{f08fc03c-0b7e-4fca-9b3c-3a384d18a9f3}";
     20 
     21         // File written when uninstalling the test exe
     22         private const string UninstallTestExeUninstalledFile = "TestExeUninstalled.txt";
     23 
     24         // Name of a file installed by the MSI that will be removed during uninstall
     25         private const string UninstallTestMsiInstalledFile = "AppInstallerTestExeInstaller.exe";
     26 
     27         // Package name of the test MSIX package
     28         private const string UninstallTestMsixName = "6c6338fe-41b7-46ca-8ba6-b5ad5312bb0e";
     29 
     30         /// <summary>
     31         /// Test uninstall exe.
     32         /// </summary>
     33         [Test]
     34         public void UninstallTestExe()
     35         {
     36             // Uninstall an Exe
     37             var installDir = TestCommon.GetRandomTestDir();
     38             TestCommon.RunAICLICommand("install", $"{Constants.ExeInstallerPackageId} --silent -l {installDir}");
     39             var result = TestCommon.RunAICLICommand("uninstall", Constants.ExeInstallerPackageId);
     40             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
     41             Assert.True(result.StdOut.Contains("Successfully uninstalled"));
     42             Assert.True(TestCommon.VerifyTestExeUninstalled(installDir));
     43         }
     44 
     45         /// <summary>
     46         /// Test uninstall msi.
     47         /// </summary>
     48         [Test]
     49         public void UninstallTestMsi()
     50         {
     51             if (string.IsNullOrEmpty(TestIndex.MsiInstaller))
     52             {
     53                 Assert.Ignore("MSI installer not available");
     54             }
     55 
     56             // Uninstall an MSI
     57             var installDir = TestCommon.GetRandomTestDir();
     58             TestCommon.RunAICLICommand("install", $"{Constants.MsiInstallerPackageId} -l {installDir}");
     59             var result = TestCommon.RunAICLICommand("uninstall", Constants.MsiInstallerPackageId);
     60             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
     61             Assert.True(result.StdOut.Contains("Successfully uninstalled"));
     62             Assert.True(TestCommon.VerifyTestMsiUninstalled(installDir));
     63         }
     64 
     65         /// <summary>
     66         /// Test uninstall msix.
     67         /// </summary>
     68         [Test]
     69         public void UninstallTestMsix()
     70         {
     71             // Uninstall an MSIX
     72             TestCommon.RunAICLICommand("install", Constants.MsixInstallerPackageId);
     73             var result = TestCommon.RunAICLICommand("uninstall", Constants.MsixInstallerPackageId);
     74             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
     75             Assert.True(result.StdOut.Contains("Successfully uninstalled"));
     76             Assert.True(TestCommon.VerifyTestMsixUninstalled());
     77         }
     78 
     79         /// <summary>
     80         /// Test uninstall msix package with machine scope.
     81         /// </summary>
     82         [Test]
     83         public void UninstallTestMsixMachineScope()
     84         {
     85             // TODO: Provision and Deprovision api not supported in build server.
     86             Assert.Ignore();
     87 
     88             // Uninstall an MSIX
     89             TestCommon.RunAICLICommand("install", $"{Constants.MsixInstallerPackageId} --scope machine");
     90             var result = TestCommon.RunAICLICommand("uninstall", $"{Constants.MsixInstallerPackageId} --scope machine");
     91             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
     92             Assert.True(result.StdOut.Contains("Successfully uninstalled"));
     93             Assert.True(TestCommon.VerifyTestMsixUninstalled(true));
     94         }
     95 
     96         /// <summary>
     97         /// Test uninstall portable package.
     98         /// </summary>
     99         [Test]
    100         public void UninstallPortable()
    101         {
    102             // Uninstall a Portable
    103             string installDir = Path.Combine(System.Environment.GetEnvironmentVariable("LocalAppData"), "Microsoft", "WinGet", "Packages");
    104             string packageId, commandAlias, fileName, packageDirName, productCode;
    105             packageId = "AppInstallerTest.TestPortableExe";
    106             packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier;
    107             commandAlias = fileName = "AppInstallerTestExeInstaller.exe";
    108 
    109             TestCommon.RunAICLICommand("install", $"{packageId}");
    110             var result = TestCommon.RunAICLICommand("uninstall", $"{packageId}");
    111             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
    112             Assert.True(result.StdOut.Contains("Successfully uninstalled"));
    113             TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, false);
    114         }
    115 
    116         /// <summary>
    117         /// Test uninstall portable package with product code.
    118         /// </summary>
    119         [Test]
    120         public void UninstallPortableWithProductCode()
    121         {
    122             // Uninstall a Portable with ProductCode
    123             string installDir = TestCommon.GetPortablePackagesDirectory();
    124             string packageId, commandAlias, fileName, packageDirName, productCode;
    125             packageId = "AppInstallerTest.TestPortableExe";
    126             packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier;
    127             commandAlias = fileName = "AppInstallerTestExeInstaller.exe";
    128 
    129             TestCommon.RunAICLICommand("install", $"{packageId}");
    130             var result = TestCommon.RunAICLICommand("uninstall", $"--product-code {productCode}");
    131             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
    132             Assert.True(result.StdOut.Contains("Successfully uninstalled"));
    133             TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, false);
    134         }
    135 
    136         /// <summary>
    137         /// Test uninstall portable package with modified symlink.
    138         /// </summary>
    139         [Test]
    140         public void UninstallPortableModifiedSymlink()
    141         {
    142             string installDir = TestCommon.GetPortablePackagesDirectory();
    143             string packageId, commandAlias, fileName, packageDirName, productCode;
    144             packageId = "AppInstallerTest.TestPortableExe";
    145             packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier;
    146             commandAlias = fileName = "AppInstallerTestExeInstaller.exe";
    147 
    148             TestCommon.RunAICLICommand("install", $"{packageId}");
    149 
    150             string symlinkDirectory = TestCommon.GetPortableSymlinkDirectory(TestCommon.Scope.User);
    151             string symlinkPath = Path.Combine(symlinkDirectory, commandAlias);
    152 
    153             // Replace symlink with modified symlink
    154             File.Delete(symlinkPath);
    155             FileSystemInfo modifiedSymlinkInfo = File.CreateSymbolicLink(symlinkPath, "fakeTargetExe");
    156 
    157             var result = TestCommon.RunAICLICommand("uninstall", $"{packageId}");
    158             Assert.AreEqual(Constants.ErrorCode.ERROR_PORTABLE_UNINSTALL_FAILED, result.ExitCode);
    159             Assert.True(result.StdOut.Contains("Unable to remove Portable package as it has been modified; to override this check use --force"));
    160             Assert.True(modifiedSymlinkInfo.Exists, "Modified symlink should still exist");
    161 
    162             // Try again with --force
    163             var result2 = TestCommon.RunAICLICommand("uninstall", $"{packageId} --force");
    164             Assert.AreEqual(Constants.ErrorCode.S_OK, result2.ExitCode);
    165             Assert.True(result2.StdOut.Contains("Portable package has been modified; proceeding due to --force"));
    166             Assert.True(result2.StdOut.Contains("Successfully uninstalled"));
    167 
    168             TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, false);
    169         }
    170 
    171         /// <summary>
    172         /// Test uninstall zip portable package.
    173         /// </summary>
    174         [Test]
    175         public void UninstallZip_Portable()
    176         {
    177             string installDir = TestCommon.GetPortablePackagesDirectory();
    178             string packageId, commandAlias, fileName, packageDirName, productCode;
    179             packageId = "AppInstallerTest.TestZipInstallerWithPortable";
    180             packageDirName = productCode = packageId + "_" + Constants.TestSourceIdentifier;
    181             commandAlias = "TestPortable.exe";
    182             fileName = "AppInstallerTestExeInstaller.exe";
    183 
    184             var testResult = TestCommon.RunAICLICommand("install", $"{packageId}");
    185             var result = TestCommon.RunAICLICommand("uninstall", $"{packageId}");
    186             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
    187             Assert.True(result.StdOut.Contains("Successfully uninstalled"));
    188             TestCommon.VerifyPortablePackage(Path.Combine(installDir, packageDirName), commandAlias, fileName, productCode, false);
    189         }
    190 
    191         /// <summary>
    192         /// Test uninstall not indexed.
    193         /// </summary>
    194         [Test]
    195         public void UninstallNotIndexed()
    196         {
    197             // Uninstalls a package found with ARP not matching any known manifest.
    198             // Install the test EXE providing a custom Product Code so that it cannot be mapped
    199             // back to its manifest, then uninstall it using its Product Code
    200             var installDir = TestCommon.GetRandomTestDir();
    201             TestCommon.RunAICLICommand("install", $"{Constants.ExeInstallerPackageId} --override \"/ProductID {CustomProductCode} /InstallDir {installDir}");
    202             var result = TestCommon.RunAICLICommand("uninstall", CustomProductCode);
    203             Assert.AreEqual(Constants.ErrorCode.S_OK, result.ExitCode);
    204             Assert.True(result.StdOut.Contains("Successfully uninstalled"));
    205             Assert.True(TestCommon.VerifyTestExeUninstalled(installDir));
    206         }
    207 
    208         /// <summary>
    209         /// Test uninstalled app not found.
    210         /// </summary>
    211         [Test]
    212         public void UninstallAppNotInstalled()
    213         {
    214             // Verify failure when trying to uninstall an app that is not installed.
    215             var result = TestCommon.RunAICLICommand("uninstall", $"TestMsixInstaller");
    216             Assert.AreEqual(Constants.ErrorCode.ERROR_NO_APPLICATIONS_FOUND, result.ExitCode);
    217             Assert.True(result.StdOut.Contains("No installed package found matching input criteria."));
    218         }
    219     }
    220 }