Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jan 3, 2024
1 parent 7df40ca commit 9425197
Showing 1 changed file with 56 additions and 33 deletions.
89 changes: 56 additions & 33 deletions src/Hedgehog/Extras/Test/Golden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,57 @@ 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
createGoldenFiles :: Bool
createGoldenFiles = IO.unsafePerformIO $ do
value <- IO.lookupEnv "CREATE_GOLDEN_FILES"
return $ value == Just "1"

-- | Whether the test should create the golden files if the file does ont exist.
recreateFiles :: Bool
recreateFiles = IO.unsafePerformIO $ do
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, run with CREATE_GOLDEN_FILES=1."
, "To recreate, 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_ $ "Golden test failed against golden file: " <> goldenFile
failMessage callStack $ ppDiff difference

-- | Diff contents against the golden file. If CREATE_GOLDEN_FILES environment is
-- 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
Expand All @@ -76,38 +116,21 @@ diffVsGoldenFile
=> 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 && (createFiles || not recreateFiles)
then do
-- RECREATE_GOLDEN_FILES is not set so we compare the actual content against
-- the golden file.
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 || recreateFiles
then do
-- Either CREATE_GOLDEN_FILES or RECREATE_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
-- Neither CREATE_GOLDEN_FILES nor RECREATE_GOLDEN_FILES is set, so we fail
H.note_ $ mconcat
[ "Golden file " <> referenceFile
, " does not exist. To create, run with CREATE_GOLDEN_FILES=1"
]
H.failure
if recreateGoldenFiles
then writeGoldenFile goldenFile actualContent
else if createGoldenFiles
then if fileExists
then checkAgainstGoldenFile goldenFile actualLines
else writeGoldenFile goldenFile actualContent
else if fileExists
then checkAgainstGoldenFile goldenFile actualLines
else reportGoldenFileMissing goldenFile
where
actualLines = List.lines actualContent

Expand Down

0 comments on commit 9425197

Please sign in to comment.