Skip to content
Merged
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
21 changes: 19 additions & 2 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public async Task Check_Should_Write_To_StdError_For_Piped_Invalid_File()
}

[Test]
public async Task Check_Should_Write_Unformatted_File()
public async Task Check_Should_Write_Error_With_Unformatted_File()
{
var unformattedContent = "public class ClassName1 {\n\n}";

Expand All @@ -493,7 +493,24 @@ public async Task Check_Should_Write_Unformatted_File()
result.ExitCode.Should().Be(1);
}

// TODO overrides tests for piping files
[Test]
public async Task Check_Should_Write_Warning_With_Unformatted_File()
{
var unformattedContent = "public class ClassName1 {\n\n}";

await WriteFileAsync("CheckUnformatted.cs", unformattedContent);

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

result
.Output.Replace('\\', '/')
.Should()
.StartWith("Warning ./CheckUnformatted.cs - Was not formatted.");
result.ExitCode.Should().Be(0);
}

[TestCase("\n")]
[TestCase("\r\n")]
public async Task PipeFiles_Should_Format_Multiple_Piped_Files(string lineEnding)
Expand Down
17 changes: 15 additions & 2 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ CommandLineFormatterResult result
{
if (
(!commandLineOptions.CompilationErrorsAsWarnings && result.FailedCompilation > 0)
|| (commandLineOptions.Check && result.UnformattedFiles > 0)
|| (
commandLineOptions.Check
&& result.UnformattedFiles > 0
&& !commandLineOptions.UnformattedAsWarnings
)
|| result.FailedFormattingValidation > 0
|| result.ExceptionsFormatting > 0
|| result.ExceptionsValidatingSource > 0
Expand Down Expand Up @@ -546,7 +550,16 @@ ref commandLineFormatterResult.ExceptionsValidatingSource
codeFormattingResult.Code,
fileToFormatInfo.FileContents
);
fileIssueLogger.WriteError($"Was not formatted.\n{difference}\n");
var message = $"Was not formatted.\n{difference}\n";
if (commandLineOptions.UnformattedAsWarnings)
{
fileIssueLogger.WriteWarning(message);
}
else
{
fileIssueLogger.WriteError(message);
}

Interlocked.Increment(ref commandLineFormatterResult.UnformattedFiles);
}

Expand Down
1 change: 1 addition & 0 deletions Src/CSharpier.Cli/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ internal class CommandLineOptions
public bool NoCache { get; init; }
public bool NoMSBuildCheck { get; init; }
public bool CompilationErrorsAsWarnings { get; init; }
public bool UnformattedAsWarnings { get; init; }
public bool IncludeGenerated { get; init; }
public string? StandardInFileContents { get; init; }
public string? ConfigPath { get; init; }
Expand Down
13 changes: 13 additions & 0 deletions Src/CSharpier.Cli/FormattingCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static Command CreateFormatCommand()
noMSBuildCheck,
includeGenerated,
compilationErrorsAsWarnings,
false,
configPath,
ignorePath,
stdinPath,
Expand Down Expand Up @@ -96,6 +97,7 @@ public static Command CreateCheckCommand()
checkCommand.AddOption(IncludeGeneratedOption);
checkCommand.AddOption(NoMsBuildCheckOption);
checkCommand.AddOption(CompilationErrorsAsWarningsOption);
checkCommand.AddOption(UnformattedAsWarningsOption);

