-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebc4c16
commit c7c1e84
Showing
2 changed files
with
45 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,59 @@ | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Exercism.TestRunner.CSharp | ||
{ | ||
internal static class TestRunWriter | ||
{ | ||
public static void WriteToFile(this TestRun testRun, Options options) => | ||
File.WriteAllText(options.ResultsJsonPath(), testRun.ToJson()); | ||
private static readonly JsonWriterOptions JsonWriterOptions = new() {Indented = true}; | ||
|
||
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()); | ||
|
||
private static JsonSerializerOptions CreateJsonSerializerOptions() | ||
public static void WriteToFile(this TestRun testRun, Options options) | ||
{ | ||
using var fileStream = File.Create(options.ResultsJsonPath()); | ||
using var jsonWriter = new Utf8JsonWriter(fileStream, JsonWriterOptions); | ||
jsonWriter.WriteStartObject(); | ||
jsonWriter.WriteNumber("version", testRun.Version); | ||
jsonWriter.WriteString("status", testRun.Status.ToString().ToLower()); | ||
jsonWriter.WriteStringIfNotEmpty("message", testRun.Message); | ||
jsonWriter.WriteTestResults(testRun.Tests); | ||
jsonWriter.WriteEndObject(); | ||
jsonWriter.Flush(); | ||
fileStream.WriteByte((byte)'\n'); | ||
} | ||
|
||
private static void WriteTestResults(this Utf8JsonWriter jsonWriter, TestResult[] testResults) | ||
{ | ||
var options = new JsonSerializerOptions | ||
if (testResults.Length == 0) | ||
return; | ||
|
||
jsonWriter.WriteStartArray("tests"); | ||
|
||
foreach (var testResult in testResults) | ||
{ | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
WriteIndented = true, | ||
}; | ||
options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); | ||
jsonWriter.WriteStartObject(); | ||
jsonWriter.WriteString("name", testResult.Name); | ||
jsonWriter.WriteString("status", testResult.Status.ToString().ToLower()); | ||
if (testResult.TaskId.HasValue) | ||
jsonWriter.WriteNumber("task_id", testResult.TaskId.Value); | ||
jsonWriter.WriteStringIfNotEmpty("message", testResult.Message); | ||
jsonWriter.WriteStringIfNotEmpty("output", testResult.Output); | ||
jsonWriter.WriteStringIfNotEmpty("test_code", testResult.TestCode); | ||
jsonWriter.WriteEndObject(); | ||
} | ||
|
||
jsonWriter.WriteEndArray(); | ||
} | ||
|
||
return options; | ||
private static void WriteStringIfNotEmpty(this Utf8JsonWriter jsonWriter, string property, string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) | ||
return; | ||
|
||
jsonWriter.WriteString(property, value); | ||
} | ||
|
||
|
||
private static string ResultsJsonPath(this Options options) => | ||
Path.GetFullPath(Path.Combine(options.OutputDirectory, "results.json")); | ||
} | ||
} |