Skip to content

Commit

Permalink
Use MultiwayIf to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jan 3, 2024
1 parent 1b41ea3 commit b09f7cc
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/Hedgehog/Extras/Test/Golden.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE MultiWayIf #-}

module Hedgehog.Extras.Test.Golden
( diffVsGoldenFile,
diffFileVsGoldenFile,
Expand Down Expand Up @@ -43,13 +45,13 @@ mGoldenFileLogFile :: Maybe FilePath
mGoldenFileLogFile = IO.unsafePerformIO $
IO.lookupEnv "GOLDEN_FILE_LOG_FILE"

-- | Whether the test should create the golden files if the file does ont exist.
-- | 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 create the golden files if the file does ont exist.
-- | 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"
Expand All @@ -74,8 +76,8 @@ reportGoldenFileMissing :: ()
reportGoldenFileMissing goldenFile = do
H.note_ $ unlines
[ "Golden file " <> goldenFile <> " does not exist."
, "To create golden file, run with CREATE_GOLDEN_FILES=1."
, "To recreate golden file, run with RECREATE_GOLDEN_FILES=1."
, "To create it, run with CREATE_GOLDEN_FILES=1."
, "To recreate it, run with RECREATE_GOLDEN_FILES=1."
]
H.failure

Expand Down Expand Up @@ -125,15 +127,12 @@ diffVsGoldenFile actualContent goldenFile = GHC.withFrozenCallStack $ do

fileExists <- liftIO $ IO.doesFileExist goldenFile

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
if
| recreateGoldenFiles -> writeGoldenFile goldenFile actualContent
| fileExists -> checkAgainstGoldenFile goldenFile actualLines
| createGoldenFiles -> writeGoldenFile goldenFile actualContent
| otherwise -> reportGoldenFileMissing goldenFile

where
actualLines = List.lines actualContent

Expand Down

0 comments on commit b09f7cc

Please sign in to comment.