From badde27bf66fcacef2d625af0bd7590ecdc5804f Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Wed, 17 Feb 2021 15:45:19 -0600 Subject: Create WixTestTools project and reorganize files. --- src/WixTestTools/WixTestContext.cs | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/WixTestTools/WixTestContext.cs (limited to 'src/WixTestTools/WixTestContext.cs') 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 @@ +// 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. + +namespace WixTestTools +{ + using System; + using System.IO; + using System.Linq; + using System.Reflection; + using Microsoft.Win32; + using WixBuildTools.TestSupport; + using Xunit.Abstractions; + + public class WixTestContext + { + static readonly string RootDataPath = Path.GetFullPath(TestData.Get("TestData")); + + public WixTestContext(ITestOutputHelper testOutputHelper) + { + var test = GetTest(testOutputHelper); + var splitClassName = test.TestCase.TestMethod.TestClass.Class.Name.Split('.'); + + this.TestGroupName = splitClassName.Last(); + this.TestName = test.TestCase.TestMethod.Method.Name; + + this.TestDataFolder = Path.Combine(RootDataPath, this.TestGroupName); + } + + public string TestDataFolder { get; } + + /// + /// Gets the name of the current test group. + /// + public string TestGroupName { get; } + + public string TestName { get; } + + /// + /// Gets the test install directory for the current test. + /// + /// Additional subdirectories under the test install directory. + /// Full path to the test install directory. + /// + /// The package or bundle must install into [ProgramFilesFolder]\~Test WiX\[TestGroupName]\([Additional]). + /// + public string GetTestInstallFolder(string additionalPath = null) + { + return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "~Test WiX", this.TestGroupName, additionalPath ?? String.Empty); + } + + /// + /// Gets the test registry key for the current test. + /// + /// Additional subkeys under the test registry key. + /// Full path to the test registry key. + /// + /// The package must write into HKLM\Software\WiX\Tests\[TestGroupName]\([Additional]). + /// + public RegistryKey GetTestRegistryRoot(string additionalPath = null) + { + var key = String.Format(@"Software\WOW6432Node\WiX\Tests\{0}\{1}", this.TestGroupName, additionalPath ?? String.Empty); + return Registry.LocalMachine.OpenSubKey(key, true); + } + + private static ITest GetTest(ITestOutputHelper output) + { + // https://github.com/xunit/xunit/issues/416#issuecomment-378512739 + var type = output.GetType(); + var testMember = type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic); + var test = (ITest)testMember.GetValue(output); + return test; + } + } +} -- cgit v1.2.3-55-g6feb