Skip to content

Commit

Permalink
adding some tests for check command now that it is split
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Jan 2, 2025
1 parent 08a7d64 commit 1851f28
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
70 changes: 69 additions & 1 deletion Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public async Task Format_Should_Handle_Unicode()
[TestCase("BasicFile.cs")]
[TestCase("./BasicFile.cs")]
[TestCase("/BasicFile.cs")]
public async Task Format_Print_NotFound(string path)
public async Task Format_Should_Print_NotFound(string path)
{
var result = await new CsharpierProcess().WithArguments($"format {path}").ExecuteAsync();

Expand All @@ -233,6 +233,18 @@ public async Task Format_Print_NotFound(string path)
result.ExitCode.Should().Be(1);
}

[TestCase("BasicFile.cs")]
[TestCase("./BasicFile.cs")]
[TestCase("/BasicFile.cs")]
public async Task Check_Should_Print_NotFound(string path)
{
var result = await new CsharpierProcess().WithArguments($"check {path}").ExecuteAsync();

result.Output.Should().BeEmpty();
result.ErrorOutput.Should().StartWith("There was no file or directory found at " + path);
result.ExitCode.Should().Be(1);
}

[Test]
public async Task Format_Should_Write_To_StdError_For_Piped_Invalid_File()
{
Expand All @@ -248,6 +260,21 @@ public async Task Format_Should_Write_To_StdError_For_Piped_Invalid_File()
result.ErrorOutput.Should().Contain("Failed to compile so was not formatted");
}

[Test]
public async Task Check_Should_Write_To_StdError_For_Piped_Invalid_File()
{
const string invalidFile = "public class ClassName { ";

var result = await new CsharpierProcess()
.WithArguments("check")
.WithPipedInput(invalidFile)
.ExecuteAsync();

result.Output.Should().BeEmpty();
result.ExitCode.Should().Be(1);
result.ErrorOutput.Should().Contain("Failed to compile so was not formatted");
}

[Test]
public async Task Check_Should_Write_Unformatted_File()
{
Expand Down Expand Up @@ -415,6 +442,27 @@ await this.WriteFileAsync(
result.Output.Should().StartWith("Formatted 0 files in ");
}

[Test]
public async Task Check_Should_Not_Fail_On_Mismatched_MSBuild_With_No_Check()
{
await this.WriteFileAsync(
"Test.csproj",
@"<Project Sdk=""Microsoft.NET.Sdk"">
<ItemGroup>
<PackageReference Include=""CSharpier.MsBuild"" Version=""99"" />
</ItemGroup>
</Project>"
);

var result = await new CsharpierProcess()
.WithArguments("check --no-msbuild-check .")
.ExecuteAsync();

result.ErrorOutput.Should().BeEmpty();
result.ExitCode.Should().Be(0);
result.Output.Should().StartWith("Formatted 0 files in ");
}

[Test]
public async Task Format_Should_Fail_On_Mismatched_MSBuild()
{
Expand All @@ -435,6 +483,26 @@ await this.WriteFileAsync(
result.ExitCode.Should().Be(1);
}

[Test]
public async Task Check_Should_Fail_On_Mismatched_MSBuild()
{
await this.WriteFileAsync(
"Test.csproj",
@"<Project Sdk=""Microsoft.NET.Sdk"">
<ItemGroup>
<PackageReference Include=""CSharpier.MsBuild"" Version=""99"" />
</ItemGroup>
</Project>"
);

var result = await new CsharpierProcess().WithArguments("check .").ExecuteAsync();

result
.ErrorOutput.Should()
.Contain("uses version 99 of CSharpier.MsBuild which is a mismatch with version");
result.ExitCode.Should().Be(1);
}

[Test]
public async Task Format_Should_Cache_And_Validate_Too_Many_Things()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.CommandLine;
using System.CommandLine.Parsing;
using System.IO.Abstractions;
using CSharpier.Cli.Server;
using Microsoft.Extensions.Logging;

namespace CSharpier.Cli;

internal static class FormatingCommands
internal static class FormattingCommands
{
public static Command CreateFormatCommand()
{
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using CSharpier.Cli;

var rootCommand = new RootCommand();
rootCommand.AddCommand(FormatingCommands.CreateFormatCommand());
rootCommand.AddCommand(FormatingCommands.CreateCheckCommand());
rootCommand.AddCommand(FormattingCommands.CreateFormatCommand());
rootCommand.AddCommand(FormattingCommands.CreateCheckCommand());
rootCommand.AddCommand(PipeCommand.Create());
rootCommand.AddCommand(ServerCommand.Create());

Expand Down

0 comments on commit 1851f28

Please sign in to comment.