diff options
Diffstat (limited to 'src/WixTestTools/PackageInstaller.cs')
| -rw-r--r-- | src/WixTestTools/PackageInstaller.cs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/WixTestTools/PackageInstaller.cs b/src/WixTestTools/PackageInstaller.cs new file mode 100644 index 00000000..86376b9f --- /dev/null +++ b/src/WixTestTools/PackageInstaller.cs | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | namespace WixTestTools | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.IO; | ||
| 7 | using static WixTestTools.MSIExec; | ||
| 8 | |||
| 9 | public partial class PackageInstaller : IDisposable | ||
| 10 | { | ||
| 11 | public PackageInstaller(WixTestContext testContext, string filename) | ||
| 12 | { | ||
| 13 | this.Package = Path.Combine(testContext.TestDataFolder, $"{filename}.msi"); | ||
| 14 | this.PackagePdb = Path.Combine(testContext.TestDataFolder, $"{filename}.wixpdb"); | ||
| 15 | this.TestContext = testContext; | ||
| 16 | } | ||
| 17 | |||
| 18 | public string Package { get; } | ||
| 19 | |||
| 20 | private WixTestContext TestContext { get; } | ||
| 21 | |||
| 22 | public string TestGroupName => this.TestContext.TestGroupName; | ||
| 23 | |||
| 24 | public string TestName => this.TestContext.TestName; | ||
| 25 | |||
| 26 | /// <summary> | ||
| 27 | /// Installs a .msi file | ||
| 28 | /// </summary> | ||
| 29 | /// <param name="expectedExitCode">Expected exit code</param> | ||
| 30 | /// <param name="otherArguments">Other arguments to pass to MSIExec.</param> | ||
| 31 | /// <returns>MSIExec log File</returns> | ||
| 32 | public string InstallProduct(MSIExecReturnCode expectedExitCode = MSIExecReturnCode.SUCCESS, params string[] otherArguments) | ||
| 33 | { | ||
| 34 | return this.RunMSIExec(MSIExecMode.Install, otherArguments, expectedExitCode); | ||
| 35 | } | ||
| 36 | |||
| 37 | /// <summary> | ||
| 38 | /// Uninstalls a .msi file | ||
| 39 | /// </summary> | ||
| 40 | /// <param name="expectedExitCode">Expected exit code</param> | ||
| 41 | /// <param name="otherArguments">Other arguments to pass to MSIExec.</param> | ||
| 42 | /// <returns>MSIExec log File</returns> | ||
| 43 | public string UninstallProduct(MSIExecReturnCode expectedExitCode = MSIExecReturnCode.SUCCESS, params string[] otherArguments) | ||
| 44 | { | ||
| 45 | return this.RunMSIExec(MSIExecMode.Uninstall, otherArguments, expectedExitCode); | ||
| 46 | } | ||
| 47 | |||
| 48 | /// <summary> | ||
| 49 | /// Repairs a .msi file | ||
| 50 | /// </summary> | ||
| 51 | /// <param name="expectedExitCode">Expected exit code</param> | ||
| 52 | /// <param name="otherArguments">Other arguments to pass to msiexe.exe.</param> | ||
| 53 | /// <returns>MSIExec log File</returns> | ||
| 54 | public string RepairProduct(MSIExecReturnCode expectedExitCode = MSIExecReturnCode.SUCCESS, params string[] otherArguments) | ||
| 55 | { | ||
| 56 | return this.RunMSIExec(MSIExecMode.Repair, otherArguments, expectedExitCode); | ||
| 57 | } | ||
| 58 | |||
| 59 | /// <summary> | ||
| 60 | /// Executes MSIExec on a .msi file | ||
| 61 | /// </summary> | ||
| 62 | /// <param name="mode">Mode of execution for MSIExec</param> | ||
| 63 | /// <param name="otherArguments">Other arguments to pass to MSIExec.</param> | ||
| 64 | /// <param name="expectedExitCode">Expected exit code</param> | ||
| 65 | /// <returns>MSIExec exit code</returns> | ||
| 66 | private string RunMSIExec(MSIExecMode mode, string[] otherArguments, MSIExecReturnCode expectedExitCode, bool assertOnError = true) | ||
| 67 | { | ||
| 68 | // Generate the log file name. | ||
| 69 | var logFile = Path.Combine(Path.GetTempPath(), String.Format("{0}_{1}_{2:yyyyMMddhhmmss}_{4}_{3}.log", this.TestGroupName, this.TestName, DateTime.UtcNow, Path.GetFileNameWithoutExtension(this.Package), mode)); | ||
| 70 | |||
| 71 | var msiexec = new MSIExec | ||
| 72 | { | ||
| 73 | Product = this.Package, | ||
| 74 | ExecutionMode = mode, | ||
| 75 | OtherArguments = null != otherArguments ? String.Join(" ", otherArguments) : null, | ||
| 76 | ExpectedExitCode = expectedExitCode, | ||
| 77 | LogFile = logFile, | ||
| 78 | }; | ||
| 79 | |||
| 80 | msiexec.Run(assertOnError); | ||
| 81 | return msiexec.LogFile; | ||
| 82 | } | ||
| 83 | |||
| 84 | public void Dispose() | ||
| 85 | { | ||
| 86 | string[] args = { "IGNOREDEPENDENCIES=ALL", "WIXFAILWHENDEFERRED=0" }; | ||
| 87 | this.RunMSIExec(MSIExecMode.Cleanup, args, MSIExecReturnCode.SUCCESS, assertOnError: false); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
