-
Notifications
You must be signed in to change notification settings - Fork 850
Expand file tree
/
Copy pathConfigurationServiceTests.cs
More file actions
290 lines (222 loc) · 10.1 KB
/
ConfigurationServiceTests.cs
File metadata and controls
290 lines (222 loc) · 10.1 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text.Json;
using System.Text.Json.Nodes;
using Aspire.Cli.Configuration;
using Aspire.Cli.Tests.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging.Abstractions;
namespace Aspire.Cli.Tests.Configuration;
public class ConfigurationServiceTests(ITestOutputHelper outputHelper)
{
private static (ConfigurationService Service, string SettingsFilePath) CreateService(
TemporaryWorkspace workspace,
string? existingContent = null)
{
var globalSettingsDir = workspace.CreateDirectory(".aspire-global");
var globalSettingsFile = new FileInfo(Path.Combine(globalSettingsDir.FullName, AspireConfigFile.FileName));
var settingsFilePath = Path.Combine(workspace.WorkspaceRoot.FullName, AspireConfigFile.FileName);
if (existingContent is not null)
{
File.WriteAllText(settingsFilePath, existingContent);
}
var logsDir = new DirectoryInfo(Path.Combine(workspace.WorkspaceRoot.FullName, "logs"));
var executionContext = new CliExecutionContext(
workspace.WorkspaceRoot,
new DirectoryInfo(Path.Combine(workspace.WorkspaceRoot.FullName, ".aspire", "hives")),
new DirectoryInfo(Path.Combine(workspace.WorkspaceRoot.FullName, ".aspire", "cache")),
new DirectoryInfo(Path.Combine(Path.GetTempPath(), "aspire-test-runtimes")),
logsDir,
"test.log");
var configBuilder = new ConfigurationBuilder();
var configuration = configBuilder.Build();
var logger = NullLogger<ConfigurationService>.Instance;
var service = new ConfigurationService(configuration, executionContext, globalSettingsFile, logger);
return (service, settingsFilePath);
}
[Fact]
public async Task SetConfigurationAsync_WorksWithJsonComments()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var contentWithComments = """
{
// This is a comment about the apphost
"appHost": {
"path": "MyApp.csproj" // path to the project
}
}
""";
var (service, settingsFilePath) = CreateService(workspace, contentWithComments);
await service.SetConfigurationAsync("channel", "daily", isGlobal: false);
var result = File.ReadAllText(settingsFilePath);
Assert.Contains("daily", result);
Assert.Contains("appHost", result);
}
[Fact]
public async Task SetConfigurationAsync_WorksWithTrailingCommas()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var contentWithTrailingCommas = """
{
"appHost": {
"path": "MyApp.csproj",
},
"channel": "stable",
}
""";
var (service, settingsFilePath) = CreateService(workspace, contentWithTrailingCommas);
await service.SetConfigurationAsync("features.polyglotSupportEnabled", "true", isGlobal: false);
var result = File.ReadAllText(settingsFilePath);
Assert.Contains("polyglotSupportEnabled", result);
}
[Fact]
public async Task SetConfigurationAsync_WorksWithCommentsAndTrailingCommas()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var content = """
{
// Comment
"appHost": {
"path": "MyApp.csproj", // trailing comma
},
}
""";
var (service, settingsFilePath) = CreateService(workspace, content);
await service.SetConfigurationAsync("channel", "daily", isGlobal: false);
var result = File.ReadAllText(settingsFilePath);
Assert.Contains("daily", result);
}
[Fact]
public async Task SetConfigurationAsync_CreatesNewFile_WhenNoneExists()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
// Delete the sentinel .aspire/settings.json so there is truly no settings file
var sentinelPath = Path.Combine(workspace.WorkspaceRoot.FullName, ".aspire", "settings.json");
if (File.Exists(sentinelPath))
{
File.Delete(sentinelPath);
}
var (service, settingsFilePath) = CreateService(workspace);
await service.SetConfigurationAsync("channel", "staging", isGlobal: false);
Assert.True(File.Exists(settingsFilePath));
var result = File.ReadAllText(settingsFilePath);
Assert.Contains("staging", result);
}
[Fact]
public async Task SetConfigurationAsync_HandlesEmptyFile()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var (service, settingsFilePath) = CreateService(workspace, "");
await service.SetConfigurationAsync("channel", "daily", isGlobal: false);
var result = File.ReadAllText(settingsFilePath);
Assert.Contains("daily", result);
}
[Fact]
public async Task DeleteConfigurationAsync_WorksWithJsonComments()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var contentWithComments = """
{
// Comment
"channel": "daily",
"appHost": { "path": "MyApp.csproj" }
}
""";
var (service, settingsFilePath) = CreateService(workspace, contentWithComments);
var deleted = await service.DeleteConfigurationAsync("channel", isGlobal: false);
Assert.True(deleted);
var result = File.ReadAllText(settingsFilePath);
Assert.DoesNotContain("daily", result);
Assert.Contains("appHost", result);
}
[Fact]
public async Task DeleteConfigurationAsync_ReturnsFalse_WhenFileDoesNotExist()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var (service, _) = CreateService(workspace);
var deleted = await service.DeleteConfigurationAsync("channel", isGlobal: false);
Assert.False(deleted);
}
[Fact]
public async Task DeleteConfigurationAsync_ReturnsFalse_WhenFileIsEmpty()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var (service, _) = CreateService(workspace, "");
var deleted = await service.DeleteConfigurationAsync("channel", isGlobal: false);
Assert.False(deleted);
}
[Fact]
public async Task GetAllConfigurationAsync_ParsesCommentsCorrectly()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var contentWithComments = """
{
// This config has comments
"channel": "daily",
"features": {
"polyglotSupportEnabled": true // enabled for testing
}
}
""";
var (service, _) = CreateService(workspace, contentWithComments);
var config = await service.GetAllConfigurationAsync();
Assert.Contains("channel", config.Keys);
Assert.Equal("daily", config["channel"]);
}
[Fact]
public async Task SetConfigurationAsync_SetsNestedValues()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var (service, settingsFilePath) = CreateService(workspace, "{}");
await service.SetConfigurationAsync("appHost.path", "MyApp/MyApp.csproj", isGlobal: false);
var result = File.ReadAllText(settingsFilePath);
Assert.Contains("appHost", result);
Assert.Contains("MyApp/MyApp.csproj", result);
}
[Fact]
public async Task SetConfigurationAsync_WritesBooleanStringAsJsonString()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var (service, settingsFilePath) = CreateService(workspace, "{}");
await service.SetConfigurationAsync("features.polyglotSupportEnabled", "true", isGlobal: false);
// Value is written as a JSON string "true", not a JSON boolean true.
// The FlexibleBooleanConverter handles parsing "true" -> bool on read.
var json = JsonNode.Parse(File.ReadAllText(settingsFilePath));
var node = json!["features"]!["polyglotSupportEnabled"];
Assert.Equal(JsonValueKind.String, node!.GetValueKind());
Assert.Equal("true", node.GetValue<string>());
// Verify round-trip through AspireConfigFile.Load still works
var config = AspireConfigFile.Load(workspace.WorkspaceRoot.FullName);
Assert.NotNull(config?.Features);
Assert.True(config.Features["polyglotSupportEnabled"]);
}
[Fact]
public async Task SetConfigurationAsync_ChannelWithBooleanLikeValue_StaysAsString()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var (service, settingsFilePath) = CreateService(workspace, "{}");
// "true" is a valid channel value and must remain a string in JSON
// to avoid corrupting the string-typed Channel property.
await service.SetConfigurationAsync("channel", "true", isGlobal: false);
// Must be a JSON string "true", not a JSON boolean true
var json = JsonNode.Parse(File.ReadAllText(settingsFilePath));
var node = json!["channel"];
Assert.Equal(JsonValueKind.String, node!.GetValueKind());
Assert.Equal("true", node.GetValue<string>());
// Verify it round-trips correctly through AspireConfigFile.Load
var config = AspireConfigFile.Load(workspace.WorkspaceRoot.FullName);
Assert.NotNull(config);
Assert.Equal("true", config.Channel);
}
[Fact]
public async Task SetConfigurationAsync_WritesStringValueAsString()
{
using var workspace = TemporaryWorkspace.Create(outputHelper);
var (service, settingsFilePath) = CreateService(workspace, "{}");
await service.SetConfigurationAsync("channel", "daily", isGlobal: false);
var json = JsonNode.Parse(File.ReadAllText(settingsFilePath));
var node = json!["channel"];
Assert.Equal(JsonValueKind.String, node!.GetValueKind());
Assert.Equal("daily", node.GetValue<string>());
}
}