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

Ignore .editorconfig file included in the .csharpierignore file #1030

Merged
merged 4 commits into from
Nov 23, 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
4 changes: 3 additions & 1 deletion Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal static class EditorConfigParser
/// <summary>Finds all configs above the given directory as well as within the subtree of this directory</summary>
public static List<EditorConfigSections> FindForDirectoryName(
string directoryName,
IFileSystem fileSystem
IFileSystem fileSystem,
IgnoreFile ignoreFile
)
{
if (directoryName is "")
Expand All @@ -24,6 +25,7 @@ IFileSystem fileSystem
// TODO this is probably killing performance if nothing else when piping a single file
var editorConfigFiles = directoryInfo
.EnumerateFiles(".editorconfig", SearchOption.AllDirectories)
.Where(x => !ignoreFile.IsIgnored(x.FullName))
.ToList();

// already found any in this directory above
Expand Down
1 change: 0 additions & 1 deletion Src/CSharpier.Cli/IgnoreFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO.Abstractions;
using Microsoft.Extensions.Logging;

namespace CSharpier.Cli;

Expand Down
13 changes: 9 additions & 4 deletions Src/CSharpier.Cli/Options/OptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,25 @@ CancellationToken cancellationToken

IList<EditorConfigSections>? editorConfigSections = null;

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

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

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

return new OptionsProvider(
editorConfigSections ?? Array.Empty<EditorConfigSections>(),
csharpierConfigs,
Expand Down
47 changes: 47 additions & 0 deletions Src/CSharpier.Tests/OptionsProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,53 @@ public async Task Should_Not_Prefer_Closer_EditorConfig()
result.TabWidth.Should().Be(1);
}

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

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

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

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

context.WhenAFileExists(
"c:/test/.editorconfig",
@"
[*]
indent_size = 1
"
);

context.WhenAFileExists("c:/test/.csharpierignore", "/subfolder/.editorconfig");

var result = await context.CreateProviderAndGetOptionsFor(
"c:/test",
"c:/test/subfolder/test.cs"
);
result.TabWidth.Should().Be(1);
}

[Test]
public async Task Should_Prefer_Closer_CSharpierrc()
{
Expand Down
Loading