forked from babel/minify
/
smoke-test.js
193 lines (165 loc) · 4.75 KB
/
smoke-test.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");
const babel = require("babel-core");
const chalk = require("chalk");
const glob = require("glob");
const babiliPreset = require("../packages/babel-preset-babili");
const breakMeTransform = require("./break");
const SMOKE_ASSETS_DIR = path.join(__dirname, "../smoke/assets");
const promisify = fn => (...args) =>
new Promise((resolve, reject) =>
fn.apply(null, [
...args,
(err, result) => (err ? reject(err) : resolve(result))
])
);
const globFiles = promisify(glob);
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);
class SmokeTest {
constructor(
options,
{ skipInstall = false, skipBuild = false, verbose = true }
) {
this.options = Object.assign({}, options);
this.verbose = verbose;
this.skipBuild = skipBuild;
this.skipInstall = skipInstall;
// verify required
required(options, ["dir", "files", "test"]);
this.path = path.join(SMOKE_ASSETS_DIR, this.options.dir);
this.installCommand = this.options.install
? `cd ${this.path} && ${this.options.install}`
: `cd ${this.path} && npm install`;
this.buildCommand = this.options.build
? `cd ${this.path} && ${this.options.build}`
: null;
this.testCommand = `cd ${this.path} && ${this.options.test}`;
this.cleanupCommand = `cd ${this.path} && git reset --hard HEAD`;
this.loggedStep = 1;
}
log(...messages) {
console.log(
chalk.cyan(`${this.loggedStep++}.`),
...messages.map((m, i) => (i === 0 ? chalk.cyan(m) : chalk.bold(m))),
"\n"
);
}
run() {
return this.cleanup()
.then(() => this.install())
.then(() => this.build())
.then(() => this.minifyAll())
.then(() => this.test())
.then(() => this.cleanup())
.then(() => this.verifyFailure())
.then(() => this.cleanup())
.catch(err => {
this.log("Errored - ", err);
return Promise.reject(err);
});
}
install() {
if (this.skipInstall) {
this.log("Skipping Install");
return Promise.resolve();
}
this.log("Installing Dependencies");
return this.exec(this.installCommand);
}
build() {
if (this.skipBuild) {
this.log("Skipping Build");
return Promise.resolve();
}
if (this.buildCommand === null) {
this.log("No BuildCommand found. Skipping Build.");
return Promise.resolve();
}
this.log("Building");
return this.exec(this.buildCommand);
}
minifyAll() {
return this.getAllFiles().then(files =>
Promise.all(files.map(file => this.minifyFile(file)))
);
}
getAllFiles() {
return globFiles(`${this.path}/${this.options.files}`, {
ignore: this.options.ignore
});
}
minifyFile(file) {
this.log("Minifying", file);
return readFile(file)
.then(contents => this.minify(contents.toString()))
.then(({ code }) => writeFile(file, code));
}
minify(contents) {
return babel.transform(contents, {
minified: true,
presets: [[babiliPreset, this.options.babiliOptions]]
});
}
test() {
this.log("Running Tests");
return this.exec(this.testCommand);
}
verifyFailure() {
this.log("Verifying Failure case");
return this.getAllFiles()
.then(files =>
Promise.all(
files.map(file =>
readFile(file)
.then(contents => {
this.log("Applying breaking changes to", file);
return babel.transform(contents.toString(), {
plugins: [breakMeTransform],
minified: true
});
})
.then(({ code }) => writeFile(file, code))
)
)
)
.then(() =>
// ensure the test fails
this.test().then(
() =>
// reject if it passes
Promise.reject(new Error("Verification of Breaking case failed")),
err => {
this.log("Error Verified -", err.message.toString().split("\n")[0]);
// and resolve if it fails
return Promise.resolve();
}
)
);
}
cleanup() {
this.log("Cleanup");
return this.exec(this.cleanupCommand);
}
exec(command) {
return new Promise((resolve, reject) => {
const p = exec(command, err => (err ? reject(err) : resolve()));
if (this.verbose) {
p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stderr);
}
});
}
}
module.exports = function smoke(options, flags) {
const test = new SmokeTest(options, flags);
return test.run();
};
function required(o, keys) {
for (let key of keys) {
if (o[key] === void 0) {
throw new Error(`${key} is a required option`);
}
}
}