Passed
Push — master ( 695a88...75a95e )
by Andrii
02:59
created

test-runner.ts ➔ rfsl   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
1
import {readFileSync} from 'fs'
2
import type { Options } from './src/options.types'
3
import postcss7 = require("postcss")
4
import creator7 = require("./src/7")
5
import postcss8 from 'postcss8'
6
import creator8 = require("./src")
7
8
export type RunOpts = Partial<{
9
  from: string
10
  input: string
11
  outputPath: string | false
12
  errorsCount: number
13
}>
14
15
const {parse: $parse} = JSON
16
, launch = (opts?: Options) => [postcss7([creator7(opts)]), postcss8([creator8(opts)])]
17
18
export {
19
  launch, run,
20
  rfs, rfsl, readOpts, suiteName
21
}
22
23
async function run(launchers: ReturnType<typeof launch>, runOpts: RunOpts) {
24
  const {
25
    errorsCount = 0,
26
    from,
27
    input = from && rfs(from),
28
  } = runOpts
29
  if (!input)
30
    throw Error("no test input")
31
32
  for (let i = 0; i < launchers.length; i++) {
33
    const result = await launchers[i].process(input, { from })
34
    , { outputPath: output } = runOpts
35
36
    expect(result.warnings()).toHaveLength(errorsCount)
37
38
    output && expect(rfsl(`${from}.d.ts`)).toStrictEqual(rfsl(output))
39
  }
40
}
41
42
function rfs(path: string) {
43
  return readFileSync(path).toString()
44
}
45
46
function rfsl(path: string, eol = "\n") {
47
  return rfs(path).split(eol)
48
}
49
50
function readOpts(path: string) {
51
  return $parse(rfs(path)) as Options
52
}
53
54
function suiteName(path: string) {
55
  return path
56
  .replace(/^.*[\/\\]/, '')
57
  .replace(/\..*$/, '')
58
}
59