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

test-runner.ts   A

Complexity

Total Complexity 8
Complexity/F 1.33

Size

Lines of Code 67
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 8
mnd 2
bc 2
fnc 6
bpm 0.3333
cpm 1.3333
noi 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A ➔ launcher 0 3 1
A ➔ run 0 24 3
A ➔ rfs 0 3 1
A ➔ suiteName 0 5 1
A ➔ readOpts 0 3 1
A ➔ rfsl 0 3 1
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
}