-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathyaml-file.test.ts
112 lines (97 loc) · 3.32 KB
/
yaml-file.test.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
import { readFileSync } from 'fs';
import * as path from 'path';
import * as YAML from 'yaml';
import { withTemporaryDirectory } from './testutil';
import { JsonPatch } from '../src/json-patch';
import { YamlFile } from '../src/yaml-file';
describe('patch', () => {
function patchTest(patches: JsonPatch[], initialObj: any, assertObj: any, updateObj?: any) {
withTemporaryDirectory((dir) => {
const fileName = path.join(dir, 'file.yml');
const yamlFile = new YamlFile(fileName, {
obj: initialObj,
});
yamlFile.patch(...patches);
if (updateObj) {
yamlFile.update(updateObj);
}
yamlFile.writeFile();
expect(YAML.parse(readFileSync(fileName, 'utf-8'))).toStrictEqual(assertObj);
});
}
test('patch(p, v) can add to an existing array', () => {
patchTest(
[
JsonPatch.add('/first/second/array/-', '1'),
JsonPatch.add('/first/second/array/-', '2'),
JsonPatch.add('/first/second/array/1', '3'),
],
{ first: { second: { array: ['0'] } } },
{ first: { second: { array: ['0', '3', '1', '2'] } } },
);
});
test('patch(p, v) can add to an existing array after update', () => {
patchTest(
[
JsonPatch.add('/first/second/array/-', '1'),
JsonPatch.add('/first/second/array/-', '2'),
JsonPatch.add('/first/second/array/1', '3'),
],
{ first: { second: { array: ['?'] } } }, // shouldn't use
{ first: { second: { array: ['0', '3', '1', '2'] } } },
{ first: { second: { array: ['0'] } } }, // should use
);
});
test('patch(p, v) can create an array', () => {
patchTest(
[
JsonPatch.add('/first/second/array', []),
JsonPatch.add('/first/second/array/-', '1'),
JsonPatch.add('/first/second/array/-', '2'),
],
{ first: { third: {} } }, // shouldn't use
{ first: { second: { array: ['1', '2'] } } },
{ first: { second: {} } }, // should use
);
});
test('toYaml is idempotent', () => {
const yamlFile = new YamlFile('/dev/null', {
obj: {
array: [1, 2, 3],
},
});
yamlFile.patch(JsonPatch.remove('/array/1'));
expect(YAML.parse(yamlFile.toYaml())).toEqual({ array: [1, 3] });
expect(YAML.parse(yamlFile.toYaml())).toEqual({ array: [1, 3] });
});
});
describe('yaml file comments', () => {
function commentTest(commentAtTop: string | undefined, initialObj: any, assetDoc: string) {
withTemporaryDirectory((dir) => {
const fileName = path.join(dir, 'file.yml');
const yamlFile = new YamlFile(fileName, {
obj: initialObj,
});
yamlFile.commentAtTop = commentAtTop;
yamlFile.writeFile();
expect(readFileSync(fileName, 'utf-8')).toMatchSnapshot(assetDoc);
expect(yamlFile.commentAtTop).toStrictEqual(commentAtTop);
});
}
test('comment at top works', () => {
const commentBeforeYaml = 'commentAtTopTest.yml';
commentTest(
'Comment before',
{ first: { second: { array: ['0'] } } },
commentBeforeYaml,
);
});
test('multi-line comments work', () => {
const commentAfterYaml = 'multilineCommentAtTopAndBottomTest.yml';
commentTest(
`Comment before:\n\n${['A', 'B', 'C'].map((x) => ` - ${x}\n`).join('')}`,
{ first: { second: { array: ['0'] } } },
commentAfterYaml,
);
});
});