schematic-test-runner.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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;
  35. _engine;
  36. _collection;
  37. _logger;
  38. constructor(_collectionName, collectionPath) {
  39. this._collectionName = _collectionName;
  40. this._engineHost = new tools_1.NodeModulesTestEngineHost([
  41. // Leverage the specified collection path as an additional base for resolving other
  42. // collections by name. This is useful in e.g. pnpm workspaces where `@angular-devkit/schematics`
  43. // doesn't necessarily have access to e.g. `@schematics/angular`.
  44. collectionPath,
  45. ]);
  46. this._engine = new src_1.SchematicEngine(this._engineHost);
  47. this._engineHost.registerCollection(_collectionName, collectionPath);
  48. this._logger = new core_1.logging.Logger('test');
  49. const registry = new core_1.schema.CoreSchemaRegistry(src_1.formats.standardFormats);
  50. registry.addPostTransform(core_1.schema.transforms.addUndefinedDefaults);
  51. this._engineHost.registerOptionsTransform((0, tools_1.validateOptionsWithSchema)(registry));
  52. this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.NodePackage);
  53. this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.RepositoryInitializer);
  54. this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.RunSchematic);
  55. this._collection = this._engine.createCollection(this._collectionName);
  56. }
  57. get engine() {
  58. return this._engine;
  59. }
  60. get logger() {
  61. return this._logger;
  62. }
  63. get tasks() {
  64. return [...this._engineHost.tasks];
  65. }
  66. registerCollection(collectionName, collectionPath) {
  67. this._engineHost.registerCollection(collectionName, collectionPath);
  68. }
  69. async runSchematic(schematicName, opts, tree) {
  70. const schematic = this._collection.createSchematic(schematicName, true);
  71. const host = (0, rxjs_1.of)(tree || new src_1.HostTree());
  72. this._engineHost.clearTasks();
  73. const newTree = await (0, rxjs_1.lastValueFrom)(schematic.call(opts || {}, host, { logger: this._logger }));
  74. return new UnitTestTree(newTree);
  75. }
  76. async runExternalSchematic(collectionName, schematicName, opts, tree) {
  77. const externalCollection = this._engine.createCollection(collectionName);
  78. const schematic = externalCollection.createSchematic(schematicName, true);
  79. const host = (0, rxjs_1.of)(tree || new src_1.HostTree());
  80. this._engineHost.clearTasks();
  81. const newTree = await (0, rxjs_1.lastValueFrom)(schematic.call(opts || {}, host, { logger: this._logger }));
  82. return new UnitTestTree(newTree);
  83. }
  84. callRule(rule, tree, parentContext) {
  85. const context = this._engine.createContext({}, parentContext);
  86. return (0, call_1.callRule)(rule, (0, rxjs_1.of)(tree), context);
  87. }
  88. }
  89. exports.SchematicTestRunner = SchematicTestRunner;