schematic-test-runner.js 3.4 KB

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