From 38de2b9d1777e57d622552ddf7c63425727ee9d9 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 11 Apr 2024 14:39:33 +0200 Subject: [PATCH 1/5] Support additional files --- src/Exercism.TestRunner.CSharp/FilesParser.cs | 3 +++ .../TestCompilation.cs | 5 +++-- tests/AdditionalFiles/.meta/config.json | 7 +++++++ tests/AdditionalFiles/Fake.cs | 4 ++++ tests/AdditionalFiles/Fake.csproj | 18 ++++++++++++++++++ tests/AdditionalFiles/FakeTests.cs | 6 ++++++ tests/AdditionalFiles/Helper.cs | 4 ++++ tests/AdditionalFiles/expected_results.json | 11 +++++++++++ 8 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 tests/AdditionalFiles/.meta/config.json create mode 100644 tests/AdditionalFiles/Fake.cs create mode 100644 tests/AdditionalFiles/Fake.csproj create mode 100644 tests/AdditionalFiles/FakeTests.cs create mode 100644 tests/AdditionalFiles/Helper.cs create mode 100644 tests/AdditionalFiles/expected_results.json diff --git a/src/Exercism.TestRunner.CSharp/FilesParser.cs b/src/Exercism.TestRunner.CSharp/FilesParser.cs index c290dd9..5fd08ae 100644 --- a/src/Exercism.TestRunner.CSharp/FilesParser.cs +++ b/src/Exercism.TestRunner.CSharp/FilesParser.cs @@ -35,6 +35,9 @@ internal class Files [JsonPropertyName("editor")] public string[] Editor { get; set; } = Array.Empty(); + + [JsonIgnore] + public string[] Additional { get; set; } = Array.Empty(); } internal class Configuration diff --git a/src/Exercism.TestRunner.CSharp/TestCompilation.cs b/src/Exercism.TestRunner.CSharp/TestCompilation.cs index 77a2ae2..cad0dba 100644 --- a/src/Exercism.TestRunner.CSharp/TestCompilation.cs +++ b/src/Exercism.TestRunner.CSharp/TestCompilation.cs @@ -21,10 +21,11 @@ public static Compilation Compile(Options options, Files files) => private static IEnumerable SyntaxTrees(Options options, Files files) { var solutionFiles = files.Solution.Select(file => ParseSyntaxTree(file, options)); - var editorFiles = files.Editor.Select(file => ParseSyntaxTree(file, options)); + var editorFiles = files.Editor.Select(file => ParseSyntaxTree(file, options)); var testFiles = files.Test.Select(file => ParseSyntaxTree(file, options).Rewrite()); + var additionalFiles = files.Solution.Select(file => ParseSyntaxTree(file, options)); - return solutionFiles.Concat(editorFiles).Concat(testFiles); + return solutionFiles.Concat(editorFiles).Concat(testFiles).Concat(additionalFiles); } private static SyntaxTree ParseSyntaxTree(string file, Options options) diff --git a/tests/AdditionalFiles/.meta/config.json b/tests/AdditionalFiles/.meta/config.json new file mode 100644 index 0000000..b6395cc --- /dev/null +++ b/tests/AdditionalFiles/.meta/config.json @@ -0,0 +1,7 @@ +{ + "files": { + "solution": ["Fake.cs"], + "test": ["FakeTests.cs"], + "example": [".meta/Example.cs"] + } +} \ No newline at end of file diff --git a/tests/AdditionalFiles/Fake.cs b/tests/AdditionalFiles/Fake.cs new file mode 100644 index 0000000..ad3dcd5 --- /dev/null +++ b/tests/AdditionalFiles/Fake.cs @@ -0,0 +1,4 @@ +public static class Fake +{ + public static int Add(int x, int y) => Helper.Add(x, y); +} \ No newline at end of file diff --git a/tests/AdditionalFiles/Fake.csproj b/tests/AdditionalFiles/Fake.csproj new file mode 100644 index 0000000..a55016c --- /dev/null +++ b/tests/AdditionalFiles/Fake.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/AdditionalFiles/FakeTests.cs b/tests/AdditionalFiles/FakeTests.cs new file mode 100644 index 0000000..fecec13 --- /dev/null +++ b/tests/AdditionalFiles/FakeTests.cs @@ -0,0 +1,6 @@ +using Xunit; +public class FakeTests +{ + [Fact] + public void Add_should_add_numbers() => Assert.Equal(2, Fake.Add(1, 1)); +} \ No newline at end of file diff --git a/tests/AdditionalFiles/Helper.cs b/tests/AdditionalFiles/Helper.cs new file mode 100644 index 0000000..d9750c1 --- /dev/null +++ b/tests/AdditionalFiles/Helper.cs @@ -0,0 +1,4 @@ +public static class Helper +{ + public static int Add(int x, int y) => x + y; +} \ No newline at end of file diff --git a/tests/AdditionalFiles/expected_results.json b/tests/AdditionalFiles/expected_results.json new file mode 100644 index 0000000..24c081e --- /dev/null +++ b/tests/AdditionalFiles/expected_results.json @@ -0,0 +1,11 @@ +{ + "version": 3, + "status": "pass", + "tests": [ + { + "name": "Add should add numbers", + "status": "pass", + "test_code": "Assert.Equal(2, Fake.Add(1, 1))" + } + ] +} From 7e86565feb8aca180c62010a33e30d7376382335 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 11 Apr 2024 15:01:41 +0200 Subject: [PATCH 2/5] Support additional files --- src/Exercism.TestRunner.CSharp/FilesParser.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Exercism.TestRunner.CSharp/FilesParser.cs b/src/Exercism.TestRunner.CSharp/FilesParser.cs index 5fd08ae..e62d7a2 100644 --- a/src/Exercism.TestRunner.CSharp/FilesParser.cs +++ b/src/Exercism.TestRunner.CSharp/FilesParser.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.Json; @@ -11,9 +12,18 @@ internal static class FilesParser public static Files Parse(Options options) { var configuration = JsonSerializer.Deserialize(ConfigJson(options)); - return configuration!.Files; + configuration.Files.Additional = GetAdditionalFiles(options, configuration); + return configuration.Files; } + private static string[] GetAdditionalFiles(Options options, Configuration configuration) => + Directory.EnumerateFiles(options.InputDirectory, "*.cs", SearchOption.AllDirectories) + .Select(path => Path.GetFullPath(path)[(options.InputDirectory.Length + 1)..]) + .Except(configuration.Files.Solution) + .Except(configuration.Files.Editor) + .Except(configuration.Files.Test) + .ToArray(); + private static string ConfigJson(Options options) => File.ReadAllText(options.ConfigJsonPath()); From 93057e0fad57f722f39d949135381eaa10c06619 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 11 Apr 2024 15:08:08 +0200 Subject: [PATCH 3/5] Another fix --- src/Exercism.TestRunner.CSharp/FilesParser.cs | 8 ++++++++ src/Exercism.TestRunner.CSharp/TestCompilation.cs | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Exercism.TestRunner.CSharp/FilesParser.cs b/src/Exercism.TestRunner.CSharp/FilesParser.cs index e62d7a2..39ef508 100644 --- a/src/Exercism.TestRunner.CSharp/FilesParser.cs +++ b/src/Exercism.TestRunner.CSharp/FilesParser.cs @@ -22,6 +22,8 @@ private static string[] GetAdditionalFiles(Options options, Configuration config .Except(configuration.Files.Solution) .Except(configuration.Files.Editor) .Except(configuration.Files.Test) + .Except(configuration.Files.Example) + .Except(configuration.Files.Exemplar) .ToArray(); private static string ConfigJson(Options options) => @@ -46,6 +48,12 @@ internal class Files [JsonPropertyName("editor")] public string[] Editor { get; set; } = Array.Empty(); + [JsonPropertyName("example")] + public string[] Example { get; set; } = Array.Empty(); + + [JsonPropertyName("exemplar")] + public string[] Exemplar { get; set; } = Array.Empty(); + [JsonIgnore] public string[] Additional { get; set; } = Array.Empty(); } diff --git a/src/Exercism.TestRunner.CSharp/TestCompilation.cs b/src/Exercism.TestRunner.CSharp/TestCompilation.cs index cad0dba..955f605 100644 --- a/src/Exercism.TestRunner.CSharp/TestCompilation.cs +++ b/src/Exercism.TestRunner.CSharp/TestCompilation.cs @@ -23,7 +23,7 @@ private static IEnumerable SyntaxTrees(Options options, Files files) var solutionFiles = files.Solution.Select(file => ParseSyntaxTree(file, options)); var editorFiles = files.Editor.Select(file => ParseSyntaxTree(file, options)); var testFiles = files.Test.Select(file => ParseSyntaxTree(file, options).Rewrite()); - var additionalFiles = files.Solution.Select(file => ParseSyntaxTree(file, options)); + var additionalFiles = files.Additional.Select(file => ParseSyntaxTree(file, options)); return solutionFiles.Concat(editorFiles).Concat(testFiles).Concat(additionalFiles); } From 113f2c9f2854869c937c7b9b015ecd02acf9c883 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 11 Apr 2024 15:51:32 +0200 Subject: [PATCH 4/5] Fix paths --- src/Exercism.TestRunner.CSharp/FilesParser.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Exercism.TestRunner.CSharp/FilesParser.cs b/src/Exercism.TestRunner.CSharp/FilesParser.cs index 39ef508..898481a 100644 --- a/src/Exercism.TestRunner.CSharp/FilesParser.cs +++ b/src/Exercism.TestRunner.CSharp/FilesParser.cs @@ -18,12 +18,14 @@ public static Files Parse(Options options) private static string[] GetAdditionalFiles(Options options, Configuration configuration) => Directory.EnumerateFiles(options.InputDirectory, "*.cs", SearchOption.AllDirectories) - .Select(path => Path.GetFullPath(path)[(options.InputDirectory.Length + 1)..]) + .Select(Path.GetFullPath) + .Select(path => path[options.InputDirectory.Length..].TrimStart(Path.DirectorySeparatorChar)) .Except(configuration.Files.Solution) .Except(configuration.Files.Editor) .Except(configuration.Files.Test) .Except(configuration.Files.Example) .Except(configuration.Files.Exemplar) + .Where(path => !Path.GetDirectoryName(path)!.StartsWith('.')) .ToArray(); private static string ConfigJson(Options options) => From 6c33415926123e8995865980295419f7ed39f852 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 11 Apr 2024 15:53:35 +0200 Subject: [PATCH 5/5] Update src/Exercism.TestRunner.CSharp/TestCompilation.cs --- src/Exercism.TestRunner.CSharp/TestCompilation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exercism.TestRunner.CSharp/TestCompilation.cs b/src/Exercism.TestRunner.CSharp/TestCompilation.cs index 955f605..aebc7b0 100644 --- a/src/Exercism.TestRunner.CSharp/TestCompilation.cs +++ b/src/Exercism.TestRunner.CSharp/TestCompilation.cs @@ -21,7 +21,7 @@ public static Compilation Compile(Options options, Files files) => private static IEnumerable SyntaxTrees(Options options, Files files) { var solutionFiles = files.Solution.Select(file => ParseSyntaxTree(file, options)); - var editorFiles = files.Editor.Select(file => ParseSyntaxTree(file, options)); + var editorFiles = files.Editor.Select(file => ParseSyntaxTree(file, options)); var testFiles = files.Test.Select(file => ParseSyntaxTree(file, options).Rewrite()); var additionalFiles = files.Additional.Select(file => ParseSyntaxTree(file, options));