Skip to content

Commit

Permalink
Implementing options with a file #10
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Apr 26, 2021
1 parent 0698e13 commit 72158aa
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 18 deletions.
1 change: 1 addition & 0 deletions .idea/.idea.CSharpier/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

220 changes: 220 additions & 0 deletions Src/CSharpier.Tests/ConfigurationOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
using System.IO.Abstractions.TestingHelpers;
using FluentAssertions;
using NUnit.Framework;

namespace CSharpier.Tests
{
[TestFixture]
public class ConfigurationOptionsTests
{
private MockFileSystem fileSystem;

[SetUp]
public void SetUp()
{
this.fileSystem = new MockFileSystem();
}

[Test]
public void Should_Return_Default_Options_With_Empty_Json()
{
WhenThereExists("c:/test/.csharpierrc", "{}");

var result = CreateConfigurationOptions("c:/test");

ShouldHaveDefaultOptions(result);
}

[Test]
public void Should_Return_Default_Options_With_No_File()
{
var result = CreateConfigurationOptions("c:/test");

ShouldHaveDefaultOptions(result);
}

[Test]
public void Should_Return_Json_Extension_Options()
{
WhenThereExists(
"c:/test/.csharpierrc.json",
"{ \"printWidth\": 10 }"
);

var result = CreateConfigurationOptions("c:/test");

result.PrintWidth.Should().Be(10);
}

[TestCase("yaml")]
[TestCase("yml")]
public void Should_Return_Yaml_Extension_Options(string extension)
{
WhenThereExists(
$"c:/test/.csharpierrc.{extension}",
"printWidth: 10"
);

var result = CreateConfigurationOptions("c:/test");

result.PrintWidth.Should().Be(10);
}

[TestCase("{ \"printWidth\": 10 }")]
[TestCase("printWidth: 10")]
public void Should_Read_ExtensionLess_File(string contents)
{
WhenThereExists($"c:/test/.csharpierrc", contents);

var result = CreateConfigurationOptions("c:/test");

result.PrintWidth.Should().Be(10);
}

[Test]
public void Should_Prefer_No_Extension()
{
WhenThereExists("c:/test/.csharpierrc", "{ \"printWidth\": 1 }");

WhenThereExists(
"c:/test/.csharpierrc.json",
"{ \"printWidth\": 2 }"
);
WhenThereExists("c:/test/.csharpierrc.yaml", "printWidth: 3");

var result = CreateConfigurationOptions("c:/test");

result.PrintWidth.Should().Be(1);
}

[Test]
public void Should_Return_PrintWidth_With_Json()
{
WhenThereExists("c:/test/.csharpierrc", "{ \"printWidth\": 10 }");

var result = CreateConfigurationOptions("c:/test");

result.PrintWidth.Should().Be(10);
}

[Test]
public void Should_Return_TabWidth_With_Json()
{
WhenThereExists("c:/test/.csharpierrc", "{ \"tabWidth\": 10 }");

var result = CreateConfigurationOptions("c:/test");

result.TabWidth.Should().Be(10);
}

[Test]
public void Should_Return_UseTabs_With_Json()
{
WhenThereExists("c:/test/.csharpierrc", "{ \"useTabs\": true }");

var result = CreateConfigurationOptions("c:/test");

result.UseTabs.Should().BeTrue();
}

[Test]
public void Should_Return_Exclude_With_Json()
{
WhenThereExists(
"c:/test/.csharpierrc",
"{ \"exclude\": [\"src\\\\ignoreFile.cs\"] }"
);

var result = CreateConfigurationOptions("c:/test");

result.Exclude.Should().Contain("src/ignoreFile.cs");
}

[Test]
public void Should_Return_EndOfLine_With_Json()
{
WhenThereExists(
"c:/test/.csharpierrc",
"{ \"endOfLine\": \"crlf\" }"
);

var result = CreateConfigurationOptions("c:/test");

result.EndOfLine.Should().Be("crlf");
}

[Test]
public void Should_Return_PrintWidth_With_Yaml()
{
WhenThereExists("c:/test/.csharpierrc", "printWidth: 10");

var result = CreateConfigurationOptions("c:/test");

result.PrintWidth.Should().Be(10);
}

[Test]
public void Should_Return_TabWidth_With_Yaml()
{
WhenThereExists("c:/test/.csharpierrc", "tabWidth: 10");

var result = CreateConfigurationOptions("c:/test");

result.TabWidth.Should().Be(10);
}

[Test]
public void Should_Return_UseTabs_With_Yaml()
{
WhenThereExists("c:/test/.csharpierrc", "useTabs: true");

var result = CreateConfigurationOptions("c:/test");

result.UseTabs.Should().BeTrue();
}

[Test]
public void Should_Return_Exclude_With_Yaml()
{
WhenThereExists(
"c:/test/.csharpierrc",
"exclude:\n - src\\ignoreFile.cs"
);

var result = CreateConfigurationOptions("c:/test");

result.Exclude.Should().Contain("src/ignoreFile.cs");
}

[Test]
public void Should_Return_EndOfLine_With_Yaml()
{
WhenThereExists("c:/test/.csharpierrc", "endOfLine: crlf");

var result = CreateConfigurationOptions("c:/test");

result.EndOfLine.Should().Be("crlf");
}

private void ShouldHaveDefaultOptions(
ConfigurationOptions configurationOptions
) {
configurationOptions.Exclude.Should().BeEmpty();
configurationOptions.PrintWidth.Should().Be(100);
configurationOptions.TabWidth.Should().Be(4);
configurationOptions.UseTabs.Should().BeFalse();
configurationOptions.EndOfLine.Should().Be("lf");
}

private ConfigurationOptions CreateConfigurationOptions(
string rootPath
) {
return ConfigurationOptions.Create(rootPath, fileSystem);
}

private void WhenThereExists(string path, string contents)
{
this.fileSystem.AddFile(path, new MockFileData(contents));
}
}
}
11 changes: 10 additions & 1 deletion Src/CSharpier.Tests/DocPrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,16 @@ private static void PrintedDocShouldBe(

private static string Print(Doc doc, int width = 80)
{
return DocPrinter.Print(doc, new Options { Width = width })
return DocPrinter.Print(
doc,
// TODO if we could use auto now, this wouldn't be needed
// or maybe all the tests get converted to lf
new Options
{
Width = width,
EndOfLine = "\r\n"
}
)
.TrimEnd('\r', '\n');
}
}
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier.Tests/TestFiles/BaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void RunTest(string folderName, string fileName)
var formatter = new CodeFormatter();
var result = formatter.Format(
fileReaderResult.FileContents,
new Options()
new Options() { Width = 80, EndOfLine = "\r\n" }
);

