Merge pull request #5715 from ahocevar/smarter-check-example
Create a check-example task for smarter example checking with PhantomJS
This commit is contained in:
2
Makefile
2
Makefile
@@ -184,7 +184,7 @@ build/timestamps/check-%-timestamp: $(BUILD_HOSTED)/examples/%.html \
|
||||
$(BUILD_HOSTED)/build/ol.js \
|
||||
$(BUILD_HOSTED)/css/ol.css
|
||||
@mkdir -p $(@D)
|
||||
./node_modules/.bin/serve-files 8000 . & pid=$$! && ./node_modules/.bin/phantomjs --ssl-protocol=any --ignore-ssl-errors=true bin/check-example.js http://localhost:8000/$< && kill -9 $$pid
|
||||
node tasks/check-example.js $<
|
||||
@touch $@
|
||||
|
||||
build/compiled-examples/all.js: $(EXAMPLES_JS)
|
||||
|
||||
65
tasks/check-example.js
Normal file
65
tasks/check-example.js
Normal file
@@ -0,0 +1,65 @@
|
||||
/*eslint-env es6*/
|
||||
|
||||
const http = require('http');
|
||||
const path = require('path');
|
||||
const serveFiles = require('serve-files');
|
||||
const spawn = require('child_process').spawn;
|
||||
|
||||
if (!process.argv[2]) {
|
||||
process.stdout.write(`USAGE: node ${path.basename(module.filename)} [example_path]\n`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const root = process.cwd();
|
||||
const port = 8000;
|
||||
const host = null;
|
||||
const examplePath = process.argv[2];
|
||||
const phantomPath = require('phantomjs-prebuilt').path;
|
||||
|
||||
const server = http.createServer(serveFiles.createFileResponseHandler({
|
||||
documentRoot : root,
|
||||
followSymbolicLinks: false,
|
||||
cacheTimeInSeconds : 3600
|
||||
}));
|
||||
|
||||
server.listen(port, host, null, function() {
|
||||
const childProcess = spawn(phantomPath, ['--ssl-protocol=any', '--ignore-ssl-errors=true', path.join(__dirname, '..', 'bin', 'check-example.js'), 'http://localhost:8000/' + examplePath]);
|
||||
childProcess.stdout.pipe(process.stdout);
|
||||
childProcess.stderr.pipe(process.stderr);
|
||||
process.stdin.pipe(childProcess.stdin);
|
||||
|
||||
childProcess.on('error', function(err) {
|
||||
process.stderr.write(`Error executing phantom on ${phantomPath}\n`);
|
||||
process.stderr.write(err.stack + '\n');
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
childProcess.on('exit', function(code) {
|
||||
process.exit(code);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// Keep track of connections, to enforce killing them when server must be stopped.
|
||||
var connections = {};
|
||||
server.on('connection', function(socket) {
|
||||
socket._cid = process.hrtime();
|
||||
connections[socket._cid] = socket;
|
||||
|
||||
socket.on('end', function() {
|
||||
delete connections[this._cid];
|
||||
});
|
||||
});
|
||||
|
||||
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
|
||||
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'].forEach(signal => {
|
||||
process.once(signal, () => {
|
||||
process.stdout.write(`Got ${signal}, stopping...\n`),
|
||||
server.close(() => {
|
||||
process.stdout.write('Stopped.\n');
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
Object.keys(connections).forEach(cid => connections[cid].destroy());
|
||||
});
|
||||
});
|
||||
@@ -109,6 +109,14 @@ Called internally to parse the library for annotations and write out a `build/in
|
||||
|
||||
Builds examples and the example index.
|
||||
|
||||
## `check-example.js`
|
||||
|
||||
Runs an example in PhantomJS and returns an exit code != 0 after printing a stack trace when something is wrong with the example.
|
||||
|
||||
To check the `simple.html` example when on master, first run the `build-examples.js` task, then invoke
|
||||
|
||||
node tasks/check-example.js build/hosted/master/simple.html
|
||||
|
||||
|
||||
## `serve.js`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user