UnitTestFixture.cs (4635B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="UnitTestFixture.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace Microsoft.Management.Configuration.UnitTests.Fixtures 8 { 9 using System; 10 using System.Diagnostics.CodeAnalysis; 11 using System.IO; 12 using System.Reflection; 13 using Microsoft.Management.Configuration.Processor; 14 using Microsoft.Management.Configuration.Processor.PowerShell.ProcessorEnvironments; 15 using WinRT; 16 using Xunit.Abstractions; 17 18 /// <summary> 19 /// Unit test fixture. 20 /// </summary> 21 public class UnitTestFixture 22 { 23 /// <summary> 24 /// Initializes a new instance of the <see cref="UnitTestFixture"/> class. 25 /// </summary> 26 /// <param name="messageSink">The message sink for the fixture.</param> 27 public UnitTestFixture(IMessageSink messageSink) 28 { 29 this.MessageSink = messageSink; 30 31 string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) 32 ?? throw new ArgumentException(); 33 34 this.TestModulesPath = Path.Combine(assemblyPath, "TestCollateral", "PowerShellModules"); 35 if (!Directory.Exists(this.TestModulesPath)) 36 { 37 throw new DirectoryNotFoundException(this.TestModulesPath); 38 } 39 40 // Use the environment variable if present, which is how ADO pipelines will find it. 41 string? gitSearchPath = Environment.GetEnvironmentVariable("BUILD_SOURCESDIRECTORY"); 42 43 if (string.IsNullOrWhiteSpace(gitSearchPath)) 44 { 45 gitSearchPath = Path.GetDirectoryName(assemblyPath); 46 47 while (!string.IsNullOrEmpty(gitSearchPath)) 48 { 49 if (Directory.Exists(Path.Combine(gitSearchPath, ".git"))) 50 { 51 break; 52 } 53 54 gitSearchPath = Path.GetDirectoryName(gitSearchPath); 55 } 56 } 57 58 this.GitRootPath = gitSearchPath ?? throw new DirectoryNotFoundException("git root path"); 59 60 this.ExternalModulesPath = Path.Combine(this.GitRootPath, "src", "PowerShell", "ExternalModules"); 61 if (!Directory.Exists(this.ExternalModulesPath)) 62 { 63 throw new DirectoryNotFoundException(this.ExternalModulesPath); 64 } 65 66 this.RecreateStatics(); 67 } 68 69 /// <summary> 70 /// Gets the message sink for the fixture. 71 /// </summary> 72 public IMessageSink MessageSink { get; private init; } 73 74 /// <summary> 75 /// Gets the test module path. 76 /// </summary> 77 public string TestModulesPath { get; } 78 79 /// <summary> 80 /// Gets the git root path. 81 /// </summary> 82 public string GitRootPath { get; } 83 84 /// <summary> 85 /// Gets the external module path. 86 /// </summary> 87 public string ExternalModulesPath { get; } 88 89 /// <summary> 90 /// Gets the configuration statics object to use. 91 /// </summary> 92 internal IConfigurationStatics2 ConfigurationStatics { get; private set; } 93 94 /// <summary> 95 /// Creates a new statics object for use by the tests. 96 /// </summary> 97 [MemberNotNull("ConfigurationStatics")] 98 public void RecreateStatics() 99 { 100 this.ConfigurationStatics = new ConfigurationStaticFunctions().As<IConfigurationStatics2>(); 101 } 102 103 /// <summary> 104 /// Creates a runspace adding the test module path. 105 /// </summary> 106 /// <param name="validate">Validate runspace.</param> 107 /// <returns>PowerShellRunspace.</returns> 108 internal IProcessorEnvironment PrepareTestProcessorEnvironment(bool validate = false) 109 { 110 var processorEnv = new ProcessorEnvironmentFactory(PowerShellConfigurationProcessorType.Hosted).CreateEnvironment(null, PowerShellConfigurationProcessorPolicy.Unrestricted); 111 processorEnv.PrependPSModulePath(this.ExternalModulesPath); 112 processorEnv.PrependPSModulePath(this.TestModulesPath); 113 114 if (validate) 115 { 116 processorEnv.ValidateRunspace(); 117 } 118 119 return processorEnv; 120 } 121 } 122 }