UNPKG

3.52 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.SchematicTestRunner = exports.UnitTestTree = void 0;
11const core_1 = require("@angular-devkit/core");
12const rxjs_1 = require("rxjs");
13const operators_1 = require("rxjs/operators");
14const src_1 = require("../src");
15const call_1 = require("../src/rules/call");
16const node_1 = require("../tasks/node");
17const tools_1 = require("../tools");
18class UnitTestTree extends src_1.DelegateTree {
19 get files() {
20 const result = [];
21 this.visit((path) => result.push(path));
22 return result;
23 }
24 readContent(path) {
25 const buffer = this.read(path);
26 if (buffer === null) {
27 return '';
28 }
29 return buffer.toString();
30 }
31}
32exports.UnitTestTree = UnitTestTree;
33class SchematicTestRunner {
34 constructor(_collectionName, collectionPath) {
35 this._collectionName = _collectionName;
36 this._engineHost = new tools_1.NodeModulesTestEngineHost();
37 this._engine = new src_1.SchematicEngine(this._engineHost);
38 this._engineHost.registerCollection(_collectionName, collectionPath);
39 this._logger = new core_1.logging.Logger('test');
40 const registry = new core_1.schema.CoreSchemaRegistry(src_1.formats.standardFormats);
41 registry.addPostTransform(core_1.schema.transforms.addUndefinedDefaults);
42 this._engineHost.registerOptionsTransform((0, tools_1.validateOptionsWithSchema)(registry));
43 this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.NodePackage);
44 this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.RepositoryInitializer);
45 this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.RunSchematic);
46 this._collection = this._engine.createCollection(this._collectionName);
47 }
48 get engine() {
49 return this._engine;
50 }
51 get logger() {
52 return this._logger;
53 }
54 get tasks() {
55 return [...this._engineHost.tasks];
56 }
57 registerCollection(collectionName, collectionPath) {
58 this._engineHost.registerCollection(collectionName, collectionPath);
59 }
60 runSchematicAsync(schematicName, opts, tree) {
61 const schematic = this._collection.createSchematic(schematicName, true);
62 const host = (0, rxjs_1.of)(tree || new src_1.HostTree());
63 this._engineHost.clearTasks();
64 return schematic
65 .call(opts || {}, host, { logger: this._logger })
66 .pipe((0, operators_1.map)((tree) => new UnitTestTree(tree)));
67 }
68 runExternalSchematicAsync(collectionName, schematicName, opts, tree) {
69 const externalCollection = this._engine.createCollection(collectionName);
70 const schematic = externalCollection.createSchematic(schematicName, true);
71 const host = (0, rxjs_1.of)(tree || new src_1.HostTree());
72 this._engineHost.clearTasks();
73 return schematic
74 .call(opts || {}, host, { logger: this._logger })
75 .pipe((0, operators_1.map)((tree) => new UnitTestTree(tree)));
76 }
77 callRule(rule, tree, parentContext) {
78 const context = this._engine.createContext({}, parentContext);
79 return (0, call_1.callRule)(rule, (0, rxjs_1.of)(tree), context);
80 }
81}
82exports.SchematicTestRunner = SchematicTestRunner;