Passed
Push — master ( 657ba7...501391 )
by Andrii
02:17
created

test-runner.ts ➔ run   A

Complexity

Conditions 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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