From d5c1aa3a17508fb050e3149044af9abfc225a296 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Fri, 26 Apr 2024 10:09:12 -0500 Subject: [PATCH] Limit config searching the same way editorconfig searching is limited closes #1240 --- .../Options/ConfigurationFileOptions.cs | 10 +++++++-- Src/CSharpier.Cli/Options/OptionsProvider.cs | 11 +++++++--- Src/CSharpier.Tests/OptionsProviderTests.cs | 22 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Src/CSharpier.Cli/Options/ConfigurationFileOptions.cs b/Src/CSharpier.Cli/Options/ConfigurationFileOptions.cs index 010f7443f..e5ea7888b 100644 --- a/Src/CSharpier.Cli/Options/ConfigurationFileOptions.cs +++ b/Src/CSharpier.Cli/Options/ConfigurationFileOptions.cs @@ -46,14 +46,20 @@ ConfigurationFileOptions configurationFileOptions internal static List FindForDirectoryName( string directoryName, IFileSystem fileSystem, - ILogger logger + ILogger logger, + bool limitEditorConfigSearch ) { var results = new List(); var directoryInfo = fileSystem.DirectoryInfo.New(directoryName); var filesByDirectory = directoryInfo - .EnumerateFiles(".csharpierrc*", SearchOption.AllDirectories) + .EnumerateFiles( + ".csharpierrc*", + limitEditorConfigSearch + ? SearchOption.TopDirectoryOnly + : SearchOption.AllDirectories + ) .GroupBy(o => o.DirectoryName); foreach (var group in filesByDirectory) diff --git a/Src/CSharpier.Cli/Options/OptionsProvider.cs b/Src/CSharpier.Cli/Options/OptionsProvider.cs index 20437448a..3cd7b0e79 100644 --- a/Src/CSharpier.Cli/Options/OptionsProvider.cs +++ b/Src/CSharpier.Cli/Options/OptionsProvider.cs @@ -36,7 +36,7 @@ public static async Task Create( IFileSystem fileSystem, ILogger logger, CancellationToken cancellationToken, - bool limitEditorConfigSearch = false + bool limitConfigSearch = false ) { var specifiedPrinterOptions = configPath is not null @@ -44,7 +44,12 @@ public static async Task Create( : null; var csharpierConfigs = configPath is null - ? ConfigurationFileOptions.FindForDirectoryName(directoryName, fileSystem, logger) + ? ConfigurationFileOptions.FindForDirectoryName( + directoryName, + fileSystem, + logger, + limitConfigSearch + ) : Array.Empty().ToList(); IList? editorConfigSections = null; @@ -56,7 +61,7 @@ public static async Task Create( editorConfigSections = EditorConfigParser.FindForDirectoryName( directoryName, fileSystem, - limitEditorConfigSearch, + limitConfigSearch, ignoreFile ); } diff --git a/Src/CSharpier.Tests/OptionsProviderTests.cs b/Src/CSharpier.Tests/OptionsProviderTests.cs index 6f63fc91a..f5ff96962 100644 --- a/Src/CSharpier.Tests/OptionsProviderTests.cs +++ b/Src/CSharpier.Tests/OptionsProviderTests.cs @@ -647,6 +647,28 @@ public async Task Should_Not_Look_For_Subfolders_EditorConfig_When_Limited() result.TabWidth.Should().Be(4); } + [Test] + public async Task Should_Not_Look_For_Subfolders_CSharpierRc_When_Limited() + { + var context = new TestContext(); + context.WhenAFileExists( + "c:/test/subfolder/.csharpierrc", + @" + [*] + tabWidth: 1 + " + ); + + // this shouldn't happen in the real world, but validates we correctly limit + // the search to the top directory only + var result = await context.CreateProviderAndGetOptionsFor( + "c:/test/", + "c:/test/subfolder/test.cs", + limitEditorConfigSearch: true + ); + result.TabWidth.Should().Be(4); + } + [Test] public async Task Should_Look_For_Subfolders_When_Limited() {