| 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 System.Linq; |
| 8 | using System.Reflection; |
| 9 | using Microsoft.Win32; |
| 10 | using WixInternal.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(bool x64, string additionalPath = null) |
| 46 | { |
| 47 | var baseDirectory = x64 ? Environment.SpecialFolder.ProgramFiles : Environment.SpecialFolder.ProgramFilesX86; |
| 48 | return Path.Combine(Environment.GetFolderPath(baseDirectory), "~Test WiX", this.TestGroupName, additionalPath ?? String.Empty); |
| 49 | } |
| 50 | |
| 51 | /// <summary> |
| 52 | /// Gets the test registry key for the current test. |
| 53 | /// </summary> |
| 54 | /// <param name="additionalPath">Additional subkeys under the test registry key.</param> |
| 55 | /// <returns>Full path to the test registry key.</returns> |
| 56 | /// <remarks> |
| 57 | /// The package must write into HKLM\Software\WiX\Tests\[TestGroupName]\([Additional]). |
| 58 | /// </remarks> |
| 59 | public RegistryKey GetTestRegistryRoot(bool x64, string additionalPath = null) |
| 60 | { |
| 61 | var baseKey = x64 ? "Software" : @"Software\WOW6432Node"; |
| 62 | var key = String.Format(@"{0}\WiX\Tests\{1}\{2}", baseKey, this.TestGroupName, additionalPath ?? String.Empty); |
| 63 | return Registry.LocalMachine.OpenSubKey(key, true); |
| 64 | } |
| 65 | |
| 66 | private static ITest GetTest(ITestOutputHelper output) |
| 67 | { |
| 68 | // https://github.com/xunit/xunit/issues/416#issuecomment-378512739 |
| 69 | var type = output.GetType(); |
| 70 | var testMember = type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic); |
| 71 | var test = (ITest)testMember.GetValue(output); |
| 72 | return test; |
| 73 | } |
| 74 | } |
| 75 | } |