aboutsummaryrefslogtreecommitdiff
path: root/src/WixTestTools/WixTestContext.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/WixTestTools/WixTestContext.cs')
-rw-r--r--src/WixTestTools/WixTestContext.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/WixTestTools/WixTestContext.cs b/src/WixTestTools/WixTestContext.cs
new file mode 100644
index 00000000..c00f5723
--- /dev/null
+++ b/src/WixTestTools/WixTestContext.cs
@@ -0,0 +1,73 @@
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.Linq;
8 using System.Reflection;
9 using Microsoft.Win32;
10 using WixBuildTools.TestSupport;
11 using Xunit.Abstractions;
12
13 public class WixTestContext
14 {
15 static readonly string RootDataPath = Path.GetFullPath(TestData.Get("TestData"));
16
17 public WixTestContext(ITestOutputHelper testOutputHelper)
18 {
19 var test = GetTest(testOutputHelper);
20 var splitClassName = test.TestCase.TestMethod.TestClass.Class.Name.Split('.');
21
22 this.TestGroupName = splitClassName.Last();
23 this.TestName = test.TestCase.TestMethod.Method.Name;
24
25 this.TestDataFolder = Path.Combine(RootDataPath, this.TestGroupName);
26 }
27
28 public string TestDataFolder { get; }
29
30 /// <summary>
31 /// Gets the name of the current test group.
32 /// </summary>
33 public string TestGroupName { get; }
34
35 public string TestName { get; }
36
37 /// <summary>
38 /// Gets the test install directory for the current test.
39 /// </summary>
40 /// <param name="additionalPath">Additional subdirectories under the test install directory.</param>
41 /// <returns>Full path to the test install directory.</returns>
42 /// <remarks>
43 /// The package or bundle must install into [ProgramFilesFolder]\~Test WiX\[TestGroupName]\([Additional]).
44 /// </remarks>
45 public string GetTestInstallFolder(string additionalPath = null)
46 {
47 return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "~Test WiX", this.TestGroupName, additionalPath ?? String.Empty);
48 }
49
50 /// <summary>
51 /// Gets the test registry key for the current test.
52 /// </summary>
53 /// <param name="additionalPath">Additional subkeys under the test registry key.</param>
54 /// <returns>Full path to the test registry key.</returns>
55 /// <remarks>
56 /// The package must write into HKLM\Software\WiX\Tests\[TestGroupName]\([Additional]).
57 /// </remarks>
58 public RegistryKey GetTestRegistryRoot(string additionalPath = null)
59 {
60 var key = String.Format(@"Software\WOW6432Node\WiX\Tests\{0}\{1}", this.TestGroupName, additionalPath ?? String.Empty);
61 return Registry.LocalMachine.OpenSubKey(key, true);
62 }
63
64 private static ITest GetTest(ITestOutputHelper output)
65 {
66 // https://github.com/xunit/xunit/issues/416#issuecomment-378512739
67 var type = output.GetType();
68 var testMember = type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic);
69 var test = (ITest)testMember.GetValue(output);
70 return test;
71 }
72 }
73}