-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
171 lines (136 loc) 路 4.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import resolve from "try-resolve";
import path from "path";
import fs from "fs";
import _ from "lodash";
function humanize(val, noext) {
if (noext) val = path.basename(val, path.extname(val));
return val.replace(/-/g, " ");
}
type TestFile = {
loc: string;
code: string;
filename: string;
};
type Test = {
title: string;
disabled: boolean;
options: Object;
exec: TestFile;
actual: TestFile;
expected: TestFile;
};
type Suite = {
options: Object;
tests: Array<Test>;
title: string;
filename: string;
};
function assertDirectory(loc) {
if (!fs.statSync(loc).isDirectory()) {
throw new Error(`Expected ${loc} to be a directory.`);
}
}
function shouldIgnore(name, blacklist?: Array<string>) {
if (blacklist && blacklist.indexOf(name) >= 0) {
return true;
}
let ext = path.extname(name);
let base = path.basename(name, ext);
return name[0] === "." || ext === ".md" || base === "LICENSE" || base === "options";
}
export default function get(entryLoc): Array<Suite> {
let suites = [];
let rootOpts = {};
let rootOptsLoc = resolve(entryLoc + "/options");
if (rootOptsLoc) rootOpts = require(rootOptsLoc);
for (let suiteName of fs.readdirSync(entryLoc)) {
if (shouldIgnore(suiteName)) continue;
let suite = {
options: _.clone(rootOpts),
tests: [],
title: humanize(suiteName),
filename: entryLoc + "/" + suiteName
};
assertDirectory(suite.filename);
suites.push(suite);
let suiteOptsLoc = resolve(suite.filename + "/options");
if (suiteOptsLoc) suite.options = require(suiteOptsLoc);
for (let taskName of fs.readdirSync(suite.filename)) {
if (shouldIgnore(taskName)) continue;
push(taskName, suite.filename + "/" + taskName);
}
function push(taskName, taskDir) {
let actualLocAlias = suiteName + "/" + taskName + "/actual.js";
let expectLocAlias = suiteName + "/" + taskName + "/expected.js";
let execLocAlias = suiteName + "/" + taskName + "/exec.js";
let actualLoc = taskDir + "/actual.js";
let expectLoc = taskDir + "/expected.js";
let execLoc = taskDir + "/exec.js";
if (fs.statSync(taskDir).isFile()) {
let ext = path.extname(taskDir);
if (ext !== ".js" && ext !== ".module.js") return;
execLoc = taskDir;
}
if (resolve.relative(expectLoc + "on")) {
expectLoc += "on";
expectLocAlias += "on";
}
let taskOpts = _.cloneDeep(suite.options);
let taskOptsLoc = resolve(taskDir + "/options");
if (taskOptsLoc) _.merge(taskOpts, require(taskOptsLoc));
let test = {
title: humanize(taskName, true),
disabled: taskName[0] === ".",
options: taskOpts,
exec: {
loc: execLoc,
code: readFile(execLoc),
filename: execLocAlias,
},
actual: {
loc: actualLoc,
code: readFile(actualLoc),
filename: actualLocAlias,
},
expect: {
loc: expectLoc,
code: readFile(expectLoc),
filename: expectLocAlias
}
};
// traceur checks
if (test.exec.code.indexOf("// Async.") >= 0) {
return;
}
suite.tests.push(test);
let sourceMappingsLoc = taskDir + "/source-mappings.json";
if (fs.existsSync(sourceMappingsLoc)) {
test.sourceMappings = JSON.parse(readFile(sourceMappingsLoc));
}
let sourceMapLoc = taskDir + "/source-map.json";
if (fs.existsSync(sourceMapLoc)) {
test.sourceMap = JSON.parse(readFile(sourceMapLoc));
}
}
}
return suites;
}
export function multiple(entryLoc, ignore?: Array<string>) {
let categories = {};
for (let name of fs.readdirSync(entryLoc)) {
if (shouldIgnore(name, ignore)) continue;
let loc = path.join(entryLoc, name);
assertDirectory(loc);
categories[name] = get(loc);
}
return categories;
}
export function readFile(filename) {
if (fs.existsSync(filename)) {
let file = _.trimEnd(fs.readFileSync(filename, "utf8"));
file = file.replace(/\r\n/g, "\n");
return file;
} else {
return "";
}
}