Skip to content

Commit

Permalink
Flush atomic file writes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 9, 2022
1 parent 2026631 commit 4d8105c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions test/cli/samples/watch/watch-config-early-update/_config.js
@@ -1,6 +1,6 @@
const fs = require('fs');
const path = require('path');
const { atomicWriteFileSync } = require('../../../../utils');
const { syncFile } = require('../../../../utils');

let configFile;

Expand Down Expand Up @@ -36,7 +36,7 @@ module.exports = {
},
abortOnStderr(data) {
if (data === 'initial\n') {
atomicWriteFileSync(
fs.writeFileSync(
configFile,
`
console.error('updated');
Expand All @@ -49,6 +49,7 @@ module.exports = {
};
`
);
syncFile(configFile);
return false;
}
if (data.includes(`created _actual${path.sep}output2.js`)) {
Expand Down
8 changes: 8 additions & 0 deletions test/utils.js
Expand Up @@ -16,6 +16,7 @@ exports.assertDirectoriesAreEqual = assertDirectoriesAreEqual;
exports.assertFilesAreEqual = assertFilesAreEqual;
exports.assertIncludes = assertIncludes;
exports.atomicWriteFileSync = atomicWriteFileSync;
exports.syncFile = syncFile;
exports.getFileNamesAndRemoveOutput = getFileNamesAndRemoveOutput;

function normaliseError(error) {
Expand Down Expand Up @@ -232,3 +233,10 @@ function atomicWriteFileSync(filePath, contents) {
fs.writeFileSync(stagingPath, contents);
fs.renameSync(stagingPath, filePath);
}

// It appears that on MacOS, it sometimes takes long for the file system to update
function syncFile(filePath) {
const file = fs.openSync(filePath);
fs.fsyncSync(file);
fs.closeSync(file);
}

0 comments on commit 4d8105c

Please sign in to comment.