|
4 | 4 | using System.IO;
|
5 | 5 | using System.Linq;
|
6 | 6 | using System.Reflection;
|
| 7 | +using System.Runtime.InteropServices; |
7 | 8 |
|
8 | 9 | namespace Fonlow.Testing
|
9 | 10 | {
|
10 |
| - /// <summary> |
11 |
| - /// Data model of the "Testing" section "appsettings.json" and optionally "appsettings.BuildConfiguration.json"; |
12 |
| - /// and the singleton object to access the settings read from the JSON files |
13 |
| - /// </summary> |
14 |
| - public sealed class TestingSettings |
15 |
| - { |
16 |
| - private TestingSettings() |
17 |
| - { |
18 |
| - } |
19 |
| - |
20 |
| - private static readonly Lazy<TestingSettings> lazy = |
21 |
| - new Lazy<TestingSettings>(() => Create()); |
22 |
| - |
23 |
| - public static TestingSettings Instance { get { return lazy.Value; } } |
24 |
| - |
25 |
| - static TestingSettings Create() |
26 |
| - { |
27 |
| - var obj = new TestingSettings(); |
28 |
| - IConfigurationBuilder configBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json"); |
29 |
| - obj.BuildConfiguration = GetBuildConfiguration(); |
30 |
| - |
31 |
| - if (Environment.OSVersion.Platform == PlatformID.Win32NT) |
32 |
| - { |
33 |
| - obj.ExecutableExt = ".exe"; |
34 |
| - } |
35 |
| - |
36 |
| - string specificAppSettingsFilename = $"appsettings.{obj.BuildConfiguration}.json"; |
37 |
| - if (Path.Exists(specificAppSettingsFilename)) |
38 |
| - { |
39 |
| - configBuilder.AddJsonFile(specificAppSettingsFilename); |
40 |
| - } |
41 |
| - |
42 |
| - var config = configBuilder.Build(); |
43 |
| - config.Bind("Testing", obj);//work only when properties are not with private setter. |
44 |
| - |
45 |
| - return obj; |
46 |
| - } |
47 |
| - |
48 |
| - static string GetBuildConfiguration() |
49 |
| - { |
50 |
| - var executingAssembly = Assembly.GetExecutingAssembly(); |
51 |
| - var location = executingAssembly.Location; |
52 |
| - var dir = Path.GetDirectoryName(location); |
53 |
| - var pathNames = Directory.GetFiles(dir, "*.dll"); |
54 |
| - List<Assembly> testAssemblies = new List<Assembly>(); |
55 |
| - foreach (var p in pathNames) |
56 |
| - { |
57 |
| - try |
58 |
| - { |
59 |
| - var a = Assembly.LoadFile(p); |
60 |
| - var referencedAssemblies = a.GetReferencedAssemblies(); |
61 |
| - if (referencedAssemblies.Any(d => d.Name == "xunit.core") && !Path.GetFileName(p).StartsWith("xunit.", StringComparison.OrdinalIgnoreCase)) |
62 |
| - { |
63 |
| - testAssemblies.Add(a); |
64 |
| - } |
65 |
| - } |
66 |
| - catch (FileLoadException) |
67 |
| - { |
68 |
| - continue; |
69 |
| - } |
70 |
| - catch (BadImageFormatException) |
71 |
| - { |
72 |
| - continue; |
73 |
| - } |
74 |
| - } |
75 |
| - |
76 |
| - if (testAssemblies.Count > 0) |
77 |
| - { |
78 |
| - var assemblyConfigurationAttribute = testAssemblies[0].GetCustomAttribute<AssemblyConfigurationAttribute>(); |
79 |
| - return assemblyConfigurationAttribute?.Configuration; |
80 |
| - } |
81 |
| - else |
82 |
| - { |
83 |
| - return "Debug"; |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - public ServiceCommand[] ServiceCommands { get; set; } |
88 |
| - |
89 |
| - public CopyItem[] CopyItems { get; set; } |
90 |
| - |
91 |
| - /// <summary> |
92 |
| - /// Used when Web resource is there, no need to be under the control of the test suite. |
93 |
| - /// </summary> |
94 |
| - public string BaseUrl { get; set; } |
95 |
| - |
96 |
| - public string Username { get; set; } |
97 |
| - public string Password { get; set; } |
98 |
| - |
99 |
| - /// <summary> |
100 |
| - /// For testing with many different user credentials. |
101 |
| - /// </summary> |
102 |
| - public UsernamePassword[] Users { get; set; } |
103 |
| - |
104 |
| - /// <summary> |
105 |
| - /// Build configuration of the test suite such as Debug, Release or whatever custom build configuration. |
106 |
| - /// ServiceCommandFixture will replace {BuildConfiguration} in commandPath and arguments with this. |
107 |
| - /// </summary> |
108 |
| - [System.Text.Json.Serialization.JsonIgnore] |
109 |
| - public string BuildConfiguration { get; private set; } |
110 |
| - |
111 |
| - /// <summary> |
112 |
| - /// The extention name of executable file. On Win, .exe, and on Linux and MacOs, empty. |
113 |
| - /// </summary> |
114 |
| - [System.Text.Json.Serialization.JsonIgnore] |
115 |
| - public string ExecutableExt { get; private set; } = string.Empty; |
116 |
| - |
117 |
| - [Obsolete("In favor of ServiceCommandFixture")] |
118 |
| - public string DotNetServiceAssemblyPath { get; set; } |
119 |
| - |
120 |
| - /// <summary> |
121 |
| - /// For IIS Express, host site name |
122 |
| - /// </summary> |
123 |
| - [Obsolete("In favor of ServiceCommandFixture")] |
124 |
| - public string HostSite { get; set; } |
125 |
| - |
126 |
| - /// <summary> |
127 |
| - /// For IIS Express, application pool |
128 |
| - /// </summary> |
129 |
| - [Obsolete("In favor of ServiceCommandFixture")] |
130 |
| - public string HostSiteApplicationPool { get; set; } |
131 |
| - |
132 |
| - /// <summary> |
133 |
| - /// For IIS Express, the lib needs to be aware the SLN root |
134 |
| - /// </summary> |
135 |
| - [Obsolete("In favor of ServiceCommandFixture")] |
136 |
| - public string SlnRoot { get; set; } |
137 |
| - |
138 |
| - /// <summary> |
139 |
| - /// For IIS Express, the lib needs to know the SLN name |
140 |
| - /// </summary> |
141 |
| - [Obsolete("In favor of ServiceCommandFixture")] |
142 |
| - public string SlnName { get; set; } |
143 |
| - } |
144 |
| - |
145 |
| - public sealed class UsernamePassword |
146 |
| - { |
147 |
| - public string Username { get; set; } |
148 |
| - public string Password { get; set; } |
149 |
| - } |
150 |
| - |
151 |
| - public sealed class ServiceCommand |
152 |
| - { |
153 |
| - public string CommandPath { get; set; } |
154 |
| - |
155 |
| - public bool IsPowerShellCommand { get; set; } |
156 |
| - |
157 |
| - public string Arguments { get; set; } |
158 |
| - |
159 |
| - /// <summary> |
160 |
| - /// Some services may take some seconds to launch then listen, especially in GitHub Actions which VM/container could be slow. A good bet may be 5 seconds. |
161 |
| - /// </summary> |
162 |
| - public int Delay { get; set; } |
163 |
| - public string ConnectionString { get; set; } |
164 |
| - public string BaseUrl { get; set; } |
165 |
| - |
166 |
| - /// <summary> |
167 |
| - /// For testing with many different user credentials with different authorization. |
168 |
| - /// </summary> |
169 |
| - /// <remarks>Obviously 2FA and alike are not welcome. Good enough for integration tests, but not E2E.</remarks> |
170 |
| - public UsernamePassword[] Users { get; set; } |
171 |
| - } |
172 |
| - |
173 |
| - public sealed class CopyItem |
174 |
| - { |
175 |
| - public string Source { get; set; } |
176 |
| - public string Destination { get; set; } |
177 |
| - } |
| 11 | + /// <summary> |
| 12 | + /// Data model of the "Testing" section "appsettings.json" and optionally "appsettings.BuildConfiguration.json"; |
| 13 | + /// and the singleton object to access the settings read from the JSON files |
| 14 | + /// </summary> |
| 15 | + public sealed class TestingSettings |
| 16 | + { |
| 17 | + #region Singleton and init |
| 18 | + private TestingSettings() |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + private static readonly Lazy<TestingSettings> lazy = |
| 23 | + new Lazy<TestingSettings>(() => Create()); |
| 24 | + |
| 25 | + public static TestingSettings Instance { get { return lazy.Value; } } |
| 26 | + |
| 27 | + static TestingSettings Create() |
| 28 | + { |
| 29 | + var obj = new TestingSettings(); |
| 30 | + IConfigurationBuilder configBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json"); |
| 31 | + obj.BuildConfiguration = GetBuildConfiguration(); |
| 32 | + obj.RuntimeId = RuntimeInformation.RuntimeIdentifier; |
| 33 | + |
| 34 | + if (Environment.OSVersion.Platform == PlatformID.Win32NT) |
| 35 | + { |
| 36 | + obj.ExecutableExt = ".exe"; |
| 37 | + } |
| 38 | + |
| 39 | + string specificAppSettingsFilename = $"appsettings.{obj.BuildConfiguration}.json"; |
| 40 | + if (Path.Exists(specificAppSettingsFilename)) |
| 41 | + { |
| 42 | + configBuilder.AddJsonFile(specificAppSettingsFilename); |
| 43 | + } |
| 44 | + |
| 45 | + var config = configBuilder.Build(); |
| 46 | + config.Bind("Testing", obj);//work only when properties are not with private setter. |
| 47 | + |
| 48 | + return obj; |
| 49 | + } |
| 50 | + |
| 51 | + static string GetBuildConfiguration() |
| 52 | + { |
| 53 | + var executingAssembly = Assembly.GetExecutingAssembly(); |
| 54 | + var location = executingAssembly.Location; |
| 55 | + var dir = Path.GetDirectoryName(location); |
| 56 | + var pathNames = Directory.GetFiles(dir, "*.dll"); |
| 57 | + List<Assembly> testAssemblies = new List<Assembly>(); |
| 58 | + foreach (var p in pathNames) |
| 59 | + { |
| 60 | + try |
| 61 | + { |
| 62 | + var a = Assembly.LoadFile(p); |
| 63 | + var referencedAssemblies = a.GetReferencedAssemblies(); |
| 64 | + if (referencedAssemblies.Any(d => d.Name == "xunit.core") && !Path.GetFileName(p).StartsWith("xunit.", StringComparison.OrdinalIgnoreCase)) |
| 65 | + { |
| 66 | + testAssemblies.Add(a); |
| 67 | + } |
| 68 | + } |
| 69 | + catch (FileLoadException) |
| 70 | + { |
| 71 | + continue; |
| 72 | + } |
| 73 | + catch (BadImageFormatException) |
| 74 | + { |
| 75 | + continue; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (testAssemblies.Count > 0) |
| 80 | + { |
| 81 | + var assemblyConfigurationAttribute = testAssemblies[0].GetCustomAttribute<AssemblyConfigurationAttribute>(); |
| 82 | + return assemblyConfigurationAttribute?.Configuration; |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + return "Debug"; |
| 87 | + } |
| 88 | + } |
| 89 | + #endregion |
| 90 | + |
| 91 | + #region Settings in appsettings.json |
| 92 | + /// <summary> |
| 93 | + /// Service Commands are executed in the order of JSON data initialization. |
| 94 | + /// </summary> |
| 95 | + public IReadOnlyDictionary<string, ServiceCommand> ServiceCommands { get; set; } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Each CopyItem is executed synchronous, so items are executed subsequently. |
| 99 | + /// </summary> |
| 100 | + public CopyItem[] CopyItems { get; set; } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Used when Web resource is there, no need to be under the control of the test suite. |
| 104 | + /// </summary> |
| 105 | + public string BaseUrl { get; set; } |
| 106 | + |
| 107 | + public string Username { get; set; } |
| 108 | + public string Password { get; set; } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// For testing with many different user credentials. |
| 112 | + /// </summary> |
| 113 | + public UsernamePassword[] Users { get; set; } |
| 114 | + |
| 115 | + #endregion |
| 116 | + |
| 117 | + |
| 118 | + #region Environment variables |
| 119 | + /// <summary> |
| 120 | + /// Build configuration of the test suite such as Debug, Release or whatever custom build configuration. |
| 121 | + /// ServiceCommandFixture will replace {BuildConfiguration} in commandPath and arguments with this. |
| 122 | + /// </summary> |
| 123 | + [System.Text.Json.Serialization.JsonIgnore] |
| 124 | + public string BuildConfiguration { get; private set; } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// RuntimeInformation.RuntimeIdentifier such as win-x64, linux-x64. Generally used for publishing to a specific platform. |
| 128 | + /// </summary> |
| 129 | + [System.Text.Json.Serialization.JsonIgnore] |
| 130 | + public string RuntimeId { get; private set; } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// The extention name of executable file. On Win, it is ".exe" including the dot, and on Linux and MacOs, empty. |
| 134 | + /// </summary> |
| 135 | + [System.Text.Json.Serialization.JsonIgnore] |
| 136 | + public string ExecutableExt { get; private set; } = string.Empty; |
| 137 | + #endregion |
| 138 | + |
| 139 | + #region Obsolete |
| 140 | + [Obsolete("In favor of ServiceCommandFixture")] |
| 141 | + public string DotNetServiceAssemblyPath { get; set; } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// For IIS Express, host site name |
| 145 | + /// </summary> |
| 146 | + [Obsolete("In favor of ServiceCommandFixture")] |
| 147 | + public string HostSite { get; set; } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// For IIS Express, application pool |
| 151 | + /// </summary> |
| 152 | + [Obsolete("In favor of ServiceCommandFixture")] |
| 153 | + public string HostSiteApplicationPool { get; set; } |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// For IIS Express, the lib needs to be aware the SLN root |
| 157 | + /// </summary> |
| 158 | + [Obsolete("In favor of ServiceCommandFixture")] |
| 159 | + public string SlnRoot { get; set; } |
| 160 | + |
| 161 | + /// <summary> |
| 162 | + /// For IIS Express, the lib needs to know the SLN name |
| 163 | + /// </summary> |
| 164 | + [Obsolete("In favor of ServiceCommandFixture")] |
| 165 | + public string SlnName { get; set; } |
| 166 | + #endregion |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + public sealed class UsernamePassword |
| 171 | + { |
| 172 | + public string Username { get; set; } |
| 173 | + public string Password { get; set; } |
| 174 | + } |
| 175 | + |
| 176 | + public sealed class ServiceCommand |
| 177 | + { |
| 178 | + public string CommandPath { get; set; } |
| 179 | + |
| 180 | + public bool IsPowerShellCommand { get; set; } |
| 181 | + |
| 182 | + public string Arguments { get; set; } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// Some services may take some seconds to launch then listen, especially in GitHub Actions which VM/container could be slow. A good bet may be 5 seconds. |
| 186 | + /// </summary> |
| 187 | + public int Delay { get; set; } |
| 188 | + public string ConnectionString { get; set; } |
| 189 | + public string BaseUrl { get; set; } |
| 190 | + |
| 191 | + /// <summary> |
| 192 | + /// For testing with many different user credentials with different authorization. |
| 193 | + /// </summary> |
| 194 | + /// <remarks>Obviously 2FA and alike are not welcome. Good enough for integration tests, but not E2E.</remarks> |
| 195 | + public UsernamePassword[] Users { get; set; } |
| 196 | + } |
| 197 | + |
| 198 | + public sealed class CopyItem |
| 199 | + { |
| 200 | + public string Source { get; set; } |
| 201 | + public string Destination { get; set; } |
| 202 | + } |
178 | 203 | }
|
0 commit comments