Skip to content

Commit

Permalink
Merge branch 'master' into array-find-last
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Dec 30, 2021
2 parents 63d6df6 + 7f55571 commit 8a591b2
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 61 deletions.
5 changes: 3 additions & 2 deletions test/cli/samples/wait-for-bundle-input-object/_config.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const { atomicWriteFileSync } = require('../../../utils');

let second;
let third;
Expand All @@ -17,9 +18,9 @@ module.exports = {
},
abortOnStderr(data) {
if (data.includes('waiting for input second')) {
fs.writeFileSync(second, "export default 'second'");
atomicWriteFileSync(second, "export default 'second'");
} else if (data.includes('waiting for input third')) {
fs.writeFileSync(third, "export default 'third'");
atomicWriteFileSync(third, "export default 'third'");
}
}
};
3 changes: 2 additions & 1 deletion test/cli/samples/wait-for-bundle-input/_config.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const { atomicWriteFileSync } = require('../../../utils');

let mainFile;

Expand All @@ -15,7 +16,7 @@ module.exports = {
abortOnStderr(data) {
if (data.includes('waiting for input main.js')) {
// wait longer than one polling interval
setTimeout(() => fs.writeFileSync(mainFile, 'export default 42;'), 600);
setTimeout(() => atomicWriteFileSync(mainFile, 'export default 42;'), 600);
}
}
};
3 changes: 2 additions & 1 deletion test/cli/samples/watch/bundle-error/_config.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const { atomicWriteFileSync } = require('../../../../utils');

let mainFile;

Expand All @@ -16,7 +17,7 @@ module.exports = {
},
abortOnStderr(data) {
if (data.includes('Error: Unexpected token')) {
setTimeout(() => fs.writeFileSync(mainFile, 'export default 42;'), 500);
setTimeout(() => atomicWriteFileSync(mainFile, 'export default 42;'), 500);
return false;
}
if (data.includes('created _actual')) {
Expand Down
3 changes: 2 additions & 1 deletion test/cli/samples/watch/watch-config-early-update/_config.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const { atomicWriteFileSync } = require('../../../../utils');

let configFile;

Expand Down Expand Up @@ -34,7 +35,7 @@ module.exports = {
},
abortOnStderr(data) {
if (data === 'initial\n') {
fs.writeFileSync(
atomicWriteFileSync(
configFile,
`
console.error('updated');
Expand Down
5 changes: 3 additions & 2 deletions test/cli/samples/watch/watch-config-error/_config.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const { atomicWriteFileSync } = require('../../../../utils');

let configFile;

Expand All @@ -25,12 +26,12 @@ module.exports = {
},
abortOnStderr(data) {
if (data.includes(`created _actual${path.sep}main1.js`)) {
fs.writeFileSync(configFile, 'throw new Error("Config contains errors");');
atomicWriteFileSync(configFile, 'throw new Error("Config contains errors");');
return false;
}
if (data.includes('Config contains errors')) {
setTimeout(() => {
fs.writeFileSync(
atomicWriteFileSync(
configFile,
'export default {\n' +
'\tinput: "main.js",\n' +
Expand Down
3 changes: 2 additions & 1 deletion test/cli/samples/watch/watch-config-initial-error/_config.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const { atomicWriteFileSync } = require('../../../../utils');

let configFile;

Expand All @@ -16,7 +17,7 @@ module.exports = {
async abortOnStderr(data) {
if (data.includes('Config contains initial errors')) {
await new Promise(resolve => setTimeout(resolve, 100));
fs.writeFileSync(
atomicWriteFileSync(
configFile,
'export default {\n' +
'\tinput: "main.js",\n' +
Expand Down
3 changes: 2 additions & 1 deletion test/cli/samples/watch/watch-config-no-update/_config.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const { atomicWriteFileSync } = require('../../../../utils');

let configFile;
const configContent =
Expand All @@ -23,7 +24,7 @@ module.exports = {
},
abortOnStderr(data) {
if (data.includes('created _actual/main.js')) {
fs.writeFileSync(configFile, configContent);
atomicWriteFileSync(configFile, configContent);
return new Promise(resolve => setTimeout(() => resolve(true), 500));
}
},
Expand Down
11 changes: 11 additions & 0 deletions test/utils.js
@@ -1,4 +1,5 @@
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const fixturify = require('fixturify');
const sander = require('sander');
Expand All @@ -14,6 +15,7 @@ exports.runTestSuiteWithSamples = runTestSuiteWithSamples;
exports.assertDirectoriesAreEqual = assertDirectoriesAreEqual;
exports.assertFilesAreEqual = assertFilesAreEqual;
exports.assertIncludes = assertIncludes;
exports.atomicWriteFileSync = atomicWriteFileSync;

function normaliseError(error) {
delete error.stack;
Expand Down Expand Up @@ -220,3 +222,12 @@ function assertIncludes(actual, expected) {
throw err;
}
}

// Workaround a race condition in fs.writeFileSync that temporarily creates
// an empty file for a brief moment which may be read by rollup watch - even
// if the content being overwritten is identical.
function atomicWriteFileSync(filePath, contents) {
const stagingPath = filePath + '_';
fs.writeFileSync(stagingPath, contents);
fs.renameSync(stagingPath, filePath);
}

0 comments on commit 8a591b2

Please sign in to comment.