Skip to content

Commit aa162eb

Browse files
committed
Add diffFileVsGoldenFile function and ability to create golden files from tests
1 parent 818dabb commit aa162eb

File tree

1 file changed

+72
-12
lines changed

1 file changed

+72
-12
lines changed

cardano-cli/test/Test/Utilities.hs

+72-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,87 @@
1-
module Test.Utilities (diffVsGoldenFile) where
1+
module Test.Utilities
2+
( diffVsGoldenFile,
3+
diffFileVsGoldenFile,
4+
) where
25

3-
import Cardano.Prelude (ConvertText (..))
6+
import Cardano.Prelude (ConvertText (..), HasCallStack)
47

58
import Control.Monad.IO.Class (MonadIO (liftIO))
69
import Data.Algorithm.Diff (PolyDiff (Both), getGroupedDiff)
710
import Data.Algorithm.DiffOutput (ppDiff)
811
import GHC.Stack (callStack)
12+
import qualified GHC.Stack as GHC
913
import Hedgehog (MonadTest)
14+
import qualified Hedgehog.Extras.Test as H
1015
import Hedgehog.Extras.Test.Base (failMessage)
16+
import qualified Hedgehog.Internal.Property as H
17+
import qualified System.Directory as IO
18+
import qualified System.Environment as IO
19+
import qualified System.IO.Unsafe as IO
1120

21+
-- | Whether the test should create the golden files if the file does ont exist.
22+
createFiles :: Bool
23+
createFiles = IO.unsafePerformIO $ do
24+
value <- IO.lookupEnv "CREATE_GOLDEN_FILES"
25+
return $ value == Just "1"
26+
27+
-- | Diff contents against the golden file. If CREATE_GOLDEN_FILES environment is
28+
-- set to "1", then should the gold file not exist it would be created.
29+
--
30+
-- Set the environment variable when you intend to generate or re-generate the golden
31+
-- file for example when running the test for the first time or if the golden file
32+
-- genuinely needs to change.
33+
--
34+
-- To re-generate a golden file you must also delete the golden file because golden
35+
-- files are never overwritten.
36+
--
1237
-- TODO: Improve the help output by saying the difference of
1338
-- each input.
1439
diffVsGoldenFile
15-
:: (MonadIO m, MonadTest m)
16-
=> String -- ^ actual content
17-
-> FilePath -- ^ reference file
40+
:: HasCallStack
41+
=> (MonadIO m, MonadTest m)
42+
=> String -- ^ Actual content
43+
-> FilePath -- ^ Reference file
1844
-> m ()
19-
diffVsGoldenFile actualContent referenceFile =
20-
do
21-
referenceLines <- map toS . lines <$> liftIO (readFile referenceFile)
22-
let difference = getGroupedDiff actualLines referenceLines
23-
case difference of
24-
[Both{}] -> pure ()
25-
_ -> failMessage callStack $ ppDiff difference
45+
diffVsGoldenFile actualContent referenceFile = GHC.withFrozenCallStack $ do
46+
fileExists <- liftIO $ IO.doesFileExist referenceFile
47+
48+
if fileExists
49+
then do
50+
referenceLines <- map toS . lines <$> H.readFile referenceFile
51+
let difference = getGroupedDiff actualLines referenceLines
52+
case difference of
53+
[Both{}] -> pure ()
54+
_ -> failMessage callStack $ ppDiff difference
55+
else if createFiles
56+
then do
57+
-- CREATE_GOLDEN_FILES is set, so we create any golden files that don't
58+
-- already exist.
59+
H.note_ $ "Creating golden file " <> referenceFile
60+
H.writeFile referenceFile actualContent
61+
else do
62+
H.note_ $ mconcat
63+
[ "Golden file " <> referenceFile
64+
, " does not exist. To create, run with CREATE_GOLDEN_FILES=1"
65+
]
66+
H.failure
2667
where
2768
actualLines = Prelude.lines actualContent
69+
70+
-- | Diff file against the golden file. If CREATE_GOLDEN_FILES environment is
71+
-- set to "1", then should the gold file not exist it would be created.
72+
--
73+
-- Set the environment variable when you intend to generate or re-generate the golden
74+
-- file for example when running the test for the first time or if the golden file
75+
-- genuinely needs to change.
76+
--
77+
-- To re-generate a golden file you must also delete the golden file because golden
78+
-- files are never overwritten.
79+
diffFileVsGoldenFile
80+
:: HasCallStack
81+
=> (MonadIO m, MonadTest m)
82+
=> FilePath -- ^ Actual file
83+
-> FilePath -- ^ Reference file
84+
-> m ()
85+
diffFileVsGoldenFile actualFile referenceFile = GHC.withFrozenCallStack $ do
86+
contents <- H.readFile actualFile
87+
diffVsGoldenFile contents referenceFile

0 commit comments

Comments
 (0)