forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_spec.ts
156 lines (135 loc) · 4.7 KB
/
host_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
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* @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.dev/license
*/
import { normalize, virtualFs } from '@angular-devkit/core';
import { HostCreateTree, HostTree } from '../tree/host-tree';
import { HostSink } from './host';
describe('FileSystemSink', () => {
it('works', (done) => {
const host = new virtualFs.test.TestHost({
'/hello': 'world',
'/sub/directory/file2': '',
'/sub/file1': '',
});
const tree = new HostCreateTree(host);
tree.create('/test', 'testing 1 2');
const recorder = tree.beginUpdate('/test');
recorder.insertLeft(8, 'testing ');
tree.commitUpdate(recorder);
const files = ['/hello', '/sub/directory/file2', '/sub/file1', '/test'];
const treeFiles: string[] = [];
tree.visit((path) => treeFiles.push(path));
treeFiles.sort();
expect(treeFiles).toEqual(files);
const outputHost = new virtualFs.test.TestHost();
const sink = new HostSink(outputHost);
sink
.commit(tree)
.toPromise()
.then(() => {
const tmpFiles = outputHost.files.sort();
expect(tmpFiles as string[]).toEqual(files);
expect(outputHost.sync.read(normalize('/test')).toString()).toBe('testing testing 1 2');
})
.then(done, done.fail);
});
describe('complex tests', () => {
beforeEach((done) => {
// Commit a version of the tree.
const host = new virtualFs.test.TestHost({
'/file0': '/file0',
'/sub/directory/file2': '/sub/directory/file2',
'/sub/file1': '/sub/file1',
});
const tree = new HostCreateTree(host);
const outputHost = new virtualFs.test.TestHost();
const sink = new HostSink(outputHost);
sink.commit(tree).toPromise().then(done, done.fail);
});
it('can rename files', (done) => {
const host = new virtualFs.test.TestHost({
'/file0': '/file0',
});
const tree = new HostTree(host);
tree.rename('/file0', '/file1');
const sink = new HostSink(host);
sink
.commit(tree)
.toPromise()
.then(() => {
expect(host.sync.exists(normalize('/file0'))).toBe(false);
expect(host.sync.exists(normalize('/file1'))).toBe(true);
})
.then(done, done.fail);
});
it('can rename nested files', (done) => {
const host = new virtualFs.test.TestHost({
'/sub/directory/file2': '',
});
const tree = new HostTree(host);
tree.rename('/sub/directory/file2', '/another-directory/file2');
const sink = new HostSink(host);
sink
.commit(tree)
.toPromise()
.then(() => {
expect(host.sync.exists(normalize('/sub/directory/file2'))).toBe(false);
expect(host.sync.exists(normalize('/another-directory/file2'))).toBe(true);
})
.then(done, done.fail);
});
it('can delete and create the same file', (done) => {
const host = new virtualFs.test.TestHost({
'/file0': 'world',
});
const tree = new HostTree(host);
tree.delete('/file0');
tree.create('/file0', 'hello');
const sink = new HostSink(host);
sink
.commit(tree)
.toPromise()
.then(() => {
expect(host.sync.read(normalize('/file0')).toString()).toBe('hello');
})
.then(done, done.fail);
});
it('can rename then create the same file', (done) => {
const host = new virtualFs.test.TestHost({
'/file0': 'world',
});
const tree = new HostTree(host);
tree.rename('/file0', '/file1');
expect(tree.exists('/file0')).toBeFalsy();
expect(tree.exists('/file1')).toBeTruthy();
tree.create('/file0', 'hello');
expect(tree.exists('/file0')).toBeTruthy();
const sink = new HostSink(host);
sink
.commit(tree)
.toPromise()
.then(() => {
expect(host.sync.read(normalize('/file0')).toString()).toBe('hello');
expect(virtualFs.fileBufferToString(host.sync.read(normalize('/file1')))).toBe('world');
})
.then(done, done.fail);
});
it('can rename then modify the same file', async () => {
const host = new virtualFs.test.TestHost({
'/file0': 'world',
});
const tree = new HostTree(host);
tree.rename('/file0', '/file1');
expect(tree.exists('/file0')).toBeFalsy();
expect(tree.exists('/file1')).toBeTruthy();
tree.overwrite('/file1', 'hello');
const sink = new HostSink(host);
await sink.commit(tree).toPromise();
expect(virtualFs.fileBufferToString(host.sync.read(normalize('/file1')))).toBe('hello');
});
});
});