|
| 1 | +using Microsoft.Extensions.Configuration; |
| 2 | +using Microsoft.Extensions.Hosting; |
| 3 | +using Songhay.Models; |
| 4 | + |
| 5 | +namespace Songhay.Tests.Extensions; |
| 6 | + |
| 7 | +// ReSharper disable once InconsistentNaming |
| 8 | +public class IConfigurationExtensionsTests |
| 9 | +{ |
| 10 | + public IConfigurationExtensionsTests(ITestOutputHelper helper) |
| 11 | + { |
| 12 | + _testOutputHelper = helper; |
| 13 | + } |
| 14 | + |
| 15 | + [Theory] |
| 16 | + [InlineData("../../../json", "configuration.json")] |
| 17 | + public async Task ReadSettings_Test(string baseDirectory, string configFile) |
| 18 | + { |
| 19 | + #region arrange input: |
| 20 | + |
| 21 | + baseDirectory = ProgramAssemblyUtility |
| 22 | + .GetPathFromAssembly(GetType().Assembly, baseDirectory); |
| 23 | + |
| 24 | + _testOutputHelper.WriteLine($"{nameof(baseDirectory)}: `{baseDirectory}`"); |
| 25 | + _testOutputHelper.WriteLine($"{nameof(configFile)}: `{configFile}`"); |
| 26 | + |
| 27 | + #endregion |
| 28 | + |
| 29 | + #region arrange args: |
| 30 | + |
| 31 | + string[] args = { |
| 32 | + ConsoleArgsScalars.BaseDirectoryRequired, ConsoleArgsScalars.FlagSpacer, |
| 33 | + ConsoleArgsScalars.BaseDirectory, baseDirectory, |
| 34 | + ConsoleArgsScalars.SettingsFile, configFile, |
| 35 | + }; |
| 36 | + |
| 37 | + #endregion |
| 38 | + |
| 39 | + IConfiguration? configuration = null; |
| 40 | + IHostBuilder builder = Host.CreateDefaultBuilder(args); |
| 41 | + |
| 42 | + #region arrange `IHost` builder |
| 43 | + |
| 44 | + builder.ConfigureHostConfiguration(builder => |
| 45 | + { |
| 46 | + builder.AddCommandLine(new[] {"--ENVIRONMENT", Environments.Development}); |
| 47 | + }); |
| 48 | + |
| 49 | + builder.ConfigureAppConfiguration((hostingContext, configBuilder) => |
| 50 | + { |
| 51 | + configuration = hostingContext.Configuration; |
| 52 | + |
| 53 | + string path = configuration.GetSettingsFilePath(); |
| 54 | + Assert.True(File.Exists(path), $"The expected path, `{path}`, does not exist."); |
| 55 | + |
| 56 | + configBuilder.AddJsonFile(path, optional: false); |
| 57 | + }); |
| 58 | + |
| 59 | + #endregion |
| 60 | + |
| 61 | + using IHost host = builder.Build(); |
| 62 | + try |
| 63 | + { |
| 64 | + host.Start(); |
| 65 | + // FUNKYKB: `IHost.Run` cannot be called here |
| 66 | + // because it will await forever |
| 67 | + // and any JSON files will not load until it is called. |
| 68 | + |
| 69 | + ReportOnConfigState(configuration); |
| 70 | + |
| 71 | + Assert.Equal(baseDirectory, configuration.GetBasePathValue()); |
| 72 | + Assert.Equal(configFile, Path.GetFileName(configuration.GetSettingsFilePath())); |
| 73 | + |
| 74 | + string json = configuration.ReadSettings(); |
| 75 | + Assert.False(string.IsNullOrWhiteSpace(json)); |
| 76 | + |
| 77 | + _testOutputHelper.WriteLine(json); |
| 78 | + } |
| 79 | + finally |
| 80 | + { |
| 81 | + await host.StopAsync(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + [Theory] |
| 86 | + [InlineData("../../../json", "input.json")] |
| 87 | + [InlineData(null, "../../../json/input.json")] |
| 88 | + [InlineData(null, @"{ ""arg1"": 1, ""arg2"": 2, ""arg3"": 3 }")] |
| 89 | + public async Task ReadStringInput_Test(string? baseDirectory, string? input) |
| 90 | + { |
| 91 | + #region arrange input: |
| 92 | + |
| 93 | + bool isJsonFile = input?.EndsWith(".json") == true; |
| 94 | + |
| 95 | + if (isJsonFile && string.IsNullOrWhiteSpace(baseDirectory)) |
| 96 | + { |
| 97 | + input = ProgramAssemblyUtility.GetPathFromAssembly(GetType().Assembly, input); |
| 98 | + } |
| 99 | + else if(!string.IsNullOrWhiteSpace(baseDirectory)) |
| 100 | + { |
| 101 | + baseDirectory = ProgramAssemblyUtility.GetPathFromAssembly(GetType().Assembly, baseDirectory); |
| 102 | + } |
| 103 | + |
| 104 | + #endregion |
| 105 | + |
| 106 | + #region arrange args: |
| 107 | + |
| 108 | + string?[] args = isJsonFile ? |
| 109 | + new[] |
| 110 | + { |
| 111 | + ConsoleArgsScalars.BaseDirectoryRequired, ConsoleArgsScalars.FlagSpacer, |
| 112 | + ConsoleArgsScalars.BaseDirectory, baseDirectory, |
| 113 | + ConsoleArgsScalars.InputFile, input, |
| 114 | + } |
| 115 | + : |
| 116 | + new[] |
| 117 | + { |
| 118 | + ConsoleArgsScalars.BaseDirectoryRequired, ConsoleArgsScalars.FlagSpacer, |
| 119 | + ConsoleArgsScalars.BaseDirectory, baseDirectory, |
| 120 | + ConsoleArgsScalars.InputString, input, |
| 121 | + }; |
| 122 | + |
| 123 | + #endregion |
| 124 | + |
| 125 | + IConfiguration? configuration = null; |
| 126 | + IHostBuilder builder = Host.CreateDefaultBuilder(args); |
| 127 | + |
| 128 | + builder.ConfigureAppConfiguration((hostingContext, _) => configuration = hostingContext.Configuration); |
| 129 | + |
| 130 | + using IHost host = builder.Build(); |
| 131 | + try |
| 132 | + { |
| 133 | + // FUNKYKB: `IHost.Run` cannot be called here |
| 134 | + // because it will await forever |
| 135 | + // and any JSON files will not load until it is called. |
| 136 | + |
| 137 | + ReportOnConfigState(configuration); |
| 138 | + |
| 139 | + input = configuration.ReadStringInput(); |
| 140 | + |
| 141 | + Assert.False(string.IsNullOrWhiteSpace(input)); |
| 142 | + |
| 143 | + _testOutputHelper.WriteLine($"{nameof(input)}:"); |
| 144 | + _testOutputHelper.WriteLine(input); |
| 145 | + |
| 146 | + } |
| 147 | + finally |
| 148 | + { |
| 149 | + await host.StopAsync(); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + [Fact] |
| 154 | + public void WithDefaultHelpText_Test() |
| 155 | + { |
| 156 | + IConfiguration? configuration = null; |
| 157 | + IHostBuilder builder = Host.CreateDefaultBuilder(Array.Empty<string>()); |
| 158 | + |
| 159 | + builder.ConfigureAppConfiguration((hostingContext, _) => |
| 160 | + { |
| 161 | + configuration = hostingContext.Configuration; |
| 162 | + |
| 163 | + Assert.NotNull(configuration); |
| 164 | + |
| 165 | + configuration.WithDefaultHelpText(); |
| 166 | + }); |
| 167 | + |
| 168 | + IHost _ = builder.Build(); |
| 169 | + // FUNKYKB: `IHost.Run` cannot be called here |
| 170 | + // because it will await forever |
| 171 | + // and any JSON files will not load until it is called. |
| 172 | + |
| 173 | + IReadOnlyCollection<string> keys = configuration.ToKeys(); |
| 174 | + Assert.NotEmpty(keys); |
| 175 | + _testOutputHelper.WriteLine($"{nameof(keys)}:"); |
| 176 | + keys.ForEachInEnumerable(k => _testOutputHelper.WriteLine($" `{k}`")); |
| 177 | + |
| 178 | + string? helpText = configuration.ToHelpDisplayText(); |
| 179 | + Assert.False(string.IsNullOrWhiteSpace(helpText)); |
| 180 | + _testOutputHelper.WriteLine(helpText); |
| 181 | + } |
| 182 | + |
| 183 | + [Theory] |
| 184 | + [InlineData("../../../json", "output.json")] |
| 185 | + [InlineData(null, "../../../json/output.json")] |
| 186 | + public void WriteOutputToFile_Test(string? baseDirectory, string outputFile) |
| 187 | + { |
| 188 | + #region arrange input: |
| 189 | + |
| 190 | + if (string.IsNullOrWhiteSpace(baseDirectory)) |
| 191 | + { |
| 192 | + outputFile = ProgramAssemblyUtility |
| 193 | + .GetPathFromAssembly(GetType().Assembly, outputFile); |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + baseDirectory = ProgramAssemblyUtility |
| 198 | + .GetPathFromAssembly(GetType().Assembly, baseDirectory); |
| 199 | + } |
| 200 | + |
| 201 | + #endregion |
| 202 | + |
| 203 | + #region arrange args: |
| 204 | + |
| 205 | + string[] args = string.IsNullOrWhiteSpace(baseDirectory) ? |
| 206 | + new[] |
| 207 | + { |
| 208 | + ConsoleArgsScalars.OutputFile, outputFile, |
| 209 | + } |
| 210 | + : |
| 211 | + new[] |
| 212 | + { |
| 213 | + ConsoleArgsScalars.BaseDirectoryRequired, ConsoleArgsScalars.FlagSpacer, |
| 214 | + ConsoleArgsScalars.BaseDirectory, baseDirectory, |
| 215 | + ConsoleArgsScalars.OutputFile, outputFile, |
| 216 | + ConsoleArgsScalars.OutputUnderBasePath, ConsoleArgsScalars.FlagSpacer, |
| 217 | + } |
| 218 | + ; |
| 219 | + |
| 220 | + #endregion |
| 221 | + |
| 222 | + IConfiguration? configuration = null; |
| 223 | + IHostBuilder builder = Host.CreateDefaultBuilder(args); |
| 224 | + |
| 225 | + builder.ConfigureAppConfiguration((hostingContext, _) => configuration = hostingContext.Configuration); |
| 226 | + |
| 227 | + using IHost _ = builder.Build(); |
| 228 | + // FUNKYKB: `IHost.Run` cannot be called here |
| 229 | + // because it will await forever |
| 230 | + // and any JSON files will not load until it is called. |
| 231 | + |
| 232 | + ReportOnConfigState(configuration); |
| 233 | + |
| 234 | + var anon = new { modificationDate = DateTime.Now }; |
| 235 | + |
| 236 | + configuration.WriteOutputToFile(JsonSerializer.Serialize(anon)); |
| 237 | + |
| 238 | + string actual = configuration.GetOutputPath(); |
| 239 | + |
| 240 | + _testOutputHelper.WriteLine($"output path: {actual}"); |
| 241 | + |
| 242 | + _testOutputHelper.WriteLine($"file content: {File.ReadAllText(actual)}"); |
| 243 | + } |
| 244 | + |
| 245 | + void ReportOnConfigState(IConfiguration? configuration) |
| 246 | + { |
| 247 | + Assert.NotNull(configuration); |
| 248 | + IReadOnlyCollection<string> keys = configuration.ToKeys(); |
| 249 | + Assert.NotEmpty(keys); |
| 250 | + _testOutputHelper.WriteLine($"{nameof(IConfiguration)}:"); |
| 251 | + keys.ForEachInEnumerable(k => _testOutputHelper.WriteLine($" `{k}`: `{configuration[k]}`")); |
| 252 | + |
| 253 | + _testOutputHelper.WriteLine($"{nameof(Environment)} variables:"); |
| 254 | + Environment |
| 255 | + .GetEnvironmentVariables() |
| 256 | + .Keys |
| 257 | + .OfType<string>() |
| 258 | + .ForEachInEnumerable(k => _testOutputHelper.WriteLine($" `{k}`: `{Environment.GetEnvironmentVariable(k)}`")); |
| 259 | + } |
| 260 | + |
| 261 | + readonly ITestOutputHelper _testOutputHelper; |
| 262 | +} |
0 commit comments