Skip to content

Commit d8fa6ea

Browse files
committed
Add OVERWRITE_GOLDEN_FILES behavior
1 parent d7bac45 commit d8fa6ea

File tree

3 files changed

+67
-37
lines changed

3 files changed

+67
-37
lines changed

src/Elm/Kernel/Test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function _Test_runThunk(thunk)
2121
const fs = require('node:fs');
2222
const path = require('node:path');
2323
const os = require('node:os');
24+
const process = require('node:process');
2425
const crypto = require('node:crypto');
2526

2627
function _Test_readFile(filePath)
@@ -98,4 +99,13 @@ var _Test_writeTempFile = F2(function(filePath, contents)
9899
}
99100

100101
return WriteFile(tempDir, filePath, contents);
101-
})
102+
})
103+
104+
var overwriteGoldenFiles = null;
105+
function _Test_overwriteGoldenFiles(unused)
106+
{
107+
if (overwriteGoldenFiles === null)
108+
overwriteGoldenFiles = process.env.OVERWRITE_GOLDEN_FILES == '1';
109+
110+
return overwriteGoldenFiles;
111+
}

src/Expect.elm

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -594,52 +594,65 @@ All file paths are scoped to be within the "tests/" directory.
594594
-}
595595
equalToFile : String -> String -> Expectation
596596
equalToFile 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.

src/File.elm

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module File exposing (AbsolutePath, FileError(..), RelativePath, readFile, writeFile, writeTempFile)
1+
module File exposing (AbsolutePath, FileError(..), RelativePath, overwriteGoldenFiles, readFile, writeFile, writeTempFile)
22

33
import Elm.Kernel.Test
44

@@ -43,3 +43,10 @@ Returns the absolute file path if successful.
4343
writeTempFile : RelativePath -> String -> Result FileError AbsolutePath
4444
writeTempFile =
4545
Elm.Kernel.Test.writeTempFile
46+
47+
48+
{-| Checks the OVERWRITE\_GOLDEN\_FILES environment variable
49+
-}
50+
overwriteGoldenFiles : () -> Bool
51+
overwriteGoldenFiles =
52+
Elm.Kernel.Test.overwriteGoldenFiles

0 commit comments

Comments
 (0)