@@ -594,52 +594,65 @@ All file paths are scoped to be within the "tests/" directory.
594594-}
595595equalToFile : String -> String -> Expectation
596596equalToFile filePath actual =
597- case File . readFile filePath of
598- Err File . FileNotFound ->
597+ let
598+ writeGoldenFile () =
599599 case File . writeFile filePath actual of
600+ Err File . FileNotFound ->
601+ -- Impossible
602+ pass
603+
604+ Err File . IsDirectory ->
605+ Test . Expectation . fail { description = " Expect.equalToFile was given a directory instead of a file" , reason = Custom }
606+
607+ Err File . PathEscapesDirectory ->
608+ Test . Expectation . fail { description = " Expect.equalToFile was given a path that would escape the tests/ directory" , reason = Custom }
609+
600610 Err ( File . GeneralFileError fileError) ->
601611 Test . Expectation . fail { description = " Expect.equalToFile encountered a general file error: " ++ fileError, reason = Custom }
602612
603- -- This case should be impossible non general file errors should have been surfaced in the call to `readFile` above.
604- Err _ ->
605- Test . Expectation . fail { description = " Expect.equalToFile encountered an unexpected error" , reason = Custom }
606-
607613 Ok _ ->
608614 pass
615+ in
616+ if File . overwriteGoldenFiles () then
617+ writeGoldenFile ()
609618
610- Err File . IsDirectory ->
611- Test . Expectation . fail { description = " Expect.equalToFile was given a directory instead of a file" , reason = Custom }
619+ else
620+ case File . readFile filePath of
621+ Err File . FileNotFound ->
622+ writeGoldenFile ()
612623
613- Err File . PathEscapesDirectory ->
614- Test . Expectation . fail { description = " Expect.equalToFile was given a path that would escape the tests/ directory " , reason = Custom }
624+ Err File . IsDirectory ->
625+ Test . Expectation . fail { description = " Expect.equalToFile was given a directory instead of a file " , reason = Custom }
615626
616- Err ( File . GeneralFileError fileError ) ->
617- Test . Expectation . fail { description = " Expect.equalToFile encountered a general file error: " ++ fileError , reason = Custom }
627+ Err File . PathEscapesDirectory ->
628+ Test . Expectation . fail { description = " Expect.equalToFile was given a path that would escape the tests/ directory " , reason = Custom }
618629
619- Ok ( existingAbsolutePath, contents ) ->
620- if actual == contents then
621- pass
630+ Err ( File . GeneralFileError fileError) ->
631+ Test . Expectation . fail { description = " Expect.equalToFile encountered a general file error: " ++ fileError, reason = Custom }
622632
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 }
633+ Ok ( existingAbsolutePath, contents ) ->
634+ if actual == contents then
635+ pass
636+
637+ else
638+ case File . writeTempFile filePath actual of
639+ Ok newAbsolutePath ->
640+ let
641+ message =
642+ [ Just <| " The contents of \" " ++ filePath ++ " \" changed!"
643+ , Just <| " To compare run: git diff --no-index " ++ existingAbsolutePath ++ " " ++ newAbsolutePath
644+ , if String . endsWith " .html" filePath then
645+ Just <| " To visually compare run: open file://" ++ existingAbsolutePath ++ " file://" ++ newAbsolutePath
646+
647+ else
648+ Nothing
649+ , Just <| " To accept these changes delete \" " ++ filePath ++ " \" or specify OVERWRITE_GOLDEN_FILES=1 when running elm-test"
650+ ]
651+ in
652+ Test . Expectation . fail { description = String . join " \n\n " ( List . filterMap identity message) , reason = Custom }
653+
654+ _ ->
655+ Test . Expectation . fail { description = " Expect.equalToFile encountered an unexpected error" , reason = Custom }
643656
644657
645658{- | Always passes.
0 commit comments