From f79ebeab6f7b7eb18b045ae781ebb9ff480763de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=BF=20corey?= Date: Fri, 3 Nov 2023 11:09:02 -0700 Subject: [PATCH] Run tests with JUnit from env vars, if enabled (#129) * Run tests with JUnit available * Set source path prefix by default * Run with JUnit when enabled through env * Version bump * Minor bump --- CHANGELOG.md | 12 ++++++++++- freckle-app.cabal | 2 +- library/Freckle/App/Test/Hspec/Runner.hs | 26 +++++++++++++----------- package.yaml | 2 +- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc15d9b9..1ce609e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/freckle-app.cabal b/freckle-app.cabal index ab876164..0c7d3f89 100644 --- a/freckle-app.cabal +++ b/freckle-app.cabal @@ -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 diff --git a/library/Freckle/App/Test/Hspec/Runner.hs b/library/Freckle/App/Test/Hspec/Runner.hs index 573b03b3..2a37ae8f 100644 --- a/library/Freckle/App/Test/Hspec/Runner.hs +++ b/library/Freckle/App/Test/Hspec/Runner.hs @@ -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 @@ -25,8 +26,8 @@ import Test.Hspec.Runner , configSkipPredicate , defaultConfig , evaluateSummary + , hspecWithResult , readConfig - , runSpec ) import qualified Prelude as Unsafe (read) @@ -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 @@ -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 diff --git a/package.yaml b/package.yaml index 3f028725..6b171c39 100644 --- a/package.yaml +++ b/package.yaml @@ -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