Skip to content

Commit

Permalink
Support additional files (#229)
Browse files Browse the repository at this point in the history
* Support additional files

* Support additional files

* Another fix

* Fix paths

* Update src/Exercism.TestRunner.CSharp/TestCompilation.cs
  • Loading branch information
ErikSchierboom authored Apr 11, 2024
1 parent 6ab0fda commit 8c2e08b
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 2 deletions.
25 changes: 24 additions & 1 deletion src/Exercism.TestRunner.CSharp/FilesParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
Expand All @@ -11,9 +12,22 @@ internal static class FilesParser
public static Files Parse(Options options)
{
var configuration = JsonSerializer.Deserialize<Configuration>(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());

Expand All @@ -35,6 +49,15 @@ internal class Files

[JsonPropertyName("editor")]
public string[] Editor { get; set; } = Array.Empty<string>();

[JsonPropertyName("example")]
public string[] Example { get; set; } = Array.Empty<string>();

[JsonPropertyName("exemplar")]
public string[] Exemplar { get; set; } = Array.Empty<string>();

[JsonIgnore]
public string[] Additional { get; set; } = Array.Empty<string>();
}

internal class Configuration
Expand Down
3 changes: 2 additions & 1 deletion src/Exercism.TestRunner.CSharp/TestCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ private static IEnumerable<SyntaxTree> 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)
Expand Down
7 changes: 7 additions & 0 deletions tests/AdditionalFiles/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": {
"solution": ["Fake.cs"],
"test": ["FakeTests.cs"],
"example": [".meta/Example.cs"]
}
}
4 changes: 4 additions & 0 deletions tests/AdditionalFiles/Fake.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public static class Fake
{
public static int Add(int x, int y) => Helper.Add(x, y);
}
18 changes: 18 additions & 0 deletions tests/AdditionalFiles/Fake.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Example.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="Exercism.Tests" Version="0.1.0-alpha" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions tests/AdditionalFiles/FakeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Xunit;
public class FakeTests
{
[Fact]
public void Add_should_add_numbers() => Assert.Equal(2, Fake.Add(1, 1));
}
4 changes: 4 additions & 0 deletions tests/AdditionalFiles/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public static class Helper
{
public static int Add(int x, int y) => x + y;
}
11 changes: 11 additions & 0 deletions tests/AdditionalFiles/expected_results.json
Original file line number Diff line number Diff line change
@@ -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))"
}
]
}

0 comments on commit 8c2e08b

Please sign in to comment.