Skip to content

Commit

Permalink
Read files from .meta/config.json (#45)
Browse files Browse the repository at this point in the history
* Remove results file path from Options class

* Read solution files from .meta/config.json
  • Loading branch information
ErikSchierboom authored May 12, 2021
1 parent c3ca3f5 commit a57de03
Show file tree
Hide file tree
Showing 32 changed files with 244 additions and 45 deletions.
36 changes: 36 additions & 0 deletions src/Exercism.TestRunner.CSharp/FilesParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Exercism.TestRunner.CSharp
{
internal static class FilesParser
{
public static Files Parse(Options options)
{
var configuration = JsonSerializer.Deserialize<Configuration>(ConfigJson(options));
return configuration!.Files;
}

private static string ConfigJson(Options options) =>
File.ReadAllText(options.ConfigJsonPath());

private static string ConfigJsonPath(this Options options) =>
Path.Combine(options.InputDirectory, ".meta", "config.json");
}

internal class Files
{
[JsonPropertyName("solution")]
public string[] Solution { get; set; }

[JsonPropertyName("test")]
public string[] Test { get; set; }
}

internal class Configuration
{
[JsonPropertyName("files")]
public Files Files { get; set; }
}
}
10 changes: 0 additions & 10 deletions src/Exercism.TestRunner.CSharp/Options.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System.IO;

using CommandLine;

using Humanizer;

namespace Exercism.TestRunner.CSharp
{
internal class Options
Expand All @@ -19,11 +15,5 @@ internal class Options

public Options(string slug, string inputDirectory, string outputDirectory) =>
(Slug, InputDirectory, OutputDirectory) = (slug, inputDirectory, outputDirectory);

public string TestsFilePath => Path.Combine(InputDirectory, $"{Exercise}Tests.cs");

public string ResultsJsonFilePath => Path.GetFullPath(Path.Combine(OutputDirectory, "results.json"));

private string Exercise => Slug.Dehumanize().Pascalize();
}
}
2 changes: 1 addition & 1 deletion src/Exercism.TestRunner.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private static void CreateTestResults(Options options)
Console.WriteLine($"[{DateTimeOffset.UtcNow:u}] Running test runner for '{options.Slug}' solution...");

var testRun = TestSuite.RunTests(options);
testRun.WriteToFile(options.ResultsJsonFilePath);
testRun.WriteToFile(options);

