|
5 | 5 | module Hedgehog.Extras.Test.Golden |
6 | 6 | ( diffVsGoldenFile, |
7 | 7 | diffFileVsGoldenFile, |
| 8 | + diffVsGoldenFileExcludeTrace, |
8 | 9 | ) where |
9 | 10 |
|
10 | 11 | import Control.Applicative |
@@ -33,6 +34,7 @@ import qualified Control.Concurrent.QSem as IO |
33 | 34 | import qualified Control.Concurrent.STM as STM |
34 | 35 | import qualified Data.List as List |
35 | 36 | import qualified Data.Map as Map |
| 37 | +import qualified Data.Text as Text |
36 | 38 | import qualified GHC.Stack as GHC |
37 | 39 | import qualified Hedgehog.Extras.Test as H |
38 | 40 | import qualified Hedgehog.Internal.Property as H |
@@ -201,3 +203,32 @@ diffFileVsGoldenFile |
201 | 203 | diffFileVsGoldenFile actualFile referenceFile = GHC.withFrozenCallStack $ do |
202 | 204 | contents <- H.readFile actualFile |
203 | 205 | diffVsGoldenFile contents referenceFile |
| 206 | + |
| 207 | +-- | Diff contents against the golden file, excluding the trace. If CREATE_GOLDEN_FILES environment is |
| 208 | +-- set to "1", then should the golden file not exist it would be created. If |
| 209 | +-- RECREATE_GOLDEN_FILES is set to "1", then should the golden file exist it would |
| 210 | +-- be recreated. If GOLDEN_FILE_LOG_FILE is set to a filename, then the golden file |
| 211 | +-- path will be logged to the specified file. |
| 212 | +-- |
| 213 | +-- Set the environment variable when you intend to generate or re-generate the golden |
| 214 | +-- file for example when running the test for the first time or if the golden file |
| 215 | +-- genuinely needs to change. |
| 216 | +-- |
| 217 | +-- To re-generate a golden file you must also delete the golden file because golden |
| 218 | +-- files are never overwritten. |
| 219 | +diffVsGoldenFileExcludeTrace |
| 220 | + :: MonadBaseControl IO m |
| 221 | + => MonadIO m |
| 222 | + => MonadTest m |
| 223 | + => HasCallStack |
| 224 | + => String -> FilePath -> m () |
| 225 | +diffVsGoldenFileExcludeTrace inputString refFile = |
| 226 | + GHC.withFrozenCallStack $ do |
| 227 | + case List.uncons $ Text.splitOn "CallStack" $ Text.pack inputString of |
| 228 | + Just (stackTraceRemoved, _) -> diffVsGoldenFile (Text.unpack stackTraceRemoved) refFile |
| 229 | + Nothing -> |
| 230 | + H.failWith Nothing $ |
| 231 | + List.unlines |
| 232 | + [ "Input string was empty" |
| 233 | + , "Reference file: " <> refFile |
| 234 | + ] |
0 commit comments