Skip to content

Commit 4a642e1

Browse files
committed
Hard fail to prevent IndentSize of <= 0 to avoid any issues.
closes #1736
1 parent 8363c47 commit 4a642e1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Src/CSharpier.Cli.Tests/CliTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,19 @@ public async Task Format_Should_Reformat_When_Options_Change_With_Cache()
743743
result.Should().Contain("\n\t// break\n");
744744
}
745745

746+
[Test]
747+
public async Task Format_Should_Throw_With_IndentSize_Zero()
748+
{
749+
var unformattedContent = "public class ClassName { \n// break\n }\n";
750+
751+
await WriteFileAsync("Unformatted.cs", unformattedContent);
752+
await WriteFileAsync(".csharpierrc", "indentSize: 0");
753+
var result = await new CsharpierProcess().WithArguments("format .").ExecuteAsync();
754+
755+
result.ExitCode.Should().Be(1);
756+
result.ErrorOutput.Should().Contain("An indent size of 0 is not valid");
757+
}
758+
746759
[Test]
747760
public void Format_Should_Handle_Concurrent_Processes()
748761
{

Src/CSharpier.Core/PrinterOptions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,24 @@ namespace CSharpier.Core;
44

55
internal class PrinterOptions(Formatter formatter)
66
{
7+
private int indentSize = formatter == Formatter.XML ? 2 : 4;
78
public bool IncludeAST { get; init; }
89
public bool IncludeDocTree { get; init; }
910
public bool UseTabs { get; set; }
10-
public int IndentSize { get; set; } = formatter == Formatter.XML ? 2 : 4;
11+
12+
public int IndentSize
13+
{
14+
get => this.indentSize;
15+
set
16+
{
17+
if (value <= 0)
18+
{
19+
throw new ArgumentException("An indent size of 0 is not valid");
20+
}
21+
this.indentSize = value;
22+
}
23+
}
24+
1125
public int Width { get; set; } = 100;
1226
public EndOfLine EndOfLine { get; set; } = EndOfLine.Auto;
1327
public bool TrimInitialLines { get; init; } = true;

0 commit comments

Comments
 (0)