checkCommand.SetHandler(async context =>
{
Expand All @@ -105,6 +107,9 @@ public static Command CreateCheckCommand()
var compilationErrorsAsWarnings = context.ParseResult.GetValueForOption(
CompilationErrorsAsWarningsOption
);
var unformattedAsWarningsOption = context.ParseResult.GetValueForOption(
UnformattedAsWarningsOption
);
var configPath = context.ParseResult.GetValueForOption(ConfigPathOption);
var ignorePath = context.ParseResult.GetValueForOption(IgnorePathOption);
var logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
Expand All @@ -121,6 +126,7 @@ public static Command CreateCheckCommand()
noMSBuildCheck,
includeGenerated,
compilationErrorsAsWarnings,
unformattedAsWarningsOption,
configPath,
ignorePath,
null,
Expand All @@ -143,6 +149,7 @@ private static async Task<int> FormatOrCheck(
bool noMSBuildCheck,
bool includeGenerated,
bool compilationErrorsAsWarnings,
bool unformattedAsWarnings,
string? configPath,
string? ignorePath,
string? stdinPath,
Expand Down Expand Up @@ -193,6 +200,7 @@ CancellationToken cancellationToken
ConfigPath = configPath,
IgnorePath = ignorePath,
CompilationErrorsAsWarnings = compilationErrorsAsWarnings,
UnformattedAsWarnings = unformattedAsWarnings,
LogFormat = logFormat,
};

Expand Down Expand Up @@ -271,4 +279,9 @@ Log output format
["--compilation-errors-as-warnings"],
"Treat compilation errors from files as warnings instead of errors."
);

private static readonly Option<bool> UnformattedAsWarningsOption = new(
["--unformatted-as-warnings"],
"Treat unformatted files as warnings instead of errors."
);
}
1 change: 1 addition & 0 deletions Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<CSharpierCommand>format</CSharpierCommand>
<CSharpierCommand Condition="'$(CSharpier_Check)' == 'true'">check</CSharpierCommand>
<CSharpierArgs Condition="'$(CSharpier_LogLevel)' != ''">$(CSharpierArgs) --log-level $(CSharpier_LogLevel)</CSharpierArgs>
<CSharpierArgs Condition="'$(CSharpier_UnformattedAsWarnings)' == 'true'">$(CSharpierArgs) --unformatted-as-warnings</CSharpierArgs>
<FirstTargetFramework Condition=" '$(TargetFrameworks)' == '' ">$(TargetFramework)</FirstTargetFramework>
<FirstTargetFramework Condition=" '$(FirstTargetFramework)' == '' ">$(TargetFrameworks.Split(';')[0])</FirstTargetFramework>
<NullOutput>NUL</NullOutput>
Expand Down
7 changes: 7 additions & 0 deletions Tests/MsBuild/Run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ if ($result.Length -gt 1) {
}
Write-Host "::endgroup::"

Write-Host "::group::UnformattedFileCausesWarning"
$result = [TestHelper]::RunTestCase("UnformattedFileCausesWarning", $false)
if ($result.Length -gt 1) {
$failureMessages += $result[1]
}
Write-Host "::endgroup::"

Write-Host "::group::LogLevel"
$result = [TestHelper]::RunTestCase("LogLevel", $true)
if ($result.Length -gt 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
<CSharpier_UnformattedAsWarnings>true</CSharpier_UnformattedAsWarnings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Net8;

public class Class1
{


}
8 changes: 6 additions & 2 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,14 @@ If a list of paths is not supplied, then stdin is read as a file, formatted and
`dotnet csharpier check [<directoryOrFile\>]`

Used to check if your files are already formatted. Outputs any files that have not already been formatted.
This will return exit code 1 if there are unformatted files which is useful for CI pipelines.
By default this will return exit code 1 if there are unformatted files which is useful for CI pipelines.

#### Options
See the `format` command for descriptions of these options
`--unformatted-as-warnings`

Treat unformatted files as a warning instead of an error. If there are unformatted files they will be printed to the output as a warning and the process will return an exit code of 0.

See the `format` command for descriptions of these additional options
- `--include-generated`
- `--no-msbuild-check`
- `--compilation-errors-as-warnings`
Expand Down
8 changes: 8 additions & 0 deletions docs/MSBuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Valid options are:
- Information (default)
- Debug

### Unformatted as Warnings
If you do not want to fail the build when running `check` and encountering unformatted files you may set the following property.
```xml
<PropertyGroup>
<CSharpier_UnformattedAsWarnings>true</CSharpier_UnformattedAsWarnings>
</PropertyGroup>
```

### Target Frameworks
CSharpier.MSBuild will be run with net8.0 or net9.0 if the project targets one of the three frameworks. In cases where the project targets something else (net48, netstandard2.0) `CSharpier_FrameworkVersion` will default to net8.0
This can be controlled with the following property. This property is required if the csproj is targeting < net8.0 (netstandard2.0, net48, etc) and net8.0 is not installed.
Expand Down