Passed
Push — master ( e7b455...695a88 )
by Andrii
02:39
created

test-runner.ts   A

Complexity

Total Complexity 7
Complexity/F 1.4

Size

Lines of Code 59
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 47
mnd 2
bc 2
fnc 5
dl 0
loc 59
rs 10
bpm 0.4
cpm 1.4
noi 0
c 0
b 0
f 0
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