|
1
|
|
|
import {statSync, appendFileSync, unlinkSync, existsSync} from 'fs' |
|
2
|
|
|
import {resolve} from 'path' |
|
3
|
|
|
import run, { rfsl, rfs } from './test-runner' |
|
4
|
|
|
|
|
5
|
|
|
import { platform } from "os" |
|
6
|
|
|
|
|
7
|
|
|
const osBasedAssertion = platform() === "darwin" ? "toBeGreaterThan" : "toBeGreaterThanOrEqual" |
|
8
|
|
|
, FALSY = ["", undefined, null, false, 0] |
|
9
|
|
|
, suitFolder = "__unit__" |
|
10
|
|
|
, from = `${suitFolder}/index.css` |
|
11
|
|
|
, fromContent = rfs(from) |
|
12
|
|
|
, dtsPath = `${from}.d.ts` |
|
13
|
|
|
, modifiedTime = () => statSync(dtsPath).mtimeMs |
|
14
|
|
|
|
|
15
|
|
|
let dtsContent: Readonly<string[]> |
|
16
|
|
|
beforeAll(async () => { |
|
17
|
|
|
const dtsPath = `${from}.d.ts` |
|
18
|
|
|
existsSync(dtsPath) && unlinkSync(dtsPath) |
|
19
|
|
|
await run({from}) |
|
20
|
|
|
dtsContent = rfsl(dtsPath) |
|
21
|
|
|
}) |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
describe('features', () => { |
|
25
|
|
|
it('falsy file', async () => await Promise.all( |
|
26
|
|
|
FALSY.map(from => run({ |
|
27
|
|
|
//@ts-expect-error |
|
28
|
|
|
from, |
|
29
|
|
|
|
|
30
|
|
|
input: ".class{}" |
|
31
|
|
|
})) |
|
32
|
|
|
)) |
|
33
|
|
|
|
|
34
|
|
|
describe('content overwrite', () => { |
|
35
|
|
|
beforeAll(async () => { |
|
36
|
|
|
existsSync(dtsPath) && unlinkSync(dtsPath) |
|
37
|
|
|
await run({from}) |
|
38
|
|
|
dtsContent = rfsl(dtsPath) |
|
39
|
|
|
}) |
|
40
|
|
|
|
|
41
|
|
|
it('no overwrite on same content', async () => { |
|
42
|
|
|
const modified = modifiedTime() |
|
43
|
|
|
await run({from}) |
|
44
|
|
|
expect(modifiedTime()).toBe(modified) |
|
45
|
|
|
}) |
|
46
|
|
|
|
|
47
|
|
|
it('overwrite after append new line', async () => { |
|
48
|
|
|
appendFileSync(dtsPath, "\n") |
|
49
|
|
|
const modified = modifiedTime() |
|
50
|
|
|
await run({from}) |
|
51
|
|
|
expect(modifiedTime())[osBasedAssertion](modified) |
|
52
|
|
|
}) |
|
53
|
|
|
|
|
54
|
|
|
it('overwrite after append new content', async () => { |
|
55
|
|
|
appendFileSync(dtsPath, "/**/") |
|
56
|
|
|
const modified = modifiedTime() |
|
57
|
|
|
await run({from}) |
|
58
|
|
|
expect(modifiedTime())[osBasedAssertion](modified) |
|
59
|
|
|
}) |
|
60
|
|
|
|
|
61
|
|
|
it('no overwrite on template without last newline', async () => { |
|
62
|
|
|
const modified = modifiedTime() |
|
63
|
|
|
await run({from}, {"template": `${suitFolder}/template_without_last_newline.d.ts`}) |
|
64
|
|
|
expect(modifiedTime()).toBe(modified) |
|
65
|
|
|
}) |
|
66
|
|
|
}) |
|
67
|
|
|
}) |
|
68
|
|
|
|
|
69
|
|
|
describe('options', () => { |
|
70
|
|
|
|
|
71
|
|
|
describe('identifierPattern', () => { |
|
72
|
|
|
const runOpts = { |
|
73
|
|
|
from, |
|
74
|
|
|
input: fromContent |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
it('not global pattern', async () => await run( |
|
78
|
|
|
{...runOpts, errorsCount: 1}, |
|
79
|
|
|
{identifierPattern: /\.([\w-]+)/} |
|
80
|
|
|
)) |
|
81
|
|
|
}) |
|
82
|
|
|
|
|
83
|
|
|
describe("destination", () => { |
|
84
|
|
|
it('here', async () => { |
|
85
|
|
|
const destination = {} |
|
86
|
|
|
await run({from}, {destination}) |
|
87
|
|
|
expect( |
|
88
|
|
|
destination |
|
89
|
|
|
).toStrictEqual({ |
|
90
|
|
|
[resolve(from)]: dtsContent |
|
91
|
|
|
}) |
|
92
|
|
|
}) |
|
93
|
|
|
|
|
94
|
|
|
it('falsy', async () => await Promise.all( |
|
95
|
|
|
FALSY.map(destination => destination === false ? void 0 : |
|
96
|
|
|
run({from, errorsCount: 1}, { |
|
97
|
|
|
//@ts-expect-error |
|
98
|
|
|
destination |
|
99
|
|
|
}) |
|
100
|
|
|
) |
|
101
|
|
|
)) |
|
102
|
|
|
}) |
|
103
|
|
|
}) |
|
104
|
|
|
|