Skip to content

Commit 3325ea3

Browse files
committed
Adding an option for 'unformatted as warnings'
closes #1687
1 parent c71c0d9 commit 3325ea3

File tree

10 files changed

+90
-6
lines changed

10 files changed

+90
-6
lines changed

Src/CSharpier.Cli.Tests/CliTests.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public async Task Check_Should_Write_To_StdError_For_Piped_Invalid_File()
476476
}
477477

478478
[Test]
479-
public async Task Check_Should_Write_Unformatted_File()
479+
public async Task Check_Should_Write_Error_With_Unformatted_File()
480480
{
481481
var unformattedContent = "public class ClassName1 {\n\n}";
482482

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

496-
// TODO overrides tests for piping files
496+
[Test]
497+
public async Task Check_Should_Write_Warning_With_Unformatted_File()
498+
{
499+
var unformattedContent = "public class ClassName1 {\n\n}";
500+
501+
await WriteFileAsync("CheckUnformatted.cs", unformattedContent);
502+
503+
var result = await new CsharpierProcess()
504+
.WithArguments("check CheckUnformatted.cs --unformatted-as-warnings")
505+
.ExecuteAsync();
506+
507+
result
508+
.Output.Replace('\\', '/')
509+
.Should()
510+
.StartWith("Warning ./CheckUnformatted.cs - Was not formatted.");
511+
result.ExitCode.Should().Be(0);
512+
}
513+
497514
[TestCase("\n")]
498515
[TestCase("\r\n")]
499516
public async Task PipeFiles_Should_Format_Multiple_Piped_Files(string lineEnding)

Src/CSharpier.Cli/CommandLineFormatter.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,11 @@ CommandLineFormatterResult result
371371
{
372372
if (
373373
(!commandLineOptions.CompilationErrorsAsWarnings && result.FailedCompilation > 0)
374-
|| (commandLineOptions.Check && result.UnformattedFiles > 0)
374+
|| (
375+
commandLineOptions.Check
376+
&& result.UnformattedFiles > 0
377+
&& !commandLineOptions.UnformattedAsWarnings
378+
)
375379
|| result.FailedFormattingValidation > 0
376380
|| result.ExceptionsFormatting > 0
377381
|| result.ExceptionsValidatingSource > 0
@@ -546,7 +550,16 @@ ref commandLineFormatterResult.ExceptionsValidatingSource
546550
codeFormattingResult.Code,
547551
fileToFormatInfo.FileContents
548552
);
549-
fileIssueLogger.WriteError($"Was not formatted.\n{difference}\n");
553+
var message = $"Was not formatted.\n{difference}\n";
554+
if (commandLineOptions.UnformattedAsWarnings)
555+
{
556+
fileIssueLogger.WriteWarning(message);
557+
}
558+
else
559+
{
560+
fileIssueLogger.WriteError(message);
561+
}
562+
550563
Interlocked.Increment(ref commandLineFormatterResult.UnformattedFiles);
551564
}
552565

Src/CSharpier.Cli/CommandLineOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ internal class CommandLineOptions
1515
public bool NoCache { get; init; }
1616
public bool NoMSBuildCheck { get; init; }
1717
public bool CompilationErrorsAsWarnings { get; init; }
18+
public bool UnformattedAsWarnings { get; init; }
1819
public bool IncludeGenerated { get; init; }
1920
public string? StandardInFileContents { get; init; }
2021
public string? ConfigPath { get; init; }

Src/CSharpier.Cli/FormattingCommands.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public static Command CreateFormatCommand()
6969
noMSBuildCheck,
7070
includeGenerated,
7171
compilationErrorsAsWarnings,
72+
false,
7273
configPath,
7374
ignorePath,
7475
stdinPath,
@@ -96,6 +97,7 @@ public static Command CreateCheckCommand()
9697
checkCommand.AddOption(IncludeGeneratedOption);
9798
checkCommand.AddOption(NoMsBuildCheckOption);
9899
checkCommand.AddOption(CompilationErrorsAsWarningsOption);
100+
checkCommand.AddOption(UnformattedAsWarningsOption);
99101

100102
checkCommand.SetHandler(async context =>
101103
{
@@ -105,6 +107,9 @@ public static Command CreateCheckCommand()
105107
var compilationErrorsAsWarnings = context.ParseResult.GetValueForOption(
106108
CompilationErrorsAsWarningsOption
107109
);
110+
var unformattedAsWarningsOption = context.ParseResult.GetValueForOption(
111+
UnformattedAsWarningsOption
112+
);
108113
var configPath = context.ParseResult.GetValueForOption(ConfigPathOption);
109114
var ignorePath = context.ParseResult.GetValueForOption(IgnorePathOption);
110115
var logLevel = context.ParseResult.GetValueForOption(LogLevelOption);
@@ -121,6 +126,7 @@ public static Command CreateCheckCommand()
121126
noMSBuildCheck,
122127
includeGenerated,
123128
compilationErrorsAsWarnings,
129+
unformattedAsWarningsOption,
124130
configPath,
125131
ignorePath,
126132
null,
@@ -143,6 +149,7 @@ private static async Task<int> FormatOrCheck(
143149
bool noMSBuildCheck,
144150
bool includeGenerated,
145151
bool compilationErrorsAsWarnings,
152+
bool unformattedAsWarnings,
146153
string? configPath,
147154
string? ignorePath,
148155
string? stdinPath,
@@ -193,6 +200,7 @@ CancellationToken cancellationToken
193200
ConfigPath = configPath,
194201
IgnorePath = ignorePath,
195202
CompilationErrorsAsWarnings = compilationErrorsAsWarnings,
203+
UnformattedAsWarnings = unformattedAsWarnings,
196204
LogFormat = logFormat,
197205
};
198206

@@ -271,4 +279,9 @@ Log output format
271279
["--compilation-errors-as-warnings"],
272280
"Treat compilation errors from files as warnings instead of errors."
273281
);
282+
283+
private static readonly Option<bool> UnformattedAsWarningsOption = new(
284+
["--unformatted-as-warnings"],
285+
"Treat unformatted files as warnings instead of errors."
286+
);
274287
}

Src/CSharpier.MsBuild/build/CSharpier.MsBuild.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<CSharpierCommand>format</CSharpierCommand>
1010
<CSharpierCommand Condition="'$(CSharpier_Check)' == 'true'">check</CSharpierCommand>
1111
<CSharpierArgs Condition="'$(CSharpier_LogLevel)' != ''">$(CSharpierArgs) --log-level $(CSharpier_LogLevel)</CSharpierArgs>
12+
<CSharpierArgs Condition="'$(CSharpier_UnformattedAsWarnings)' == 'true'">$(CSharpierArgs) --unformatted-as-warnings</CSharpierArgs>
1213
<FirstTargetFramework Condition=" '$(TargetFrameworks)' == '' ">$(TargetFramework)</FirstTargetFramework>
1314
<FirstTargetFramework Condition=" '$(FirstTargetFramework)' == '' ">$(TargetFrameworks.Split(';')[0])</FirstTargetFramework>
1415
<NullOutput>NUL</NullOutput>

Tests/MsBuild/Run.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ if ($result.Length -gt 1) {
9898
}
9999
Write-Host "::endgroup::"
100100

101+
Write-Host "::group::UnformattedFileCausesWarning"
102+
$result = [TestHelper]::RunTestCase("UnformattedFileCausesWarning", $false)
103+
if ($result.Length -gt 1) {
104+
$failureMessages += $result[1]
105+
}
106+
Write-Host "::endgroup::"
107+
101108
Write-Host "::group::LogLevel"
102109
$result = [TestHelper]::RunTestCase("LogLevel", $true)
103110
if ($result.Length -gt 1) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
5+
<CSharpier_UnformattedAsWarnings1>true</CSharpier_UnformattedAsWarnings1>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="CSharpier.MsBuild" Version="0.0.1">
9+
<PrivateAssets>all</PrivateAssets>
10+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
11+
</PackageReference>
12+
</ItemGroup>
13+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Net8;
2+
3+
public class Class1
4+
{
5+
6+
7+
}

docs/CLI.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,14 @@ If a list of paths is not supplied, then stdin is read as a file, formatted and
159159
`dotnet csharpier check [<directoryOrFile\>]`
160160

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

164164
#### Options
165-
See the `format` command for descriptions of these options
165+
`--unformatted-as-warnings`
166+
167+
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.
168+
169+
See the `format` command for descriptions of these additional options
166170
- `--include-generated`
167171
- `--no-msbuild-check`
168172
- `--compilation-errors-as-warnings`

docs/MSBuild.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ Valid options are:
5050
- Information (default)
5151
- Debug
5252

53+
### Unformatted as Warnings
54+
If you do not want to fail the build when running `check` and encountering unformatted files you may set the following property.
55+
```xml
56+
<PropertyGroup>
57+
<CSharpier_UnformattedAsWarnings>true</CSharpier_UnformattedAsWarnings>
58+
</PropertyGroup>
59+
```
60+
5361
### Target Frameworks
5462
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
5563
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.

0 commit comments

Comments
 (0)