UNPKG

4.17 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.test = void 0;
11const path_1 = require("../path");
12const buffer_1 = require("./buffer");
13const memory_1 = require("./memory");
14const sync_1 = require("./sync");
15// eslint-disable-next-line @typescript-eslint/no-namespace
16var test;
17(function (test) {
18 class TestHost extends memory_1.SimpleMemoryHost {
19 constructor(map = {}) {
20 super();
21 this._records = [];
22 this._sync = null;
23 for (const filePath of Object.getOwnPropertyNames(map)) {
24 this._write((0, path_1.normalize)(filePath), (0, buffer_1.stringToFileBuffer)(map[filePath]));
25 }
26 }
27 get records() {
28 return [...this._records];
29 }
30 clearRecords() {
31 this._records = [];
32 }
33 get files() {
34 const sync = this.sync;
35 function _visit(p) {
36 return sync
37 .list(p)
38 .map((fragment) => (0, path_1.join)(p, fragment))
39 .reduce((files, path) => {
40 if (sync.isDirectory(path)) {
41 return files.concat(_visit(path));
42 }
43 else {
44 return files.concat(path);
45 }
46 }, []);
47 }
48 return _visit((0, path_1.normalize)('/'));
49 }
50 get sync() {
51 if (!this._sync) {
52 this._sync = new sync_1.SyncDelegateHost(this);
53 }
54 return this._sync;
55 }
56 clone() {
57 const newHost = new TestHost();
58 newHost._cache = new Map(this._cache);
59 return newHost;
60 }
61 // Override parents functions to keep a record of all operators that were done.
62 _write(path, content) {
63 this._records.push({ kind: 'write', path });
64 return super._write(path, content);
65 }
66 _read(path) {
67 this._records.push({ kind: 'read', path });
68 return super._read(path);
69 }
70 _delete(path) {
71 this._records.push({ kind: 'delete', path });
72 return super._delete(path);
73 }
74 _rename(from, to) {
75 this._records.push({ kind: 'rename', from, to });
76 return super._rename(from, to);
77 }
78 _list(path) {
79 this._records.push({ kind: 'list', path });
80 return super._list(path);
81 }
82 _exists(path) {
83 this._records.push({ kind: 'exists', path });
84 return super._exists(path);
85 }
86 _isDirectory(path) {
87 this._records.push({ kind: 'isDirectory', path });
88 return super._isDirectory(path);
89 }
90 _isFile(path) {
91 this._records.push({ kind: 'isFile', path });
92 return super._isFile(path);
93 }
94 _stat(path) {
95 this._records.push({ kind: 'stat', path });
96 return super._stat(path);
97 }
98 _watch(path, options) {
99 this._records.push({ kind: 'watch', path });
100 return super._watch(path, options);
101 }
102 $write(path, content) {
103 return super._write((0, path_1.normalize)(path), (0, buffer_1.stringToFileBuffer)(content));
104 }
105 $read(path) {
106 return (0, buffer_1.fileBufferToString)(super._read((0, path_1.normalize)(path)));
107 }
108 $list(path) {
109 return super._list((0, path_1.normalize)(path));
110 }
111 $exists(path) {
112 return super._exists((0, path_1.normalize)(path));
113 }
114 $isDirectory(path) {
115 return super._isDirectory((0, path_1.normalize)(path));
116 }
117 $isFile(path) {
118 return super._isFile((0, path_1.normalize)(path));
119 }
120 }
121 test.TestHost = TestHost;
122})(test = exports.test || (exports.test = {}));