Skip to content

Commit

Permalink
Wait a little after initial config write
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 11, 2022
1 parent c0928dd commit 964dfd4
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 91 deletions.
173 changes: 88 additions & 85 deletions test/cli/index.js
Expand Up @@ -35,113 +35,116 @@ function runTest(dir, config, pass) {
if (pass > 0) {
getFileNamesAndRemoveOutput(dir);
}
if (config.before) config.before();

const command = config.command.replace(
/(^| )rollup($| )/g,
`node ${path.resolve(__dirname, '../../dist/bin')}${path.sep}rollup `
);

const childProcess = exec(
command,
{
timeout: 40000,
env: { ...process.env, FORCE_COLOR: '0', ...config.env }
},
(err, code, stderr) => {
if (config.after) config.after(err, code, stderr);
if (err && !err.killed) {
if (config.error) {
const shouldContinue = config.error(err);
Promise.resolve(config.before && config.before()).then(() => {
const childProcess = exec(
command,
{
timeout: 40000,
env: { ...process.env, FORCE_COLOR: '0', ...config.env }
},
(err, code, stderr) => {
if (config.after) config.after(err, code, stderr);
if (err && !err.killed) {
if (config.error) {
const shouldContinue = config.error(err);
if (!shouldContinue) return done();
} else {
throw err;
}
}

if ('stderr' in config) {
const shouldContinue = config.stderr(stderr);
if (!shouldContinue) return done();
} else {
throw err;
} else if (stderr) {
console.error(stderr);
}
}

if ('stderr' in config) {
const shouldContinue = config.stderr(stderr);
if (!shouldContinue) return done();
} else if (stderr) {
console.error(stderr);
}
let unintendedError;

let unintendedError;
if (config.execute) {
try {
const fn = new Function('require', 'module', 'exports', 'assert', code);
const module = {
exports: {}
};
fn(require, module, module.exports, assert);

if (config.execute) {
try {
const fn = new Function('require', 'module', 'exports', 'assert', code);
const module = {
exports: {}
};
fn(require, module, module.exports, assert);
if (config.error) {
unintendedError = new Error('Expected an error while executing output');
}

if (config.error) {
unintendedError = new Error('Expected an error while executing output');
if (config.exports) {
config.exports(module.exports);
}
} catch (err) {
if (config.error) {
config.error(err);
} else {
unintendedError = err;
}
}

if (config.exports) {
config.exports(module.exports);
if (config.show || unintendedError) {
console.log(code + '\n\n\n');
}
} catch (err) {
if (config.error) {
config.error(err);
} else {
unintendedError = err;
}
}

if (config.show || unintendedError) {
console.log(code + '\n\n\n');
}
if (config.solo) console.groupEnd();

if (config.solo) console.groupEnd();

unintendedError ? done(unintendedError) : done();
} else if (config.result) {
try {
config.result(code);
done();
} catch (err) {
done(err);
}
} else if (config.test) {
try {
config.test();
done();
} catch (err) {
done(err);
}
} else if (sander.existsSync('_expected') && sander.statSync('_expected').isDirectory()) {
try {
assertDirectoriesAreEqual('_actual', '_expected');
done();
} catch (err) {
done(err);
}
} else {
const expected = sander.readFileSync('_expected.js').toString();
try {
assert.equal(normaliseOutput(code), normaliseOutput(expected));
done();
} catch (err) {
done(err);
unintendedError ? done(unintendedError) : done();
} else if (config.result) {
try {
config.result(code);
done();
} catch (err) {
done(err);
}
} else if (config.test) {
try {
config.test();
done();
} catch (err) {
done(err);
}
} else if (
sander.existsSync('_expected') &&
sander.statSync('_expected').isDirectory()
) {
try {
assertDirectoriesAreEqual('_actual', '_expected');
done();
} catch (err) {
done(err);
}
} else {
const expected = sander.readFileSync('_expected.js').toString();
try {
assert.equal(normaliseOutput(code), normaliseOutput(expected));
done();
} catch (err) {
done(err);
}
}
}
}
);
);

childProcess.stderr.on('data', async data => {
if (config.abortOnStderr) {
try {
if (await config.abortOnStderr(data)) {
childProcess.stderr.on('data', async data => {
if (config.abortOnStderr) {
try {
if (await config.abortOnStderr(data)) {
childProcess.kill('SIGTERM');
}
} catch (err) {
childProcess.kill('SIGTERM');
done(err);
}
} catch (err) {
childProcess.kill('SIGTERM');
done(err);
}
}
});
});
}
).timeout(50000);
Expand Down
15 changes: 9 additions & 6 deletions test/cli/samples/watch/watch-config-early-update/_config.js
Expand Up @@ -5,6 +5,7 @@ const { writeAndSync } = require('../../../../utils');
let configFile;

module.exports = {
solo: true,
repeat: 100,
description: 'immediately reloads the config file if a change happens while it is parsed',
command: 'rollup -cw',
Expand All @@ -13,14 +14,15 @@ module.exports = {
// config. The stderr message is observed by the parent process and triggers overwriting the
// config file. That way, we simulate a complicated config file being changed while it is parsed.
configFile = path.resolve(__dirname, 'rollup.config.js');
fs.writeFileSync(
console.time('testTime');
writeAndSync(
configFile,
`
import { watchFile, unwatchFile } from 'fs';
import { watch } from 'fs';
export default new Promise(resolve => {
const listener = () => {
const watcher = watch(${JSON.stringify(configFile)}, () => {
console.error('config update detected');
unwatchFile(${JSON.stringify(configFile)}, listener);
watcher.close();
setTimeout(() => {
console.error('resolve original config');
resolve({
Expand All @@ -32,14 +34,15 @@ module.exports = {
})
// wait a moment to make sure we do not trigger before Rollup's watcher
}, 600)
};
watchFile(${JSON.stringify(configFile)}, { interval: 100 }, listener);
});
console.error('initial');
});
`
);
return new Promise(resolve => setTimeout(resolve, 600));
},
after() {
console.timeEnd('testTime');
fs.unlinkSync(configFile);
},
abortOnStderr(data) {
Expand Down

0 comments on commit 964dfd4

Please sign in to comment.