Debian Codegraph @

index.js

raw
1 /* eslint max-lines: "off" */
2
3 "use strict";
4
5 var spread = require("es5-ext/function/#/spread")
6 , deferred = require("deferred")
7 , path = require("path")
8 , commonPath = require("path2/common")
9 , runInContext = require("vm").runInContext
10 , out = require("./lib/console")
11 , configure = require("./lib/configure")
12 , load = require("./lib/load")
13 , run = require("./lib/run");
14
15 var resolve = path.resolve, map = Array.prototype.map, suite, isError;
16
17 isError = function (e, context) {
18 if (e instanceof Error) return true;
19 if (context !== global) {
20 return runInContext("(function () { return this instanceof Error; })", context).call(e);
21 }
22 return false;
23 };
24
25 suite = {
26 init: function (paths, options) {
27 var conf, d, projectRoot;
28 d = deferred();
29 paths = map.call(paths, function (testPath) { return resolve(testPath); });
30 this.resolve = d.resolve;
31 this.console = out(options);
32 this.tail = deferred(null);
33 if (paths.length > 1) {
34 projectRoot = commonPath.apply(null, paths);
35 this.rindex = projectRoot ? projectRoot.length + 1 : 0;
36 } else if (paths.length) {
37 this.rindex = paths[0].length + 1;
38 }
39
40 conf = configure(paths);
41 conf("data", this.ondata.bind(this));
42 conf("end", this.onend.bind(this));
43 return d.promise;
44 },
45 ondata: function () { this.tail = this.tail(spread.call(this.process).bind(this, arguments)); },
46 process: function (modulePath, fpath, tpath, context) {
47 var pname = modulePath.slice(this.rindex), fname, logger, testModuleConfig, d;
48 d = deferred();
49 this.console.break();
50 if (fpath instanceof Error) {
51 // Wrong path
52 this.console.error(pname, null, fpath);
53 return d.resolve();
54 }
55
56 fname = fpath.slice(this.rindex);
57 if (tpath instanceof Error) {
58 if (tpath.type === "testfile") {
59 // Input is a test file, ignore
60 return d.resolve();
61 }
62 // Could not assume test file path (not within package)
63 // or there were problems with obtaining context
64 this.console.error(pname, fname, tpath);
65 return d.resolve();
66 }
67
68 // Configured ok, load files
69 testModuleConfig = load(fpath, tpath, context);
70
71 // Any files missing, any evaluation errors ?
72 if (testModuleConfig.testee === undefined) {
73 // File not accessible
74 this.console.error(pname, fname, "Couldn't load module '" + fpath + "'");
75 return d.resolve();
76 }
77
78 if (isError(testModuleConfig.test, context)) {
79 this.console.error(pname, fname, testModuleConfig.test);
80 return d.resolve();
81 }
82 if (isError(testModuleConfig.testee, context)) {
83 this.console.error(pname, fname, testModuleConfig.testee);
84 return d.resolve();
85 }
86 if (!testModuleConfig.test) {
87 this.console.error(pname, fname, "Tests could not be loaded, tried '" + tpath + "'");
88 return d.resolve();
89 }
90
91 // Loaded ok, run tests
92 logger = run(testModuleConfig.testee, testModuleConfig.test);
93 logger.on(
94 "data",
95 function (testResult) {
96 if (d.resolved) {
97 var error = new Error(
98 "Unexpected state: Assertions are issued after suite finalized test run"
99 );
100 // Create error in test context (to expose proper stack trace)
101 // Throw in other context to avoid catch clause
102 process.nextTick(function () { throw error; });
103 return;
104 }
105 var name = [fname].concat(testResult.msg);
106 if (testResult.type === "pass") {
107 name.push(testResult.data);
108 } else if (testResult.type === "fail" && testResult.data.operator) {
109 name.push(testResult.data.message);
110 }
111 name = name.filter(Boolean).join(": ");
112 this.console[testResult.type](fname, name, testResult.data);
113 }.bind(this)
114 );
115 logger.on("end", function () { d.resolve(); });
116
117 return d.promise;
118 },
119 onend: function () {
120 this.tail(this.end.bind(this)).done();
121 delete this.tail;
122 },
123 end: function () {
124 this.console.end();
125 this.resolve(this);
126 }
127 };
128
129 module.exports = function (paths, options) { return Object.create(suite).init(paths, options); };