-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCliCommandTests.cs
More file actions
397 lines (339 loc) · 14.1 KB
/
Copy pathCliCommandTests.cs
File metadata and controls
397 lines (339 loc) · 14.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System.Diagnostics;
namespace NpgsqlRestTests.CliTests;
public class CliCommandTests
{
private static readonly string DllPath = ResolveDllPath();
private static string ResolveDllPath()
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "NpgsqlRest.sln")))
{
dir = dir.Parent;
}
if (dir is null)
{
throw new InvalidOperationException(
"Could not find NpgsqlRest.sln in parent directories of " + AppContext.BaseDirectory);
}
// Find the built DLL matching the current build configuration.
// AppContext.BaseDirectory is e.g. .../NpgsqlRestTests/bin/Debug/net10.0/
// Extract the configuration (Debug/Release) from that path.
var baseParts = AppContext.BaseDirectory.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries);
var binIndex = Array.LastIndexOf(baseParts, "bin");
var configuration = binIndex >= 0 && binIndex + 1 < baseParts.Length ? baseParts[binIndex + 1] : "Debug";
var dllPath = Path.Combine(dir.FullName, "NpgsqlRestClient", "bin", configuration, "net10.0", "NpgsqlRestClient.dll");
if (!File.Exists(dllPath))
{
throw new InvalidOperationException("NpgsqlRestClient.dll not found at " + dllPath);
}
return dllPath;
}
private static async Task<(string Stdout, string Stderr, int ExitCode)> RunCliAsync(params string[] args)
{
var psi = new ProcessStartInfo
{
FileName = "dotnet",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
psi.ArgumentList.Add(DllPath);
foreach (var arg in args)
{
psi.ArgumentList.Add(arg);
}
using var process = new Process { StartInfo = psi };
process.Start();
var stdoutTask = process.StandardOutput.ReadToEndAsync();
var stderrTask = process.StandardError.ReadToEndAsync();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
await process.WaitForExitAsync(cts.Token);
var stdout = await stdoutTask;
var stderr = await stderrTask;
return (stdout, stderr, process.ExitCode);
}
[Theory]
[InlineData("--help")]
[InlineData("-h")]
[InlineData("/?")]
public async Task Help_WritesUsageToStdout(string flag)
{
var (stdout, stderr, exitCode) = await RunCliAsync(flag);
exitCode.Should().Be(0);
stdout.Should().Contain("Usage:");
stdout.Should().Contain("npgsqlrest");
stderr.Should().BeEmpty();
}
[Theory]
[InlineData("--version")]
[InlineData("-v")]
[InlineData("/v")]
public async Task Version_WritesVersionInfoToStdout(string flag)
{
var (stdout, stderr, exitCode) = await RunCliAsync(flag);
exitCode.Should().Be(0);
stdout.Should().Contain("Versions:");
stdout.Should().Contain("NpgsqlRestClient");
stderr.Should().BeEmpty();
}
[Fact]
public async Task VersionJson_WritesValidJsonToStdout()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--version", "--json");
exitCode.Should().Be(0);
stdout.TrimStart().Should().StartWith("{");
var json = JsonNode.Parse(stdout);
json.Should().NotBeNull();
json!["versions"].Should().NotBeNull();
json["versions"]!["NpgsqlRestClient"].Should().NotBeNull();
stderr.Should().BeEmpty();
}
[Fact]
public async Task Hash_WritesHashedValueToStdout()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--hash", "MyPassword123");
exitCode.Should().Be(0);
var lines = stdout.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var hashLine = lines.Last();
hashLine.Should().NotBe("MyPassword123");
hashLine.Should().NotBeEmpty();
stderr.Should().BeEmpty();
}
[Fact]
public async Task BasicAuth_WritesAuthorizationHeaderToStdout()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--basic_auth", "myuser", "mypass");
exitCode.Should().Be(0);
var expectedBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes("myuser:mypass"));
stdout.Should().Contain($"Authorization: Basic {expectedBase64}");
stderr.Should().BeEmpty();
}
[Fact]
public async Task ConfigSchema_WritesJsonSchemaToStdout()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--config-schema");
exitCode.Should().Be(0);
var json = JsonNode.Parse(stdout);
json.Should().NotBeNull();
(json!["$schema"] is not null || json["properties"] is not null).Should().BeTrue();
// Verify descriptions are present in schema properties
var appNameProp = json["properties"]?["ApplicationName"];
appNameProp.Should().NotBeNull();
appNameProp!["description"].Should().NotBeNull();
stderr.Should().BeEmpty();
}
[Fact]
public async Task Annotations_WritesJsonArrayToStdout()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--annotations");
exitCode.Should().Be(0);
var json = JsonNode.Parse(stdout);
json.Should().NotBeNull();
json.Should().BeOfType<JsonArray>();
json!.AsArray().Count.Should().BeGreaterThan(0);
stderr.Should().BeEmpty();
}
[Fact]
public async Task Config_WithOverride_ShowsUpdatedValue()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--ApplicationName=test", "--config");
exitCode.Should().Be(0);
var json = ParseJsonc(stdout);
json.Should().NotBeNull();
json!["ApplicationName"]?.GetValue<string>().Should().Be("test");
stderr.Should().BeEmpty();
}
[Fact]
public async Task Config_WithCaseInsensitiveOverride_UpdatesExistingKey()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--Applicationname=test", "--config");
exitCode.Should().Be(0);
var json = ParseJsonc(stdout);
json.Should().NotBeNull();
// Should update the existing "ApplicationName" key (canonical casing), not create a new "Applicationname"
json!["ApplicationName"]?.GetValue<string>().Should().Be("test");
json["Applicationname"].Should().BeNull();
stderr.Should().BeEmpty();
}
[Fact]
public async Task Config_WithUnknownKey_DefaultWarningMode_StillDumpsConfig()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--xxx=test", "--config");
exitCode.Should().Be(0);
stderr.Should().Contain("Warning");
stderr.Should().Contain("unknown key");
stderr.Should().Contain("xxx");
stdout.TrimStart().Should().StartWith("{");
var json = ParseJsonc(stdout);
json.Should().NotBeNull();
}
[Fact]
public async Task Config_WithUnknownKey_ErrorMode_FailsValidation()
{
var (stdout, stderr, exitCode) = await RunCliAsync(
"--Config:ValidateConfigKeys=Error",
"--xxx=test",
"--config");
exitCode.Should().Be(1);
stderr.Should().Contain("failed");
stderr.Should().Contain("unknown key");
stderr.Should().Contain("xxx");
stdout.Should().BeEmpty();
}
[Fact]
public async Task Config_WithUnknownKey_IgnoreMode_DumpsConfigSilently()
{
var (stdout, stderr, exitCode) = await RunCliAsync(
"--Config:ValidateConfigKeys=Ignore",
"--xxx=test",
"--config");
exitCode.Should().Be(0);
stderr.Should().BeEmpty();
stdout.TrimStart().Should().StartWith("{");
}
[Fact]
public async Task Validate_Json_WithUnknownKey_DefaultWarningMode_ConfigStillValid()
{
var (stdout, _, _) = await RunCliAsync("--xxx=test", "--validate", "--json");
var json = JsonNode.Parse(stdout);
json.Should().NotBeNull();
json!["configValid"]!.GetValue<bool>().Should().BeTrue();
json["warningsAreFatal"]!.GetValue<bool>().Should().BeFalse();
json["validationMode"]!.GetValue<string>().Should().Be("Warning");
json["warnings"]!.AsArray().Count.Should().BeGreaterThan(0);
}
[Fact]
public async Task Validate_Json_WithUnknownKey_ErrorMode_ConfigInvalid()
{
var (stdout, _, _) = await RunCliAsync(
"--Config:ValidateConfigKeys=Error",
"--xxx=test",
"--validate",
"--json");
var json = JsonNode.Parse(stdout);
json.Should().NotBeNull();
json!["configValid"]!.GetValue<bool>().Should().BeFalse();
json["warningsAreFatal"]!.GetValue<bool>().Should().BeTrue();
json["validationMode"]!.GetValue<string>().Should().Be("Error");
json["warnings"]!.AsArray().Count.Should().BeGreaterThan(0);
}
[Fact]
public async Task Config_OutputContainsComments()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--config");
exitCode.Should().Be(0);
// JSONC output should contain // comments
stdout.Should().Contain("//");
// Verify it's parseable as JSONC
var json = ParseJsonc(stdout);
json.Should().NotBeNull();
json!.AsObject().ContainsKey("ApplicationName").Should().BeTrue();
stderr.Should().BeEmpty();
}
[Fact]
public async Task Config_OutputMatchesAppsettingsJson()
{
// Run --config from a temp directory (no appsettings.json) to get pure defaults
var tempDir = Path.Combine(Path.GetTempPath(), $"npgsqlrest_test_{Guid.NewGuid():N}");
Directory.CreateDirectory(tempDir);
try
{
var psi = new ProcessStartInfo
{
FileName = "dotnet",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = tempDir,
};
psi.ArgumentList.Add(DllPath);
psi.ArgumentList.Add("--config");
using var process = new Process { StartInfo = psi };
process.Start();
var stdoutTask = process.StandardOutput.ReadToEndAsync();
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
await process.WaitForExitAsync(cts.Token);
var configOutput = await stdoutTask;
process.ExitCode.Should().Be(0);
// Resolve appsettings.json path
var dir = new DirectoryInfo(AppContext.BaseDirectory);
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "NpgsqlRest.sln")))
{
dir = dir.Parent;
}
dir.Should().NotBeNull("NpgsqlRest.sln must be found");
var appsettingsPath = Path.Combine(dir!.FullName, "NpgsqlRestClient", "appsettings.json");
File.Exists(appsettingsPath).Should().BeTrue($"appsettings.json must exist at {appsettingsPath}");
var appsettingsContent = await File.ReadAllTextAsync(appsettingsPath);
// Normalize line endings for comparison
configOutput = configOutput.Replace("\r\n", "\n").TrimEnd();
appsettingsContent = appsettingsContent.Replace("\r\n", "\n").TrimEnd();
configOutput.Should().Be(appsettingsContent, "the --config output (defaults) should be identical to the checked-in appsettings.json");
}
finally
{
Directory.Delete(tempDir, recursive: true);
}
}
[Fact]
public async Task InvalidArg_WritesErrorToStderr()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--invalid-arg");
exitCode.Should().Be(0);
stderr.Should().Contain("Unknown parameter --invalid-arg");
stderr.Should().Contain("--help");
}
[Theory]
[InlineData("--config", "cors")]
[InlineData("--config=cors")]
public async Task Config_WithFilter_ShowsFilteredOutput(string arg1, string? arg2 = null)
{
var args = arg2 is not null ? new[] { arg1, arg2 } : new[] { arg1 };
var (stdout, stderr, exitCode) = await RunCliAsync(args);
exitCode.Should().Be(0);
// Output is valid JSONC wrapped in { }
stdout.TrimStart().Should().StartWith("{");
stdout.TrimEnd().Should().EndWith("}");
var parsed = ParseJsonc(stdout);
parsed.Should().NotBeNull("filtered config should be valid JSONC");
// Cors section preserved with children
parsed!["Cors"].Should().NotBeNull();
parsed["Cors"]!["AllowedOrigins"].Should().NotBeNull();
parsed["Cors"]!["AllowedMethods"].Should().NotBeNull();
// Should NOT contain unrelated sections
parsed["ThreadPool"].Should().BeNull();
parsed["Urls"].Should().BeNull();
}
[Fact]
public async Task Config_WithFilter_PartialSection()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--config", "minworker");
exitCode.Should().Be(0);
var parsed = ParseJsonc(stdout);
parsed.Should().NotBeNull();
// ThreadPool section should exist (as wrapper for matched key)
var threadPool = parsed!["ThreadPool"]!.AsObject();
threadPool.Should().NotBeNull();
threadPool.ContainsKey("MinWorkerThreads").Should().BeTrue();
// Other ThreadPool keys should NOT be present
threadPool.ContainsKey("MaxWorkerThreads").Should().BeFalse();
threadPool.ContainsKey("MinCompletionPortThreads").Should().BeFalse();
}
[Fact]
public async Task Config_WithFilter_NoMatch_ShowsNoResults()
{
var (stdout, stderr, exitCode) = await RunCliAsync("--config", "xyz_absolutely_no_match_here");
exitCode.Should().Be(0);
stdout.Should().Contain("No results for");
stdout.Should().Contain("xyz_absolutely_no_match_here");
}
private static JsonNode? ParseJsonc(string jsonc)
{
return JsonNode.Parse(jsonc, documentOptions: new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip
});
}
}