Use a separate docker nginx image to serve test files

This obviates the need to start/stop a web-server for each package test and paves the way to something we'll need later.
This commit is contained in:
Timothy Farrell 2018-07-25 03:51:02 -05:00
parent ee79695d04
commit 79b9cd734b
4 changed files with 20 additions and 53 deletions

View File

@ -1,4 +1,11 @@
#!/bin/sh
docker-compose up --detach test-web
nodejs node_modules/lerna/bin/lerna.js run pre-commit \
&& npm run check_code_format
EXIT=$?
docker-compose stop test-web
exit $EXIT

View File

@ -3,8 +3,6 @@
const fs = require('fs');
const path = require('path');
const url = require('url');
const http = require('http');
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
@ -12,12 +10,11 @@ const CDP = require('chrome-remote-interface');
const SPEC_DIR = 'spec';
const TEST_FILENAME = '.spec_runner.html';
const HOST = '127.0.0.1';
// Randomize the port so we can just run again if there's a stuck process.
const PORT = Math.floor(Math.random() * 40000) + 1025;
const PORT = 8090;
const DEBUG =
typeof process.env['DEBUG'] === 'string'
? process.env['DEBUG'].toLower() == 'true'
? process.env['DEBUG'].toLowerCase() == 'true'
: !!process.env['DEBUG'];
const PORTFOLIO_DIR = path.normalize(path.join(__dirname, '..'));
const JASMINE_DIR = path.relative(
@ -38,7 +35,6 @@ const items = fs.readdirSync(packageRoot);
let chrome, protocol;
writeSpecRunner();
startStaticServer();
runTestsInChrome();
// ----------------------
@ -85,49 +81,6 @@ function writeSpecRunner() {
fs.writeFileSync(testPath, runnerText);
}
function startStaticServer() {
// An adaptation of https://gist.github.com/ryanflorence/701407
const server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(PORTFOLIO_DIR, uri);
fs.access(filename, fs.constants.R_OK, function(err) {
if (err) {
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.write('404 Not Found\n');
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, 'binary', function(err, file) {
if (err) {
response.writeHead(500, { 'Content-Type': 'text/plain' });
response.write(err + '\n');
response.end();
return;
}
response.writeHead(200, {
'Content-Type': mime(filename)
});
response.write(file, 'binary');
response.end();
});
});
});
server.on('error', e => {
if (e.code === 'EADDRINUSE') {
setTimeout(() => {
server.close();
server.listen(PORT, HOST);
}, 500);
}
});
server.listen(parseInt(PORT, 10));
}
async function runTestsInChrome() {
chrome = await launchChrome();
protocol = await CDP({

View File

@ -1,6 +1,12 @@
version: '2'
services:
test-web:
image: nginx:stable-alpine
ports:
- 8090:80
volumes:
- ./:/usr/share/nginx/html:ro
cloud9:
image: sapk/cloud9:latest
restart: unless-stopped

View File

@ -7,11 +7,12 @@
},
"scripts": {
"check_code_format": "nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --list-different \"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\"",
"format_code": "nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --write \"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\""
"format_code": "nodejs node_modules/prettier/bin-prettier.js --config ./prettier.config.js --write \"{.,{packages,bin}/**/!(dist)}/*.{js,json,md}\"",
"pre-commit": "sh ./bin/pre-commit.sh"
},
"husky": {
"hooks": {
"pre-commit": "sh ./bin/pre-commit.sh"
"pre-commit": "npm run pre-commit"
}
}
}