-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathConfigurationManagerTests.cs
More file actions
180 lines (141 loc) · 10 KB
/
ConfigurationManagerTests.cs
File metadata and controls
180 lines (141 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#if !NETFRAMEWORK
using System.Text.Json;
#endif
using Microsoft.Testing.Platform.CommandLine;
using Microsoft.Testing.Platform.Configurations;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.Services;
using Moq;
namespace Microsoft.Testing.Platform.UnitTests;
[TestClass]
public sealed class ConfigurationManagerTests
{
[TestMethod]
[DynamicData(nameof(GetConfigurationValueFromJsonData))]
public async ValueTask GetConfigurationValueFromJson(string jsonFileConfig, string key, string? result)
{
Mock<IFileSystem> fileSystem = new();
fileSystem.Setup(x => x.ExistFile(It.IsAny<string>())).Returns(true);
fileSystem.Setup(x => x.NewFileStream(It.IsAny<string>(), FileMode.Open, FileAccess.Read))
.Returns(new MemoryFileStream(Encoding.UTF8.GetBytes(jsonFileConfig)));
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() => new JsonConfigurationSource(testApplicationModuleInfo, fileSystem.Object, null));
IConfiguration configuration = await configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), []));
Assert.AreEqual(result, configuration[key], $"Expected '{result}' found '{configuration[key]}'");
}
internal static IEnumerable<(string JsonFileConfig, string Key, string? Result)> GetConfigurationValueFromJsonData()
{
yield return ("{\"platformOptions\": {\"Troubleshooting\": {\"CrashDump\": {\"Enable\": true}}}}", "platformOptions:Troubleshooting:CrashDump:Enable", "True");
yield return ("{\"platformOptions\": {\"Troubleshooting\": {\"CrashDump\": {\"Enable\": true}}}}", "platformOptions:Troubleshooting:CrashDump:enable", "True");
yield return ("{\"platformOptions\": {\"Troubleshooting\": {\"CrashDump\": {\"Enable\": true}}}}", "platformOptions:Troubleshooting:CrashDump:Missing", null);
yield return ("{\"platformOptions\": {\"Troubleshooting\": {\"CrashDump\": {\"Enable\": true}}}}", "platformOptions:Troubleshooting:CrashDump", "{\"Enable\": true}");
yield return ("{\"platformOptions\": {\"Troubleshooting\": {\"CrashDump\": {\"Enable\": true} , \"CrashDump2\": {\"Enable\": true}}}}", "platformOptions:Troubleshooting:CrashDump", "{\"Enable\": true}");
yield return ("{\"platformOptions\": {\"Troubleshooting\": {\"CrashDump\": {\"Enable\": true}}}}", "platformOptions:", null);
yield return ("{}", "platformOptions:Troubleshooting:CrashDump:Enable", null);
yield return ("{\"platformOptions\": [1,2] }", "platformOptions:0", "1");
yield return ("{\"platformOptions\": [1,2] }", "platformOptions:1", "2");
yield return ("{\"platformOptions\": [1,2] }", "platformOptions", "[1,2]");
yield return ("{\"platformOptions\": { \"Array\" : [ {\"Key\" : \"Value\"} , {\"Key\" : 3} ] } }", "platformOptions:Array:0", null);
yield return ("{\"platformOptions\": { \"Array\" : [ {\"Key\" : \"Value\"} , {\"Key\" : 3} ] } }", "platformOptions:Array:0:Key", "Value");
yield return ("{\"platformOptions\": { \"Array\" : [ {\"Key\" : \"Value\"} , {\"Key\" : 3} ] } }", "platformOptions:Array:1:Key", "3");
}
[TestMethod]
public async ValueTask InvalidJson_Fail()
{
Mock<IFileSystem> fileSystem = new();
fileSystem.Setup(x => x.ExistFile(It.IsAny<string>())).Returns(true);
fileSystem.Setup(x => x.NewFileStream(It.IsAny<string>(), FileMode.Open, FileAccess.Read)).Returns(() => new MemoryFileStream(Encoding.UTF8.GetBytes(string.Empty)));
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() =>
new JsonConfigurationSource(testApplicationModuleInfo, fileSystem.Object, null));
// The behavior difference is System.Text.Json vs Jsonite
#if NETFRAMEWORK
await Assert.ThrowsAsync<FormatException>(() => configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), [])), ex => ex?.ToString() ?? "No exception was thrown");
#else
await Assert.ThrowsAsync<JsonException>(() => configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), [])), ex => ex?.ToString() ?? "No exception was thrown");
#endif
}
[TestMethod]
[DynamicData(nameof(GetConfigurationValueFromJsonData))]
public async ValueTask GetConfigurationValueFromJsonWithFileLoggerProvider(string jsonFileConfig, string key, string? result)
{
byte[] bytes = Encoding.UTF8.GetBytes(jsonFileConfig);
Mock<IFileSystem> fileSystem = new();
fileSystem.Setup(x => x.ExistFile(It.IsAny<string>())).Returns(true);
fileSystem.Setup(x => x.NewFileStream(It.IsAny<string>(), FileMode.Open, FileAccess.Read))
.Returns(() => new MemoryFileStream(bytes));
Mock<ILogger> loggerMock = new();
loggerMock.Setup(x => x.IsEnabled(LogLevel.Trace)).Returns(true);
Mock<IFileLoggerProvider> loggerProviderMock = new();
loggerProviderMock.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(loggerMock.Object);
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(fileSystem.Object, testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() =>
new JsonConfigurationSource(testApplicationModuleInfo, fileSystem.Object, null));
IConfiguration configuration = await configurationManager.BuildAsync(loggerProviderMock.Object, new CommandLineParseResult(null, new List<CommandLineParseOption>(), []));
Assert.AreEqual(result, configuration[key], $"Expected '{result}' found '{configuration[key]}'");
loggerMock.Verify(x => x.LogAsync(LogLevel.Trace, It.IsAny<string>(), null, LoggingExtensions.Formatter), Times.Once);
}
[TestMethod]
public async ValueTask BuildAsync_EmptyConfigurationSources_ThrowsException()
{
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo, new SystemEnvironment());
await Assert.ThrowsAsync<InvalidOperationException>(() => configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), [])));
}
[TestMethod]
public async ValueTask BuildAsync_ConfigurationSourcesNotEnabledAsync_ThrowsException()
{
Mock<IConfigurationSource> mockConfigurationSource = new();
mockConfigurationSource.Setup(x => x.IsEnabledAsync()).ReturnsAsync(false);
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() => mockConfigurationSource.Object);
await Assert.ThrowsAsync<InvalidOperationException>(() => configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), [])));
mockConfigurationSource.Verify(x => x.IsEnabledAsync(), Times.Once);
}
[TestMethod]
public async ValueTask BuildAsync_ConfigurationSourceIsAsyncInitializableExtension_InitializeAsyncIsCalled()
{
Mock<IConfigurationProvider> mockConfigurationProvider = new();
mockConfigurationProvider.Setup(x => x.LoadAsync()).Callback(() => { });
FakeConfigurationSource fakeConfigurationSource = new()
{
ConfigurationProvider = mockConfigurationProvider.Object,
};
CurrentTestApplicationModuleInfo testApplicationModuleInfo = new(new SystemEnvironment(), new SystemProcessHandler());
ConfigurationManager configurationManager = new(new SystemFileSystem(), testApplicationModuleInfo, new SystemEnvironment());
configurationManager.AddConfigurationSource(() => fakeConfigurationSource);
await Assert.ThrowsAsync<InvalidOperationException>(() => configurationManager.BuildAsync(null, new CommandLineParseResult(null, new List<CommandLineParseOption>(), [])));
}
private class FakeConfigurationSource : IConfigurationSource, IAsyncInitializableExtension
{
public string Uid => nameof(FakeConfigurationSource);
public string Version => "1.0.0";
public string DisplayName => nameof(FakeConfigurationSource);
public string Description => nameof(FakeConfigurationSource);
public required IConfigurationProvider ConfigurationProvider { get; set; }
public int Order => 100;
public Task<IConfigurationProvider> BuildAsync(CommandLineParseResult commandLineParseResult) => Task.FromResult(ConfigurationProvider);
public Task InitializeAsync() => Task.CompletedTask;
public Task<bool> IsEnabledAsync() => Task.FromResult(true);
}
private class MemoryFileStream : IFileStream
{
private readonly MemoryStream _stream;
public MemoryFileStream(byte[] bytes) => _stream = new MemoryStream(bytes);
Stream IFileStream.Stream => _stream;
string IFileStream.Name => string.Empty;
void IDisposable.Dispose()
=> _stream.Dispose();
#if NETCOREAPP
ValueTask IAsyncDisposable.DisposeAsync()
=> _stream.DisposeAsync();
#endif
}
}