var actualFilePath = filePath.Replace(".cst", ".actual.cst");
Expand Down
3 changes: 3 additions & 0 deletions Src/CSharpier/CSharpier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
<PackageReference Include="System.IO.Abstractions" Version="13.2.29" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="13.2.29" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
<PackageReference Include="UTF.Unknown" Version="2.3.0" />
<PackageReference Include="YamlDotNet" Version="11.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 21 additions & 2 deletions Src/CSharpier/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -22,6 +23,7 @@ public class CommandLineFormatter
protected readonly Stopwatch Stopwatch;

protected readonly ConfigurationOptions ConfigurationOptions;
protected readonly Options Options;

private CommandLineFormatter(
string rootPath,
Expand All @@ -30,7 +32,24 @@ private CommandLineFormatter(
bool skipWrite
) {
this.RootPath = rootPath;
this.ConfigurationOptions = ConfigurationOptions.Create(rootPath);
this.ConfigurationOptions = ConfigurationOptions.Create(
rootPath,
new FileSystem()
);
this.Options = new Options
{
TabWidth = this.ConfigurationOptions.TabWidth,
UseTabs = this.ConfigurationOptions.UseTabs,
Width = this.ConfigurationOptions.PrintWidth,
EndOfLine = this.ConfigurationOptions.EndOfLine == "lf"
? "\n"
: this.ConfigurationOptions.EndOfLine == "crlf"
? "\r\n"
: throw new Exception(
"Unhandled value from EndOfLine options " +
this.ConfigurationOptions.EndOfLine
)
};
this.Check = check;
this.Validate = !fast;
this.SkipWrite = skipWrite;
Expand Down Expand Up @@ -132,7 +151,7 @@ CancellationToken cancellationToken
{
result = await new CodeFormatter().FormatAsync(
fileReaderResult.FileContents,
new Options(),
this.Options,
cancellationToken
);
}
Expand Down
Loading

0 comments on commit 72158aa

Please sign in to comment.