From 79b9cd734b5b1cfdc95f7e8729974a564f041d15 Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Wed, 25 Jul 2018 03:51:02 -0500 Subject: [PATCH] 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. --- bin/pre-commit.sh | 11 ++++++++-- bin/runTests.js | 51 ++-------------------------------------------- docker-compose.yml | 6 ++++++ package.json | 5 +++-- 4 files changed, 20 insertions(+), 53 deletions(-) diff --git a/bin/pre-commit.sh b/bin/pre-commit.sh index 79f46b5..38b785d 100644 --- a/bin/pre-commit.sh +++ b/bin/pre-commit.sh @@ -1,4 +1,11 @@ #!/bin/sh -nodejs node_modules/lerna/bin/lerna.js run pre-commit\ -&& npm run check_code_format \ No newline at end of file +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 diff --git a/bin/runTests.js b/bin/runTests.js index 06a9a7b..b9321e5 100755 --- a/bin/runTests.js +++ b/bin/runTests.js @@ -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({ diff --git a/docker-compose.yml b/docker-compose.yml index 17e9bbc..56c0de7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/package.json b/package.json index 50e49c7..2709c90 100644 --- a/package.json +++ b/package.json @@ -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" } } }