forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_spec.ts
127 lines (111 loc) · 4.47 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
/**
* @license
* Copyright Google Inc. 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
*/
// tslint:disable:no-any
// tslint:disable:no-non-null-assertion
// tslint:disable:no-implicit-dependencies
import { normalize, virtualFs } from '@angular-devkit/core';
import { NodeJsAsyncHost, NodeJsSyncHost } from '@angular-devkit/core/node';
import * as fs from 'fs';
import { Observable, Subscription } from 'rxjs';
const temp = require('temp');
// TODO: replace this with an "it()" macro that's reusable globally.
let linuxOnlyIt: typeof it = it;
if (process.platform.startsWith('win') || process.platform.startsWith('darwin')) {
linuxOnlyIt = xit;
}
describe('NodeJsAsyncHost', () => {
let root: string;
let host: virtualFs.Host<fs.Stats>;
beforeEach(() => {
root = temp.mkdirSync('core-node-spec-');
host = new virtualFs.ScopedHost(new NodeJsAsyncHost(), normalize(root));
});
afterEach(done => host.delete(normalize('/')).toPromise().then(done, done.fail));
linuxOnlyIt('can watch', done => {
let obs: Observable<virtualFs.HostWatchEvent>;
let subscription: Subscription;
const content = virtualFs.stringToFileBuffer('hello world');
const content2 = virtualFs.stringToFileBuffer('hello world 2');
const allEvents: virtualFs.HostWatchEvent[] = [];
Promise.resolve()
.then(() => fs.mkdirSync(root + '/sub1'))
.then(() => fs.writeFileSync(root + '/sub1/file1', 'hello world'))
.then(() => {
obs = host.watch(normalize('/sub1'), { recursive: true }) !;
expect(obs).not.toBeNull();
subscription = obs.subscribe(event => { allEvents.push(event); });
})
.then(() => new Promise(resolve => setTimeout(resolve, 100)))
// Discard the events registered so far.
.then(() => allEvents.splice(0))
.then(() => host.write(normalize('/sub1/sub2/file3'), content).toPromise())
.then(() => host.write(normalize('/sub1/file2'), content2).toPromise())
.then(() => host.delete(normalize('/sub1/file1')).toPromise())
.then(() => new Promise(resolve => setTimeout(resolve, 2000)))
.then(() => {
expect(allEvents.length).toBe(3);
subscription.unsubscribe();
})
.then(done, done.fail);
}, 30000);
});
describe('NodeJsSyncHost', () => {
let root: string;
let host: virtualFs.SyncDelegateHost<fs.Stats>;
beforeEach(() => {
root = temp.mkdirSync('core-node-spec-');
host = new virtualFs.SyncDelegateHost(
new virtualFs.ScopedHost(new NodeJsSyncHost(), normalize(root)));
});
afterEach(() => {
host.delete(normalize('/'));
});
linuxOnlyIt('can watch', done => {
let obs: Observable<virtualFs.HostWatchEvent>;
let subscription: Subscription;
const content = virtualFs.stringToFileBuffer('hello world');
const content2 = virtualFs.stringToFileBuffer('hello world 2');
const allEvents: virtualFs.HostWatchEvent[] = [];
Promise.resolve()
.then(() => fs.mkdirSync(root + '/sub1'))
.then(() => fs.writeFileSync(root + '/sub1/file1', 'hello world'))
.then(() => {
obs = host.watch(normalize('/sub1'), { recursive: true })!;
expect(obs).not.toBeNull();
subscription = obs.subscribe(event => { allEvents.push(event); });
})
.then(() => new Promise(resolve => setTimeout(resolve, 100)))
// Discard the events registered so far.
.then(() => allEvents.splice(0))
.then(() => {
host.write(normalize('/sub1/sub2/file3'), content);
host.write(normalize('/sub1/file2'), content2);
host.delete(normalize('/sub1/file1'));
})
.then(() => new Promise(resolve => setTimeout(resolve, 2000)))
.then(() => {
expect(allEvents.length).toBe(3);
subscription.unsubscribe();
})
.then(done, done.fail);
}, 30000);
linuxOnlyIt('rename to a non-existing dir', done => {
Promise.resolve()
.then(() => fs.mkdirSync(root + '/rename'))
.then(() => fs.writeFileSync(root + '/rename/a.txt', 'hello world'))
.then(() => {
host.rename(normalize('/rename/a.txt'), normalize('/rename/b/c/d/a.txt'));
if (fs.existsSync(root + '/rename/b/c/d/a.txt')) {
const resContent = host.read(normalize('/rename/b/c/d/a.txt'));
const content = virtualFs.fileBufferToString(resContent);
expect(content).toEqual('hello world');
}
})
.then(done, done.fail);
}, 30000);
});