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
closes #1240
  • Loading branch information
belav committed Apr 26, 2024
1 parent 5d898a3 commit d5c1aa3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
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 d5c1aa3

Please sign in to comment.