Skip to content

Commit 7852922

Browse files
committed
Add diffVsGoldenFileExcludeTrace function
1 parent 61b2b12 commit 7852922

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Hedgehog/Extras/Test/Golden.hs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module Hedgehog.Extras.Test.Golden
66
( diffVsGoldenFile,
77
diffFileVsGoldenFile,
8+
diffVsGoldenFileExcludeTrace,
89
) where
910

1011
import Control.Applicative
@@ -33,6 +34,7 @@ import qualified Control.Concurrent.QSem as IO
3334
import qualified Control.Concurrent.STM as STM
3435
import qualified Data.List as List
3536
import qualified Data.Map as Map
37+
import qualified Data.Text as Text
3638
import qualified GHC.Stack as GHC
3739
import qualified Hedgehog.Extras.Test as H
3840
import qualified Hedgehog.Internal.Property as H
@@ -201,3 +203,32 @@ diffFileVsGoldenFile
201203
diffFileVsGoldenFile actualFile referenceFile = GHC.withFrozenCallStack $ do
202204
contents <- H.readFile actualFile
203205
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

Comments
 (0)