Skip to content

Commit

Permalink
Limit config searching the same way editorconfig searching is limited (
Browse files Browse the repository at this point in the history
…#1243)

* Limit config searching the same way editorconfig searching is limited

closes #1240

* don't forget this file
  • Loading branch information
belav authored Apr 26, 2024
1 parent 86f6690 commit aea004c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CancellationToken cancellationToken
fileSystem,
logger,
cancellationToken,
limitEditorConfigSearch: true
limitConfigSearch: true
);

if (
Expand Down
10 changes: 8 additions & 2 deletions Src/CSharpier.Cli/Options/ConfigurationFileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ ConfigurationFileOptions configurationFileOptions
internal static List<CSharpierConfigData> FindForDirectoryName(
string directoryName,
IFileSystem fileSystem,
ILogger logger
ILogger logger,
bool limitEditorConfigSearch
)
{
var results = new List<CSharpierConfigData>();
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)
Expand Down
11 changes: 8 additions & 3 deletions Src/CSharpier.Cli/Options/OptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ public static async Task<OptionsProvider> Create(
IFileSystem fileSystem,
ILogger logger,
CancellationToken cancellationToken,
bool limitEditorConfigSearch = false
bool limitConfigSearch = false
)
{
var specifiedPrinterOptions = configPath is not null
? ConfigurationFileOptions.CreatePrinterOptionsFromPath(configPath, fileSystem, logger)
: null;

var csharpierConfigs = configPath is null
? ConfigurationFileOptions.FindForDirectoryName(directoryName, fileSystem, logger)
? ConfigurationFileOptions.FindForDirectoryName(
directoryName,
fileSystem,
logger,
limitConfigSearch
)
: Array.Empty<CSharpierConfigData>().ToList();

IList<EditorConfigSections>? editorConfigSections = null;
Expand All @@ -56,7 +61,7 @@ public static async Task<OptionsProvider> Create(
editorConfigSections = EditorConfigParser.FindForDirectoryName(
directoryName,
fileSystem,
limitEditorConfigSearch,
limitConfigSearch,
ignoreFile
);
}
Expand Down
22 changes: 22 additions & 0 deletions Src/CSharpier.Tests/OptionsProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit aea004c

Please sign in to comment.