diff --git a/README.md b/README.md index 40d5700..8e11055 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,8 @@ It's also possible to specify a report name from the `submit` function * `--session`, `--no-session` Enable or disable session storage for information not strictly needed by the browser. By default it's enabled. Disabling this means that more code is sent to and from the client. * `-i` or `--ignore` Ignore file or folder. This file/folder won't be instrumented. Path is relative to document root. * `--proxy` Proxy mode. You can use node-coverage to instrument files on a differnt host. -* `-v` or `--verbose` Enable more verbose logging information. Default `false` +* `--exit-on-submit` The default behavior is to keep the server running in order to collect multiple reports. By enabling this options the server will automatically shut down when a coverage report is received. This is useful for some continous integration environment. +* `-v` or `--verbose` Enable more verbose logging information. Default `false`. By default function coverage is disabled, to enable it you can run diff --git a/lib/server/administration.js b/lib/server/administration.js index 9609162..4bef7e0 100644 --- a/lib/server/administration.js +++ b/lib/server/administration.js @@ -240,7 +240,7 @@ exports.start = function (docRoot, port, adminRoot, adminPort) { docRoot : docRoot, adminRoot: adminRoot }); - app.listen(adminPort); + return app.listen(adminPort); }; function sortReport (reports, what, how) { diff --git a/lib/server/instrumentation.js b/lib/server/instrumentation.js index 5618c2b..f8aae84 100644 --- a/lib/server/instrumentation.js +++ b/lib/server/instrumentation.js @@ -10,7 +10,7 @@ var sourceCodeCache = { highlight : {} }; -exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions) { +exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions, onClose) { var app = express.createServer(); app.use(express.bodyParser()); @@ -27,10 +27,15 @@ exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions) console.log("Requesting", url); } + var headers = { + "Content-Type" : "text/javascript" + }; + if (coverageOptions["exit-on-submit"]) { + headers["Connection"] = "close"; + } + if (instrumentedCode) { - res.send(instrumentedCode.clientCode, { - "Content-Type" : "text/javascript" - }); + res.send(instrumentedCode.clientCode, headers); } else { fs.readFile(docRoot + url, "utf-8", function (err, content) { if (err) { @@ -47,9 +52,7 @@ exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions) req.session.highlightInMemory = true; } - res.send(code.clientCode, { - "Content-Type" : "text/javascript" - }); + res.send(code.clientCode, headers); } }); } @@ -62,7 +65,13 @@ exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions) if (error) { res.send(error, 500); } else { - res.send(200); + res.send(200, coverageOptions["exit-on-submit"] ? {"Connection": "close"} : null); + } + + if (coverageOptions["exit-on-submit"]) { + process.nextTick(function () { + server.close(onClose); + }); } }); }); @@ -73,7 +82,8 @@ exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions) app.use(express.static(docRoot)); - app.listen(port); + var server = app.listen(port); + return server; }; function mergeCodeFromMemory (inside, from) { diff --git a/server.js b/server.js index a2b1903..94f523c 100644 --- a/server.js +++ b/server.js @@ -34,9 +34,13 @@ var argv = require("optimist") .default("proxy", false) .describe("proxy", "Start the instrumentation server in HTTP proxy mode on port specified by -p.") .boolean("v").alias("v", "verbose").default("v", false) + .boolean("exit-on-submit") + .default("exit-on-submit", false) + .describe("exit-on-submit", "Close the server and exit the process after a coverage report is submitted.") .argv; +var admin_server; if (argv.h) { require("optimist").showHelp(); } else { @@ -75,15 +79,16 @@ if (argv.h) { return path; } }), - "verbose" : argv.v + "verbose" : argv.v, + "exit-on-submit" : argv["exit-on-submit"] }; - require("./lib/server/instrumentation").start(argv.d, argv.p, argv.r, argv.a, config); + require("./lib/server/instrumentation").start(argv.d, argv.p, argv.r, argv.a, config, onClose); console.log("Starting server on port", argv.p); } /* Admin server */ - require("./lib/server/administration").start(argv.d, argv.p, argv.r, argv.a); + admin_server = require("./lib/server/administration").start(argv.d, argv.p, argv.r, argv.a); console.log("Starting administration interface on port", argv.a); if (argv.v) { @@ -92,4 +97,9 @@ if (argv.h) { } catch (ex) { console.error("Please specify a valid report directory", ex); } +} + +// Called when the instrumentation server closes +function onClose () { + admin_server.close(); } \ No newline at end of file