Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use System.CommandLine to parse cmdline args and allow provider to offer cmds #273

Merged
merged 2 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ git credential-manager-core [<command> [<args>]]

## Commands

### help / --help
### --help / -h / -?

Displays a list of available commands.

### version / --version
### --version

Displays the current version.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ namespace Microsoft.Git.CredentialManager.Tests.Commands
{
public class ConfigureCommandTests
{
[Theory]
[InlineData("configure", true)]
[InlineData("CONFIGURE", true)]
[InlineData("cOnFiGuRe", true)]
[InlineData("get", false)]
[InlineData("store", false)]
[InlineData("unconfigure", false)]
[InlineData("", false)]
[InlineData(null, false)]
public void ConfigureCommand_CanExecuteAsync(string argString, bool expected)
{
var command = new ConfigureCommand(Mock.Of<IConfigurationService>());

bool result = command.CanExecute(argString?.Split(null));

Assert.Equal(expected, result);
}

[Fact]
public async Task ConfigureCommand_ExecuteAsync_User_InvokesConfigurationServiceConfigureUser()
{
Expand All @@ -37,11 +19,9 @@ public async Task ConfigureCommand_ExecuteAsync_User_InvokesConfigurationService
.Verifiable();

var context = new TestCommandContext();
var command = new ConfigureCommand(context, configService.Object);

string[] cmdArgs = {"configure"};
var command = new ConfigureCommand(configService.Object);

await command.ExecuteAsync(context, cmdArgs);
await command.ExecuteAsync(false);

configService.Verify(x => x.ConfigureAsync(ConfigurationTarget.User), Times.Once);
}
Expand All @@ -55,11 +35,9 @@ public async Task ConfigureCommand_ExecuteAsync_System_InvokesConfigurationServi
.Verifiable();

var context = new TestCommandContext();
var command = new ConfigureCommand(context, configService.Object);

string[] cmdArgs = {"configure", "--system"};
var command = new ConfigureCommand(configService.Object);

await command.ExecuteAsync(context, cmdArgs);
await command.ExecuteAsync(true);

configService.Verify(x => x.ConfigureAsync(ConfigurationTarget.System), Times.Once);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@ namespace Microsoft.Git.CredentialManager.Tests.Commands
{
public class EraseCommandTests
{
[Theory]
[InlineData("erase", true)]
[InlineData("ERASE", true)]
[InlineData("eRaSe", true)]
[InlineData("get", false)]
[InlineData("store", false)]
[InlineData("foobar", false)]
[InlineData("", false)]
[InlineData(null, false)]
public void EraseCommand_CanExecuteAsync(string argString, bool expected)
{
var command = new EraseCommand(Mock.Of<IHostProviderRegistry>());

bool result = command.CanExecute(argString?.Split(null));

Assert.Equal(expected, result);
}

[Fact]
public async Task EraseCommand_ExecuteAsync_CallsHostProvider()
{
Expand All @@ -50,10 +32,9 @@ public async Task EraseCommand_ExecuteAsync_CallsHostProvider()
Streams = {In = stdin}
};

string[] cmdArgs = {"erase"};
var command = new EraseCommand(providerRegistry);
var command = new EraseCommand(context, providerRegistry);

await command.ExecuteAsync(context, cmdArgs);
await command.ExecuteAsync();

providerMock.Verify(
x => x.EraseCredentialAsync(It.Is<InputArguments>(y => AreInputArgumentsEquivalent(expectedInput, y))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,6 @@ namespace Microsoft.Git.CredentialManager.Tests.Commands
{
public class GetCommandTests
{
[Theory]
[InlineData("get", true)]
[InlineData("GET", true)]
[InlineData("gEt", true)]
[InlineData("erase", false)]
[InlineData("store", false)]
[InlineData("foobar", false)]
[InlineData("", false)]
[InlineData(null, false)]
public void GetCommand_CanExecuteAsync(string argString, bool expected)
{
var command = new GetCommand(Mock.Of<IHostProviderRegistry>());

bool result = command.CanExecute(argString?.Split(null));

if (expected)
{
Assert.True(result);
}
else
{
Assert.False(result);
}
}

[Fact]
public async Task GetCommand_ExecuteAsync_CallsHostProviderAndWritesCredential()
{
Expand All @@ -56,10 +31,9 @@ public async Task GetCommand_ExecuteAsync_CallsHostProviderAndWritesCredential()
var providerRegistry = new TestHostProviderRegistry {Provider = providerMock.Object};
var context = new TestCommandContext();

string[] cmdArgs = {"get"};
var command = new GetCommand(providerRegistry);
var command = new GetCommand(context, providerRegistry);

await command.ExecuteAsync(context, cmdArgs);
await command.ExecuteAsync();

IDictionary<string, string> actualStdOutDict = ParseDictionary(context.Streams.Out);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

namespace Microsoft.Git.CredentialManager.Tests.Commands
{
public class HostProviderCommandBaseTests
public class GitCommandBaseTests
{
[Fact]
public async Task HostProviderCommandBase_ExecuteAsync_CallsExecuteInternalAsyncWithCorrectArgs()
public async Task GitCommandBase_ExecuteAsync_CallsExecuteInternalAsyncWithCorrectArgs()
{
var mockContext = new Mock<ICommandContext>();
var mockStreams = new Mock<IStandardStreams>();
Expand All @@ -34,23 +34,22 @@ public async Task HostProviderCommandBase_ExecuteAsync_CallsExecuteInternalAsync
mockContext.Setup(x => x.Trace).Returns(Mock.Of<ITrace>());
mockContext.Setup(x => x.Settings).Returns(Mock.Of<ISettings>());

HostProviderCommandBase testCommand = new TestCommand(mockHostRegistry.Object)
GitCommandBase testCommand = new TestCommand(mockContext.Object, mockHostRegistry.Object)
{
VerifyExecuteInternalAsync = (context, input, provider) =>
VerifyExecuteInternalAsync = (input, provider) =>
{
Assert.Same(mockContext.Object, context);
Assert.Same(mockProvider.Object, provider);
Assert.Equal("test", input.Protocol);
Assert.Equal("example.com", input.Host);
Assert.Equal("a/b/c", input.Path);
}
};

await testCommand.ExecuteAsync(mockContext.Object, new string[0]);
await testCommand.ExecuteAsync();
}

[Fact]
public async Task HostProviderCommandBase_ExecuteAsync_ConfiguresSettingsRemoteUri()
public async Task GitCommandBase_ExecuteAsync_ConfiguresSettingsRemoteUri()
{
var mockContext = new Mock<ICommandContext>();
var mockStreams = new Mock<IStandardStreams>();
Expand All @@ -73,29 +72,27 @@ public async Task HostProviderCommandBase_ExecuteAsync_ConfiguresSettingsRemoteU
mockContext.Setup(x => x.Trace).Returns(Mock.Of<ITrace>());
mockContext.Setup(x => x.Settings).Returns(mockSettings.Object);

HostProviderCommandBase testCommand = new TestCommand(mockHostRegistry.Object);
GitCommandBase testCommand = new TestCommand(mockContext.Object, mockHostRegistry.Object);

await testCommand.ExecuteAsync(mockContext.Object, new string[0]);
await testCommand.ExecuteAsync();

Assert.Equal(remoteUri, mockSettings.Object.RemoteUri);
}

private class TestCommand : HostProviderCommandBase
private class TestCommand : GitCommandBase
{
public TestCommand(IHostProviderRegistry hostProviderRegistry)
: base(hostProviderRegistry)
public TestCommand(ICommandContext context, IHostProviderRegistry hostProviderRegistry)
: base(context, "test", null, hostProviderRegistry)
{
}

protected override string Name { get; }

protected override Task ExecuteInternalAsync(ICommandContext context, InputArguments input, IHostProvider provider)
protected override Task ExecuteInternalAsync(InputArguments input, IHostProvider provider)
{
VerifyExecuteInternalAsync?.Invoke(context, input, provider);
VerifyExecuteInternalAsync?.Invoke(input, provider);
return Task.CompletedTask;
}

public Action<ICommandContext, InputArguments, IHostProvider> VerifyExecuteInternalAsync { get; set; }
public Action<InputArguments, IHostProvider> VerifyExecuteInternalAsync { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,7 @@
namespace Microsoft.Git.CredentialManager.Tests.Commands
{
public class StoreCommandTests
{
[Theory]
[InlineData("store", true)]
[InlineData("STORE", true)]
[InlineData("sToRe", true)]
[InlineData("get", false)]
[InlineData("erase", false)]
[InlineData("foobar", false)]
[InlineData("", false)]
[InlineData(null, false)]
public void StoreCommand_CanExecuteAsync(string argString, bool expected)
{
var command = new StoreCommand(Mock.Of<IHostProviderRegistry>());

bool result = command.CanExecute(argString?.Split(null));

if (expected)
{
Assert.True(result);
}
else
{
Assert.False(result);
}
}

[Fact]
{[Fact]
public async Task StoreCommand_ExecuteAsync_CallsHostProvider()
{
const string testUserName = "john.doe";
Expand All @@ -57,10 +31,9 @@ public async Task StoreCommand_ExecuteAsync_CallsHostProvider()
Streams = {In = stdin}
};

string[] cmdArgs = {"store"};
var command = new StoreCommand(providerRegistry);
var command = new StoreCommand(context, providerRegistry);

await command.ExecuteAsync(context, cmdArgs);
await command.ExecuteAsync();

providerMock.Verify(
x => x.StoreCredentialAsync(It.Is<InputArguments>(y => AreInputArgumentsEquivalent(expectedInput, y))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@ namespace Microsoft.Git.CredentialManager.Tests.Commands
{
public class UnconfigureCommandTests
{
[Theory]
[InlineData("unconfigure", true)]
[InlineData("UNCONFIGURE", true)]
[InlineData("uNcOnFiGuRe", true)]
[InlineData("get", false)]
[InlineData("store", false)]
[InlineData("configure", false)]
[InlineData("", false)]
[InlineData(null, false)]
public void UnconfigureCommand_CanExecuteAsync(string argString, bool expected)
{
var command = new UnconfigureCommand(Mock.Of<IConfigurationService>());

bool result = command.CanExecute(argString?.Split(null));

Assert.Equal(expected, result);
}

[Fact]
public async Task UnconfigureCommand_ExecuteAsync_User_InvokesConfigurationServiceUnconfigureUser()
{
Expand All @@ -37,11 +19,9 @@ public async Task UnconfigureCommand_ExecuteAsync_User_InvokesConfigurationServi
.Verifiable();

var context = new TestCommandContext();
var command = new UnconfigureCommand(context, configService.Object);

string[] cmdArgs = {"unconfigure"};
var command = new UnconfigureCommand(configService.Object);

await command.ExecuteAsync(context, cmdArgs);
await command.ExecuteAsync(false);

configService.Verify(x => x.UnconfigureAsync(ConfigurationTarget.User), Times.Once);
}
Expand All @@ -55,11 +35,9 @@ public async Task UnconfigureCommand_ExecuteAsync_System_InvokesConfigurationSer
.Verifiable();

var context = new TestCommandContext();
var command = new UnconfigureCommand(context, configService.Object);

string[] cmdArgs = {"unconfigure", "--system"};
var command = new UnconfigureCommand(configService.Object);

await command.ExecuteAsync(context, cmdArgs);
await command.ExecuteAsync(true);

configService.Verify(x => x.UnconfigureAsync(ConfigurationTarget.System), Times.Once);
}
Expand Down
Loading