Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Mar 29, 2020
1 parent af6a284 commit b906590
Show file tree
Hide file tree
Showing 23 changed files with 203 additions and 89 deletions.
80 changes: 38 additions & 42 deletions cli/run/watch.ts
Expand Up @@ -32,50 +32,46 @@ export default async function watch(command: any) {
}

if (configFile) {
if (configFile.startsWith('node:')) {
({ options: configs, warnings } = await loadAndParseConfigFile(configFile, command));
} else {
let reloadingConfig = false;
let aborted = false;
let configFileData: string | null = null;

configWatcher = fs.watch(configFile, (event: string) => {
if (event === 'change') reloadConfigFile();
});

await reloadConfigFile();

async function reloadConfigFile() {
try {
const newConfigFileData = fs.readFileSync(configFile!, 'utf-8');
if (newConfigFileData === configFileData) {
return;
}
if (reloadingConfig) {
aborted = true;
return;
}
if (configFileData) {
stderr(`\nReloading updated config...`);
}
configFileData = newConfigFileData;
reloadingConfig = true;
({ options: configs, warnings } = await loadAndParseConfigFile(configFile!, command));
reloadingConfig = false;
if (aborted) {
aborted = false;
reloadConfigFile();
} else {
if (watcher) {
watcher.close();
}
start(configs);
let reloadingConfig = false;
let aborted = false;
let configFileData: string | null = null;

configWatcher = fs.watch(configFile, (event: string) => {
if (event === 'change') reloadConfigFile();
});

await reloadConfigFile();

async function reloadConfigFile() {
try {
const newConfigFileData = fs.readFileSync(configFile!, 'utf-8');
if (newConfigFileData === configFileData) {
return;
}
if (reloadingConfig) {
aborted = true;
return;
}
if (configFileData) {
stderr(`\nReloading updated config...`);
}
configFileData = newConfigFileData;
reloadingConfig = true;
({ options: configs, warnings } = await loadAndParseConfigFile(configFile!, command));
reloadingConfig = false;
if (aborted) {
aborted = false;
reloadConfigFile();
} else {
if (watcher) {
watcher.close();
}
} catch (err) {
configs = [];
reloadingConfig = false;
handleError(err, true);
start(configs);
}
} catch (err) {
configs = [];
reloadingConfig = false;
handleError(err, true);
}
}
} else {
Expand Down
22 changes: 22 additions & 0 deletions test/cli/samples/watch/bundle-error/_config.js
@@ -0,0 +1,22 @@
const fs = require('fs');
const path = require('path');

let mainFile;

module.exports = {
description: 'recovers from errors during bundling',
command: 'rollup -cw',
before() {
mainFile = path.resolve(__dirname, 'main.js');
fs.writeFileSync(mainFile, '<=>');
},
abortOnStderr(data) {
if (data.includes('Error: Unexpected token')) {
setTimeout(() => fs.writeFileSync(mainFile, 'export default 42;'), 50);
return false;
}
if (data.includes('created _actual')) {
return true;
}
},
};
3 changes: 3 additions & 0 deletions test/cli/samples/watch/bundle-error/_expected/main.js
@@ -0,0 +1,3 @@
var main = 42;

export default main;
1 change: 1 addition & 0 deletions test/cli/samples/watch/bundle-error/main.js
@@ -0,0 +1 @@
export default 42;
7 changes: 7 additions & 0 deletions test/cli/samples/watch/bundle-error/rollup.config.js
@@ -0,0 +1,7 @@
export default {
input: 'main.js',
output: {
dir: "_actual",
format: "es"
}
};
9 changes: 9 additions & 0 deletions test/cli/samples/watch/no-config-file/_config.js
@@ -0,0 +1,9 @@
module.exports = {
description: 'watches without a config file',
command: 'rollup main.js --watch --format es --file _actual/main.js',
abortOnStderr(data) {
if (data.includes('created _actual/main.js')) {
return true;
}
},
};
@@ -1,3 +1,3 @@
const foo = 47;
const foo = 42;

export { foo };
1 change: 1 addition & 0 deletions test/cli/samples/watch/no-config-file/main.js
@@ -0,0 +1 @@
export const foo = 42;
9 changes: 9 additions & 0 deletions test/cli/samples/watch/node-config-file/_config.js
@@ -0,0 +1,9 @@
module.exports = {
description: 'watches using a node_modules config files',
command: 'rollup --watch --config node:custom',
abortOnStderr(data) {
if (data.includes('created _actual/main.js')) {
return true;
}
},
};
3 changes: 3 additions & 0 deletions test/cli/samples/watch/node-config-file/_expected/main.js
@@ -0,0 +1,3 @@
const foo = 42;

export { foo };
1 change: 1 addition & 0 deletions test/cli/samples/watch/node-config-file/main.js
@@ -0,0 +1 @@
export const foo = 42;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions test/cli/samples/watch/watch-config-early-update/_config.js
@@ -0,0 +1,73 @@
const fs = require('fs');
const path = require('path');

let configFile;
let messageFile;
let reloadTriggered = false;

module.exports = {
description: 'immediately reloads the config file if a change happens while it is parsed',
command: 'rollup -cw',
before() {
configFile = path.resolve(__dirname, 'rollup.config.js');
messageFile = path.resolve(__dirname, '_actual', 'message.txt');
fs.mkdirSync(path.resolve(__dirname, '_actual'));
fs.writeFileSync(messageFile, 'initial');
fs.writeFileSync(
configFile,
`
import path from 'path';
import fs from 'fs';
const messageFile = path.resolve(__dirname, '_actual', 'message.txt');
export default new Promise(resolve => {
fs.writeFileSync(messageFile, 'loading');
const watcher = fs.watch(messageFile, event => {
if (event === 'change') {
const content = fs.readFileSync(messageFile, 'utf8');
if (content === 'loaded') {
watcher.close();
fs.writeFileSync(messageFile, 'resolved');
resolve({
input: {output1: "main.js"},
output: {
dir: "_actual",
format: "es"
}
});
}
}
});
});
`
);
const watcher = fs.watch(messageFile, (event) => {
if (event === 'change') {
const content = fs.readFileSync(messageFile, 'utf8');
if (content === 'loading') {
watcher.close();
fs.writeFileSync(
configFile,
`
export default {
input: {output2: "main.js"},
output: {
dir: "_actual",
format: "es"
}
};
`
);
fs.writeFileSync(messageFile, 'loaded');
}
}
});
},
abortOnStderr(data) {
if (reloadTriggered && data.includes('created _actual')) {
return true;
} else if (data.includes('Reloading updated config')) {
reloadTriggered = true;
return false;
}
},
};
@@ -0,0 +1 @@
resolved
@@ -0,0 +1,3 @@
const foo = 42;

export { foo };
1 change: 1 addition & 0 deletions test/cli/samples/watch/watch-config-early-update/main.js
@@ -0,0 +1 @@
export const foo = 42;
@@ -0,0 +1,9 @@

export default {
input: {output2: "main.js"},
output: {
dir: "_actual",
format: "es"
}
};

51 changes: 8 additions & 43 deletions test/cli/samples/watch/watch-config/_config.js
Expand Up @@ -4,6 +4,9 @@ const path = require('path');
let configFile;
let currentlyBundling;

const updateConfigDelayed = (content) =>
setTimeout(() => fs.writeFileSync(configFile, content), 100);

module.exports = {
description: 'watches the config file',
command: 'rollup -cw',
Expand All @@ -17,8 +20,7 @@ module.exports = {
return false;
}
if (data.includes('Config contains initial errors')) {
fs.writeFileSync(
configFile,
updateConfigDelayed(
'export default {\n' +
'\tinput: "main1.js",\n' +
'\toutput: {\n' +
Expand All @@ -30,8 +32,7 @@ module.exports = {
return false;
}
if (data.includes('Config contains further errors')) {
fs.writeFileSync(
configFile,
updateConfigDelayed(
'export default {\n' +
'\tinput: ["main2.js", "main3.js"],\n' +
'\toutput: {\n' +
Expand All @@ -45,13 +46,12 @@ module.exports = {
if (data.includes('created _actual')) {
switch (currentlyBundling) {
case 'main1.js':
fs.writeFileSync(configFile, 'throw new Error("Config contains further errors");');
updateConfigDelayed('throw new Error("Config contains further errors");');
return false;
case 'main2.js, main3.js':
fs.writeFileSync(
configFile,
updateConfigDelayed(
'export default {\n' +
'\tinput: {output1: "main4.js"},\n' +
'\tinput: {output: "main4.js"},\n' +
'\toutput: {\n' +
'\t\tdir: "_actual",\n' +
'\t\tformat: "es"\n' +
Expand All @@ -60,43 +60,8 @@ module.exports = {
);
return false;
case 'main4.js':
currentlyBundling = 'preparing main5.js';
fs.writeFileSync(
configFile,
'export default {\n' +
'\tinput: {output2: "main5.js"},\n' +
'\toutput: {\n' +
'\t\tdir: "_actual",\n' +
'\t\tformat: "es"\n' +
'\t}\n' +
'};'
);
return false;
case 'main6.js':
fs.writeFileSync(
configFile,
'export default {\n' +
'\tinput: {output3: "main6.js"},\n' +
'\toutput: {\n' +
'\t\tdir: "_actual",\n' +
'\t\tformat: "es"\n' +
'\t}\n' +
'};'
);
return true;
}
}
if (data.includes('Reloading updated config') && currentlyBundling === 'preparing main5.js') {
fs.writeFileSync(
configFile,
'export default {\n' +
'\tinput: {output3: "main6.js"},\n' +
'\toutput: {\n' +
'\t\tdir: "_actual",\n' +
'\t\tformat: "es"\n' +
'\t}\n' +
'};'
);
}
},
};
1 change: 0 additions & 1 deletion test/cli/samples/watch/watch-config/main5.js

This file was deleted.

1 change: 0 additions & 1 deletion test/cli/samples/watch/watch-config/main6.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/cli/samples/watch/watch-config/rollup.config.js
@@ -1,5 +1,5 @@
export default {
input: {output3: "main6.js"},
input: {output: "main4.js"},
output: {
dir: "_actual",
format: "es"
Expand Down

0 comments on commit b906590

Please sign in to comment.