|
1 |
| -module Test.Utilities (diffVsGoldenFile) where |
| 1 | +module Test.Utilities |
| 2 | + ( diffVsGoldenFile, |
| 3 | + diffFileVsGoldenFile, |
| 4 | + ) where |
2 | 5 |
|
3 |
| -import Cardano.Prelude (ConvertText (..)) |
| 6 | +import Cardano.Prelude (ConvertText (..), HasCallStack) |
4 | 7 |
|
5 | 8 | import Control.Monad.IO.Class (MonadIO (liftIO))
|
6 | 9 | import Data.Algorithm.Diff (PolyDiff (Both), getGroupedDiff)
|
7 | 10 | import Data.Algorithm.DiffOutput (ppDiff)
|
8 | 11 | import GHC.Stack (callStack)
|
| 12 | +import qualified GHC.Stack as GHC |
9 | 13 | import Hedgehog (MonadTest)
|
| 14 | +import qualified Hedgehog.Extras.Test as H |
10 | 15 | 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 |
11 | 20 |
|
| 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 | +-- |
12 | 37 | -- TODO: Improve the help output by saying the difference of
|
13 | 38 | -- each input.
|
14 | 39 | 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 |
18 | 44 | -> 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 |
26 | 67 | where
|
27 | 68 | 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