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

Reimplement CLI Commands to SubCommands #1333

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
59 changes: 36 additions & 23 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ public async Task Should_Format_Basic_File(string lineEnding)

await this.WriteFileAsync("BasicFile.cs", unformattedContent);

var result = await new CsharpierProcess().WithArguments("BasicFile.cs").ExecuteAsync();
var result = await new CsharpierProcess()
.WithArguments("format BasicFile.cs")
.ExecuteAsync();

result.ErrorOutput.Should().BeNullOrEmpty();
result.Output.Should().StartWith("Formatted 1 files in ");
result.ExitCode.Should().Be(0);
(await this.ReadAllTextAsync("BasicFile.cs")).Should().Be(formattedContent);
}

[TestCase("Subdirectory")]
[TestCase("./Subdirectory")]
[TestCase("format Subdirectory")]
[TestCase("format ./Subdirectory")]
public async Task Should_Format_Subdirectory(string subdirectory)
{
var formattedContent = "public class ClassName { }\n";
Expand Down Expand Up @@ -106,7 +108,7 @@ public async Task Should_Support_Config_Path()
await this.WriteFileAsync("config/.csharpierrc", "printWidth: 10");

await new CsharpierProcess()
.WithArguments("--config-path config/.csharpierrc . ")
.WithArguments("format --config-path config/.csharpierrc . ")
.ExecuteAsync();

var result = await this.ReadAllTextAsync(fileName);
Expand Down Expand Up @@ -138,6 +140,7 @@ public async Task Should_Format_Piped_File(string lineEnding)
var unformattedContent1 = $"public class ClassName1 {{{lineEnding}{lineEnding}}}";

var result = await new CsharpierProcess()
.WithArguments("format")
.WithPipedInput(unformattedContent1)
.ExecuteAsync();

Expand All @@ -154,6 +157,7 @@ public async Task Should_Format_Piped_File_With_Config()
var unformattedContent1 = "var x = _________________longName;\n";

var result = await new CsharpierProcess()
.WithArguments("format")
.WithPipedInput(unformattedContent1)
.ExecuteAsync();

Expand All @@ -174,6 +178,7 @@ await this.WriteFileAsync(
var unformattedContent1 = "var x = _________________longName;\n";

var result = await new CsharpierProcess()
.WithArguments("format")
.WithPipedInput(unformattedContent1)
.ExecuteAsync();

Expand All @@ -187,7 +192,10 @@ public async Task Should_Format_Unicode()
// use the \u so that we don't accidentally reformat this to be '?'
var unicodeContent = $"var test = '{'\u3002'}';\n";

var result = await new CsharpierProcess().WithPipedInput(unicodeContent).ExecuteAsync();
var result = await new CsharpierProcess()
.WithArguments("format")
.WithPipedInput(unicodeContent)
.ExecuteAsync();

result.ErrorOutput.Should().BeEmpty();
result.Output.Should().Be(unicodeContent);
Expand All @@ -199,7 +207,7 @@ public async Task Should_Format_Unicode()
[TestCase("/BasicFile.cs")]
public async Task Should_Print_NotFound(string path)
{
var result = await new CsharpierProcess().WithArguments(path).ExecuteAsync();
var result = await new CsharpierProcess().WithArguments($"format {path}").ExecuteAsync();

result.Output.Should().BeEmpty();
result.ErrorOutput.Should().StartWith("There was no file or directory found at " + path);
Expand All @@ -211,7 +219,10 @@ public async Task Should_Write_To_StdError_For_Piped_Invalid_File()
{
const string invalidFile = "public class ClassName { ";

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

result.Output.Should().BeEmpty();
result.ExitCode.Should().Be(1);
Expand All @@ -226,7 +237,7 @@ public async Task With_Check_Should_Write_Unformatted_File()
await this.WriteFileAsync("CheckUnformatted.cs", unformattedContent);

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

result
Expand All @@ -251,7 +262,7 @@ public async Task Should_Format_Multiple_Piped_Files(string lineEnding)
+ $"Test2.cs{'\u0003'}{unformattedContent2}{'\u0003'}";

var result = await new CsharpierProcess()
.WithArguments("--pipe-multiple-files")
.WithArguments("pipe-files")
.WithPipedInput(input)
.ExecuteAsync();

Expand All @@ -269,7 +280,7 @@ public async Task Should_Write_Error_With_Multiple_Piped_Files(string input, str
const string invalidFile = "public class ClassName { ";

var result = await new CsharpierProcess()
.WithArguments("--pipe-multiple-files")
.WithArguments("pipe-files")
.WithPipedInput($"{input}{'\u0003'}{invalidFile}{'\u0003'}")
.ExecuteAsync();

Expand All @@ -289,7 +300,7 @@ public async Task Should_Ignore_Piped_File_With_Multiple_Piped_Files()
await this.WriteFileAsync(".csharpierignore", "Ignored.cs");

var result = await new CsharpierProcess()
.WithArguments("--pipe-multiple-files")
.WithArguments("pipe-files")
.WithPipedInput($"{fileName}{'\u0003'}{ignoredFile}{'\u0003'}")
.ExecuteAsync();

Expand All @@ -305,7 +316,7 @@ public async Task Should_Support_Config_With_Multiple_Piped_Files()
await this.WriteFileAsync(".csharpierrc", "printWidth: 10");

var result = await new CsharpierProcess()
.WithArguments("--pipe-multiple-files")
.WithArguments("pipe-files")
.WithPipedInput($"{fileName}{'\u0003'}{fileContent}{'\u0003'}")
.ExecuteAsync();

Expand All @@ -329,7 +340,7 @@ await this.WriteFileAsync(
);

var result = await new CsharpierProcess()
.WithArguments("--pipe-multiple-files")
.WithArguments("pipe-files")
.WithPipedInput($"{fileName}{'\u0003'}{fileContent}{'\u0003'}")
.ExecuteAsync();

Expand All @@ -342,7 +353,7 @@ public async Task Should_Not_Fail_On_Empty_File()
{
await this.WriteFileAsync("BasicFile.cs", "");

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

result.Output.Should().StartWith("Formatted 0 files in ");
result.ErrorOutput.Should().BeEmpty();
Expand All @@ -354,7 +365,7 @@ public async Task Should_Not_Fail_On_Bad_Csproj()
{
await this.WriteFileAsync("Empty.csproj", "");

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

result.ErrorOutput.Should().BeEmpty();
result.ExitCode.Should().Be(0);
Expand All @@ -374,7 +385,7 @@ await this.WriteFileAsync(
);

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

result.ErrorOutput.Should().BeEmpty();
Expand All @@ -394,7 +405,7 @@ await this.WriteFileAsync(
</Project>"
);

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

result
.ErrorOutput.Should()
Expand All @@ -410,12 +421,12 @@ public async Task Should_Cache_And_Validate_Too_Many_Things()
var filePath = "Unformatted.cs";
await this.WriteFileAsync(filePath, unformattedContent);

await new CsharpierProcess().WithArguments(".").ExecuteAsync();
await new CsharpierProcess().WithArguments("format .").ExecuteAsync();
var firstModifiedDate = GetLastWriteTime(filePath);
await new CsharpierProcess().WithArguments(".").ExecuteAsync();
await new CsharpierProcess().WithArguments("format .").ExecuteAsync();
var secondModifiedDate = GetLastWriteTime(filePath);
await this.WriteFileAsync(filePath, unformattedContent);
await new CsharpierProcess().WithArguments(".").ExecuteAsync();
await new CsharpierProcess().WithArguments("format .").ExecuteAsync();
var thirdModifiedDate = GetLastWriteTime(filePath);

// I don't know that this exactly validates caching, because I don't think we write out a file unless it changes.
Expand All @@ -431,9 +442,9 @@ public async Task Should_Reformat_When_Options_Change_With_Cache()
var unformattedContent = "public class ClassName { \n// break\n }\n";

await this.WriteFileAsync("Unformatted.cs", unformattedContent);
await new CsharpierProcess().WithArguments(".").ExecuteAsync();
await new CsharpierProcess().WithArguments("format .").ExecuteAsync();
await this.WriteFileAsync(".csharpierrc", "useTabs: true");
await new CsharpierProcess().WithArguments(".").ExecuteAsync();
await new CsharpierProcess().WithArguments("format .").ExecuteAsync();

var result = await this.ReadAllTextAsync("Unformatted.cs");
result.Should().Contain("\n\t// break\n");
Expand Down Expand Up @@ -464,7 +475,9 @@ async Task WriteFiles(string folder)

async Task FormatFolder(string folder)
{
var result = await new CsharpierProcess().WithArguments(folder).ExecuteAsync();
var result = await new CsharpierProcess()
.WithArguments($"format {folder}")
.ExecuteAsync();
result.ErrorOutput.Should().BeEmpty();
}

Expand Down
Loading
Loading