hedgehog-extras 0.5.0.0 → 0.5.1.0
raw patch · 2 files changed
+69/−34 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- hedgehog-extras.cabal +2/−2
- src/Hedgehog/Extras/Test/Golden.hs +67/−32
hedgehog-extras.cabal view
@@ -1,7 +1,7 @@ cabal-version: 2.4 name: hedgehog-extras-version: 0.5.0.0+version: 0.5.1.0 synopsis: Supplemental library for hedgehog description: Supplemental library for hedgehog. category: Test@@ -34,7 +34,7 @@ common process { build-depends: process } common resourcet { build-depends: resourcet } common stm { build-depends: stm }-common tar { build-depends: tar }+common tar { build-depends: tar < 0.6 } common temporary { build-depends: temporary } common text { build-depends: text } common time { build-depends: time >= 1.9.1 }
src/Hedgehog/Extras/Test/Golden.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE MultiWayIf #-}+ module Hedgehog.Extras.Test.Golden ( diffVsGoldenFile, diffFileVsGoldenFile,@@ -43,16 +45,66 @@ mGoldenFileLogFile = IO.unsafePerformIO $ IO.lookupEnv "GOLDEN_FILE_LOG_FILE" --- | Whether the test should create the golden files if the file does ont exist.-createFiles :: Bool-createFiles = IO.unsafePerformIO $ do+-- | Whether the test should create the golden files if the files do not exist.+createGoldenFiles :: Bool+createGoldenFiles = IO.unsafePerformIO $ do value <- IO.lookupEnv "CREATE_GOLDEN_FILES" return $ value == Just "1" +-- | Whether the test should recreate the golden files if the files already exist.+recreateGoldenFiles :: Bool+recreateGoldenFiles = IO.unsafePerformIO $ do+ value <- IO.lookupEnv "RECREATE_GOLDEN_FILES"+ return $ value == Just "1"++writeGoldenFile :: ()+ => MonadIO m+ => MonadTest m+ => FilePath+ -> String+ -> m ()+writeGoldenFile goldenFile actualContent = do+ H.note_ $ "Creating golden file " <> goldenFile+ H.createDirectoryIfMissing_ (takeDirectory goldenFile)+ H.writeFile goldenFile actualContent++reportGoldenFileMissing :: ()+ => MonadIO m+ => MonadTest m+ => FilePath+ -> m ()+reportGoldenFileMissing goldenFile = do+ H.note_ $ unlines+ [ "Golden file " <> goldenFile <> " does not exist."+ , "To create it, run with CREATE_GOLDEN_FILES=1."+ , "To recreate it, run with RECREATE_GOLDEN_FILES=1."+ ]+ H.failure++checkAgainstGoldenFile :: ()+ => MonadIO m+ => MonadTest m+ => FilePath+ -> [String]+ -> m ()+checkAgainstGoldenFile goldenFile actualLines = do+ referenceLines <- List.lines <$> H.readFile goldenFile+ let difference = getGroupedDiff actualLines referenceLines+ case difference of+ [] -> pure ()+ [Both{}] -> pure ()+ _ -> do+ H.note_ $ unlines+ [ "Golden test failed against golden file: " <> goldenFile+ , "To recreate golden file, run with RECREATE_GOLDEN_FILES=1."+ ]+ failMessage callStack $ ppDiff difference+ -- | Diff contents against the golden file. If CREATE_GOLDEN_FILES environment is--- set to "1", then should the gold file not exist it would be created. If--- GOLDEN_FILE_LOG_FILE is set to a filename, then the golden file path will be--- logged to the specified file.+-- set to "1", then should the golden file not exist it would be created. If+-- RECREATE_GOLDEN_FILES is set to "1", then should the golden file exist it would+-- be recreated. If GOLDEN_FILE_LOG_FILE is set to a filename, then the golden file+-- path will be logged to the specified file. -- -- Set the environment variable when you intend to generate or re-generate the golden -- file for example when running the test for the first time or if the golden file@@ -69,35 +121,18 @@ => String -- ^ Actual content -> FilePath -- ^ Reference file -> m ()-diffVsGoldenFile actualContent referenceFile = GHC.withFrozenCallStack $ do+diffVsGoldenFile actualContent goldenFile = GHC.withFrozenCallStack $ do forM_ mGoldenFileLogFile $ \logFile ->- liftIO $ semBracket $ IO.appendFile logFile $ referenceFile <> "\n"+ liftIO $ semBracket $ IO.appendFile logFile $ goldenFile <> "\n" - fileExists <- liftIO $ IO.doesFileExist referenceFile+ fileExists <- liftIO $ IO.doesFileExist goldenFile - if fileExists- then do- referenceLines <- List.lines <$> H.readFile referenceFile- let difference = getGroupedDiff actualLines referenceLines- case difference of- [] -> pure ()- [Both{}] -> pure ()- _ -> do- H.note_ $ "Golden test failed against golden file: " <> referenceFile- failMessage callStack $ ppDiff difference- else if createFiles- then do- -- CREATE_GOLDEN_FILES is set, so we create any golden files that don't- -- already exist.- H.note_ $ "Creating golden file " <> referenceFile- H.createDirectoryIfMissing_ (takeDirectory referenceFile)- H.writeFile referenceFile actualContent- else do- H.note_ $ mconcat- [ "Golden file " <> referenceFile- , " does not exist. To create, run with CREATE_GOLDEN_FILES=1"- ]- H.failure+ if+ | recreateGoldenFiles -> writeGoldenFile goldenFile actualContent+ | fileExists -> checkAgainstGoldenFile goldenFile actualLines+ | createGoldenFiles -> writeGoldenFile goldenFile actualContent+ | otherwise -> reportGoldenFileMissing goldenFile+ where actualLines = List.lines actualContent