Skip to content

Commit 687b492

Browse files
committed
Add FileArgument tests, update readme
1 parent 0c7c090 commit 687b492

File tree

3 files changed

+103
-5
lines changed

3 files changed

+103
-5
lines changed

README.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Options:
2222
Commands:
2323
merge <file> <files> merge two or more json files into one
2424
set <file> <key> <value> set a value in a json file
25+
remove <file> <key> Remove a value from the json
2526
```
2627

2728
### Commands
@@ -38,11 +39,11 @@ Usage:
3839
dotnet-json merge [options] <file> <files>...
3940
4041
Arguments:
41-
<file> The name of the first file (use '-' to read from STDIN)
42+
<file> The JSON file (use '-' for STDIN)
4243
<files> The names of the files to merge with the first file.
4344
4445
Options:
45-
-o, --output <file> The filename to write the merge result into, leave out to write back into the first file (use '-' to write to STDOUT).
46+
-o, --output <file> The output file (use '-' for STDOUT, defaults to <file>)
4647
-?, -h, --help Show help and usage information
4748
```
4849

@@ -58,10 +59,31 @@ Usage:
5859
dotnet-json set [options] <file> <key> <value>
5960
6061
Arguments:
61-
<file> The JSON file (use '-' to read from STDIN and write to STDOUT)
62+
<file> The JSON file (use '-' for STDIN)
6263
<key> The key to set (use ':' to set nested object and use index numbers to set array values eg. nested:key or nested:1:key)
6364
<value> The value to set
6465
6566
Options:
66-
-?, -h, --help Show help and usage information
67+
-o, --output <file> The output file (use '-' for STDOUT, defaults to <file>)
68+
-?, -h, --help Show help and usage information
69+
```
70+
71+
#### remove
72+
73+
Updates the json file to remove a key from the file. Use `:` as separator for nested objects.
74+
75+
```
76+
remove:
77+
Remove a value from the json
78+
79+
Usage:
80+
dotnet-json remove [options] <file> <key>
81+
82+
Arguments:
83+
<file> The JSON file (use '-' for STDIN)
84+
<key> The JSON key to remove
85+
86+
Options:
87+
-o, --output <file> The output file (use '-' for STDOUT, defaults to <file>)
88+
-?, -h, --help Show help and usage information
6789
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System;
2+
using System.CommandLine;
3+
using System.CommandLine.Invocation;
4+
using System.CommandLine.IO;
5+
using System.IO;
6+
using System.Threading.Tasks;
7+
using dotnet_json.Commands;
8+
using FluentAssertions;
9+
using Xunit;
10+
11+
namespace dotnet_json.Tests
12+
{
13+
public class FileArgumentTests
14+
{
15+
[Fact]
16+
public async Task SucceedsWhenFileExists()
17+
{
18+
var tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
19+
Directory.CreateDirectory(tmpDir);
20+
21+
try
22+
{
23+
var filename = Path.Combine(tmpDir, "file.json");
24+
await File.WriteAllTextAsync(Path.Combine(tmpDir, filename), "{}");
25+
26+
var (exitCode, console) = await RunCommand(filename);
27+
28+
exitCode.Should().Be(0);
29+
console.Error.ToString().Should().BeEmpty();
30+
console.Out.ToString().Should().Contain($"Success {filename}");
31+
}
32+
finally
33+
{
34+
Directory.Delete(tmpDir, true);
35+
}
36+
}
37+
38+
[Fact]
39+
public async Task SucceedsWithStandardInputOutput()
40+
{
41+
var (exitCode, console) = await RunCommand("-");
42+
43+
exitCode.Should().Be(0);
44+
console.Error.ToString().Should().BeEmpty();
45+
console.Out.ToString().Should().Contain("Success -");
46+
}
47+
48+
[Fact]
49+
public async Task ThrowsWhenFileDoesNotExist()
50+
{
51+
var (exitCode, console) = await RunCommand("this-file-does-not-exist.json");
52+
53+
exitCode.Should().NotBe(0);
54+
console.Error.ToString().Should().Contain("File does not exist: this-file-does-not-exist.json");
55+
}
56+
57+
private async Task<(int ExitCode, IConsole Console)> RunCommand(string filename)
58+
{
59+
var file = new FileArgument("file");
60+
61+
var command = new RootCommand();
62+
command.AddArgument(file);
63+
64+
var console = new TestConsole();
65+
66+
command.Handler = CommandHandler.Create((string file) =>
67+
{
68+
console.Out.Write("Success " + file);
69+
return 0;
70+
});
71+
72+
var exitCode = await command.InvokeAsync(filename, console);
73+
return (exitCode, console);
74+
}
75+
}
76+
}

dotnet-json/Commands/MergeCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected override async Task<int> ExecuteAsync()
4242
return 0;
4343
}
4444

45-
private Stream GetStream(string filename) => filename switch
45+
private static Stream GetStream(string filename) => filename switch
4646
{
4747
"-" => Console.OpenStandardInput(),
4848
_ => File.OpenRead(filename),

0 commit comments

Comments
 (0)