Skip to content

Commit

Permalink
Improved unit test coverage using Spectre.Console.Tests.Data.VersionC…
Browse files Browse the repository at this point in the history
…ommand
  • Loading branch information
FrankRay78 committed Oct 9, 2024
1 parent 0c972b9 commit 9919872
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public void Help_Should_Not_Include_Application_Version_Flag_If_Not_Specified(st
{
// Given
var fixture = new CommandAppTester();
fixture.Configure(configurator =>
{
configurator.AddCommand<EmptyCommand>("empty");
});

// When
var result = fixture.Run(helpOption);
Expand All @@ -50,42 +46,42 @@ public void Help_Should_Not_Include_Application_Version_Flag_If_Not_Specified(st
[InlineData("-?")]
[InlineData("-h")]
[InlineData("--help")]
public void Help_Should_Not_Include_Application_Version_Flag_For_Command(string helpOption)
public void Help_Should_Include_Application_Version_Flag_For_Default_Command(string helpOption)
{
// Given
var fixture = new CommandAppTester();
fixture.SetDefaultCommand<EmptyCommand>();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<EmptyCommand>("empty");
});

// When
var result = fixture.Run("empty", helpOption);
var result = fixture.Run(helpOption);

// Then
result.Output.ShouldNotContain("-v, --version Prints version information");
result.Output.ShouldContain("-v, --version Prints version information");
}

[Theory]
[InlineData("-?")]
[InlineData("-h")]
[InlineData("--help")]
public void Help_Should_Include_Application_Version_Flag_For_Default_Command(string helpOption)
public void Help_Should_Not_Include_Application_Version_Flag_For_Command(string helpOption)
{
// Given
var fixture = new CommandAppTester();
fixture.SetDefaultCommand<EmptyCommand>();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<EmptyCommand>("empty");
});

// When
var result = fixture.Run(helpOption);
var result = fixture.Run("empty", helpOption);

// Then
result.Output.ShouldContain("-v, --version Prints version information");
result.Output.ShouldNotContain("-v, --version Prints version information");
}

[Theory]
Expand Down Expand Up @@ -125,61 +121,61 @@ public void Help_Should_Not_Include_Application_Version_Flag_For_Branch_Command(
configurator.SetApplicationVersion("1.0");
configurator.AddBranch<EmptyCommandSettings>("branch", branch =>
{
branch.AddCommand<EmptyCommand>("hello");
branch.AddCommand<EmptyCommand>("empty");
});
});

// When
var result = fixture.Run("branch", "hello", helpOption);
var result = fixture.Run("branch", "empty", helpOption);

// Then
result.Output.ShouldNotContain("-v, --version Prints version information");
}

/// <summary>
/// When a command with a version flag in the settings is set as the application default command,
/// then override the in-built Application Version flag with the command version flag instead.
/// Rationale: This behaviour makes the most sense because the other flags for the default command
/// will be shown in the help output and the user can set any of these when executing the application.
/// </summary>
[Theory]
[InlineData("-?")]
[InlineData("-h")]
[InlineData("--help")]
public void Help_Should_Include_Command_Version_Flag_For_Command(string helpOption)
public void Help_Should_Include_Command_Version_Flag_For_Default_Command(string helpOption)
{
// Given
var fixture = new CommandAppTester();
fixture.SetDefaultCommand<Spectre.Console.Tests.Data.VersionCommand>();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<Spectre.Console.Tests.Data.VersionCommand>("empty");
});

// When
var result = fixture.Run("empty", helpOption);
var result = fixture.Run(helpOption);

// Then
result.Output.ShouldContain("-v, --version The command version");
result.Output.ShouldNotContain("-v, --version Prints version information");
}

/// <summary>
/// When a command with a version flag in the settings is set as the application default command,
/// then override the in-built Application Version flag with the command version flag instead.
/// Rationale: This behaviour makes the most sense because the other flags for the default command
/// will be shown in the help output and the user can set any of these when executing the application.
/// </summary>
[Theory]
[InlineData("-?")]
[InlineData("-h")]
[InlineData("--help")]
public void Help_Should_Include_Command_Version_Flag_For_Default_Command(string helpOption)
public void Help_Should_Include_Command_Version_Flag_For_Command(string helpOption)
{
// Given
var fixture = new CommandAppTester();
fixture.SetDefaultCommand<Spectre.Console.Tests.Data.VersionCommand>();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<Spectre.Console.Tests.Data.VersionCommand>("hello");
});

// When
var result = fixture.Run(helpOption);
var result = fixture.Run("hello", helpOption);

