Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support additional files #229

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))"
}
]
}