-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-runner.ts
65 lines (51 loc) · 1.45 KB
/
test-runner.ts
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
import {readFileSync} from 'fs'
import type {Options} from './src/options.types'
import postcss7 = require("postcss")
import creator7 = require("./src/7")
import postcss8 from 'postcss8'
import creator8 = require("./src")
import type Processor from 'postcss8/lib/processor'
export type RunOpts = Partial<{
from: string
input: string
outputPath: string | false
errorsCount: number
}>
const {parse: $parse} = JSON
, launch = (opts?: Options) => {
const launchers = [postcss7([creator7(opts)]), postcss8([creator8(opts)])]
return (runOpts: RunOpts) => run(launchers, runOpts)
}
export default launch
export {
rfs, rfsl, readOpts, suiteName
}
async function run(launchers: (postcss7.Processor | Processor)[], runOpts: RunOpts) {
const {
errorsCount = 0,
from,
input = from && rfs(from)
} = runOpts
if (!input)
throw new Error("no test input")
for (let i = 0; i < launchers.length; i++) {
const result = await launchers[i].process(input, {from})
, {outputPath: output} = runOpts
expect(result.warnings()).toHaveLength(errorsCount)
output && expect(rfsl(`${from}.d.ts`)).toStrictEqual(rfsl(output))
}
}
function rfs(path: string) {
return readFileSync(path).toString()
}
function rfsl(path: string, eol = "\n") {
return rfs(path).split(eol)
}
function readOpts(path: string) {
return $parse(rfs(path)) as Options
}
function suiteName(path: string) {
return path
.replace(/^.*[\/\\]/, '')
.replace(/\..*$/, '')
}