Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues with duplicated editorconfig sections, handle failures parsing editorconfigs #993

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading