Skip to content

Commit

Permalink
Support flags --silent --fail-on-focused
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Dec 1, 2023
1 parent ca77f1c commit 4d8b0cf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 30 deletions.
97 changes: 74 additions & 23 deletions src/Pyxpecto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,52 @@ module Expect =
let floatGreaterThanOrClose accuracy actual expected message =
if actual<expected then floatClose accuracy actual expected message

type Language =
| TypeScript
| Python
| NET
| JavaScript
static member get() =
#if FABLE_COMPILER_JAVASCRIPT
Language.JavaScript
#endif
#if FABLE_COMPILER_PYTHON
Language.Python
#endif
#if FABLE_COMPILER_TYPESCRIPT
Language.TypeScript
#endif
#if !FABLE_COMPILER
Language.NET
#endif
member this.AsLowerCaseString = (string this).ToLower()

[<RequireQualifiedAccess>]
module ConfigArgLiterals =

let [<Literal>] FailOnFocused = @"--fail-on-focused-tests"
let [<Literal>] Silent = @"--silent"

type ConfigArg =
| FailOnFocused
| Silent
with
static member fromString (s: string) =
match s with
| ConfigArgLiterals.FailOnFocused -> Some FailOnFocused
| ConfigArgLiterals.Silent -> Some Silent
| _ -> None

static member fromStrings (arr: string []) : ConfigArg [] =
arr |> Array.choose ConfigArg.fromString

static member defaultConfigs : ConfigArg [] = [||]

type Config(args: ConfigArg []) =
let argExists(arg: ConfigArg) = Array.contains arg args
member val FailOnFocused: bool = argExists ConfigArg.FailOnFocused with get, set
member val Silent: bool = argExists ConfigArg.Silent with get, set

module Pyxpecto =

/// Flattens a tree of tests
Expand Down Expand Up @@ -436,18 +482,19 @@ module Pyxpecto =
loop [] [] SequenceMethod.Parallel test

[<AttachMembersAttribute>]
type CustomTestRunner(test:TestCase) =
type CustomTestRunner(test:TestCase, ?configArgs: ConfigArg []) =

let flatTests = flattenTests test
let hasFocused = flatTests |> List.exists (fun ft -> ft.focusState = FocusState.Focused)
let args = CommandLine.getArguments()
let testPrint = printfn "%A" args
///// If the flag '--fail-on-focused-tests' is given to py command AND focused tests exist it will fail.
//let verifyFocusedAllowed =
// let args = PyBindings.cmd_args
// let failOnFocusedTestsArg = @"--fail-on-focused-tests"
// if Array.contains failOnFocusedTestsArg args && hasFocused then failwith $"{BColors.FAIL}Cannot run focused tests with '{failOnFocusedTestsArg}' commandline arg.{BColors.ENDC}"

let codeArgs = defaultArg configArgs [||]
let _config = Array.append codeArgs (ConfigArg.fromStrings args) |> Config
/// If the flag '--fail-on-focused-tests' is given to py command AND focused tests exist it will fail.
let verifyFocusedAllowed =
if _config.FailOnFocused && hasFocused then failwith $"{BColors.FAIL}Cannot run focused tests with '{ConfigArgLiterals.FailOnFocused}' commandline arg.{BColors.ENDC}"

member val Language = Language.get() with get
member val Config = _config with get
member val SuccessfulTests = ref 0 with get, set
member val FailedTests = ref 0 with get, set
member val IgnoredTests = ref 0 with get, set
Expand All @@ -460,24 +507,28 @@ module Pyxpecto =
with get() = this.SuccessfulTests.Value + this.FailedTests.Value + this.ErrorTests.Value

member private this.printSuccessMsg (name: string) (runtime: TimeSpan option) =
let focused = if this.HasFocused then "💎 | " else ""
this.SuccessfulTests.Value <- this.SuccessfulTests.Value + 1
let timespan = if runtime.IsSome then $" ({runtime.Value.ToString()})" else ""
printfn $"{focused}✔️ {name}{timespan}"
if not this.Config.Silent then
let focused = if this.HasFocused then "💎 | " else ""
let timespan = if runtime.IsSome then $" ({runtime.Value.ToString()})" else ""
System.Console.WriteLine $"{focused}✔️ {name}{timespan}"

member private this.printErrorMsg (name: string) (msg: string) (isTrueError: bool)=
this.ErrorMessages.Add(name, msg)
let focused = if this.HasFocused then "💎 | " else ""
let errorAgainstFailHandling =
if isTrueError then
this.ErrorTests.Value <- this.ErrorTests.Value + 1
""
else
this.FailedTests.Value <- this.FailedTests.Value + 1
"🚫"
printfn $"{focused}{errorAgainstFailHandling} {name}\n\b{msg}"
if not this.Config.Silent then
let focused = if this.HasFocused then "💎 | " else ""
System.Console.WriteLine $"{focused}{errorAgainstFailHandling} {name}\n\b{msg}"

