Skip to content

Commit

Permalink
NET-879 Cleanup EditorConfigGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource authored and sonartech committed Dec 17, 2024
1 parent e02c25d commit 00ecc1e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ public void GenerateEditorConfig_EmptyCollection_ValidEditorConfig()
{
var rootPath = "C:/Users/Johnny/source/repos/WebApplication";
var editorConfig = new EditorConfigGenerator(rootPath).Generate([]);
editorConfig.Should().Be(
$"""
is_global = true
""".Replace("\n", Environment.NewLine));
editorConfig.Should().Be("is_global = true");
}

[TestMethod]
Expand All @@ -64,12 +61,11 @@ public void GenerateEditorConfig_SingleFile_ValidEditorConfig()
var rootPath = "C:/Users/Johnny/source/repos/WebApplication";
var razorFile = "C:/Users/Johnny/source/repos/WebApplication/Component.razor";
var editorConfig = new EditorConfigGenerator(rootPath).Generate([razorFile]);
editorConfig.Should().Be(
$"""
is_global = true
[C:/Users/Johnny/source/repos/WebApplication/Component.razor]
build_metadata.AdditionalFiles.TargetPath = Q29tcG9uZW50LnJhem9y
""".Replace("\n", Environment.NewLine));
editorConfig.Should().BeIgnoringLineEndings("""
is_global = true
[C:/Users/Johnny/source/repos/WebApplication/Component.razor]
build_metadata.AdditionalFiles.TargetPath = Q29tcG9uZW50LnJhem9y
""");
}

[TestMethod]
Expand All @@ -83,16 +79,15 @@ public void GenerateEditorConfig_MultipleFiles_ValidEditorConfig()
"C:/Users/Johnny/source/repos/Parent.razor"
};
var editorConfig = new EditorConfigGenerator(rootPath).Generate(razorFiles);
editorConfig.Should().Be(
$"""
is_global = true
[C:/Users/Johnny/source/repos/WebApplication/Component.razor]
build_metadata.AdditionalFiles.TargetPath = Q29tcG9uZW50LnJhem9y
[C:/Users/Johnny/source/repos/WebApplication/Folder/Child.razor]
build_metadata.AdditionalFiles.TargetPath = Rm9sZGVyXENoaWxkLnJhem9y
[C:/Users/Johnny/source/repos/Parent.razor]
build_metadata.AdditionalFiles.TargetPath = Li5cUGFyZW50LnJhem9y
""".Replace("\n", Environment.NewLine));
editorConfig.Should().BeIgnoringLineEndings("""
is_global = true
[C:/Users/Johnny/source/repos/WebApplication/Component.razor]
build_metadata.AdditionalFiles.TargetPath = Q29tcG9uZW50LnJhem9y
[C:/Users/Johnny/source/repos/WebApplication/Folder/Child.razor]
build_metadata.AdditionalFiles.TargetPath = Rm9sZGVyXENoaWxkLnJhem9y
[C:/Users/Johnny/source/repos/Parent.razor]
build_metadata.AdditionalFiles.TargetPath = Li5cUGFyZW50LnJhem9y
""");
}

[TestMethod]
Expand All @@ -117,8 +112,7 @@ public void GenerateEditorConfig_EmptyFile_Throws(string element)
public void GenerateEditorConfig_MixedFiles_Throws()
{
var rootPath = "C:/Users/Johnny/source/repos/WebApplication";
var generateMixedWithNullElement = () => _ = new EditorConfigGenerator(rootPath)
.Generate([null, "C:/Users/Johnny/source/repos/WebApplication/Component.razor", string.Empty]);
var generateMixedWithNullElement = () => _ = new EditorConfigGenerator(rootPath).Generate([null, "C:/Users/Johnny/source/repos/WebApplication/Component.razor", string.Empty]);
generateMixedWithNullElement.Should().Throw<NullReferenceException>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public string Generate(IEnumerable<string> razorFiles) =>
razorFiles.Select(x => x.Replace('\\', '/')).Select(ToConfigLine).Prepend("is_global = true").JoinStr(Environment.NewLine);

private string ToConfigLine(string file) =>
TestHelper.ReplaceLineEndings(
$"""
[{file}]
build_metadata.AdditionalFiles.TargetPath = {Convert.ToBase64String(Encoding.UTF8.GetBytes(TestHelper.GetRelativePath(rootPath, file)))}
""", Environment.NewLine);
$"""
[{file}]
build_metadata.AdditionalFiles.TargetPath = {Convert.ToBase64String(Encoding.UTF8.GetBytes(TestHelper.GetRelativePath(rootPath, file)))}
""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,4 @@ public static string GetRelativePath(string relativeTo, string path)
}
return relativePath;
}

public static string ReplaceLineEndings(string input, string replacement) =>
Regex.Replace(input, @"\r\n?|\n", replacement, RegexOptions.None, RegexConstants.DefaultTimeout);
}

0 comments on commit 00ecc1e

Please sign in to comment.