Skip to content

Commit bdf09cf

Browse files
authored
Merge pull request #171 from BryanWilhite/dev/version-6.1.2
Dev/version 6.1.2
2 parents 658b41e + 17fd9d9 commit bdf09cf

31 files changed

+2802
-525
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Songhay.Tests.Extensions;
2+
3+
public class AssemblyExtensionsTests
4+
{
5+
public AssemblyExtensionsTests(ITestOutputHelper helper)
6+
{
7+
_testOutputHelper = helper;
8+
}
9+
10+
[Fact]
11+
public void FindNetCoreProjectDirectorySiblingInfo_Test()
12+
{
13+
DirectoryInfo? actual = GetType().Assembly.FindNetCoreProjectDirectorySiblingInfo("*.OrderedTests");
14+
15+
Assert.NotNull(actual);
16+
17+
_testOutputHelper.WriteLine(actual.FullName);
18+
}
19+
20+
readonly ITestOutputHelper _testOutputHelper;
21+
}

SonghayCore.Tests/Extensions/HttpClientExtensionsTests.ShouldDownloadToFileAsync.txt

Lines changed: 980 additions & 164 deletions
Large diffs are not rendered by default.

SonghayCore.Tests/Extensions/HttpClientExtensionsTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class HttpClientExtensionsTests
55
public HttpClientExtensionsTests(ITestOutputHelper helper)
66
{
77
_testOutputHelper = helper;
8-
_basePath = ProgramAssemblyUtility.GetPathFromAssembly(GetType().Assembly, "../../../");
8+
_basePath = ProgramAssemblyUtility.GetPathFromAssembly(GetType().Assembly, TestScalars.NetCoreRelativePathToProjectFolder);
99
}
1010

1111
[Theory]
@@ -50,8 +50,7 @@ public async Task DownloadToStringAsync_HttpRequestMessage_Test(string file, str
5050

5151
static HttpClient GetHttpClient() => HttpClientLazy.Value;
5252

53-
static readonly Lazy<HttpClient> HttpClientLazy =
54-
new Lazy<HttpClient>(() => new HttpClient(), LazyThreadSafetyMode.PublicationOnly);
53+
static readonly Lazy<HttpClient> HttpClientLazy = new(() => new HttpClient(), LazyThreadSafetyMode.PublicationOnly);
5554

5655
readonly ITestOutputHelper _testOutputHelper;
5756
readonly string _basePath;
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Meziantou.Extensions.Logging.Xunit;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace Songhay.Tests.Extensions;
5+
6+
// ReSharper disable once InconsistentNaming
7+
public class ILoggerExtensionsTests
8+
{
9+
public ILoggerExtensionsTests(ITestOutputHelper helper)
10+
{
11+
_helper = helper;
12+
_loggerProvider = new XUnitLoggerProvider(helper);
13+
}
14+
15+
[Fact]
16+
public void LogErrorForMissingData_Test()
17+
{
18+
ILogger logger = _loggerProvider.CreateLogger(nameof(LogErrorForMissingData_Test));
19+
logger.LogErrorForMissingData("foo");
20+
logger.LogErrorForMissingData<TestMessage>();
21+
}
22+
23+
[Fact]
24+
public void LogTraceMethodCall_Test()
25+
{
26+
ILogger logger = _loggerProvider.CreateLogger(nameof(LogTraceMethodCall_Test));
27+
logger.LogTraceMethodCall(nameof(LogTraceMethodCall_Test));
28+
}
29+
30+
[Fact]
31+
public void LogTraceDataTypeAndValue_Test()
32+
{
33+
ILogger logger = _loggerProvider.CreateLogger(nameof(LogTraceMethodCall_Test));
34+
MyRecord data = new();
35+
logger.LogTraceDataTypeAndValue(data);
36+
}
37+
38+
record struct MyRecord
39+
{
40+
public MyRecord() { }
41+
public string One { get; init; } = "one point eight";
42+
public string Two { get; init; } = "two";
43+
public DateTime Stamp {get; init; } = DateTime.Now;
44+
}
45+
46+
readonly ITestOutputHelper _helper;
47+
readonly XUnitLoggerProvider _loggerProvider;
48+
}

0 commit comments

Comments
 (0)