Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/Elm/Kernel/Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ function _Test_runThunk(thunk)

const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const process = require('node:process');
const crypto = require('node:crypto');

function _Test_readFile(filePath)
{
Expand Down Expand Up @@ -69,6 +67,25 @@ function WriteFile(root, filePath, contents)
return __Result_Err(__File_PathEscapesDirectory);
}

// Remove failed file if it exists
var failedPath = null;
if (!fullPath.endsWith(".failed.html") && fullPath.endsWith(".html"))
failedPath = fullPath.slice(0, -5) + ".failed.html";
else if (!fullPath.endsWith(".failed"))
failedPath = fullPath + ".failed";

if (failedPath)
{
try
{
fs.unlinkSync(failedPath);
}
catch (error)
{
// Ignore failure if file doesn't exist
}
}

Comment thread
micahhahn marked this conversation as resolved.
const fullDir = path.dirname(fullPath);

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

var tempDir = null;
var _Test_writeTempFile = F2(function(filePath, contents)
{
if (tempDir === null)
{
tempDir = os.tmpdir() + "/" + crypto.randomUUID();
fs.mkdirSync(tempDir);
}

return WriteFile(tempDir, filePath, contents);
})

var overwriteGoldenFiles = null;
function _Test_overwriteGoldenFiles(unused)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Expect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,16 @@ equalToFile filePath actual =
pass

else
case File.writeTempFile filePath actual of
let
failedFilePath =
-- Be careful to make the failed final extension .html so that browsers can render it
if String.endsWith ".html" filePath then
String.dropRight (String.length ".html") filePath ++ ".failed.html"

else
filePath ++ ".failed"
in
case File.writeFile failedFilePath actual of
Ok newAbsolutePath ->
let
message =
Expand Down
12 changes: 1 addition & 11 deletions src/File.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module File exposing (AbsolutePath, FileError(..), RelativePath, overwriteGoldenFiles, readFile, writeFile, writeTempFile)
module File exposing (AbsolutePath, FileError(..), RelativePath, overwriteGoldenFiles, readFile, writeFile)

import Elm.Kernel.Test

Expand Down Expand Up @@ -35,16 +35,6 @@ writeFile =
Elm.Kernel.Test.writeFile


{-| Write the contents of the second argument to the file path in the first argument relative to a temp directory

Returns the absolute file path if successful.

-}
writeTempFile : RelativePath -> String -> Result FileError AbsolutePath
writeTempFile =
Elm.Kernel.Test.writeTempFile


{-| Checks the OVERWRITE\_GOLDEN\_FILES environment variable
-}
overwriteGoldenFiles : () -> Bool
Expand Down