forked from stylelint/stylelint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemTestUtils.js
70 lines (55 loc) · 1.74 KB
/
systemTestUtils.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
'use strict';
const _ = require('lodash');
const os = require('os');
const path = require('path');
const { promises: fs } = require('fs'); // eslint-disable-line node/no-unsupported-features/node-builtins
const replaceBackslashes = require('../lib/testUtils/replaceBackslashes');
function caseFilePath(caseNumber, fileName = 'stylesheet') {
return replaceBackslashes(path.join(__dirname, caseNumber, fileName));
}
function caseFiles(caseNumber) {
return caseFilePath(caseNumber, 'stylesheet.*');
}
function caseConfigFile(caseNumber, ext = 'json') {
return caseFilePath(caseNumber, `config.${ext}`);
}
async function caseConfig(caseNumber) {
return JSON.parse(await fs.readFile(caseConfigFile(caseNumber, 'json'), 'utf8'));
}
async function caseCode(caseNumber, ext = 'css') {
return await fs.readFile(`${caseFilePath(caseNumber)}.${ext}`, 'utf8');
}
async function caseFilesForFix(caseNumber, ext = 'css') {
const tempPath = replaceBackslashes(path.join(os.tmpdir(), `stylesheet-${_.uniqueId()}.${ext}`));
await fs.copyFile(path.join(__dirname, caseNumber, `stylesheet.${ext}`), tempPath);
return tempPath;
}
function prepForSnapshot({ results, output, ...rest }) {
// If output isn't fixed code
if (output.startsWith('[')) {
// The `source` of each file varies between platforms or if a tmp file is used
output = JSON.parse(output).map((warning) => {
delete warning.source;
return warning;
});
}
return {
// The _postcssResult object is not part of our API and is huge
results: results.map((result) => {
delete result.source;
delete result._postcssResult;
return result;
}),
output,
...rest,
};
}
module.exports = {
caseCode,
caseConfig,
caseConfigFile,
caseFilePath,
caseFilesForFix,
caseFiles,
prepForSnapshot,
};