|
| 1 | +using System; |
| 2 | +using System.CommandLine; |
| 3 | +using System.CommandLine.IO; |
| 4 | +using System.IO; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using dotnet_json.Commands; |
| 7 | +using FluentAssertions; |
| 8 | +using Newtonsoft.Json; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace dotnet_json.Tests.Commands |
| 13 | +{ |
| 14 | + public class SetCommandTests : IDisposable |
| 15 | + { |
| 16 | + private readonly string _tmpDir; |
| 17 | + |
| 18 | + public SetCommandTests() |
| 19 | + { |
| 20 | + _tmpDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 21 | + Directory.CreateDirectory(_tmpDir); |
| 22 | + } |
| 23 | + |
| 24 | + public void Dispose() |
| 25 | + { |
| 26 | + Directory.Delete(_tmpDir, true); |
| 27 | + } |
| 28 | + |
| 29 | + [Theory] |
| 30 | + [InlineData(@"{""key"": ""value""}", "key", "test")] |
| 31 | + [InlineData(@"{""key"": ""value""}", "key", "")] |
| 32 | + public async Task SavesNewValueToFile(string json, string key, string value) |
| 33 | + { |
| 34 | + await File.WriteAllTextAsync(Path.Join(_tmpDir, "test.json"), json); |
| 35 | + |
| 36 | + var (exitCode, _) = await RunCommand(Path.Join(_tmpDir, "test.json"), key, value); |
| 37 | + |
| 38 | + exitCode.Should().Be(0); |
| 39 | + |
| 40 | + var contents = await File.ReadAllTextAsync(Path.Join(_tmpDir, "test.json")); |
| 41 | + var result = JObject.Parse(contents)[key].ToString(); |
| 42 | + |
| 43 | + result.Should().Be(value); |
| 44 | + } |
| 45 | + |
| 46 | + private async Task<(int exitCode, IConsole console)> RunCommand(params string[] arguments) |
| 47 | + { |
| 48 | + var command = new SetCommand(); |
| 49 | + |
| 50 | + var console = new TestConsole(); |
| 51 | + |
| 52 | + var exitCode = await command.InvokeAsync(arguments, console); |
| 53 | + |
| 54 | + return (exitCode, console); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments