/
check-fixture.js
52 lines (45 loc) · 1.47 KB
/
check-fixture.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
"use strict"
// builtin tooling
const fs = require("fs")
// external tooling
const postcss = require("postcss")
// plugin
const atImport = require("../..")
const astCheckerPlugin = require("./ast-checker")
function read(name, ext) {
ext = ext || ".css"
return fs.readFileSync(`test/fixtures/${name}${ext}`, "utf8")
}
module.exports = function (t, file, opts, postcssOpts, warnings) {
opts = { path: "test/fixtures/imports", ...opts }
postcssOpts = { from: undefined, ...postcssOpts }
if (typeof file === "string") file = { name: file, ext: ".css" }
const { name, ext } = file
return postcss([atImport(opts), astCheckerPlugin()])
.process(read(name, ext), postcssOpts || {})
.then(result => {
const actual = result.css
const expected = read(`${name}.expected`)
// handy thing: checkout actual in the *.actual.css file
fs.writeFile(`test/fixtures/${name}.actual.css`, actual, err => {
// eslint-disable-next-line no-console
if (err) console.warn(`Warning: ${err}; not fatal, continuing`)
})
t.is(actual, expected)
if (!warnings) warnings = []
result.warnings().forEach((warning, index) => {
t.is(
warning.text,
warnings[index],
`unexpected warning: "${warning.text}"`,
)
})
t.is(
warnings.length,
result.warnings().length,
`expected ${warnings.length} warning(s), got ${
result.warnings().length
}`,
)
})
}