Skip to content

Commit

Permalink
exit on submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Crisci, Fabio | Piuccio | TRVDD committed Sep 12, 2013
1 parent e515da7 commit 6851078
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/server/administration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 19 additions & 9 deletions lib/server/instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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) {
Expand All @@ -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);
}
});
}
Expand All @@ -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);
});
}
});
});
Expand All @@ -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) {
Expand Down
16 changes: 13 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}

0 comments on commit 6851078

Please sign in to comment.