@@ -3,7 +3,8 @@ module Expect exposing
33 , lessThan, atMost, greaterThan, atLeast
44 , FloatingPointTolerance (..) , within, notWithin
55 , ok, err, equalLists, equalDicts, equalSets
6- , pass, fail, onFail, equalToFile
6+ , equalToFile
7+ , pass, fail, onFail
78 )
89
910{- | A library to create `Expectation`s, which describe a claim to be tested.
@@ -42,10 +43,12 @@ or both. For an in-depth look, see our [Guide to Floating Point Comparison](#gui
4243
4344@docs ok, err, equalLists, equalDicts, equalSets
4445
46+
4547## Golden Files
4648
4749@docs equalToFile
4850
51+
4952## Customizing
5053
5154These functions will let you build your own expectations.
@@ -106,8 +109,8 @@ Another example is comparing values that are on either side of zero. `0.0001` is
106109-}
107110
108111import Dict exposing (Dict )
109- import Set exposing (Set )
110112import File
113+ import Set exposing (Set )
111114import Test.Distribution
112115import Test.Expectation
113116import Test.Internal as Internal
@@ -580,7 +583,7 @@ equalSets expected actual =
580583 reportCollectionFailure " Expect.equalSets" expected actual missingKeys extraKeys
581584
582585
583- {- | Tests the a String is equal to the contents of the file stored at the file path.
586+ {- | Tests the a String is equal to the contents of the file stored at the file path.
584587
585588If the file does not exist, it will be created and this test will pass.
586589
@@ -591,30 +594,53 @@ All file paths are scoped to be within the "tests/" directory.
591594-}
592595equalToFile : String -> String -> Expectation
593596equalToFile filePath actual =
594- case File . readFile filePath of
597+ case File . readFile filePath of
595598 Err File . FileNotFound ->
596- case File . writeFile filePath actual of
597- Err ( File . GeneralFileError fileError) ->
599+ case File . writeFile filePath actual of
600+ Err ( File . GeneralFileError fileError) ->
598601 Test . Expectation . fail { description = " Expect.equalToFile encountered a general file error: " ++ fileError, reason = Custom }
599602
600603 -- This case should be impossible non general file errors should have been surfaced in the call to `readFile` above.
601- Err _ ->
602- Test . Expectation . fail { description = " Expect.equalToFile encountered an unexpected error" , reason = Custom }
604+ Err _ ->
605+ Test . Expectation . fail { description = " Expect.equalToFile encountered an unexpected error" , reason = Custom }
603606
604607 Ok _ ->
605608 pass
606609
607- Err File . IsDirectory ->
610+ Err File . IsDirectory ->
608611 Test . Expectation . fail { description = " Expect.equalToFile was given a directory instead of a file" , reason = Custom }
609612
610- Err File . PathEscapesDirectory ->
613+ Err File . PathEscapesDirectory ->
611614 Test . Expectation . fail { description = " Expect.equalToFile was given a path that would escape the tests/ directory" , reason = Custom }
612615
613- Err ( File . GeneralFileError fileError) ->
616+ Err ( File . GeneralFileError fileError) ->
614617 Test . Expectation . fail { description = " Expect.equalToFile encountered a general file error: " ++ fileError, reason = Custom }
615618
616- Ok contents ->
617- equateWith ( " equalToFile \' " ++ filePath ++ " \' " ) (==) contents actual
619+ Ok ( existingAbsolutePath, contents ) ->
620+ if actual == contents then
621+ pass
622+
623+ else
624+ case File . writeTempFile filePath actual of
625+ Ok newAbsolutePath ->
626+ let
627+ message =
628+ [ " The contents of " ++ filePath ++ " changed!"
629+ , " To compare run: git diff --no-index " ++ existingAbsolutePath ++ " " ++ newAbsolutePath
630+ ]
631+
632+ messageWithVisualDiff =
633+ if String . endsWith " .html" filePath then
634+ message ++ [ " To visually compare run: open file://" ++ existingAbsolutePath ++ " file://" ++ newAbsolutePath ]
635+
636+ else
637+ message
638+ in
639+ Test . Expectation . fail { description = String . join " \n\n " messageWithVisualDiff, reason = Custom }
640+
641+ _ ->
642+ Test . Expectation . fail { description = " Expect.equalToFile encountered an unexpected error" , reason = Custom }
643+
618644
619645{- | Always passes.
620646
0 commit comments