Skip to content

Commit

Permalink
feat(purescript-spec#2): allow to use beforeAll
Browse files Browse the repository at this point in the history
  • Loading branch information
srghma committed Oct 10, 2024
1 parent 76e1bc6 commit 8e1a402
Show file tree
Hide file tree
Showing 5 changed files with 1,659 additions and 1,477 deletions.
12 changes: 1 addition & 11 deletions spago.lock
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
},
"test": {
"dependencies": [
"debug",
"console",
"node-buffer",
"node-child-process",
"node-execa"
Expand All @@ -172,7 +172,6 @@
"contravariant",
"control",
"datetime",
"debug",
"distributive",
"effect",
"either",
Expand Down Expand Up @@ -947,15 +946,6 @@
"tuples"
]
},
"debug": {
"type": "registry",
"version": "6.0.2",
"integrity": "sha256-vmkYFuXYuELBzeauvgHG6E6Kf/Hp1dAnxwE9ByHfwSg=",
"dependencies": [
"functions",
"prelude"
]
},
"distributive": {
"type": "registry",
"version": "6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion spago.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ package:
test:
main: Test.Main
dependencies:
- debug
- console
- node-buffer
- node-child-process
- node-execa
63 changes: 46 additions & 17 deletions src/Node.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import Data.Identity (Identity(..))
import Data.Newtype (un)
import Effect (Effect)
import Effect.Aff (Aff, launchAff_)
import Effect.Class (liftEffect)
import Effect.Class (class MonadEffect, liftEffect)
import Node.Process (exit')
import Test.Spec (Spec)
import Test.Spec (Spec, SpecT)
import Test.Spec.Result (Result)
import Test.Spec.Runner (Reporter)
import Test.Spec.Runner as Spec
Expand All @@ -20,8 +20,7 @@ import Test.Spec.Tree (Tree)
-- | Runs the given spec, using configuration derived from CLI options (if any),
-- | and exits the process with an exit indicating success or failure.
runSpecAndExitProcess :: Array Reporter -> Spec Unit -> Effect Unit
runSpecAndExitProcess =
runSpecAndExitProcess' { defaultConfig: Cfg.defaultConfig, parseCLIOptions: true }
runSpecAndExitProcess = runSpecAndExitProcess' defaultArgs

-- | Runs the given spec and exits the process with an exit code indicating
-- | success or failure.
Expand All @@ -30,24 +29,54 @@ runSpecAndExitProcess =
-- | should be used as is or CLI options (if any provided) should be applied on
-- | top of it.
runSpecAndExitProcess' :: c.
{ defaultConfig :: Cfg.TestRunConfig' c
, parseCLIOptions :: Boolean
}
Args c
-> Array Reporter
-> Spec Unit
-> Effect Unit
runSpecAndExitProcess' args reporters spec = launchAff_ do
config <-
if args.parseCLIOptions then
Cfg.fromCommandLine' args.defaultConfig Cfg.commandLineOptionParsers
else
pure args.defaultConfig
res <- runSpecAndGetResults config reporters spec
runSpecAndExitProcess' = runSpecAndExitProcessM' (un Identity)

runSpecAndExitProcessM' ::
c m
. Functor m
=> (forall x . m (Aff x) -> Aff x)
-> Args c
-> Array Reporter
-> SpecT Aff Unit m Unit
-> Effect Unit
runSpecAndExitProcessM' evalM args reporters spec = launchAff_ do
config <- argsToConfig args
res <- runSpecAndGetResultsM evalM config reporters spec
liftEffect $ exit' $ if successful res then 0 else 1

runSpecAndGetResults :: c. Cfg.TestRunConfig' c -> Array Reporter -> Spec Unit -> Aff (Array (Tree String Void Result))
runSpecAndGetResults config reporters spec = do
type Args c =
{ defaultConfig :: Cfg.TestRunConfig' c
, parseCLIOptions :: Boolean
}

defaultArgs :: Args ()
defaultArgs = { defaultConfig: Cfg.defaultConfig, parseCLIOptions: true }

argsToConfig ::
forall c m
. MonadEffect m
=> Args c
-> m (Cfg.TestRunConfig' c)
argsToConfig args =
if args.parseCLIOptions then
Cfg.fromCommandLine' args.defaultConfig Cfg.commandLineOptionParsers
else
pure args.defaultConfig

runSpecAndGetResultsM ::
c m
. Functor m
=> (forall x . m (Aff x) -> Aff x)
-> Cfg.TestRunConfig' c
-> Array Reporter
-> SpecT Aff Unit m Unit
-> Aff (Array (Tree String Void Result))
runSpecAndGetResultsM evalM config reporters spec = do
specCfg <- Cfg.toSpecConfig config <#> _ { exit = false }
results <- un Identity $ Spec.evalSpecT specCfg reporters spec
results <- evalM $ Spec.evalSpecT specCfg reporters spec
Persist.persistResults results
pure results
Loading

0 comments on commit 8e1a402

Please sign in to comment.