Skip to content

Commit dd21c67

Browse files
committed
Write a ".failed" file next to the golden file instead of in temp
1 parent d8fa6ea commit dd21c67

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/Elm/Kernel/Test.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ function _Test_runThunk(thunk)
2020

2121
const fs = require('node:fs');
2222
const path = require('node:path');
23-
const os = require('node:os');
2423
const process = require('node:process');
25-
const crypto = require('node:crypto');
2624

2725
function _Test_readFile(filePath)
2826
{
@@ -69,6 +67,25 @@ function WriteFile(root, filePath, contents)
6967
return __Result_Err(__File_PathEscapesDirectory);
7068
}
7169

70+
// Remove failed file if it exists
71+
var failedPath = null;
72+
if (!fullPath.endsWith(".failed.html") && fullPath.endsWith(".html"))
73+
failedPath = fullPath.slice(0, -5) + ".failed.html";
74+
else if (!fullPath.endsWith(".failed"))
75+
failedPath = fullPath + ".failed";
76+
77+
if (failedPath)
78+
{
79+
try
80+
{
81+
fs.unlinkSync(failedPath);
82+
}
83+
catch (error)
84+
{
85+
// Ignore failure if file doesn't exist
86+
}
87+
}
88+
7289
const fullDir = path.dirname(fullPath);
7390

7491
// Note that this does not throw an error if the directory exists
@@ -89,18 +106,6 @@ var _Test_writeFile = F2(function(filePath, contents)
89106
return WriteFile(path.resolve("tests"), filePath, contents);
90107
})
91108

92-
var tempDir = null;
93-
var _Test_writeTempFile = F2(function(filePath, contents)
94-
{
95-
if (tempDir === null)
96-
{
97-
tempDir = os.tmpdir() + "/" + crypto.randomUUID();
98-
fs.mkdirSync(tempDir);
99-
}
100-
101-
return WriteFile(tempDir, filePath, contents);
102-
})
103-
104109
var overwriteGoldenFiles = null;
105110
function _Test_overwriteGoldenFiles(unused)
106111
{

src/Expect.elm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,16 @@ equalToFile filePath actual =
635635
pass
636636

637637
else
638-
case File.writeTempFile filePath actual of
638+
let
639+
failedFilePath =
640+
-- Be careful to make the failed final extension .html so that browsers can render it
641+
if String.endsWith ".html" filePath then
642+
String.dropRight (String.length ".html") filePath ++ ".failed.html"
643+
644+
else
645+
filePath ++ ".failed"
646+
in
647+
case File.writeFile failedFilePath actual of
639648
Ok newAbsolutePath ->
640649
let
641650
message =

src/File.elm

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

33
import Elm.Kernel.Test
44

@@ -35,16 +35,6 @@ writeFile =
3535
Elm.Kernel.Test.writeFile
3636

3737

38-
{-| Write the contents of the second argument to the file path in the first argument relative to a temp directory
39-
40-
Returns the absolute file path if successful.
41-
42-
-}
43-
writeTempFile : RelativePath -> String -> Result FileError AbsolutePath
44-
writeTempFile =
45-
Elm.Kernel.Test.writeTempFile
46-
47-
4838
{-| Checks the OVERWRITE\_GOLDEN\_FILES environment variable
4939
-}
5040
overwriteGoldenFiles : () -> Bool

0 commit comments

Comments
 (0)