Debian Codegraph @

lib/load.js

raw
1 // Imports lib and tests from given paths
2
3 "use strict";
4
5 var assign = require("es5-ext/object/assign")
6 , endsWith = require("es5-ext/string/#/ends-with")
7 , path = require("path")
8 , commonPath = require("path2/common")
9 , requireInContext = require("./require-in-context")
10 , requireFirst = require("./require-first-in-tree");
11
12 var dirname = path.dirname, sep = path.sep, ptrim;
13
14 ptrim = function (testPath) {
15 return testPath.match(/[\u0000-.0-[\]-\uffff][/\\]$/) ? testPath.slice(0, -1) : testPath;
16 };
17
18 module.exports = function (testeePath, testPath, context) {
19 var testConfig, scopes;
20 var testModule, testError, testeeModule, testeeError;
21
22 try { testModule = requireInContext(testPath, context, { isSilent: true }); }
23 catch (error) { testError = error; }
24 try { testeeModule = requireInContext(testeePath, context, { isSilent: true }); }
25 catch (error) { testeeError = error; }
26 if (testError || testeeError) {
27 return { test: testModule || testError, testee: testeeModule || testeeError };
28 }
29 testConfig = { test: testModule, testee: testeeModule };
30
31 if (!testConfig.test && endsWith.call(testeePath, sep + "index.js")) {
32 testConfig.test = require("./utils/index-test")(dirname(testeePath), null, context);
33 }
34
35 if (testConfig.test && testConfig.test.__generic) {
36 scopes = requireFirst(
37 "__scopes", dirname(testPath), ptrim(commonPath(testeePath, testPath))
38 );
39 assign(testConfig.test, require("./utils/factory")(scopes, testConfig.test.__generic));
40 delete testConfig.test.__generic;
41 }
42
43 return testConfig;
44 };