// Then
result.Output.ShouldContain("-v, --version The command version");
Expand Down Expand Up @@ -235,8 +231,6 @@ public void Help_Should_Include_Command_Version_Flag_For_Branch_Command(string h
result.Output.ShouldContain("-v, --version The command version");
result.Output.ShouldNotContain("-v, --version Prints version information");
}


}
}
}
116 changes: 101 additions & 15 deletions src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public void Should_Not_Display_Version_If_Not_Specified(string versionOption)
{
// Given
var fixture = new CommandAppTester();
fixture.Configure(configurator =>
{
configurator.AddCommand<EmptyCommand>("empty");
});

// When
var result = fixture.Run(versionOption);
Expand All @@ -59,42 +55,42 @@ public void Should_Not_Display_Version_If_Not_Specified(string versionOption)
[Theory]
[InlineData("-v")]
[InlineData("--version")]
public void Should_Execute_Command_Not_Output_Application_Version_To_The_Console(string versionOption)
public void Should_Output_Application_Version_To_The_Console_With_Default_Command(string versionOption)
{
// Given
var fixture = new CommandAppTester();
fixture.SetDefaultCommand<EmptyCommand>();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<EmptyCommand>("empty");
});

// When
var result = fixture.Run("empty", versionOption);
var result = fixture.Run(versionOption);

// Then
result.Output.ShouldBe(string.Empty);
result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null });
result.Output.ShouldBe("1.0");
}

[Theory]
[InlineData("-v")]
[InlineData("--version")]
public void Should_Output_Application_Version_To_The_Console_With_Default_Command(string versionOption)
public void Should_Execute_Command_Not_Output_Application_Version_To_The_Console(string versionOption)
{
// Given
var fixture = new CommandAppTester();
fixture.SetDefaultCommand<EmptyCommand>();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<EmptyCommand>("empty");
});

// When
var result = fixture.Run(versionOption);
var result = fixture.Run("empty", versionOption);

// Then
result.Output.ShouldBe("1.0");
result.Output.ShouldBe(string.Empty);
result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null });
}

[Theory]
Expand Down Expand Up @@ -156,16 +152,106 @@ public void Should_Execute_Branch_Command_Not_Output_Application_Version_To_The_
configurator.SetApplicationVersion("1.0");
configurator.AddBranch<EmptyCommandSettings>("branch", branch =>
{
branch.AddCommand<EmptyCommand>("hello");
branch.AddCommand<EmptyCommand>("empty");
});
});

// When
var result = fixture.Run("branch", "hello", versionOption);
var result = fixture.Run("branch", "empty", versionOption);

// Then
result.Output.ShouldBe(string.Empty);
result.Context.ShouldHaveRemainingArgument(versionOption, new[] { (string)null });
}

/// <summary>
/// When a command with a version flag in the settings is set as the application default command,
/// then execute this command instead of displaying the explicitly set Application Version.
/// </summary>
[Theory]
[InlineData("-v")]
[InlineData("--version")]
public void Should_Execute_Default_VersionCommand_Not_Output_Application_Version_To_The_Console(string versionOption)
{
// Given
var fixture = new CommandAppTester();
fixture.SetDefaultCommand<Spectre.Console.Tests.Data.VersionCommand>();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
});

// When
var result = fixture.Run(versionOption, "X.Y.Z");

// Then
result.Output.ShouldBe("VersionCommand ran, Version: X.Y.Z");
}

[Theory]
[InlineData("-v")]
[InlineData("--version")]
public void Should_Execute_VersionCommand_Not_Output_Application_Version_To_The_Console(string versionOption)
{
// Given
var fixture = new CommandAppTester();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddCommand<Spectre.Console.Tests.Data.VersionCommand>("hello");
});

// When
var result = fixture.Run("hello", versionOption, "X.Y.Z");

// Then
result.Output.ShouldBe("VersionCommand ran, Version: X.Y.Z");
}

[Theory]
[InlineData("-v")]
[InlineData("--version")]
public void Should_Execute_Branch_Default_VersionCommand_Not_Output_Application_Version_To_The_Console(string versionOption)
{
// Given
var fixture = new CommandAppTester();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddBranch<VersionSettings>("branch", branch =>
{
branch.SetDefaultCommand<Spectre.Console.Tests.Data.VersionCommand>();
});
});

// When
var result = fixture.Run("branch", versionOption, "X.Y.Z");

// Then
result.Output.ShouldBe("VersionCommand ran, Version: X.Y.Z");
}

[Theory]
[InlineData("-v")]
[InlineData("--version")]
public void Should_Execute_Branch_VersionCommand_Not_Output_Application_Version_To_The_Console(string versionOption)
{
// Given
var fixture = new CommandAppTester();
fixture.Configure(configurator =>
{
configurator.SetApplicationVersion("1.0");
configurator.AddBranch<VersionSettings>("branch", branch =>
{
branch.AddCommand<Spectre.Console.Tests.Data.VersionCommand>("hello");
});
});

// When
var result = fixture.Run("branch", "hello", versionOption, "X.Y.Z");

// Then
result.Output.ShouldBe("VersionCommand ran, Version: X.Y.Z");
}
}
}

0 comments on commit 9919872

Please sign in to comment.