Skip to content

Commit

Permalink
fix issues with duplicated editorconfig sections, handle failures par…
Browse files Browse the repository at this point in the history
…sing editorconfigs (#993)

closes #989
  • Loading branch information
belav authored Nov 9, 2023
1 parent 93baf5a commit a9c4ff1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
8 changes: 7 additions & 1 deletion Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ internal static class ConfigFileParser
private static readonly Regex CommentRegex = new("^[;#].*$");

private static readonly IniParserConfiguration Configuration =
new() { CommentRegex = CommentRegex, AllowDuplicateKeys = true };
new()
{
CommentRegex = CommentRegex,
AllowDuplicateKeys = true,
AllowDuplicateSections = true,
OverrideDuplicateKeys = true
};

public static ConfigFile Parse(string filePath, IFileSystem fileSystem)
{
Expand Down
25 changes: 18 additions & 7 deletions Src/CSharpier.Cli/Options/OptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ namespace CSharpier.Cli.Options;
using System.IO.Abstractions;
using System.Text.Json;
using CSharpier.Cli.EditorConfig;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.Logging;
using PrinterOptions = CSharpier.PrinterOptions;

internal class OptionsProvider
{
private readonly List<EditorConfigSections> editorConfigs;
private readonly IList<EditorConfigSections> editorConfigs;
private readonly List<CSharpierConfigData> csharpierConfigs;
private readonly IgnoreFile ignoreFile;
private readonly PrinterOptions? specifiedPrinterOptions;
private readonly IFileSystem fileSystem;

private OptionsProvider(
List<EditorConfigSections> editorConfigs,
IList<EditorConfigSections> editorConfigs,
List<CSharpierConfigData> csharpierConfigs,
IgnoreFile ignoreFile,
PrinterOptions? specifiedPrinterOptions,
Expand Down Expand Up @@ -45,14 +46,24 @@ CancellationToken cancellationToken
? ConfigurationFileOptions.FindForDirectoryName(directoryName, fileSystem, logger)
: Array.Empty<CSharpierConfigData>().ToList();

var editorConfigSections = EditorConfigParser.FindForDirectoryName(
directoryName,
fileSystem
);
IList<EditorConfigSections>? editorConfigSections = null;

try
{
editorConfigSections = EditorConfigParser.FindForDirectoryName(
directoryName,
fileSystem
);
}
catch (Exception ex)
{
logger.LogError(ex, $"Failure parsing editorconfig files for {directoryName}");
}

var ignoreFile = await IgnoreFile.Create(directoryName, fileSystem, cancellationToken);

return new OptionsProvider(
editorConfigSections,
editorConfigSections ?? Array.Empty<EditorConfigSections>(),
csharpierConfigs,
ignoreFile,
specifiedPrinterOptions,
Expand Down
55 changes: 55 additions & 0 deletions Src/CSharpier.Tests/OptionsProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,61 @@ public async Task Should_Support_EditorConfig_With_Comments()
result.EndOfLine.Should().Be(EndOfLine.CRLF);
}

[Test]
public async Task Should_Support_EditorConfig_With_Duplicated_Sections()
{
var context = new TestContext();
context.WhenAFileExists(
"c:/test/.editorconfig",
@"
[*]
indent_size = 2
[*]
indent_size = 4
"
);

var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");

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

[Test]
public async Task Should_Support_EditorConfig_With_Duplicated_Rules()
{
var context = new TestContext();
context.WhenAFileExists(
"c:/test/.editorconfig",
@"
[*]
indent_size = 2
indent_size = 4
"
);

var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");

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

[Test]
public async Task Should_Not_Fail_With_Bad_EditorConfig()
{
var context = new TestContext();
context.WhenAFileExists(
"c:/test/.editorconfig",
@"
[*
indent_size==
"
);

var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");

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

[TestCase("tab_width")]
[TestCase("indent_size")]
public async Task Should_Support_EditorConfig_Tabs(string propertyName)
Expand Down

0 comments on commit a9c4ff1

Please sign in to comment.