From f4ccbfcff38d1bc0c9e6b6229c501ed58b97f995 Mon Sep 17 00:00:00 2001 From: David Manthey Date: Fri, 4 May 2018 10:40:58 -0400 Subject: [PATCH] Refactor the server script to serve dist, website, and _build. This also consolidates the `tests` and `testing` directories under the `tests` directory, moving the test runners to `tests\runners`. --- CMakeLists.txt | 4 +- docs/conf.py | 2 +- package-lock.json | 21 +++ package.json | 5 +- testing/test-runners/server.js | 177 ------------------ .../runners}/baseline_images.py | 0 .../runners}/geojs_test_runner.py.in | 0 tests/runners/server.js | 70 +++++++ 8 files changed, 97 insertions(+), 182 deletions(-) delete mode 100644 testing/test-runners/server.js rename {testing/test-runners => tests/runners}/baseline_images.py (100%) rename {testing/test-runners => tests/runners}/geojs_test_runner.py.in (100%) create mode 100644 tests/runners/server.js diff --git a/CMakeLists.txt b/CMakeLists.txt index ca6684df73..bd4663be76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ function(add_geojs_test test_name) set_property(TEST "notes-report" APPEND PROPERTY DEPENDS "${test_name}") endfunction() -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/testing/test-runners/geojs_test_runner.py.in +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/tests/runners/geojs_test_runner.py.in ${CMAKE_CURRENT_BINARY_DIR}/test/geojs_test_runner.py ) @@ -163,7 +163,7 @@ if(WEBGLHEADLESS_TESTS) set_property(TEST "headed" APPEND PROPERTY DEPENDS "build_examples") configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/testing/test-runners/baseline_images.py" + "${CMAKE_CURRENT_SOURCE_DIR}/tests/runners/baseline_images.py" "${CMAKE_CURRENT_BINARY_DIR}/test/baseline_images.py" COPYONLY ) diff --git a/docs/conf.py b/docs/conf.py index c2ab344f0c..89cf41bf81 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -26,7 +26,7 @@ # documentation root, use os.path.abspath to make it absolute, like shown here. basepath = os.path.dirname(os.path.abspath(__file__)) toppath = os.path.join(basepath, '..') -for fname in glob(os.path.join(toppath, 'testing', 'test-runners', '*.py*')): +for fname in glob(os.path.join(toppath, 'tests', 'runners', '*.py*')): basename = os.path.split(fname)[-1].replace('.in', '') with open(fname, 'r') as f: with open(os.path.join(basepath, basename), 'w') as g: diff --git a/package-lock.json b/package-lock.json index 486f679783..65114b8c9f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1673,6 +1673,12 @@ "integrity": "sha1-R2iMuZu2gE8OBtPnY7HDLlfY5rY=", "dev": true }, + "batch": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/batch/-/batch-0.6.1.tgz", + "integrity": "sha1-3DQxT05nkxgJP8dgJyUl+UvyXBY=", + "dev": true + }, "bcrypt-pbkdf": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/bcrypt-pbkdf/-/bcrypt-pbkdf-1.0.2.tgz", @@ -12683,6 +12689,21 @@ "integrity": "sha512-Ga8c8NjAAp46Br4+0oZ2WxJCwIzwP60Gq1YPgU+39PiTVxyed/iKE/zyZI6+UlVYH5Q4PaQdHhcegIFPZTUfoQ==", "dev": true }, + "serve-index": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/serve-index/-/serve-index-1.9.1.tgz", + "integrity": "sha1-03aNabHn2C5c4FD/9bRTvqEqkjk=", + "dev": true, + "requires": { + "accepts": "1.3.5", + "batch": "0.6.1", + "debug": "2.6.9", + "escape-html": "1.0.3", + "http-errors": "1.6.3", + "mime-types": "2.1.18", + "parseurl": "1.3.2" + } + }, "serve-static": { "version": "1.13.2", "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.13.2.tgz", diff --git a/package.json b/package.json index 3267f70ee1..cd2fa4365b 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "pug-loader": "^2.4.0", "raw-body": "^2.1.6", "resemblejs": "^2.9.0", + "serve-index": "^1.9.1", "sinon": "^1.17.3", "string-replace-webpack-plugin": "^0.1.3", "style-loader": "^0.21.0", @@ -113,8 +114,8 @@ "codecov": "codecov", "combine-coverage": "istanbul-combine -d dist/cobertura -r cobertura 'dist/coverage/json/**/coverage-final.json'", "examples": "webpack-dev-server --config webpack-examples.config.js --host ${HOST-127.0.0.1} --port ${PORT-8082} --content-base dist/", - "start-test": "node examples/build.js; forever start ./testing/test-runners/server.js", - "stop-test": "forever stop ./testing/test-runners/server.js", + "start-test": "node examples/build.js; forever start ./tests/runners/server.js", + "stop-test": "forever stop ./tests/runners/server.js", "docs": "jsdoc --pedantic -d dist/apidocs -r src -c jsdoc.conf.json", "website": "cd website && npx hexo server", "setup-website": "cd website && npm install", diff --git a/testing/test-runners/server.js b/testing/test-runners/server.js deleted file mode 100644 index 114e2def98..0000000000 --- a/testing/test-runners/server.js +++ /dev/null @@ -1,177 +0,0 @@ -/* global require, module */ -'use strict'; - -var express = require('express'), - bodyParser = require('body-parser'), - xml = require('xmlbuilder'), - fs = require('fs'), - app = express(), - cov = {}, - notes = {}; - -/** - * Combine coverage results. - */ -function combine(files) { - var f, l; - for (f in files) { - if (files.hasOwnProperty(f)) { - if (!cov.hasOwnProperty(f)) { - cov[f] = {}; - } - - for (l in files[f]) { - if (files[f].hasOwnProperty(l)) { - if (!cov[f].hasOwnProperty(l)) { - cov[f][l] = 0; - } - cov[f][l] += files[f][l]; - } - } - } - } -} - -/** - * Get total coverage statistics - */ -function calc_stats() { - var f, l, stats; - - stats = { - totalSloc: 0, - totalHits: 0, - files: {} - }; - for (f in cov) { - if (cov.hasOwnProperty(f)) { - var sloc = 0, hits = 0; - - for (l in cov[f]) { - if (cov[f].hasOwnProperty(l)) { - sloc += 1; - hits += cov[f][l] && 1; - - } - } - - stats.totalSloc += sloc; - stats.totalHits += hits; - stats.files[f] = { - sloc: sloc, - hits: hits - }; - } - } - return stats; -} - -/** - * Convert to cobertura xml format - */ -function cobertura() { - var stats, totalPct, root, pkg, clss, cls, f, l, ln, lineRate; - - function _percent(num, den) { - if (den <= 0) { - return 1; - } else { - return num / den; - } - } - - stats = calc_stats(); - totalPct = _percent(stats.totalHits, stats.totalSloc); - - root = xml.create('coverage'); - root.a({ - 'branch-rate': 0, - 'line-rate': totalPct, - 'version': 3.6, - 'timestamp': (new Date() / 1000).toFixed() - }); - - pkg = root.ele('packages').ele('package'); - pkg.a({ - 'branch-rate': 0, - 'complexity': 0, - 'line-rate': totalPct, - 'name': '' - }); - - clss = pkg.ele('classes'); - for (f in cov) { - if (cov.hasOwnProperty(f)) { - lineRate = _percent(stats.files[f].hits, stats.files[f].sloc); - cls = clss.ele('class'); - cls.a({ - 'branch-rate': 0, - 'complexity': 0, - 'line-rate': lineRate, - 'filename': f.replace(/^src\/vgl/, 'vgl/src'), - 'name': f - }); - - ln = cls.ele('lines'); - for (l in cov[f]) { - if (cov[f].hasOwnProperty(l)) { - ln.ele('line').a({ - number: l, - hits: cov[f][l] - }); - } - } - } - } - - return root.toString(); -} - -app.use(bodyParser.json()); -app.use(express.static('dist')); - -app.put('/coverage', function (req, res, next) { - combine(req.body.files); - res.send('{}'); - next(); -}); - -app.delete('/coverage', function (req, res, next) { - cov = {}; - res.send('{}'); - next(); -}); - -app.get('/coverage', function (req, res, next) { - res.send(cobertura()); - next(); -}); - -app.post('/coverage', function (req, res, next) { - fs.writeFile(req.query.path, cobertura()); - res.send('{}'); - next(); -}); - -app.delete('/notes', function (req, res, next) { - notes = {}; - res.send('{}'); - next(); -}); - -app.put('/notes', function (req, res, next) { - notes[req.query.key] = req.body; - res.send('{}'); - next(); -}); - -app.post('/notes', function (req, res, next) { - fs.writeFile(req.query.path, JSON.stringify(notes)); - next(); -}); - -app.listen(30100, function () { - console.log('Server listening on 30100'); -}); - -module.exports = app; diff --git a/testing/test-runners/baseline_images.py b/tests/runners/baseline_images.py similarity index 100% rename from testing/test-runners/baseline_images.py rename to tests/runners/baseline_images.py diff --git a/testing/test-runners/geojs_test_runner.py.in b/tests/runners/geojs_test_runner.py.in similarity index 100% rename from testing/test-runners/geojs_test_runner.py.in rename to tests/runners/geojs_test_runner.py.in diff --git a/tests/runners/server.js b/tests/runners/server.js new file mode 100644 index 0000000000..599e2cd4ee --- /dev/null +++ b/tests/runners/server.js @@ -0,0 +1,70 @@ +/* global require, module */ +'use strict'; + +var serveIndex; +try { + serveIndex = require('serve-index'); +} catch (err) { } +var express = require('express'), + app = express(), + help = false, + skip = 2, + port = 30100, + build = false, + dist = false, + website = false; + +process.argv.forEach(function (val, idx) { + if (skip) { + skip -= 1; + return; + } + if (val === '--build' || val === '-b') { + build = true; + } else if (val === '--dist' || val === '-d') { + dist = true; + } else if ((val === '--port' || val === '-p') && idx + 1 < process.argv.length) { + port = parseInt(process.argv[idx + 1], 10); + skip = 1; + } else if (val.substr(0, 7) === '--port=') { + port = parseInt(val.substr(7), 10); + } else if (val === '--website' || val === '--web' || val === '-w') { + website = true; + } else { + help = true; + } +}); + +if (help) { + console.error('Serve the dist, website, and _build directories. Options:\n' + + '--build : serve _build directory as build/\n' + + '--dist : serve distribution directory (default)\n' + + '--port (port) : default 30100\n' + + '--website : serve website directory (if this and dist are specified,\n' + + ' dist is located as dist/)\n'); + process.exit(1); +} + +if (!website) { + if (!build || dist) { + app.use(express.static('dist')); + } +} else { + if (dist) { + app.use('/dist', express.static('dist')); + } + app.use(express.static('website/public')); +} +if (build) { + if (serveIndex) { + app.use('/build', express.static('_build'), serveIndex('_build', {icons: true, view: 'details'})); + } else { + app.use('/build', express.static('_build')); + } +} + +app.listen(port, function () { + console.log('Server listening on ' + port); +}); + +module.exports = app;