This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
147 lines (116 loc) · 4.36 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
import { exitFail, exitPass } from './lib/exit';
import { readOrWriteFile, safelyReadFile, writeFile } from './lib/utils';
import * as log from './lib/log';
import getErrorMessage from './lib/get-error-message';
import getOptions from './lib/get-options';
import path from 'path';
getOptions().then(
async options => {
let hadError = false;
// runner
for (const name in options.config) {
const test = options.config[name];
const testPlugin = typeof Object(test.plugin).process === 'function'
? test.plugin
: typeof test.plugin === 'function'
? { process: test.plugin }
: options.plugin;
const testBase = name.split(':')[0];
const testFull = name.split(':').join('.');
// test paths
const sourcePath = path.resolve(options.fixtures, test.source || `${testBase}.css`);
const expectPath = path.resolve(options.fixtures, test.expect || `${testFull}.expect.css`);
const resultPath = path.resolve(options.fixtures, test.result || `${testFull}.result.css`);
const processOptions = Object.assign({ from: sourcePath, to: resultPath }, test.processOptions);
const pluginOptions = test.options;
const pluginName = Object(testPlugin.postcss).postcssPlugin || 'postcss';
log.wait(pluginName, test.message, options.ci);
try {
if (Object(test.before) instanceof Function) {
await test.before();
}
const expectCSS = await safelyReadFile(expectPath);
const sourceCSS = await readOrWriteFile(sourcePath, expectCSS);
const result = await testPlugin.process(sourceCSS, processOptions, pluginOptions);
const resultCSS = result.css;
if (options.fix) {
await writeFile(expectPath, resultCSS);
await writeFile(resultPath, resultCSS);
} else {
await writeFile(resultPath, resultCSS);
if (expectCSS !== resultCSS) {
throw new Error([
`Expected: ${JSON.stringify(expectCSS).slice(1, -1)}`,
`Received: ${JSON.stringify(resultCSS).slice(1, -1)}`
].join('\n'));
}
}
const warnings = result.warnings();
if (typeof test.warnings === 'number') {
if (test.warnings !== warnings.length) {
const s = warnings.length !== 1 ? 's' : '';
throw new Error(`Expected: ${test.warnings} warning${s}\nReceived: ${warnings.length} warnings`);
}
} else if (warnings.length) {
const areExpectedWarnings = warnings.every(
warning => test.warnings === Object(test.warnings) && Object.keys(test.warnings).every(
key => test.warnings[key] instanceof RegExp
? test.warnings[key].test(warning[key])
: test.warnings[key] === warning[key]
)
);
if (!areExpectedWarnings) {
const s = warnings.length !== 1 ? 's' : '';
throw new Error(`Unexpected warning${s}:\n${warnings.join('\n')}`);
}
} else if (test.warnings) {
throw new Error(`Expected a warning`);
} else if (test.errors) {
throw new Error(`Expected an error`);
}
if (Object(test.after) instanceof Function) {
await test.after();
}
log.pass(pluginName, test.message, options.ci);
} catch (error) {
if ('error' in test) {
const isObjectError = test.error === Object(test.error);
if (isObjectError) {
const isExpectedError = Object.keys(test.error).every(
key => test.error[key] instanceof RegExp
? test.error[key].test(Object(error)[key])
: test.error[key] === Object(error)[key]
);
if (isExpectedError) {
log.pass(pluginName, test.message, options.ci);
} else {
const reportedError = Object.keys(test.error).reduce(
(reportedError, key) => Object.assign(reportedError, { [key]: Object(error)[key] }),
{}
);
hadError = error;
log.fail(pluginName, test.message, ` Expected Error: ${JSON.stringify(test.error)}\n Received Error: ${JSON.stringify(reportedError)}`, options.ci);
}
} else {
const isExpectedError = typeof test.error === 'boolean' && test.error;
if (isExpectedError) {
log.pass(pluginName, test.message, options.ci);
} else {
hadError = error;
log.fail(pluginName, test.message, ` Expected Error`, options.ci);
}
if (options.ci) {
break;
}
}
} else {
hadError = error;
log.fail(pluginName, test.message, getErrorMessage(error), options.ci);
}
}
}
if (hadError) {
throw hadError;
}
}
).then(exitPass, exitFail);