member private this.printSkipPendingMsg (name: string) = printfn "🚧 skipping '%s' ... pending" name
member private this.printSkipPendingMsg (name: string) =
if not this.Config.Silent then
System.Console.WriteLine (sprintf "🚧 skipping '%s' ... pending" name)

member this.RunTest(test : FlatTest) =
let name = test.fullname
Expand Down Expand Up @@ -539,8 +590,8 @@ module Pyxpecto =
) ([],[],[])
|> fun (b,c,d) -> List.rev b, List.rev c, List.rev d

let rec private runViaPy (tests: TestCase) =
let runner = CustomTestRunner(tests)
let rec private universalRun (configArgs: ConfigArg []) (tests: TestCase) =
let runner = CustomTestRunner(tests, configArgs)
let run (runner: CustomTestRunner) =
let runTests, pendingTests, unfocusedTests = sortTests runner
async {
Expand All @@ -555,7 +606,7 @@ module Pyxpecto =
#if !FABLE_COMPILER
System.Console.OutputEncoding <- System.Text.Encoding.UTF8
#endif
printfn "🚀 start running tests ..."
System.Console.WriteLine $"🚀 start running {runner.Language.AsLowerCaseString} tests ..."
async {
do! run runner
let innerMsgString = $"""{BColors.INFOBLUE}{runner.SumTests}{BColors.ENDC} tests run - {BColors.INFOBLUE}{runner.SuccessfulTests.Value}{BColors.ENDC} passed, {BColors.INFOBLUE}{runner.IgnoredTests.Value}{BColors.ENDC} ignored, {BColors.INFOBLUE}{runner.FailedTests.Value}{BColors.ENDC} failed, {BColors.INFOBLUE}{runner.ErrorTests.Value}{BColors.ENDC} errored"""
Expand All @@ -567,24 +618,24 @@ module Pyxpecto =
sb.AppendLine $"{BColors.FAIL}{i}) {name}{BColors.ENDC}\n\b{msg}" |> ignore
sb.AppendLine() |> ignore
let msg = sb.AppendLine(sep).AppendLine(innerMsgString).AppendLine(sep).ToString()
printfn "%s" msg
System.Console.WriteLine(msg)
let exitCode : int =
match runner.ErrorTests.Value, runner.FailedTests.Value with
| errors,_ when errors > 0 ->
Exception($"{BColors.FAIL}❌ Exited with error code 2{BColors.ENDC}") |> printfn "%A"
Exception($"{BColors.FAIL}❌ Exited with error code 2{BColors.ENDC}") |> System.Console.WriteLine
CommandLine.exitWith(2)
2
| _,failed when failed > 0 ->
Exception($"{BColors.FAIL}❌ Exited with error code 1{BColors.ENDC}") |> printfn "%A"
Exception($"{BColors.FAIL}❌ Exited with error code 1{BColors.ENDC}") |> System.Console.WriteLine
CommandLine.exitWith(1)
1
| _ ->
printfn $"{BColors.OKGREEN}Success!{BColors.ENDC}"
System.Console.WriteLine $"{BColors.OKGREEN}Success!{BColors.ENDC}"
CommandLine.exitWith(0)
0
return exitCode
}

let rec runTests (test: TestCase) =
let r = runViaPy test |> start
let runTests (configArgs: ConfigArg []) (test: TestCase) =
let r = universalRun configArgs test |> start
!!r
2 changes: 1 addition & 1 deletion tests/Mocha.Tests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ let all =
[<EntryPoint>]
let main argv =
#if FABLE_COMPILER_PYTHON
Pyxpecto.runTests all
Pyxpecto.runTests (ConfigArg.fromStrings argv) all
#endif
#if FABLE_COMPILER_JAVASCRIPT
Mocha.runTests all
Expand Down
9 changes: 3 additions & 6 deletions tests/Multitarget.Tests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@ let tests_sequential = testSequenced <| testList "Sequential" [

let tests_basic = testList "Basic" [
testCase "check fail" <| fun _ ->
#if !FABLE_COMPILER
Expect.equal (1 + 1) 3 "Should be equal"
#endif
Expect.equal (1 + 1) 2 "Should be equal"
//#if !FABLE_COMPILER
//Expect.equal (1 + 1) 3 "Should be equal"
//Expect.equal (1 + 1) 3 "should fail"
//#endif
Expect.equal (1 + 1) 2 "Should be equal"

testCase "testCase works with numbers" <| fun () ->
Expect.equal (1 + 1) 2 "Should be equal"
Expand Down Expand Up @@ -375,4 +372,4 @@ open Fable.Core.JsInterop
#endif

[<EntryPoint>]
let main argv = !!Pyxpecto.runTests all
let main argv = !!Pyxpecto.runTests (ConfigArg.fromStrings argv) all

0 comments on commit 4d8b0cf

Please sign in to comment.