1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.SchematicTestRunner = exports.UnitTestTree = void 0;
|
11 | const core_1 = require("@angular-devkit/core");
|
12 | const rxjs_1 = require("rxjs");
|
13 | const operators_1 = require("rxjs/operators");
|
14 | const src_1 = require("../src");
|
15 | const call_1 = require("../src/rules/call");
|
16 | const node_1 = require("../tasks/node");
|
17 | const tools_1 = require("../tools");
|
18 | class 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 | }
|
32 | exports.UnitTestTree = UnitTestTree;
|
33 | class 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 | }
|
82 | exports.SchematicTestRunner = SchematicTestRunner;
|