From 82dadf4af9ac69fc30436c7615841c4353d0521c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Girard?= <47795283+sebastieng84@users.noreply.github.com> Date: Thu, 23 Nov 2023 14:53:57 -0500 Subject: [PATCH] Ignore .editorconfig file included in the .csharpierignore file (#1030) * Ignore .editorconfig files in .csharpierignore Ignore .editorconfig files in .csharpierignore Ignore .editorconfig files in .csharpierignore * format files --------- Co-authored-by: Lasath Fernando Co-authored-by: bela.vandervoort Co-authored-by: Bela VanderVoort --- .../EditorConfig/EditorConfigParser.cs | 4 +- Src/CSharpier.Cli/IgnoreFile.cs | 1 - Src/CSharpier.Cli/Options/OptionsProvider.cs | 13 +++-- Src/CSharpier.Tests/OptionsProviderTests.cs | 47 +++++++++++++++++++ 4 files changed, 59 insertions(+), 6 deletions(-) diff --git a/Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs b/Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs index f4ebb5aeb..bac4df510 100644 --- a/Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs +++ b/Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs @@ -12,7 +12,8 @@ internal static class EditorConfigParser /// Finds all configs above the given directory as well as within the subtree of this directory public static List FindForDirectoryName( string directoryName, - IFileSystem fileSystem + IFileSystem fileSystem, + IgnoreFile ignoreFile ) { if (directoryName is "") @@ -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 diff --git a/Src/CSharpier.Cli/IgnoreFile.cs b/Src/CSharpier.Cli/IgnoreFile.cs index 97bd128cd..b2002c848 100644 --- a/Src/CSharpier.Cli/IgnoreFile.cs +++ b/Src/CSharpier.Cli/IgnoreFile.cs @@ -1,5 +1,4 @@ using System.IO.Abstractions; -using Microsoft.Extensions.Logging; namespace CSharpier.Cli; diff --git a/Src/CSharpier.Cli/Options/OptionsProvider.cs b/Src/CSharpier.Cli/Options/OptionsProvider.cs index 5e6c0c771..fa07e8b12 100644 --- a/Src/CSharpier.Cli/Options/OptionsProvider.cs +++ b/Src/CSharpier.Cli/Options/OptionsProvider.cs @@ -48,20 +48,25 @@ CancellationToken cancellationToken IList? 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(), csharpierConfigs, diff --git a/Src/CSharpier.Tests/OptionsProviderTests.cs b/Src/CSharpier.Tests/OptionsProviderTests.cs index a1cd8df1d..be3adaee8 100644 --- a/Src/CSharpier.Tests/OptionsProviderTests.cs +++ b/Src/CSharpier.Tests/OptionsProviderTests.cs @@ -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() {