aboutsummaryrefslogtreecommitdiff
path: root/src/WixTestTools/BundleInstaller.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixTestTools/BundleInstaller.cs')
-rw-r--r--src/WixTestTools/BundleInstaller.cs141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/WixTestTools/BundleInstaller.cs b/src/WixTestTools/BundleInstaller.cs
new file mode 100644
index 00000000..044486fe
--- /dev/null
+++ b/src/WixTestTools/BundleInstaller.cs
@@ -0,0 +1,141 @@
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
3namespace WixTestTools
4{
5 using System;
6 using System.IO;
7 using System.Text;
8
9 public partial class BundleInstaller : IDisposable
10 {
11 public BundleInstaller(WixTestContext testContext, string name)
12 {
13 this.Bundle = Path.Combine(testContext.TestDataFolder, $"{name}.exe");
14 this.BundlePdb = Path.Combine(testContext.TestDataFolder, $"{name}.wixpdb");
15 this.TestGroupName = testContext.TestGroupName;
16 this.TestName = testContext.TestName;
17 }
18
19 public string Bundle { get; }
20
21 public string TestGroupName { get; }
22
23 public string TestName { get; }
24
25 /// <summary>
26 /// Installs the bundle with optional arguments.
27 /// </summary>
28 /// <param name="expectedExitCode">Expected exit code, defaults to success.</param>
29 /// <param name="arguments">Optional arguments to pass to the tool.</param>
30 /// <returns>Path to the generated log file.</returns>
31 public string Install(int expectedExitCode = (int)MSIExec.MSIExecReturnCode.SUCCESS, params string[] arguments)
32 {
33 return this.RunBundleWithArguments(expectedExitCode, MSIExec.MSIExecMode.Install, arguments);
34 }
35
36 /// <summary>
37 /// Modify the bundle with optional arguments.
38 /// </summary>
39 /// <param name="expectedExitCode">Expected exit code, defaults to success.</param>
40 /// <param name="arguments">Optional arguments to pass to the tool.</param>
41 /// <returns>Path to the generated log file.</returns>
42 public string Modify(int expectedExitCode = (int)MSIExec.MSIExecReturnCode.SUCCESS, params string[] arguments)
43 {
44 return this.RunBundleWithArguments(expectedExitCode, MSIExec.MSIExecMode.Modify, arguments);
45 }
46
47 /// <summary>
48 /// Repairs the bundle with optional arguments.
49 /// </summary>
50 /// <param name="expectedExitCode">Expected exit code, defaults to success.</param>
51 /// <param name="arguments">Optional arguments to pass to the tool.</param>
52 /// <returns>Path to the generated log file.</returns>
53 public string Repair(int expectedExitCode = (int)MSIExec.MSIExecReturnCode.SUCCESS, params string[] arguments)
54 {
55 return this.RunBundleWithArguments(expectedExitCode, MSIExec.MSIExecMode.Repair, arguments);
56 }
57
58 /// <summary>
59 /// Uninstalls the bundle with optional arguments.
60 /// </summary>
61 /// <param name="expectedExitCode">Expected exit code, defaults to success.</param>
62 /// <param name="arguments">Optional arguments to pass to the tool.</param>
63 /// <returns>Path to the generated log file.</returns>
64 public string Uninstall(int expectedExitCode = (int)MSIExec.MSIExecReturnCode.SUCCESS, params string[] arguments)
65 {
66 return this.RunBundleWithArguments(expectedExitCode, MSIExec.MSIExecMode.Uninstall, arguments);
67 }
68
69 /// <summary>
70 /// Uninstalls the bundle at the given path with optional arguments.
71 /// </summary>
72 /// <param name="bundlePath">This should be the bundle in the package cache.</param>
73 /// <param name="expectedExitCode">Expected exit code, defaults to success.</param>
74 /// <param name="arguments">Optional arguments to pass to the tool.</param>
75 /// <returns>Path to the generated log file.</returns>
76 public string Uninstall(string bundlePath, int expectedExitCode = (int)MSIExec.MSIExecReturnCode.SUCCESS, params string[] arguments)
77 {
78 return this.RunBundleWithArguments(expectedExitCode, MSIExec.MSIExecMode.Uninstall, arguments, bundlePath: bundlePath);
79 }
80
81 /// <summary>
82 /// Executes the bundle with optional arguments.
83 /// </summary>
84 /// <param name="expectedExitCode">Expected exit code.</param>
85 /// <param name="mode">Install mode.</param>
86 /// <param name="arguments">Optional arguments to pass to the tool.</param>
87 /// <returns>Path to the generated log file.</returns>
88 private string RunBundleWithArguments(int expectedExitCode, MSIExec.MSIExecMode mode, string[] arguments, bool assertOnError = true, string bundlePath = null)
89 {
90 TestTool bundle = new TestTool(bundlePath ?? this.Bundle);
91 var sb = new StringBuilder();
92
93 // Be sure to run silent.
94 sb.Append(" -quiet");
95
96 // Generate the log file name.
97 string logFile = Path.Combine(Path.GetTempPath(), String.Format("{0}_{1}_{2:yyyyMMddhhmmss}_{4}_{3}.log", this.TestGroupName, this.TestName, DateTime.UtcNow, Path.GetFileNameWithoutExtension(this.Bundle), mode));
98 sb.AppendFormat(" -log \"{0}\"", logFile);
99
100 // Set operation.
101 switch (mode)
102 {
103 case MSIExec.MSIExecMode.Modify:
104 sb.Append(" -modify");
105 break;
106
107 case MSIExec.MSIExecMode.Repair:
108 sb.Append(" -repair");
109 break;
110
111 case MSIExec.MSIExecMode.Cleanup:
112 case MSIExec.MSIExecMode.Uninstall:
113 sb.Append(" -uninstall");
114 break;
115 }
116
117 // Add additional arguments.
118 if (null != arguments)
119 {
120 sb.Append(" ");
121 sb.Append(String.Join(" ", arguments));
122 }
123
124 // Set the arguments.
125 bundle.Arguments = sb.ToString();
126
127 // Run the tool and assert the expected code.
128 bundle.ExpectedExitCode = expectedExitCode;
129 bundle.Run(assertOnError);
130
131 // Return the log file name.
132 return logFile;
133 }
134
135 public void Dispose()
136 {
137 string[] args = { "-burn.ignoredependencies=ALL" };
138 this.RunBundleWithArguments((int)MSIExec.MSIExecReturnCode.SUCCESS, MSIExec.MSIExecMode.Cleanup, args, assertOnError: false);
139 }
140 }
141}