Skip to content

Commit

Permalink
Ignore .editorconfig file included in the .csharpierignore file (#1030)
Browse files Browse the repository at this point in the history
* Ignore .editorconfig files in .csharpierignore

Ignore .editorconfig files in .csharpierignore

Ignore .editorconfig files in .csharpierignore

* format files

---------

Co-authored-by: Lasath Fernando <devel@lasath.org>
Co-authored-by: bela.vandervoort <bela.vandervoort@optimizely.com>
Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
  • Loading branch information
4 people authored Nov 23, 2023
1 parent 4df56a7 commit 82dadf4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
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

0 comments on commit 82dadf4

Please sign in to comment.