-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathhost-tree_spec.ts
143 lines (127 loc) · 4.87 KB
/
host-tree_spec.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { normalize, virtualFs } from '@angular-devkit/core';
import { FilterHostTree, HostTree } from './host-tree';
import { MergeStrategy } from './interface';
describe('HostTree', () => {
describe('merge', () => {
it('should create files from each tree', () => {
const tree = new HostTree();
tree.create('/file1', 'a');
const tree2 = new HostTree();
tree2.create('/file2', 'a');
tree.merge(tree2);
expect(tree.actions[0].kind).toEqual('c');
expect(tree.actions[1].kind).toEqual('c');
});
it('should overwrite if the file exists in one tree', () => {
const tree = new HostTree();
tree.create('/file1', 'a');
const tree2 = new HostTree();
tree2.create('/file1', 'b');
tree.merge(tree2, MergeStrategy.Overwrite);
expect(tree.actions[0].kind).toEqual('c');
});
it('should throw if the file exists in one tree with the correct MergeStrategy', () => {
const tree = new HostTree();
tree.create('/file1', 'a');
const tree2 = new HostTree();
tree2.create('/file1', 'b');
expect(() => tree.merge(tree2)).toThrow();
});
it('should not have a second action if the file content is the same', () => {
const tree = new HostTree();
tree.create('/file1', 'a');
const tree2 = new HostTree();
tree2.create('/file1', 'a');
tree.merge(tree2, MergeStrategy.Overwrite);
expect(tree.actions[0].kind).toEqual('c');
expect(tree.actions.length).toEqual(1);
});
});
});
describe('FilterHostTree', () => {
it('works', () => {
const tree = new HostTree();
tree.create('/file1', '');
tree.create('/file2', '');
tree.create('/file3', '');
const filtered = new FilterHostTree(tree, (p) => p != '/file2');
const filteredFiles: string[] = [];
filtered.visit((path) => filteredFiles.push(path));
filteredFiles.sort();
expect(filteredFiles).toEqual(['/file1', '/file3'].map(normalize));
expect(filtered.actions.length).toEqual(2);
});
it('works with two filters', () => {
const tree = new HostTree();
tree.create('/file1', '');
tree.create('/file2', '');
tree.create('/file3', '');
const filtered = new FilterHostTree(tree, (p) => p != '/file2');
const filtered2 = new FilterHostTree(filtered, (p) => p != '/file3');
const filteredFiles: string[] = [];
filtered2.visit((path) => filteredFiles.push(path));
filteredFiles.sort();
expect(filteredFiles).toEqual(['/file1'].map(normalize));
expect(filtered2.actions.map((a) => a.kind)).toEqual(['c']);
});
it('works with underlying files', () => {
const fs = new virtualFs.test.TestHost({
'/file1': '',
});
const tree = new HostTree(fs);
tree.create('/file2', '');
tree.create('/file3', '');
const filtered = new FilterHostTree(tree, (p) => p != '/file2');
const filtered2 = new FilterHostTree(filtered, (p) => p != '/file3');
const filteredFiles: string[] = [];
filtered2.visit((path) => filteredFiles.push(path));
filteredFiles.sort();
expect(filteredFiles).toEqual(['/file1'].map(normalize));
expect(filtered2.actions.map((a) => a.kind)).toEqual([]);
});
it('works with created paths and files', () => {
const tree = new HostTree();
tree.create('/dir1/file1', '');
tree.create('/dir2/file2', '');
tree.create('/file3', '');
const filtered = new FilterHostTree(tree, (p) => p != '/dir2/file2');
const filtered2 = new FilterHostTree(filtered, (p) => p != '/file3');
const filteredFiles: string[] = [];
filtered2.visit((path) => filteredFiles.push(path));
filteredFiles.sort();
expect(filteredFiles).toEqual(['/dir1/file1'].map(normalize));
expect(filtered2.actions.map((a) => a.kind)).toEqual(['c']);
});
it('works with underlying paths and files', () => {
const fs = new virtualFs.test.TestHost({
'/dir1/file1': '',
'/dir2/file2': '',
});
const tree = new HostTree(fs);
tree.create('/file3', '');
const filtered = new FilterHostTree(tree, (p) => p != '/dir2/file2');
const filtered2 = new FilterHostTree(filtered, (p) => p != '/file3');
const filteredFiles: string[] = [];
filtered2.visit((path) => filteredFiles.push(path));
filteredFiles.sort();
expect(filteredFiles).toEqual(['/dir1/file1'].map(normalize));
expect(filtered2.actions.map((a) => a.kind)).toEqual([]);
});
it('subdirs only contains directories', () => {
const fs = new virtualFs.test.TestHost({
'/dir1/file1': '',
'/dir1/dir2/file2': '',
'/dir1/dir3/file3': '',
});
const tree = new HostTree(fs);
const subDirs = tree.getDir('/dir1').subdirs;
expect(subDirs as string[]).toEqual(['dir2', 'dir3']);
});
});