Skip to content
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
12 changes: 9 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ RUN dotnet new console -lang "F#" -o /tmp/fsharp-core && dotnet restore /tmp/fsh

WORKDIR /app

# Copy fsproj and restore as distinct layers
# Copy fsproj and restore as distinct layers.
COPY src/Exercism.TestRunner.FSharp/Exercism.TestRunner.FSharp.fsproj ./
RUN dotnet restore -a $TARGETARCH
RUN dotnet restore --arch $TARGETARCH

# Copy everything else and build
COPY src/Exercism.TestRunner.FSharp/ ./
RUN dotnet publish -a $TARGETARCH -c Release -o /opt/test-runner --no-restore
RUN dotnet publish --arch $TARGETARCH --output /opt/test-runner --no-restore

# ReadyToRun needs the runtime packs and the ReadyToRun compiler, which only the
# runner's own build uses. Removing them keeps them out of the runtime image.
RUN rm -rf /root/.nuget/packages/microsoft.netcore.app.crossgen2.* \
/root/.nuget/packages/microsoft.netcore.app.runtime.* \
/root/.nuget/packages/microsoft.aspnetcore.app.runtime.*

# Build runtime image
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:10.0.401-alpine3.23 AS runtime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<!-- Precompile the runner's code (mostly the F# parser in Fantomas.FCS) when it is published,
so that it doesn't go through the JIT on every run. -->
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

<ItemGroup>
Expand Down
50 changes: 45 additions & 5 deletions src/Exercism.TestRunner.FSharp/Testing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ module Process =
psi.RedirectStandardError <- true
psi.RedirectStandardOutput <- true
use p = Process.Start(psi)
// Read both streams to the end, so a child that writes a lot of output
// can't fill the pipe and block.
let stdout = p.StandardOutput.ReadToEndAsync()
let stderr = p.StandardError.ReadToEndAsync()
p.WaitForExit()
stdout.Wait()
stderr.Wait()

module TestResults =
[<AllowNullLiteral>]
Expand Down Expand Up @@ -87,6 +93,26 @@ module TestResults =
| "Failed" -> TestStatus.Fail
| _ -> TestStatus.Error

// The xUnit test executable escapes some characters in test names and failure
// messages with a backslash, such as \" for a quote and \x01 for other control
// characters. This turns them back into the original characters.
let private escapedCharacter = Regex(@"\\(x[01][0-9a-f]|.)", RegexOptions.Compiled)

let private unescape (message: string) =
escapedCharacter.Replace(message, fun m ->
match m.Groups.[1].Value with
| "n" -> "\n"
| "r" -> "\r"
| "t" -> "\t"
| "0" -> "\000"
| "a" -> "\a"
| "b" -> "\b"
| "f" -> "\f"
| "v" -> "\v"
| "\\" | "\"" as c -> c
| hex when hex.Length = 3 -> string (char (System.Convert.ToInt32(hex.[1..], 16)))
| _ -> m.Value)

let private toMessage (xmlUnitTestResult: XmlUnitTestResult) =
let removeFsUnitExceptionFromMessage (message: string) =
message.Replace(
Expand All @@ -98,6 +124,7 @@ module TestResults =
|> Option.ofObj
|> Option.bind (fun output -> output.ErrorInfo |> Option.ofObj)
|> Option.bind (fun errorInfo -> errorInfo.Message |> Option.ofObj)
|> Option.map unescape
|> Option.map removeFsUnitExceptionFromMessage
|> Option.map String.normalize

Expand Down Expand Up @@ -198,7 +225,10 @@ module TestResults =
TestCode = testMethodBinding |> (toTestCode originalTestCode)
Line = testMethodBinding |> toLine }

let private toTestResults originalTestCode originalTestTree xmlUnitTestResults =
let private toTestResults originalTestCode originalTestTree (xmlUnitTestResults: XmlUnitTestResult []) =
for xmlUnitTestResult in xmlUnitTestResults do
xmlUnitTestResult.TestName <- unescape xmlUnitTestResult.TestName

xmlUnitTestResults
|> Seq.map (toTestResult originalTestCode originalTestTree)
|> Seq.sortBy (fun testResult -> testResult.Line)
Expand Down Expand Up @@ -251,19 +281,29 @@ module DotnetCli =
|> Seq.filter (fun logLine -> logLine |> String.isNullOrWhiteSpace |> not)
|> Seq.toArray

let private parseTestResults originalTestCode originalTestTree context =
TestResults.parse originalTestCode originalTestTree context

let runTests originalTestCode originalTestTree context =
let solutionDir = Path.GetDirectoryName(context.TestsFile)

Process.exec "dotnet" "restore --source /root/.nuget/packages/" solutionDir
Process.exec "dotnet" $"test -c release --no-restore --verbosity=quiet --logger \"trx;LogFileName={Path.GetFileName(context.TestResultsFile)}\" /flp:verbosity=quiet;errorsOnly=true" solutionDir
Process.exec "dotnet" "build --configuration release --no-restore --verbosity quiet /flp:verbosity=quiet;errorsOnly=true" solutionDir

let buildErrors = parseBuildErrors context

if Array.isEmpty buildErrors then
TestRunSuccess(parseTestResults originalTestCode originalTestTree context)
// Every exercise uses xUnit v3, so the project builds to an executable
// that runs the tests itself. Running it directly skips `dotnet test`,
// which spends about 3 s starting MSBuild and the VSTest host for every run.
let assembly =
Path.Combine(solutionDir, "bin", "release", "net10.0", Path.GetFileNameWithoutExtension(context.ProjectFile) + ".dll")

Process.exec "dotnet" $"\"{assembly}\" -noLogo -noColor -trx \"{context.TestResultsFile}\"" solutionDir

try
TestRunSuccess(TestResults.parse originalTestCode originalTestTree context)
with error ->
// For example, the test executable crashed before writing its results.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestRunError [| error.Message |]
else
TestRunError buildErrors

Expand Down
Loading