Skip to content

Commit

Permalink
API to shut down the server
Browse files Browse the repository at this point in the history
In some CI environment it might be useful to shut down the server with a network request.

This is achieved with the request 'node-coverage-please-exit'

Fix #22
  • Loading branch information
Crisci, Fabio | Piuccio | TRVDD committed Sep 12, 2013
1 parent 4c607af commit 0763af8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ 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.
* `--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.
* `--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. If you want to collect more coverage reports but still be able to shut down the server when tests are done you can submit a request to '/node-coverage-please-exit'.
* `-v` or `--verbose` Enable more verbose logging information. Default `false`.

By default function coverage is disabled, to enable it you can run
Expand Down
23 changes: 19 additions & 4 deletions lib/server/instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions,
}

var headers = {
"Content-Type" : "text/javascript"
"Content-Type": "text/javascript",
"Connection": "close"
};
if (coverageOptions["exit-on-submit"]) {
headers["Connection"] = "close";
}

if (instrumentedCode) {
res.send(instrumentedCode.clientCode, headers);
Expand All @@ -58,14 +56,17 @@ exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions,
}
});

var whileStoringCoverage = 0;
app.post("/node-coverage-store", function (req, res) {
whileStoringCoverage += 1;
if (req.headers["content-type"] === "application/x-www-form-urlencoded") {
// Using a real form submit the coverage reports happens to be a string
req.body = JSON.parse(req.body.coverage);
}
mergeCodeFromMemory(req.body.code, sourceCodeCache.highlight);

common.saveCoverage(req.body, adminRoot, function (error) {
whileStoringCoverage -= 1;
if (error) {
res.send(error, 500);
} else {
Expand All @@ -80,6 +81,20 @@ exports.start = function (docRoot, port, adminRoot, adminPort, coverageOptions,
});
});

app.all("/node-coverage-please-exit", function (req, res) {
function exitWhenDone () {
if (whileStoringCoverage) {
setTimeout(exitWhenDone, 100);
} else {
res.send(200, {"Connection": "close"});
process.nextTick(function () {
server.close(onClose);
});
}
}
exitWhenDone();
});

app.post("/*", function (req, res, next) {
res.sendfile(docRoot + req.path);
});
Expand Down

0 comments on commit 0763af8

Please sign in to comment.