Skip to content

Commit

Permalink
feat(cli): print message into stdout if failed
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Feb 26, 2021
1 parent 78c629d commit d289922
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
using NineChronicles.Headless.Executable.Commands;
using NineChronicles.Headless.Executable.Tests.IO;
using Xunit;

namespace NineChronicles.Headless.Executable.Tests.Commands
{
public class ValidationCommandTest
{
private readonly StringIOConsole _console;
private readonly ValidationCommand _command;

public ValidationCommandTest()
{
_command = new ValidationCommand();
_console = new StringIOConsole();
_command = new ValidationCommand(_console);
}

[Theory]
[InlineData("", -1)]
[InlineData("invalid hexadecimal", -1)]
[InlineData("0000000000000000000000000000000000000000000000000000000000000000", -1)]
[InlineData("ab8d591ccdcce263c39eb1f353e44b64869f0afea2df643bf6839ebde650d244", 0)]
[InlineData("d6c3e0d525dac340a132ae05aaa9f3e278d61b70d2b71326570e64aee249e566", 0)]
[InlineData("761f68d68426549df5904395b5ca5bce64a3da759085d8565242db42a5a1b0b9", 0)]
public void PrivateKey(string privateKeyHex, int exitCode)
[InlineData("", -1, "The given private key, '', had an issue during parsing.\n")]
[InlineData("invalid hexadecimal", -1, "The given private key, 'invalid hexadecimal', had an issue during parsing.\n")]
[InlineData("000000000000000000000000000000000000000000000000000000000000000000", -1, "The given private key, '000000000000000000000000000000000000000000000000000000000000000000', had an issue during parsing.\n")]
[InlineData("ab8d591ccdcce263c39eb1f353e44b64869f0afea2df643bf6839ebde650d244", 0, "")]
[InlineData("d6c3e0d525dac340a132ae05aaa9f3e278d61b70d2b71326570e64aee249e566", 0, "")]
[InlineData("761f68d68426549df5904395b5ca5bce64a3da759085d8565242db42a5a1b0b9", 0, "")]
public void PrivateKey(string privateKeyHex, int exitCode, string errorOutput)
{
Assert.Equal(exitCode, _command.PrivateKey(privateKeyHex));
Assert.Equal(errorOutput, _console.Error.ToString());
}

[Theory]
[InlineData("", -1)]
[InlineData("invalid hexadecimal", -1)]
[InlineData("000000000000000000000000000000000000000000000000000000000000000000", -1)]
[InlineData("03b0868d9301b30c512d307ea67af4c8bef637ef099e39d32b808a43e6b41469c5", 0)]
[InlineData("03308c1618a75e85a5fb57f7e453a642c307dc6310e90a7418b1aec565d963534a", 0)]
[InlineData("028a6190bf643175b20e4a2d1d86fe6c4b8f7d5fe3d163632be4e59f83335824b8", 0)]
public void PublicKey(string publicKeyHex, int exitCode)
[InlineData("", -1, "The given public key, '', had an issue during parsing.\n")]
[InlineData("invalid hexadecimal", -1, "The given public key, 'invalid hexadecimal', had an issue during parsing.\n")]
[InlineData("000000000000000000000000000000000000000000000000000000000000000000", -1, "The given public key, '000000000000000000000000000000000000000000000000000000000000000000', had an issue during parsing.\n")]
[InlineData("03b0868d9301b30c512d307ea67af4c8bef637ef099e39d32b808a43e6b41469c5", 0, "")]
[InlineData("03308c1618a75e85a5fb57f7e453a642c307dc6310e90a7418b1aec565d963534a", 0, "")]
[InlineData("028a6190bf643175b20e4a2d1d86fe6c4b8f7d5fe3d163632be4e59f83335824b8", 0, "")]
public void PublicKey(string publicKeyHex, int exitCode, string errorOutput)
{
Assert.Equal(exitCode, _command.PublicKey(publicKeyHex));
Assert.Equal(errorOutput, _console.Error.ToString());
}
}
}
32 changes: 32 additions & 0 deletions NineChronicles.Headless.Executable.Tests/IO/StringIOConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.IO;
using NineChronicles.Headless.Executable.IO;

namespace NineChronicles.Headless.Executable.Tests.IO
{
public sealed class StringIOConsole : IConsole
{
public StringIOConsole(StringReader @in, StringWriter @out, StringWriter error)
{
In = @in;
Out = @out;
Error = error;
}

public StringIOConsole(string input = "")
: this(new StringReader(input), new StringWriter(), new StringWriter())
{
}

public StringReader In { get; }

public StringWriter Out { get; }

public StringWriter Error { get; }

TextReader IConsole.In => In;

TextWriter IConsole.Out => Out;

TextWriter IConsole.Error => Error;
}
}
10 changes: 10 additions & 0 deletions NineChronicles.Headless.Executable/Commands/ValidationCommand.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
using Cocona;
using Libplanet;
using Libplanet.Crypto;
using NineChronicles.Headless.Executable.IO;

namespace NineChronicles.Headless.Executable.Commands
{
public class ValidationCommand : CoconaLiteConsoleAppBase
{
private readonly IConsole _console;

public ValidationCommand(IConsole console)
{
_console = console;
}

[Command(Description = "Validate private key")]
public int PrivateKey(
[Argument(
Expand All @@ -20,6 +28,7 @@ public int PrivateKey(
}
catch
{
_console.Error.WriteLine($"The given private key, '{privateKeyHex}', had an issue during parsing.");
return -1;
}
}
Expand All @@ -38,6 +47,7 @@ public int PublicKey(
}
catch
{
_console.Error.WriteLine($"The given public key, '{publicKeyHex}', had an issue during parsing.");
return -1;
}
}
Expand Down
11 changes: 11 additions & 0 deletions NineChronicles.Headless.Executable/IO/IConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.IO;

namespace NineChronicles.Headless.Executable.IO
{
public interface IConsole
{
TextReader In { get; }
TextWriter Out { get; }
TextWriter Error { get; }
}
}
21 changes: 21 additions & 0 deletions NineChronicles.Headless.Executable/IO/StandardConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.IO;

namespace NineChronicles.Headless.Executable.IO
{
public class StandardConsole : IConsole
{
public StandardConsole()
{
In = Console.In;
Out = Console.Out;
Error = Console.Error;
}

public TextReader In { get; }

public TextWriter Out { get; }

public TextWriter Error { get; }
}
}
5 changes: 4 additions & 1 deletion NineChronicles.Headless.Executable/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Libplanet.KeyStore;
using Microsoft.Extensions.Hosting;
using NineChronicles.Headless.Executable.Commands;
using NineChronicles.Headless.Executable.IO;
using NineChronicles.Headless.Properties;
using Org.BouncyCastle.Security;
using Sentry;
Expand All @@ -35,7 +36,9 @@ static async Task Main(string[] args)
#if SENTRY || ! DEBUG
using var _ = SentrySdk.Init(ConfigureSentryOptions);
#endif
await CoconaLiteApp.RunAsync<Program>(args);
await CoconaLiteApp.Create()
.ConfigureServices(services => services.AddSingleton<IConsole, StandardConsole>())
.RunAsync<Program>(args);
}

static void ConfigureSentryOptions(SentryOptions o)
Expand Down

0 comments on commit d289922

Please sign in to comment.