Skip to content

Commit

Permalink
Improve test failure message
Browse files Browse the repository at this point in the history
On os x if jsonschema is not installed the error message was cryptic.
  • Loading branch information
pgrange committed Oct 5, 2022
1 parent 3567594 commit 99f60da
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions hydra-node/test/Hydra/JSONSchema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import System.Exit (ExitCode (..))
import System.FilePath (normalise, takeBaseName, takeExtension, (<.>), (</>))
import System.Process (readProcessWithExitCode)
import Test.Hspec (pendingWith)
import Test.Hydra.Prelude (createSystemTempDirectory)
import Test.Hydra.Prelude (createSystemTempDirectory, failure)
import Test.QuickCheck (Property, conjoin, counterexample, forAllBlind, forAllShrink, vectorOf, withMaxSuccess)
import Test.QuickCheck.Monadic (assert, monadicIO, monitor, run)
import qualified Prelude
import System.IO.Error (IOError, ioeGetErrorType)
import GHC.IO.Exception (IOErrorType(OtherError))

-- | Generate arbitrary serializable (JSON) value, and check their validity
-- against a known JSON schema.
Expand Down Expand Up @@ -158,12 +160,17 @@ ensureSystemRequirements ::
IO ()
ensureSystemRequirements = do
getToolVersion >>= \case
Just "3.2.0" -> pure ()
_ -> pendingWith "This test requires the python library 'jsonschema==3.2.0' to be in scope."
Right "3.2.0" -> pure ()
Right _ -> failure "This test requires the python library 'jsonschema==3.2.0' to be in scope."
Left errorMsg -> failure errorMsg
where
-- Returns 'Nothing' when not available and 'Just <version number>' otherwise.
getToolVersion ::
IO (Maybe String)
IO (Either String String)
getToolVersion = do
(exitCode, out, _) <- readProcessWithExitCode "jsonschema" ["--version"] mempty
pure (dropWhileEnd isSpace out <$ guard (exitCode == ExitSuccess))
try (readProcessWithExitCode "jsonschema" ["--version"] mempty) >>= \case
Right (exitCode, out, _) -> pure (dropWhileEnd isSpace out <$ guard (exitCode == ExitSuccess))
Left (err :: IOError)
| ioeGetErrorType err == OtherError ->
pure (Left $ "Check jsonschema is installed and in $PATH")
Left err -> pure (Left $ show err)

0 comments on commit 99f60da

Please sign in to comment.