From 0b45cde74767d765317cf8e7906d19f52d9e0742 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Girard?=
<47795283+sebastieng84@users.noreply.github.com>
Date: Thu, 16 Nov 2023 21:05:08 -0500
Subject: [PATCH 1/2] Ignore .editorconfig files in .csharpierignore
Ignore .editorconfig files in .csharpierignore
Ignore .editorconfig files in .csharpierignore
---
.../EditorConfig/EditorConfigParser.cs | 4 +-
Src/CSharpier.Cli/IgnoreFile.cs | 1 -
Src/CSharpier.Cli/Options/OptionsProvider.cs | 9 ++--
Src/CSharpier.Tests/OptionsProviderTests.cs | 50 +++++++++++++++++++
4 files changed, 58 insertions(+), 6 deletions(-)
diff --git a/Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs b/Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs
index b31263284..61239710b 100644
--- a/Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs
+++ b/Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs
@@ -7,7 +7,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 "")
@@ -18,6 +19,7 @@ IFileSystem fileSystem
var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(directoryName);
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..746e8e7ba 100644
--- a/Src/CSharpier.Cli/Options/OptionsProvider.cs
+++ b/Src/CSharpier.Cli/Options/OptionsProvider.cs
@@ -48,20 +48,21 @@ 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..5cbad9544 100644
--- a/Src/CSharpier.Tests/OptionsProviderTests.cs
+++ b/Src/CSharpier.Tests/OptionsProviderTests.cs
@@ -519,6 +519,56 @@ 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()
From 50f5d5217837689c9a56c54298ceaf61e59e8932 Mon Sep 17 00:00:00 2001
From: "bela.vandervoort"
Date: Thu, 23 Nov 2023 13:50:50 -0600
Subject: [PATCH 2/2] format files
---
Src/CSharpier.Cli/Options/OptionsProvider.cs | 8 ++++++--
Src/CSharpier.Tests/OptionsProviderTests.cs | 13 +++++--------
2 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/Src/CSharpier.Cli/Options/OptionsProvider.cs b/Src/CSharpier.Cli/Options/OptionsProvider.cs
index 746e8e7ba..fa07e8b12 100644
--- a/Src/CSharpier.Cli/Options/OptionsProvider.cs
+++ b/Src/CSharpier.Cli/Options/OptionsProvider.cs
@@ -49,7 +49,7 @@ CancellationToken cancellationToken
IList? editorConfigSections = null;
var ignoreFile = await IgnoreFile.Create(directoryName, fileSystem, cancellationToken);
-
+
try
{
editorConfigSections = EditorConfigParser.FindForDirectoryName(
@@ -60,7 +60,11 @@ CancellationToken cancellationToken
}
catch (Exception ex)
{
- logger.LogError(ex, "Failure parsing editorconfig files for {DirectoryName}", directoryName);
+ logger.LogError(
+ ex,
+ "Failure parsing editorconfig files for {DirectoryName}",
+ directoryName
+ );
}
return new OptionsProvider(
diff --git a/Src/CSharpier.Tests/OptionsProviderTests.cs b/Src/CSharpier.Tests/OptionsProviderTests.cs
index 5cbad9544..be3adaee8 100644
--- a/Src/CSharpier.Tests/OptionsProviderTests.cs
+++ b/Src/CSharpier.Tests/OptionsProviderTests.cs
@@ -519,7 +519,7 @@ public async Task Should_Not_Prefer_Closer_EditorConfig()
);
result.TabWidth.Should().Be(1);
}
-
+
[Test]
public async Task Should_Ignore_Invalid_EditorConfig()
{
@@ -534,10 +534,10 @@ public async Task Should_Ignore_Invalid_EditorConfig()
);
var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");
-
+
result.TabWidth.Should().Be(4);
}
-
+
[Test]
public async Task Should_Ignore_Ignored_EditorConfig()
{
@@ -557,11 +557,8 @@ public async Task Should_Ignore_Ignored_EditorConfig()
indent_size = 1
"
);
-
- context.WhenAFileExists(
- "c:/test/.csharpierignore",
- "/subfolder/.editorconfig"
- );
+
+ context.WhenAFileExists("c:/test/.csharpierignore", "/subfolder/.editorconfig");
var result = await context.CreateProviderAndGetOptionsFor(
"c:/test",