Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Add resultsPath URL param to SpecRunner to specify file path for JSON…
Browse files Browse the repository at this point in the history
… output. Ignore case in UnitTestReporter spec filter.
  • Loading branch information
jasonsanjose committed Oct 25, 2012
1 parent 940f990 commit 58b64c8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
41 changes: 40 additions & 1 deletion test/SpecRunner.js
Expand Up @@ -46,6 +46,7 @@ define(function (require, exports, module) {
Async = require("utils/Async"),
FileUtils = require("file/FileUtils"),
Menus = require("command/Menus"),
NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem,
UrlParams = require("utils/UrlParams").UrlParams,
UnitTestReporter = require("test/UnitTestReporter").UnitTestReporter,
BootstrapReporterView = require("test/BootstrapReporterView").BootstrapReporterView;
Expand Down Expand Up @@ -109,6 +110,38 @@ define(function (require, exports, module) {
jasmine.getEnv().execute();
}

/**
* Listener for UnitTestReporter "runnerEnd" event. Attached only if
* "resultsPath" URL parameter exists. Does not overwrite existing file.
* Writes UnitTestReporter spec results as formatted JSON.
* @param {!$.Event} event
* @param {!UnitTestReporter} reporter
*/
function _runnerEndHandler(event, reporter) {
var resultsPath = params.get("resultsPath"),
json = reporter.toJSON(),
deferred = new $.Deferred();

// check if the file already exists
brackets.fs.stat(resultsPath, function (err, stat) {
if (err === brackets.fs.ERR_NOT_FOUND) {
// file not found, write the new file with JSON content
brackets.fs.writeFile(resultsPath, json, NativeFileSystem._FSEncodings.UTF8, function (err) {
if (err) {
deferred.reject();
} else {
deferred.resolve();
}
});
} else {
// file exists, do not overwrite
deferred.reject();
}
});

deferred.always(function () { window.close(); });
}

function init() {
suite = params.get("suite") || localStorage.getItem("SpecRunner.suite") || "UnitTestSuite";

Expand Down Expand Up @@ -182,7 +215,13 @@ define(function (require, exports, module) {

// Create the reporter, which is really a model class that just gathers
// spec and performance data.
reporter = new UnitTestReporter(jasmineEnv, topLevelFilter);
reporter = new UnitTestReporter(jasmineEnv, topLevelFilter, params.get("spec"));

// Optionally emit JSON for automated runs
if (params.get("resultsPath")) {
$(reporter).on("runnerEnd", _runnerEndHandler);
}

jasmineEnv.addReporter(reporter);

// Create the view that displays the data from the reporter. (Usually in
Expand Down
20 changes: 10 additions & 10 deletions test/UnitTestReporter.js
Expand Up @@ -78,13 +78,12 @@ define(function (require, exports, module) {
*
* @param {!Object} env The Jasmine environment we're running in.
* @param {Function} filter The filter being used to determine whether a given spec is run or not.
* @param {string} activeSuite The suite currently selected in the URL params, or null if all are being run.
*/
function UnitTestReporter(env, filter) {
var self = this,
params = new UrlParams();
function UnitTestReporter(env, filter, activeSuite) {
var self = this;

params.parse();
this.activeSuite = params.get("spec");
this.activeSuite = activeSuite;

this.runInfo = {
app: brackets.metadata.name,
Expand Down Expand Up @@ -149,25 +148,26 @@ define(function (require, exports, module) {
* for a matching starting substring.
*/
UnitTestReporter.prototype._createSpecFilter = function (filterString) {
var self = this;
var self = this,
filter = filterString ? filterString.toLowerCase() : undefined;

return function (spec) {
// filterString is undefined when no top-level suite is active (e.g. "All", "HTMLUtils", etc.)
// When undefined, all specs fail this filter and no tests are ran. This is by design.
// This setup allows the SpecRunner to load initially without automatically running all tests.
if (filterString === undefined) {
if (filter === undefined) {
return false;
}

if (!self._topLevelFilter(spec)) {
return false;
}

if (filterString === "All") {
if (filter === "all") {
return true;
}

if (spec.getFullName() === filterString) {
if (filter === spec.getFullName().toLowerCase()) {
return true;
}

Expand All @@ -179,7 +179,7 @@ define(function (require, exports, module) {
topLevelSuite = topLevelSuite.parentSuite;
}

return topLevelSuite.description === filterString;
return filter === topLevelSuite.description.toLowerCase();
};
};

Expand Down

0 comments on commit 58b64c8

Please sign in to comment.