Skip to content

Commit

Permalink
Run tests with JUnit from env vars, if enabled (#129)
Browse files Browse the repository at this point in the history
* Run tests with JUnit available

* Set source path prefix by default

* Run with JUnit when enabled through env

* Version bump

* Minor bump
  • Loading branch information
stackptr authored Nov 3, 2023
1 parent c5a440a commit f79ebea
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
## [_Unreleased_](https://github.com/freckle/freckle-app/compare/v1.10.3.0...main)
## [_Unreleased_](https://github.com/freckle/freckle-app/compare/v1.10.4.0...main)

## [v1.10.4.0](https://github.com/freckle/freckle-app/compare/v1.10.3.0...v1.10.4.0)

- Use JUnit formatting for test output when `JUNIT_ENABLED` environment
variable is set.
- Use environment variables to [configure JUnit][junit-env]; always prefix
source paths with test suite `name` and output tests to `/tmp/junit`.
- Remove using `CIRCLECI` environment variable.

[junit-env]: https://github.com/freckle/hspec-junit-formatter/blob/main/library/Test/Hspec/JUnit/Config/Env.hs

## [v1.10.3.0](https://github.com/freckle/freckle-app/compare/v1.10.2.0...v1.10.3.0)

Expand Down
2 changes: 1 addition & 1 deletion freckle-app.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.18
-- see: https://github.com/sol/hpack

name: freckle-app
version: 1.10.3.0
version: 1.10.4.0
synopsis: Haskell application toolkit used at Freckle
description: Please see README.md
category: Utils
Expand Down
26 changes: 14 additions & 12 deletions library/Freckle/App/Test/Hspec/Runner.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import System.Environment (getArgs, lookupEnv)
import Test.Hspec (Spec)
import Test.Hspec.JUnit
( configWithJUnit
, defaultJUnitConfig
, setJUnitConfigOutputFile
, setJUnitConfigSourcePathPrefix
)
import Test.Hspec.JUnit.Config.Env (envJUnitConfig, envJUnitEnabled)
import Test.Hspec.Runner
( Config
, Path
Expand All @@ -25,8 +26,8 @@ import Test.Hspec.Runner
, configSkipPredicate
, defaultConfig
, evaluateSummary
, hspecWithResult
, readConfig
, runSpec
)
import qualified Prelude as Unsafe (read)

Expand All @@ -41,8 +42,6 @@ runParConfig name spec = do
runWith :: Config -> String -> Spec -> IO ()
runWith config name spec = do
args <- getArgs
isCircle <- isJust <$> lookupEnv "CIRCLECI"
let runner = if isCircle then junit else hspec

-- Run unreliable tests first, so local dev errors are reported for reliable
-- specs at the end
Expand All @@ -66,18 +65,21 @@ runWith config name spec = do
evaluateSummary $ reliableSummary <> isolatedSummary
where
load = flip readConfig
junit filename changeConfig =
(spec `runJUnitSpec` ("/tmp/junit", filename)) . changeConfig
hspec _ changeConfig = runSpec spec . changeConfig
runner filename changeConfig =
(spec `runWithJUnit` ("/tmp/junit", filename)) . changeConfig
noConcurrency x = x {configConcurrentJobs = Just 1}

runJUnitSpec :: Spec -> (FilePath, String) -> Config -> IO Summary
runJUnitSpec spec (path, name) config =
spec `runSpec` configWithJUnit junitConfig config
runWithJUnit :: Spec -> (FilePath, String) -> Config -> IO Summary
runWithJUnit spec (path, name) config = do
junitEnabled <- envJUnitEnabled
baseJUnitConfig <- envJUnitConfig
let modify = if junitEnabled then configWithJUnit $ withOverride baseJUnitConfig else id
spec `runSpec` modify config
where
runSpec = flip hspecWithResult
filePath = path <> "/" <> name <> "/test_results.xml"
junitConfig =
setJUnitConfigOutputFile filePath $ defaultJUnitConfig $ pack name
withOverride =
setJUnitConfigSourcePathPrefix name . setJUnitConfigOutputFile filePath

makeParallelConfig :: Config -> IO Config
makeParallelConfig config = do
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: freckle-app
version: 1.10.3.0
version: 1.10.4.0
maintainer: Freckle Education
category: Utils
github: freckle/freckle-app
Expand Down

0 comments on commit f79ebea

Please sign in to comment.