-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
index.js
174 lines (139 loc) 路 4.26 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
172
173
174
import cloneDeep from "lodash/cloneDeep";
import trimEnd from "lodash/trimEnd";
import resolve from "try-resolve";
import clone from "lodash/clone";
import merge from "lodash/merge";
import path from "path";
import fs from "fs";
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;
}
const ext = path.extname(name);
const base = path.basename(name, ext);
return name[0] === "." || ext === ".md" || base === "LICENSE" || base === "options";
}
export default function get(entryLoc): Array<Suite> {
const suites = [];
let rootOpts = {};
const rootOptsLoc = resolve(entryLoc + "/options");
if (rootOptsLoc) rootOpts = require(rootOptsLoc);
for (const suiteName of fs.readdirSync(entryLoc)) {
if (shouldIgnore(suiteName)) continue;
const suite = {
options: clone(rootOpts),
tests: [],
title: humanize(suiteName),
filename: entryLoc + "/" + suiteName
};
assertDirectory(suite.filename);
suites.push(suite);
const suiteOptsLoc = resolve(suite.filename + "/options");
if (suiteOptsLoc) suite.options = require(suiteOptsLoc);
for (const taskName of fs.readdirSync(suite.filename)) {
push(taskName, suite.filename + "/" + taskName);
}
function push(taskName, taskDir) {
const actualLocAlias = suiteName + "/" + taskName + "/actual.js";
let expectLocAlias = suiteName + "/" + taskName + "/expected.js";
const execLocAlias = suiteName + "/" + taskName + "/exec.js";
const actualLoc = taskDir + "/actual.js";
let expectLoc = taskDir + "/expected.js";
let execLoc = taskDir + "/exec.js";
if (fs.statSync(taskDir).isFile()) {
const ext = path.extname(taskDir);
if (ext !== ".js" && ext !== ".module.js") return;
execLoc = taskDir;
}
if (resolve.relative(expectLoc + "on")) {
expectLoc += "on";
expectLocAlias += "on";
}
const taskOpts = cloneDeep(suite.options);
const taskOptsLoc = resolve(taskDir + "/options");
if (taskOptsLoc) merge(taskOpts, require(taskOptsLoc));
const test = {
optionsDir: taskOptsLoc ? path.dirname(taskOptsLoc) : null,
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);
const sourceMappingsLoc = taskDir + "/source-mappings.json";
if (fs.existsSync(sourceMappingsLoc)) {
test.sourceMappings = JSON.parse(readFile(sourceMappingsLoc));
}
const sourceMapLoc = taskDir + "/source-map.json";
if (fs.existsSync(sourceMapLoc)) {
test.sourceMap = JSON.parse(readFile(sourceMapLoc));
}
}
}
return suites;
}
export function multiple(entryLoc, ignore?: Array<string>) {
const categories = {};
for (const name of fs.readdirSync(entryLoc)) {
if (shouldIgnore(name, ignore)) continue;
const 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 "";
}
}