Skip to content

Commit

Permalink
Don't blow up on empty config files
Browse files Browse the repository at this point in the history
closes #421
  • Loading branch information
belav committed Aug 26, 2021
1 parent 30f9652 commit 5339eb7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ public void File_Should_Format_With_Supplied_Symbols()
);
}

[TestCase(".csharpierrc")]
[TestCase(".csharpierrc.json")]
[TestCase(".csharpierrc.yaml")]
public void Empty_Config_Files_Should_Log_Warning(string configFileName)
{
WhenAFileExists(".csharpierrc", "");
WhenAFileExists("file1.cs", "public class ClassName { }");

this.Format();
// TODO check for warning
}

private (int exitCode, IList<string> lines) Format(
bool skipWrite = false,
bool check = false,
Expand Down
11 changes: 11 additions & 0 deletions Src/CSharpier.Tests/ConfigurationFileOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public void Should_Return_Default_Options_With_No_File()
ShouldHaveDefaultOptions(result);
}

[TestCase(".csharpierrc")]
[TestCase(".csharpierrc.json")]
[TestCase(".csharpierrc.yaml")]
public void Should_Return_Default_Options_With_Empty_File(string fileName)
{
WhenAFileExists($"c:/test/{fileName}", string.Empty);
var result = CreateConfigurationOptions("c:/test");

ShouldHaveDefaultOptions(result);
}

[Test]
public void Should_Return_Json_Extension_Options()
{
Expand Down
15 changes: 14 additions & 1 deletion Src/CSharpier/ConfigurationFileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ public class ConfigurationFileOptions
public int PrintWidth { get; init; } = 100;
public List<string>? PreprocessorSymbolSets { get; init; }

private static string[] validExtensions = { ".csharpierrc", ".json", ".yml", ".yaml" };
private static readonly string[] validExtensions =
{
".csharpierrc",
".json",
".yml",
".yaml"
};

public static PrinterOptions CreatePrinterOptions(
string baseDirectoryPath,
Expand Down Expand Up @@ -76,6 +82,13 @@ IFileSystem fileSystem
}

var contents = fileSystem.File.ReadAllText(file.FullName);

if (string.IsNullOrWhiteSpace(contents))
{
// TODO log warning
return new();
}

return contents.TrimStart().StartsWith("{")
? ReadJson(contents)
: ReadYaml(contents);
Expand Down

0 comments on commit 5339eb7

Please sign in to comment.