Console.WriteLine($"[{DateTimeOffset.UtcNow:u}] Ran test runner for '{options.Slug}' solution");
}
Expand Down
35 changes: 16 additions & 19 deletions src/Exercism.TestRunner.CSharp/TestCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,24 @@ namespace Exercism.TestRunner.CSharp
{
internal static class TestCompilation
{
public static Compilation Compile(Options options) =>
CSharpCompilation.Create(Guid.NewGuid().ToString("N"), SyntaxTrees(options), References(), CompilationOptions());
public static Compilation Compile(Options options, Files files) =>
CSharpCompilation.Create(AssemblyName(), SyntaxTrees(options, files), References(), CompilationOptions());

private static IEnumerable<SyntaxTree> SyntaxTrees(Options options)
private static string AssemblyName() => Guid.NewGuid().ToString("N");

private static IEnumerable<SyntaxTree> SyntaxTrees(Options options, Files files)
{
var solutionFiles = files.Solution.Select(file => ParseSyntaxTree(file, options));
var testFiles = files.Test.Select(file => ParseSyntaxTree(file, options).Rewrite());

return solutionFiles.Concat(testFiles);
}

private static SyntaxTree ParseSyntaxTree(string file, Options options)
{
SyntaxTree ParseSyntaxTree(string file)
{
var source = SourceText.From(File.OpenRead(file));
var syntaxTree = CSharpSyntaxTree.ParseText(source, path: file);

// We need to rewrite the test suite to un-skip all tests and capture any console output
if (file == options.TestsFilePath)
{
return syntaxTree.Rewrite();
}

return syntaxTree;
}

return Directory.EnumerateFiles(options.InputDirectory, "*.cs", SearchOption.AllDirectories)
.Select(ParseSyntaxTree);
var filePath = Path.Combine(options.InputDirectory, file);
var source = SourceText.From(File.OpenRead(filePath));
return CSharpSyntaxTree.ParseText(source, path: file);
}

private static CSharpCompilationOptions CompilationOptions() =>
Expand Down
8 changes: 4 additions & 4 deletions src/Exercism.TestRunner.CSharp/TestResultParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace Exercism.TestRunner.CSharp
{
internal static class TestResultParser
{
public static TestResult[] FromTests(IEnumerable<TestInfo> tests, SyntaxTree testsSyntaxTree)
public static TestResult[] FromTests(IEnumerable<TestInfo> tests, Compilation compilation, Files files)
{
var testMethods =
testsSyntaxTree
.GetRoot()
.DescendantNodes()
compilation.SyntaxTrees
.Where(tree => files.Test.Contains(tree.FilePath))
.SelectMany(tree => tree.GetRoot().DescendantNodes())
.OfType<MethodDeclarationSyntax>()
.ToArray();

Expand Down
7 changes: 5 additions & 2 deletions src/Exercism.TestRunner.CSharp/TestRunWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ namespace Exercism.TestRunner.CSharp
{
internal static class TestRunWriter
{
public static void WriteToFile(this TestRun testRun, string resultsJsonFilePath) =>
File.WriteAllText(resultsJsonFilePath, testRun.ToJson());
public static void WriteToFile(this TestRun testRun, Options options) =>
File.WriteAllText(options.ResultsJsonPath(), testRun.ToJson());

private static string ResultsJsonPath(this Options options) =>
Path.GetFullPath(Path.Combine(options.OutputDirectory, "results.json"));

private static string ToJson(this TestRun testRun) =>
JsonSerializer.Serialize(testRun, CreateJsonSerializerOptions());
Expand Down
10 changes: 3 additions & 7 deletions src/Exercism.TestRunner.CSharp/TestRunner.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;

Expand All @@ -14,7 +11,7 @@ namespace Exercism.TestRunner.CSharp
{
internal static class TestRunner
{
public static TestResult[] RunTests(Compilation compilation, Options options)
public static TestResult[] RunTests(Compilation compilation, Files files)
{
var outputPath = Path.Combine(Path.GetTempPath(), compilation.SourceModule.Name);
compilation.Emit(outputPath);
Expand All @@ -30,8 +27,7 @@ public static TestResult[] RunTests(Compilation compilation, Options options)
runner.Start();
finished.Wait();

var testsSyntaxTree = compilation.SyntaxTrees.First(tree => tree.FilePath == options.TestsFilePath);
return TestResultParser.FromTests(tests, testsSyntaxTree);
return TestResultParser.FromTests(tests, compilation, files);
}
}
}
5 changes: 3 additions & 2 deletions src/Exercism.TestRunner.CSharp/TestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ internal static class TestSuite
{
public static TestRun RunTests(Options options)
{
var compilation = TestCompilation.Compile(options);
var files = FilesParser.Parse(options);
var compilation = TestCompilation.Compile(options, files);

var errors = compilation.GetDiagnostics()
.Where(diag => diag.Severity == DiagnosticSeverity.Error)
Expand All @@ -17,7 +18,7 @@ public static TestRun RunTests(Options options)
if (errors.Any())
return TestRunParser.TestRunWithError(errors);

var testResults = TestRunner.RunTests(compilation, options);
var testResults = TestRunner.RunTests(compilation, files);
return TestRunParser.TestRunWithoutError(testResults);
}
}
Expand Down
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
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"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": {
"solution": ["Foo.cs"],
"test": ["FooTests.cs"],
"example": [".meta/Example.cs"]
}
}
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"]
}
}
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"]
}
}
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) => x + y;
}
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"]
}
}
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) => x + y;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.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>
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));
}
Loading

0 comments on commit a57de03

Please sign in to comment.