Skip to content

Command-style cli spike, for #428 #598

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

Closed
Closed
246 changes: 37 additions & 209 deletions src/GitVersionExe.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,244 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLine;
using GitVersion;
using GitVersion.Options;
using NUnit.Framework;
using Shouldly;

[TestFixture]
public class ArgumentParserTests
{
[Test]
public void Empty_means_use_current_directory()
{
var arguments = ArgumentParser.ParseArguments("");
arguments.TargetPath.ShouldBe(Environment.CurrentDirectory);
arguments.LogFilePath.ShouldBe(null);
arguments.IsHelp.ShouldBe(false);
}

[Test]
public void Single_means_use_as_target_directory()
{
var arguments = ArgumentParser.ParseArguments("path");
arguments.TargetPath.ShouldBe("path");
arguments.LogFilePath.ShouldBe(null);
arguments.IsHelp.ShouldBe(false);
}

[Test]
public void No_path_and_logfile_should_use_current_directory_TargetDirectory()
{
var arguments = ArgumentParser.ParseArguments("-l logFilePath");
arguments.TargetPath.ShouldBe(Environment.CurrentDirectory);
arguments.LogFilePath.ShouldBe("logFilePath");
arguments.IsHelp.ShouldBe(false);
}

[Test]
public void h_means_IsHelp()
[TestCaseSource("AllVerbs")]
public void PrintVerbHelp(string verb)
{
var arguments = ArgumentParser.ParseArguments("-h");
Assert.IsNull(arguments.TargetPath);
Assert.IsNull(arguments.LogFilePath);
arguments.IsHelp.ShouldBe(true);
Parser.Default.ParseArguments(new[] {"help", verb}, AllOptionTypes().ToArray());
}

[Test]
public void exec()
IEnumerable<string> AllVerbs()
{
var arguments = ArgumentParser.ParseArguments("-exec rake");
arguments.Exec.ShouldBe("rake");
return AllOptionTypes()
.Select(t => (VerbAttribute) Attribute.GetCustomAttribute(t, typeof(VerbAttribute)))
.Where(a => a != null).Select(a => a.Name);
}

[Test]
public void exec_with_args()
IEnumerable<Type> AllOptionTypes()
{
var arguments = ArgumentParser.ParseArguments(new List<string>
{
"-exec",
"rake",
"-execargs",
"clean build"
});
arguments.Exec.ShouldBe("rake");
arguments.ExecArgs.ShouldBe("clean build");
yield return typeof(InspectOptions);
yield return typeof(InitOptions);
yield return typeof(InspectRemoteRepositoryOptions);
yield return typeof(InjectBuildServerOptions);
yield return typeof(InjectMsBuildOptions);
yield return typeof(InjectProcess);
yield return typeof(InjectAssemblyInfo);
}

[Test]
public void msbuild()
public void InputVariablesMustHaveCorrectDefaultValues()
{
var arguments = ArgumentParser.ParseArguments("-proj msbuild.proj");
arguments.Proj.ShouldBe("msbuild.proj");
}
var iv = new InputVariables();

[Test]
public void msbuild_with_args()
{
var arguments = ArgumentParser.ParseArguments(new List<string>
{
"-proj",
"msbuild.proj",
"-projargs",
"/p:Configuration=Debug /p:Platform=AnyCPU"
});
arguments.Proj.ShouldBe("msbuild.proj");
arguments.ProjArgs.ShouldBe("/p:Configuration=Debug /p:Platform=AnyCPU");
}
iv.TargetUrl.ShouldBe(null);

[Test]
public void execwith_targetdirectory()
{
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -exec rake");
arguments.TargetPath.ShouldBe("targetDirectoryPath");
arguments.Exec.ShouldBe("rake");
}
iv.DynamicRepositoryLocation.ShouldBe(null);

[Test]
public void TargetDirectory_and_LogFilePath_can_be_parsed()
{
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -l logFilePath");
arguments.TargetPath.ShouldBe("targetDirectoryPath");
arguments.LogFilePath.ShouldBe("logFilePath");
arguments.IsHelp.ShouldBe(false);
}
iv.Authentication.ShouldNotBe(null);

[Test]
public void Username_and_Password_can_be_parsed()
{
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -u [username] -p [password]");
arguments.TargetPath.ShouldBe("targetDirectoryPath");
arguments.Authentication.Username.ShouldBe("[username]");
arguments.Authentication.Password.ShouldBe("[password]");
arguments.IsHelp.ShouldBe(false);
}
var defaultAuthentication = new Authentication();
iv.Authentication.Username.ShouldBeSameAs(defaultAuthentication.Username);
iv.Authentication.Password.ShouldBeSameAs(defaultAuthentication.Password);

[Test]
public void Unknown_output_should_throw()
{
var exception = Assert.Throws<WarningException>(() => ArgumentParser.ParseArguments("targetDirectoryPath -output invalid_value"));
exception.Message.ShouldBe("Value 'invalid_value' cannot be parsed as output type, please use 'json' or 'buildserver'");
}
iv.TargetUrl.ShouldBe(null);

[Test]
public void Output_defaults_to_json()
{
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath");
arguments.Output.ShouldBe(OutputType.Json);
}
iv.TargetBranch.ShouldBe(null);

[Test]
public void Output_json_can_be_parsed()
{
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -output json");
arguments.Output.ShouldBe(OutputType.Json);
}
iv.NoFetch.ShouldBe(true);

[Test]
public void Output_buildserver_can_be_parsed()
{
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -output buildserver");
arguments.Output.ShouldBe(OutputType.BuildServer);
iv.TargetPath.ShouldBe(Environment.CurrentDirectory);
}

[Explicit]
[Test]
public void MultipleArgsAndFlag()
public void PrintEntireHelp()
{
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -output buildserver -updateAssemblyInfo");
arguments.Output.ShouldBe(OutputType.BuildServer);
}

[Test]
public void Url_and_BranchName_can_be_parsed()
{
var arguments = ArgumentParser.ParseArguments("targetDirectoryPath -url http://github.com/Particular/GitVersion.git -b somebranch");
arguments.TargetPath.ShouldBe("targetDirectoryPath");
arguments.TargetUrl.ShouldBe("http://github.com/Particular/GitVersion.git");
arguments.TargetBranch.ShouldBe("somebranch");
arguments.IsHelp.ShouldBe(false);
}

[Test]
public void Wrong_number_of_arguments_should_throw()
{
var exception = Assert.Throws<WarningException>(() => ArgumentParser.ParseArguments("targetDirectoryPath -l logFilePath extraArg"));
exception.Message.ShouldBe("Could not parse command line parameter 'extraArg'.");
}

[Test]
public void Unknown_argument_should_throw()
{
var exception = Assert.Throws<WarningException>(() => ArgumentParser.ParseArguments("targetDirectoryPath -x logFilePath"));
exception.Message.ShouldBe("Could not parse command line parameter '-x'.");
}

[TestCase("-updateAssemblyInfo true")]
[TestCase("-updateAssemblyInfo 1")]
[TestCase("-updateAssemblyInfo")]
[TestCase("-updateAssemblyInfo -proj foo.sln")]
public void update_assembly_info_true(string command)
{
var arguments = ArgumentParser.ParseArguments(command);
arguments.UpdateAssemblyInfo.ShouldBe(true);
}

[TestCase("-updateAssemblyInfo false")]
[TestCase("-updateAssemblyInfo 0")]
public void update_assembly_info_false(string command)
{
var arguments = ArgumentParser.ParseArguments(command);
arguments.UpdateAssemblyInfo.ShouldBe(false);
}

[Test]
public void update_assembly_info_with_filename()
{
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.ShouldBe("CommonAssemblyInfo.cs");
}

[Test]
public void update_assembly_info_with_relative_filename()
{
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo ..\\..\\CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.ShouldBe("..\\..\\CommonAssemblyInfo.cs");
}

[Test]
public void dynamicRepoLocation()
{
var arguments = ArgumentParser.ParseArguments("-dynamicRepoLocation c:\\foo\\");
arguments.DynamicRepositoryLocation.ShouldBe("c:\\foo\\");
}

[Test]
public void can_log_to_console()
{
var arguments = ArgumentParser.ParseArguments("-l console -proj foo.sln");
arguments.LogFilePath.ShouldBe("console");
}

[Test]
public void nofetch_true_when_defined()
{
var arguments = ArgumentParser.ParseArguments("-nofetch");
arguments.NoFetch = true;
}

[Test]
public void other_arguments_can_be_parsed_before_nofetch()
{
var arguments = ArgumentParser.ParseArguments("targetpath -nofetch ");
arguments.TargetPath = "targetpath";
arguments.NoFetch = true;
Parser.Default.ParseArguments(new[] {"help"}, AllOptionTypes().ToArray());
foreach (var verb in AllVerbs())
{
PrintVerbHelp(verb);
}
}

[Test]
public void other_arguments_can_be_parsed_after_nofetch()
{
var arguments = ArgumentParser.ParseArguments("-nofetch -proj foo.sln");
arguments.NoFetch = true;
arguments.Proj = "foo.sln";
}
}
4 changes: 4 additions & 0 deletions src/GitVersionExe.Tests/GitVersionExe.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<Reference Include="ApprovalUtilities.Net45">
<HintPath>..\packages\ApprovalUtilities.3.0.8\lib\net45\ApprovalUtilities.Net45.dll</HintPath>
</Reference>
<Reference Include="CommandLine, Version=2.0.251.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
<HintPath>..\packages\CommandLineParser.2.0.251-beta\lib\net45\CommandLine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="LibGit2Sharp, Version=0.21.0.176, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\LibGit2Sharp.0.21.0.176\lib\net40\LibGit2Sharp.dll</HintPath>
<Private>True</Private>
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionExe.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<packages>
<package id="ApprovalTests" version="3.0.8" targetFramework="net45" />
<package id="ApprovalUtilities" version="3.0.8" targetFramework="net45" />
<package id="CommandLineParser" version="2.0.251-beta" targetFramework="net45" />
<package id="LibGit2Sharp" version="0.21.0.176" targetFramework="net45" />
<package id="NSubstitute" version="1.8.2.0" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" />
Expand Down
Loading