TestSetup.cs (7857B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="TestSetup.cs" company="Microsoft Corporation"> 3 // Copyright (c) Microsoft Corporation. Licensed under the MIT License. 4 // </copyright> 5 // ----------------------------------------------------------------------------- 6 7 namespace AppInstallerCLIE2ETests.Helpers 8 { 9 using System; 10 using System.IO; 11 using NUnit.Framework; 12 13 /// <summary> 14 /// Singleton class with test parameters. 15 /// </summary> 16 internal class TestSetup 17 { 18 private static readonly Lazy<TestSetup> Lazy = new (() => new TestSetup()); 19 20 private string settingFilePath = null; 21 22 private TestSetup() 23 { 24 if (TestContext.Parameters.Count == 0) 25 { 26 this.IsDefault = true; 27 } 28 29 // Read TestParameters and set runtime variables 30 this.PackagedContext = this.InitializeBoolParam(Constants.PackagedContextParameter, true); 31 this.VerboseLogging = this.InitializeBoolParam(Constants.VerboseLoggingParameter, true); 32 this.LooseFileRegistration = this.InitializeBoolParam(Constants.LooseFileRegistrationParameter); 33 this.SkipTestSource = this.InitializeBoolParam(Constants.SkipTestSourceParameter, this.IsDefault); 34 35 // For packaged context, default to AppExecutionAlias 36 this.AICLIPath = this.InitializeStringParam(Constants.AICLIPathParameter, this.PackagedContext ? "WinGetDev.exe" : TestCommon.GetTestFile("winget.exe")); 37 this.AICLIPackagePath = this.InitializeStringParam(Constants.AICLIPackagePathParameter, TestCommon.GetTestFile("AppInstallerCLIPackage.appxbundle")); 38 39 this.StaticFileRootPath = this.InitializeDirectoryParam(Constants.StaticFileRootPathParameter, Path.GetTempPath()); 40 41 this.LocalServerCertPath = this.InitializeFileParam(Constants.LocalServerCertPathParameter); 42 this.PackageCertificatePath = this.InitializeFileParam(Constants.PackageCertificatePathParameter); 43 this.ExeInstallerPath = this.InitializeFileParam(Constants.ExeInstallerPathParameter); 44 this.MsiInstallerPath = this.InitializeFileParam(Constants.MsiInstallerPathParameter); 45 this.MsixInstallerPath = this.InitializeFileParam(Constants.MsixInstallerPathParameter); 46 this.MsiInstallerV2Path = this.InitializeFileParam(Constants.MsiInstallerV2PathParameter); 47 48 this.ForcedExperimentalFeatures = this.InitializeStringArrayParam(Constants.ForcedExperimentalFeaturesParameter); 49 } 50 51 /// <summary> 52 /// Gets the instance object. 53 /// </summary> 54 public static TestSetup Parameters 55 { 56 get 57 { 58 return Lazy.Value; 59 } 60 } 61 62 /// <summary> 63 /// Gets the cli path. 64 /// </summary> 65 public string AICLIPath { get; } 66 67 /// <summary> 68 /// Gets the package path. 69 /// </summary> 70 public string AICLIPackagePath { get; } 71 72 /// <summary> 73 /// Gets a value indicating whether the test runs in package context. 74 /// </summary> 75 public bool PackagedContext { get; } 76 77 /// <summary> 78 /// Gets a value indicating whether the test uses verbose logging. 79 /// </summary> 80 public bool VerboseLogging { get; } 81 82 /// <summary> 83 /// Gets a value indicating whether to use loose file registration. 84 /// </summary> 85 public bool LooseFileRegistration { get; } 86 87 /// <summary> 88 /// Gets the static file root path. 89 /// </summary> 90 public string StaticFileRootPath { get; } 91 92 /// <summary> 93 /// Gets the local server cert path. 94 /// </summary> 95 public string LocalServerCertPath { get; } 96 97 /// <summary> 98 /// Gets the exe installer path. 99 /// </summary> 100 public string ExeInstallerPath { get; } 101 102 /// <summary> 103 /// Gets the msi installer path. 104 /// </summary> 105 public string MsiInstallerPath { get; } 106 107 /// <summary> 108 /// Gets the msi installer V2 path. 109 /// </summary> 110 public string MsiInstallerV2Path { get; } 111 112 /// <summary> 113 /// Gets the msix installer path. 114 /// </summary> 115 public string MsixInstallerPath { get; } 116 117 /// <summary> 118 /// Gets the zip installer path. 119 /// </summary> 120 public string ZipInstallerPath { get; } 121 122 /// <summary> 123 /// Gets the package cert path. 124 /// </summary> 125 public string PackageCertificatePath { get; } 126 127 /// <summary> 128 /// Gets a value indicating whether to skip creating test source. 129 /// </summary> 130 public bool SkipTestSource { get; } 131 132 /// <summary> 133 /// Gets the settings json path. 134 /// </summary> 135 public string SettingsJsonFilePath 136 { 137 get 138 { 139 if (this.settingFilePath == null) 140 { 141 this.settingFilePath = WinGetSettingsHelper.GetUserSettingsPath(); 142 } 143 144 return this.settingFilePath; 145 } 146 } 147 148 /// <summary> 149 /// Gets the experimental features that should be forcibly enabled. 150 /// </summary> 151 public string[] ForcedExperimentalFeatures { get; } 152 153 /// <summary> 154 /// Gets a value indicating whether is the default parameters. 155 /// </summary> 156 public bool IsDefault { get; } 157 158 private bool InitializeBoolParam(string paramName, bool defaultValue = false) 159 { 160 if (this.IsDefault || !TestContext.Parameters.Exists(paramName)) 161 { 162 return defaultValue; 163 } 164 165 return TestContext.Parameters.Get(paramName).Equals("true", StringComparison.OrdinalIgnoreCase); 166 } 167 168 private string InitializeStringParam(string paramName, string defaultValue = null) 169 { 170 if (this.IsDefault || !TestContext.Parameters.Exists(paramName)) 171 { 172 return defaultValue; 173 } 174 175 return TestContext.Parameters.Get(paramName); 176 } 177 178 private string[] InitializeStringArrayParam(string paramName, string[] defaultValue = null) 179 { 180 if (this.IsDefault || !TestContext.Parameters.Exists(paramName)) 181 { 182 return defaultValue; 183 } 184 185 return TestContext.Parameters.Get(paramName).Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); 186 } 187 188 private string InitializeFileParam(string paramName, string defaultValue = null) 189 { 190 if (!TestContext.Parameters.Exists(paramName)) 191 { 192 return defaultValue; 193 } 194 195 var value = TestContext.Parameters.Get(paramName); 196 197 if (!File.Exists(value)) 198 { 199 throw new FileNotFoundException($"{paramName}: {value}"); 200 } 201 202 return value; 203 } 204 205 private string InitializeDirectoryParam(string paramName, string defaultValue = null) 206 { 207 if (!TestContext.Parameters.Exists(paramName)) 208 { 209 return defaultValue; 210 } 211 212 var value = TestContext.Parameters.Get(paramName); 213 214 if (!Directory.Exists(value)) 215 { 216 throw new DirectoryNotFoundException($"{paramName}: {value}"); 217 } 218 219 return value; 220 } 221 } 222 }