forked from bcherny/json-schema-to-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testCLI.ts
145 lines (123 loc) · 5.19 KB
/
testCLI.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import test from 'ava'
import {execSync} from 'child_process'
import {readFileSync, unlinkSync, readdirSync, existsSync, lstatSync} from 'fs'
import {resolve, posix} from 'path'
import rimraf = require('rimraf')
export function run() {
test('pipe in, pipe out', t => {
t.snapshot(
execSync('shx cat ./test/resources/ReferencedType.json | node dist/src/cli.js', {encoding: 'utf-8'}).toString(),
)
})
test('pipe in (schema without ID), pipe out', t => {
t.snapshot(
execSync('shx cat ./test/resources/ReferencedTypeWithoutID.json | node dist/src/cli.js', {
encoding: 'utf-8',
}).toString(),
)
})
test('file in (no flags), pipe out', t => {
t.snapshot(execSync('node dist/src/cli.js ./test/resources/ReferencedType.json').toString())
})
test('file in (--input), pipe out', t => {
t.snapshot(execSync('node dist/src/cli.js --input ./test/resources/ReferencedType.json').toString())
})
test('file in (-i), pipe out', t => {
t.snapshot(execSync('node dist/src/cli.js -i ./test/resources/ReferencedType.json').toString())
})
test('file in (-i), unreachable definitions flag, pipe out', t => {
t.snapshot(
execSync('node dist/src/cli.js -i ./test/resources/DefinitionsOnly.json --unreachableDefinitions').toString(),
)
})
test('file in (-i), style flags, pipe out', t => {
t.snapshot(
execSync('node dist/src/cli.js -i ./test/resources/Enum.json --style.singleQuote --no-style.semi').toString(),
)
})
test('file in (-i), pipe out (absolute path)', t => {
t.snapshot(execSync(`node dist/src/cli.js -i ${__dirname}/../../test/resources/ReferencedType.json`).toString())
})
test('pipe in, file out (--output)', t => {
execSync('shx cat ./test/resources/ReferencedType.json | node dist/src/cli.js --output ./ReferencedType.d.ts')
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
unlinkSync('./ReferencedType.d.ts')
})
test('pipe in, file out (-o)', t => {
execSync('shx cat ./test/resources/ReferencedType.json | node dist/src/cli.js -o ./ReferencedType.d.ts')
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
unlinkSync('./ReferencedType.d.ts')
})
test('file in (no flags), file out (no flags)', t => {
execSync('node dist/src/cli.js ./test/resources/ReferencedType.json ./ReferencedType.d.ts')
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
unlinkSync('./ReferencedType.d.ts')
})
test('file in (-i), file out (-o)', t => {
execSync('node dist/src/cli.js -i ./test/resources/ReferencedType.json -o ./ReferencedType.d.ts')
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
unlinkSync('./ReferencedType.d.ts')
})
test('file in (--input), file out (--output)', t => {
execSync('node dist/src/cli.js --input ./test/resources/ReferencedType.json --output ./ReferencedType.d.ts')
t.snapshot(readFileSync('./ReferencedType.d.ts', 'utf-8'))
unlinkSync('./ReferencedType.d.ts')
})
test('--unknownAny', t => {
t.snapshot(
execSync('node dist/src/cli.js --unknownAny=false --input ./test/resources/ReferencedType.json').toString(),
)
})
test('--additionalProperties', t => {
t.snapshot(
execSync(
'node dist/src/cli.js --additionalProperties=false --input ./test/resources/ReferencedType.json',
).toString(),
)
})
test('files in (-i), files out (-o)', t => {
execSync(`node dist/src/cli.js -i "./test/resources/MultiSchema/**/*.json" -o ./test/resources/MultiSchema/out`)
readdirSync('./test/resources/MultiSchema/out').forEach(f => {
const path = `./test/resources/MultiSchema/out/${f}`
t.snapshot(path)
t.snapshot(readFileSync(path, 'utf-8'))
unlinkSync(path)
})
rimraf.sync('./test/resources/MultiSchema/out')
})
test('files in (-i), pipe out', t => {
t.snapshot(execSync(`node dist/src/cli.js -i "./test/resources/MultiSchema/**/*.json"`).toString())
})
test('files in (-i), files out (-o) nested dir does not exist', t => {
execSync(
`node dist/src/cli.js -i "./test/resources/MultiSchema/**/*.json" -o ./test/resources/MultiSchema/foo/bar/out`,
)
readdirSync('./test/resources/MultiSchema/foo/bar/out').forEach(f => {
const path = `./test/resources/MultiSchema/foo/bar/out/${f}`
t.snapshot(path)
t.snapshot(readFileSync(path, 'utf-8'))
unlinkSync(path)
})
rimraf.sync('./test/resources/MultiSchema/foo')
})
test('files in (-i), files out (-o) matching nested dir', t => {
execSync(
`node dist/src/cli.js -i "./test/resources/../../test/resources/MultiSchema2/" -o ./test/resources/MultiSchema2/out`,
)
getPaths('./test/resources/MultiSchema2/out').forEach(file => {
t.snapshot(file)
t.snapshot(readFileSync(file, 'utf-8'))
unlinkSync(file)
})
rimraf.sync('./test/resources/MultiSchema2/out')
})
}
function getPaths(path: string, paths: string[] = []) {
if (existsSync(path) && lstatSync(path).isDirectory()) {
readdirSync(resolve(path)).forEach(item => getPaths(posix.join(posix.normalize(path), item), paths))
} else {
paths.push(path)
}
// sort paths to ensure a stable order across environments
return paths.sort((a, b) => a.localeCompare(b))
}