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 7fd4dd2 commit 2e49c81
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 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 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 2e49c81

Please sign in to comment.