From 8c2e08b7f520b0ad04916caf2d93ad0335aca883 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 11 Apr 2024 15:56:16 +0200 Subject: [PATCH] Support additional files (#229) * Support additional files * Support additional files * Another fix * Fix paths * Update src/Exercism.TestRunner.CSharp/TestCompilation.cs --- src/Exercism.TestRunner.CSharp/FilesParser.cs | 25 ++++++++++++++++++- .../TestCompilation.cs | 3 ++- 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, 76 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..898481a 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,22 @@ 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.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) => File.ReadAllText(options.ConfigJsonPath()); @@ -35,6 +49,15 @@ 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(); } internal class Configuration diff --git a/src/Exercism.TestRunner.CSharp/TestCompilation.cs b/src/Exercism.TestRunner.CSharp/TestCompilation.cs index 77a2ae2..aebc7b0 100644 --- a/src/Exercism.TestRunner.CSharp/TestCompilation.cs +++ b/src/Exercism.TestRunner.CSharp/TestCompilation.cs @@ -23,8 +23,9 @@ 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.Additional.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))" + } + ] +}