schematic-test-runner.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const core_1 = require("@angular-devkit/core");
  11. const rxjs_1 = require("rxjs");
  12. const operators_1 = require("rxjs/operators");
  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. constructor(_collectionName, collectionPath) {
  34. this._collectionName = _collectionName;
  35. this._engineHost = new tools_1.NodeModulesTestEngineHost();
  36. this._engine = new src_1.SchematicEngine(this._engineHost);
  37. this._engineHost.registerCollection(_collectionName, collectionPath);
  38. this._logger = new core_1.logging.Logger('test');
  39. const registry = new core_1.schema.CoreSchemaRegistry(src_1.formats.standardFormats);
  40. registry.addPostTransform(core_1.schema.transforms.addUndefinedDefaults);
  41. this._engineHost.registerOptionsTransform(tools_1.validateOptionsWithSchema(registry));
  42. this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.NodePackage);
  43. this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.RepositoryInitializer);
  44. this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.RunSchematic);
  45. this._engineHost.registerTaskExecutor(node_1.BuiltinTaskExecutor.TslintFix);
  46. this._collection = this._engine.createCollection(this._collectionName);
  47. }
  48. get engine() { return this._engine; }
  49. get logger() { return this._logger; }
  50. get tasks() { return [...this._engineHost.tasks]; }
  51. registerCollection(collectionName, collectionPath) {
  52. this._engineHost.registerCollection(collectionName, collectionPath);
  53. }
  54. runSchematicAsync(schematicName, opts, tree) {
  55. const schematic = this._collection.createSchematic(schematicName, true);
  56. const host = rxjs_1.of(tree || new src_1.HostTree);
  57. this._engineHost.clearTasks();
  58. return schematic.call(opts || {}, host, { logger: this._logger })
  59. .pipe(operators_1.map(tree => new UnitTestTree(tree)));
  60. }
  61. /**
  62. * @deprecated Since v8.0.0 - Use {@link SchematicTestRunner.runSchematicAsync} instead.
  63. * All schematics can potentially be async.
  64. * This synchronous variant will fail if the schematic, any of its rules, or any schematics
  65. * it calls are async.
  66. */
  67. runSchematic(schematicName, opts, tree) {
  68. const schematic = this._collection.createSchematic(schematicName, true);
  69. let result = null;
  70. let error;
  71. const host = rxjs_1.of(tree || new src_1.HostTree);
  72. this._engineHost.clearTasks();
  73. schematic.call(opts || {}, host, { logger: this._logger })
  74. .subscribe(t => result = new UnitTestTree(t), e => error = e);
  75. if (error) {
  76. throw error;
  77. }
  78. if (result === null) {
  79. throw new Error('Schematic is async, please use runSchematicAsync');
  80. }
  81. return result;
  82. }
  83. runExternalSchematicAsync(collectionName, schematicName, opts, tree) {
  84. const externalCollection = this._engine.createCollection(collectionName);
  85. const schematic = externalCollection.createSchematic(schematicName, true);
  86. const host = rxjs_1.of(tree || new src_1.HostTree);
  87. this._engineHost.clearTasks();
  88. return schematic.call(opts || {}, host, { logger: this._logger })
  89. .pipe(operators_1.map(tree => new UnitTestTree(tree)));
  90. }
  91. /**
  92. * @deprecated Since v8.0.0 - Use {@link SchematicTestRunner.runExternalSchematicAsync} instead.
  93. * All schematics can potentially be async.
  94. * This synchronous variant will fail if the schematic, any of its rules, or any schematics
  95. * it calls are async.
  96. */
  97. runExternalSchematic(collectionName, schematicName, opts, tree) {
  98. const externalCollection = this._engine.createCollection(collectionName);
  99. const schematic = externalCollection.createSchematic(schematicName, true);
  100. let result = null;
  101. const host = rxjs_1.of(tree || new src_1.HostTree);
  102. this._engineHost.clearTasks();
  103. schematic.call(opts || {}, host, { logger: this._logger })
  104. .subscribe(t => result = new UnitTestTree(t));
  105. if (result === null) {
  106. throw new Error('Schematic is async, please use runSchematicAsync');
  107. }
  108. return result;
  109. }
  110. callRule(rule, tree, parentContext) {
  111. const context = this._engine.createContext({}, parentContext);
  112. return call_1.callRule(rule, rxjs_1.of(tree), context);
  113. }
  114. }
  115. exports.SchematicTestRunner = SchematicTestRunner;