Skip to content

Commit

Permalink
Implementing ability to write to stdout
Browse files Browse the repository at this point in the history
closes #282
  • Loading branch information
belav committed Jun 11, 2021
1 parent a4ba778 commit b5e82a6
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 50 deletions.
19 changes: 0 additions & 19 deletions .idea/.idea.CSharpier/.idea/riderModule.iml

This file was deleted.

2 changes: 1 addition & 1 deletion Src/CSharpier.Playground/ClientApp/src/AppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const useSetupAppContext = () => {
},
copyLeft: () => {
setEnteredCode(formattedCode);
}
},
};
};

Expand Down
55 changes: 37 additions & 18 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ namespace CSharpier.Tests
public class CommandLineFormatterTests
{
private MockFileSystem fileSystem;
private const string UnformattedClass = "public class ClassName { public int Field; }";
private const string FormattedClass =
private const string UnformattedClassContent =
"public class ClassName { public int Field; }";
private const string FormattedClassContent =
"public class ClassName\n{\n public int Field;\n}\n";

[SetUp]
Expand All @@ -38,42 +39,42 @@ public void Format_Writes_Failed_To_Compile()
public void Format_Writes_File()
{
const string unformattedFilePath = "Unformatted.cs";
WhenAFileExists(unformattedFilePath, UnformattedClass);
WhenAFileExists(unformattedFilePath, UnformattedClassContent);

this.Format();

this.GetFileContent(unformattedFilePath).Should().Be(FormattedClass);
this.GetFileContent(unformattedFilePath).Should().Be(FormattedClassContent);
}

[Test]
public void Format_Supports_Skip_Write()
{
const string unformattedFilePath = "Unformatted.cs";
WhenAFileExists(unformattedFilePath, UnformattedClass);
WhenAFileExists(unformattedFilePath, UnformattedClassContent);

this.Format(skipWrite: true);

this.GetFileContent(unformattedFilePath).Should().Be(UnformattedClass);
this.GetFileContent(unformattedFilePath).Should().Be(UnformattedClassContent);
}

[Test]
public void Format_Checks_Unformatted_File()
{
const string unformattedFilePath = "Unformatted.cs";
WhenAFileExists(unformattedFilePath, UnformattedClass);
WhenAFileExists(unformattedFilePath, UnformattedClassContent);

var (exitCode, lines) = this.Format(check: true);

exitCode.Should().Be(1);
this.GetFileContent(unformattedFilePath).Should().Be(UnformattedClass);
this.GetFileContent(unformattedFilePath).Should().Be(UnformattedClassContent);
lines.First().Should().Contain(@"Unformatted.cs - was not formatted");
}

[Test]
public void Format_Checks_Formatted_File()
{
const string formattedFilePath = "Formatted.cs";
WhenAFileExists(formattedFilePath, FormattedClass);
WhenAFileExists(formattedFilePath, FormattedClassContent);

var (exitCode, lines) = this.Format(check: true);

Expand All @@ -88,7 +89,7 @@ public void Format_Checks_Formatted_File()
public void Format_Skips_Generated_Files(string fileName)
{
var unformattedFilePath = fileName;
WhenAFileExists(unformattedFilePath, UnformattedClass);
WhenAFileExists(unformattedFilePath, UnformattedClassContent);

var (_, lines) = this.Format();

Expand All @@ -107,7 +108,7 @@ public void Format_Skips_Generated_Files(string fileName)
public void File_In_Ignore_Skips_Formatting(string fileName, string ignoreContents)
{
var unformattedFilePath = fileName;
WhenAFileExists(unformattedFilePath, UnformattedClass);
WhenAFileExists(unformattedFilePath, UnformattedClassContent);
WhenAFileExists(".csharpierignore", ignoreContents);

var (_, lines) = this.Format();
Expand All @@ -123,7 +124,7 @@ public void File_In_Ignore_Skips_Formatting_With_BaseDirectory(
string baseDirectory
) {
var unformattedFilePath = fileName;
WhenAFileExists(unformattedFilePath, UnformattedClass);
WhenAFileExists(unformattedFilePath, UnformattedClassContent);
WhenAFileExists(".csharpierignore", ignoreContents);

var (_, lines) = this.Format(
Expand All @@ -138,8 +139,8 @@ public void Multiple_Files_Should_Use_Root_Ignore()
{
var unformattedFilePath1 = "SubFolder/1/File1.cs";
var unformattedFilePath2 = "SubFolder/2/File2.cs";
WhenAFileExists(unformattedFilePath1, UnformattedClass);
WhenAFileExists(unformattedFilePath2, UnformattedClass);
WhenAFileExists(unformattedFilePath1, UnformattedClassContent);
WhenAFileExists(unformattedFilePath2, UnformattedClassContent);
WhenAFileExists(".csharpierignore", "Subfolder/**/*.cs");

var (_, lines) = this.Format(
Expand All @@ -154,8 +155,8 @@ public void Multiple_Files_Should_Use_Multiple_Ignores()
{
var unformattedFilePath1 = "SubFolder/1/File1.cs";
var unformattedFilePath2 = "SubFolder/2/File2.cs";
WhenAFileExists(unformattedFilePath1, UnformattedClass);
WhenAFileExists(unformattedFilePath2, UnformattedClass);
WhenAFileExists(unformattedFilePath1, UnformattedClassContent);
WhenAFileExists(unformattedFilePath2, UnformattedClassContent);
WhenAFileExists("SubFolder/1/.csharpierignore", "File1.cs");
WhenAFileExists("SubFolder/2/.csharpierignore", "File2.cs");

Expand All @@ -170,7 +171,7 @@ public void Multiple_Files_Should_Use_Multiple_Ignores()
public void Ignore_Should_Deal_With_Inconsistent_Slashes()
{
var unformattedFilePath1 = @"SubFolder\1\File1.cs";
WhenAFileExists(unformattedFilePath1, UnformattedClass);
WhenAFileExists(unformattedFilePath1, UnformattedClassContent);
WhenAFileExists("SubFolder/1/.csharpierignore", "File1.cs");

var (_, lines) = this.Format(directoryOrFilePaths: unformattedFilePath1);
Expand All @@ -195,9 +196,21 @@ public void Ignore_Reports_Errors()
lines.Should().Contain(@"\Src\Uploads\*.cs");
}

[Test]
public void Write_Stdout_Should_Only_Write_File()
{
WhenAFileExists("file1.cs", UnformattedClassContent);

var (_, lines) = this.Format(writeStdout: true);

lines.Should().ContainSingle();
lines.First().Should().Be(FormattedClassContent);
}

private (int exitCode, IList<string> lines) Format(
bool skipWrite = false,
bool check = false,
bool writeStdout = false,
params string[] directoryOrFilePaths
) {
if (directoryOrFilePaths.Length == 0)
Expand All @@ -219,7 +232,8 @@ params string[] directoryOrFilePaths
{
DirectoryOrFilePaths = directoryOrFilePaths,
SkipWrite = skipWrite,
Check = check
Check = check,
WriteStdout = writeStdout
},
this.fileSystem,
fakeConsole,
Expand Down Expand Up @@ -262,6 +276,11 @@ public void WriteLine(string line = null)
this.Lines.Add(line);
}
}

public void Write(string value)
{
this.Lines.Add(value);
}
}
}
}
5 changes: 5 additions & 0 deletions Src/CSharpier.Tests/Utilities/StringDifferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public void WriteLine(string line = null)
stringBuilder.AppendLine(line);
}

public void Write(string value)
{
stringBuilder.Append(value);
}

public string GetOutput()
{
return stringBuilder.ToString();
Expand Down
35 changes: 24 additions & 11 deletions Src/CSharpier/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ await IgnoreFile.Create(
}

result.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
ResultPrinter.PrintResults(result, console, commandLineOptions);
if (!commandLineOptions.WriteStdout)
{
ResultPrinter.PrintResults(result, console, commandLineOptions);
}
return ReturnExitCode(commandLineOptions, result);
}

Expand Down Expand Up @@ -231,7 +234,7 @@ private async Task FormatFile(string file, CancellationToken cancellationToken)
}
}

if (this.CommandLineOptions.Check)
if (this.CommandLineOptions.Check && !this.CommandLineOptions.WriteStdout)
{
if (result.Code != fileReaderResult.FileContents)
{
Expand All @@ -248,14 +251,21 @@ private async Task FormatFile(string file, CancellationToken cancellationToken)
cancellationToken.ThrowIfCancellationRequested();
Interlocked.Increment(ref this.Result.Files);

if (
!this.CommandLineOptions.Check
&& !this.CommandLineOptions.SkipWrite
&& result.Code != fileReaderResult.FileContents
) {
// purposely avoid async here, that way the file completely writes if the process gets cancelled while running.
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
this.FileSystem.File.WriteAllText(file, result.Code, fileReaderResult.Encoding);
if (this.CommandLineOptions.WriteStdout)
{
this.Console.Write(result.Code);
}
else
{
if (
!this.CommandLineOptions.Check
&& !this.CommandLineOptions.SkipWrite
&& result.Code != fileReaderResult.FileContents
) {
// purposely avoid async here, that way the file completely writes if the process gets cancelled while running.
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
this.FileSystem.File.WriteAllText(file, result.Code, fileReaderResult.Encoding);
}
}
}

Expand Down Expand Up @@ -288,7 +298,10 @@ private bool ShouldIgnoreFile(string filePath)

protected void WriteLine(string? line = null)
{
this.Console.WriteLine(line);
if (!this.CommandLineOptions.WriteStdout)
{
this.Console.WriteLine(line);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions Src/CSharpier/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public class CommandLineOptions
public bool Check { get; init; }
public bool Fast { get; init; }
public bool SkipWrite { get; init; }
public bool WriteStdout { get; init; }

internal delegate Task<int> Handler(
string[] directoryOrFile,
bool check,
bool fast,
bool skipWrite,
bool writeStdout,
CancellationToken cancellationToken
);

Expand All @@ -42,6 +44,10 @@ public static RootCommand Create()
new Option(
new[] { "--skip-write" },
"Skip writing changes. Generally used for testing to ensure csharpier doesn't throw any errors or cause syntax tree validation failures."
),
new Option(
new[] { "--write-stdout" },
"Write the results of formatting any files to stdout."
)
};

Expand Down
6 changes: 6 additions & 0 deletions Src/CSharpier/IConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace CSharpier
public interface IConsole
{
void WriteLine(string? line);
void Write(string value);
}

public class SystemConsole : IConsole
Expand All @@ -13,5 +14,10 @@ public void WriteLine(string? line)
{
Console.WriteLine(line);
}

public void Write(string value)
{
Console.Write(value);
}
}
}
4 changes: 3 additions & 1 deletion Src/CSharpier/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static async Task<int> Run(
bool check,
bool fast,
bool skipWrite,
bool writeStdout,
CancellationToken cancellationToken
) {
if (directoryOrFile is null or { Length: 0 })
Expand All @@ -47,7 +48,8 @@ CancellationToken cancellationToken
DirectoryOrFilePaths = directoryOrFile.ToArray(),
Check = check,
Fast = fast,
SkipWrite = skipWrite
SkipWrite = skipWrite,
WriteStdout = writeStdout
};

return await CommandLineFormatter.Format(
Expand Down

0 comments on commit b5e82a6

Please sign in to comment.