Add Spectre progress (#31)

This commit is contained in:
Patrick Stevens
2024-06-06 00:32:19 +01:00
committed by GitHub
parent 9a6eb4dc80
commit edfa216809
8 changed files with 61 additions and 6 deletions

View File

@@ -102,9 +102,9 @@ type UserMethodFailure =
override this.ToString () =
match this with
| UserMethodFailure.ReturnedNonUnit (method, ret) ->
$"User-defined method %s{method} returned a non-unit: %O{ret}"
$"User-defined method '%s{method}' returned a non-unit: %O{ret}"
| UserMethodFailure.Threw (method, exc) ->
$"User-defined method %s{method} threw: %s{exc.Message}\n %s{exc.StackTrace}"
$"User-defined method '%s{method}' threw: %s{exc.Message}\n %s{exc.StackTrace}"
/// Name (not fully-qualified) of the method which failed.
member this.Name =
@@ -132,6 +132,13 @@ type TestFailure =
| TestFailure.SetUpFailed f
| TestFailure.TearDownFailed f -> f.Name
/// Human-readable string representation.
override this.ToString () =
match this with
| TestFailure.TestFailed f -> $"execution: %O{f}"
| TestFailure.SetUpFailed f -> $"Setup failed, and we did not attempt to run test: %O{f}"
| TestFailure.TearDownFailed f -> $"Tear-down failed: %O{f}"
/// Represents the result of a test that didn't fail.
[<RequireQualifiedAccess>]
type TestMemberSuccess =
@@ -150,3 +157,17 @@ type TestMemberFailure =
/// We tried to run the test, but it failed. (A single test can fail many times, e.g. if it failed and also
/// the tear-down logic failed afterwards.)
| Failed of TestFailure list
/// Human-readable string representation
override this.ToString () : string =
match this with
| TestMemberFailure.Malformed reasons ->
let reasons = reasons |> String.concat "; "
$"Could not run test because it was malformed: %s{reasons}"
| TestMemberFailure.Failed errors ->
let errors =
errors
|> Seq.map (fun failure -> (failure : TestFailure).ToString ())
|> String.concat "\n"
$"Test failed: %s{errors}"

View File

@@ -84,7 +84,7 @@ module SingleTestMethod =
match isTest, sources, hasData, modifiers, categories, repeat, comb with
| _, _ :: _, Some _, _, _, _, _ ->
failwith
$"Test %s{method.Name} unexpectedly has both TestData and TestCaseSource; not currently supported"
$"Test '%s{method.Name}' unexpectedly has both TestData and TestCaseSource; not currently supported"
| false, [], None, [], _, _, _ -> None
| _, _ :: _, None, mods, categories, repeat, comb ->
{

View File

@@ -97,7 +97,7 @@ module TestFixture =
if valuesAttrs |> Array.exists (fun l -> l.IsSome) then
if valuesAttrs |> Array.exists (fun l -> l.IsNone) then
failwith
$"Test %s{test.Name} has a parameter with the Values attribute and a parameter without. All parameters must have Values if any one does."
$"Test '%s{test.Name}' has a parameter with the Values attribute and a parameter without. All parameters must have Values if any one does."
Some (valuesAttrs |> Array.map Option.get) |> Ok
else

View File

@@ -28,7 +28,7 @@ module Program =
| Some filter -> Filter.shouldRun filter
| None -> fun _ _ -> true
let progress = TestProgress.toStderr ()
let progress = Progress.spectre ()
use _ = new SetBaseDir (testDll)

24
TestRunner/Progress.fs Normal file
View File

@@ -0,0 +1,24 @@
namespace TestRunner
open Spectre.Console
[<RequireQualifiedAccess>]
module Progress =
let spectre () : ITestProgress =
{ new ITestProgress with
member _.OnTestFailed name failure =
AnsiConsole.Console.MarkupLine
$"[red]Test '%s{Markup.Escape name}' failed: %s{Markup.Escape (failure.ToString ())}[/]"
member _.OnTestFixtureStart name testCount =
AnsiConsole.Console.MarkupLine $"[white]Running tests: %s{Markup.Escape name}[/]"
member _.OnTestMemberFinished name =
AnsiConsole.Console.MarkupLine $"[gray]Finished test: %s{Markup.Escape name}[/]"
member _.OnTestMemberSkipped name =
AnsiConsole.Console.MarkupLine $"[yellow]Skipping test due to filter: %s{Markup.Escape name}[/]"
member _.OnTestMemberStart name =
AnsiConsole.Console.MarkupLine $"[white]Running test: %s{Markup.Escape name}[/]"
}

View File

@@ -18,6 +18,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Progress.fs" />
<Compile Include="Program.fs" />
<None Include="..\README.md">
<Pack>True</Pack>
@@ -29,4 +30,8 @@
<ProjectReference Include="..\TestRunner.Lib\TestRunner.Lib.fsproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Spectre.Console" Version="0.49.1" />
</ItemGroup>
</Project>

View File

@@ -121,6 +121,11 @@
version = "4.5.0";
sha256 = "1srx1629s0k1kmf02nmz251q07vj6pv58mdafcr5dr0bbn1fh78i";
})
(fetchNuGet {
pname = "Spectre.Console";
version = "0.49.1";
sha256 = "0fhl96p3xjd5k1wwvhs80cp35rrlgnza6mw9vy0knhmf7ji9b95n";
})
(fetchNuGet {
pname = "System.Formats.Asn1";
version = "6.0.0";