fixtures.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.FixturePool = void 0;
  6. exports.fixtureParameterNames = fixtureParameterNames;
  7. var _util = require("../util");
  8. var crypto = _interopRequireWildcard(require("crypto"));
  9. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  10. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  11. /**
  12. * Copyright Microsoft Corporation. All rights reserved.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. const kScopeOrder = ['test', 'worker'];
  27. function isFixtureTuple(value) {
  28. return Array.isArray(value) && typeof value[1] === 'object' && ('scope' in value[1] || 'auto' in value[1] || 'option' in value[1] || 'timeout' in value[1]);
  29. }
  30. function isFixtureOption(value) {
  31. return isFixtureTuple(value) && !!value[1].option;
  32. }
  33. class FixturePool {
  34. constructor(fixturesList, onLoadError, parentPool, disallowWorkerFixtures, optionOverrides) {
  35. var _optionOverrides$over;
  36. this.digest = void 0;
  37. this.registrations = void 0;
  38. this._onLoadError = void 0;
  39. this.registrations = new Map(parentPool ? parentPool.registrations : []);
  40. this._onLoadError = onLoadError;
  41. const allOverrides = (_optionOverrides$over = optionOverrides === null || optionOverrides === void 0 ? void 0 : optionOverrides.overrides) !== null && _optionOverrides$over !== void 0 ? _optionOverrides$over : {};
  42. const overrideKeys = new Set(Object.keys(allOverrides));
  43. for (const list of fixturesList) {
  44. this._appendFixtureList(list, !!disallowWorkerFixtures, false);
  45. // Process option overrides immediately after original option definitions,
  46. // so that any test.use() override it.
  47. const selectedOverrides = {};
  48. for (const [key, value] of Object.entries(list.fixtures)) {
  49. if (isFixtureOption(value) && overrideKeys.has(key)) selectedOverrides[key] = [allOverrides[key], value[1]];
  50. }
  51. if (Object.entries(selectedOverrides).length) this._appendFixtureList({
  52. fixtures: selectedOverrides,
  53. location: optionOverrides.location
  54. }, !!disallowWorkerFixtures, true);
  55. }
  56. this.digest = this.validate();
  57. }
  58. _appendFixtureList(list, disallowWorkerFixtures, isOptionsOverride) {
  59. const {
  60. fixtures,
  61. location
  62. } = list;
  63. for (const entry of Object.entries(fixtures)) {
  64. const name = entry[0];
  65. let value = entry[1];
  66. let options;
  67. if (isFixtureTuple(value)) {
  68. var _value$1$auto;
  69. options = {
  70. auto: (_value$1$auto = value[1].auto) !== null && _value$1$auto !== void 0 ? _value$1$auto : false,
  71. scope: value[1].scope || 'test',
  72. option: !!value[1].option,
  73. timeout: value[1].timeout,
  74. customTitle: value[1]._title,
  75. hideStep: value[1]._hideStep
  76. };
  77. value = value[0];
  78. }
  79. let fn = value;
  80. const previous = this.registrations.get(name);
  81. if (previous && options) {
  82. if (previous.scope !== options.scope) {
  83. this._addLoadError(`Fixture "${name}" has already been registered as a { scope: '${previous.scope}' } fixture defined in ${(0, _util.formatLocation)(previous.location)}.`, location);
  84. continue;
  85. }
  86. if (previous.auto !== options.auto) {
  87. this._addLoadError(`Fixture "${name}" has already been registered as a { auto: '${previous.scope}' } fixture defined in ${(0, _util.formatLocation)(previous.location)}.`, location);
  88. continue;
  89. }
  90. } else if (previous) {
  91. options = {
  92. auto: previous.auto,
  93. scope: previous.scope,
  94. option: previous.option,
  95. timeout: previous.timeout,
  96. customTitle: previous.customTitle,
  97. hideStep: undefined
  98. };
  99. } else if (!options) {
  100. options = {
  101. auto: false,
  102. scope: 'test',
  103. option: false,
  104. timeout: undefined,
  105. customTitle: undefined,
  106. hideStep: undefined
  107. };
  108. }
  109. if (!kScopeOrder.includes(options.scope)) {
  110. this._addLoadError(`Fixture "${name}" has unknown { scope: '${options.scope}' }.`, location);
  111. continue;
  112. }
  113. if (options.scope === 'worker' && disallowWorkerFixtures) {
  114. this._addLoadError(`Cannot use({ ${name} }) in a describe group, because it forces a new worker.\nMake it top-level in the test file or put in the configuration file.`, location);
  115. continue;
  116. }
  117. // Overriding option with "undefined" value means setting it to the default value
  118. // from the config or from the original declaration of the option.
  119. if (fn === undefined && options.option && previous) {
  120. let original = previous;
  121. while (!original.optionOverride && original.super) original = original.super;
  122. fn = original.fn;
  123. }
  124. const deps = fixtureParameterNames(fn, location, e => this._onLoadError(e));
  125. const registration = {
  126. id: '',
  127. name,
  128. location,
  129. scope: options.scope,
  130. fn,
  131. auto: options.auto,
  132. option: options.option,
  133. timeout: options.timeout,
  134. customTitle: options.customTitle,
  135. hideStep: options.hideStep,
  136. deps,
  137. super: previous,
  138. optionOverride: isOptionsOverride
  139. };
  140. registrationId(registration);
  141. this.registrations.set(name, registration);
  142. }
  143. }
  144. validate() {
  145. const markers = new Map();
  146. const stack = [];
  147. const visit = registration => {
  148. markers.set(registration, 'visiting');
  149. stack.push(registration);
  150. for (const name of registration.deps) {
  151. const dep = this.resolveDependency(registration, name);
  152. if (!dep) {
  153. if (name === registration.name) this._addLoadError(`Fixture "${registration.name}" references itself, but does not have a base implementation.`, registration.location);else this._addLoadError(`Fixture "${registration.name}" has unknown parameter "${name}".`, registration.location);
  154. continue;
  155. }
  156. if (kScopeOrder.indexOf(registration.scope) > kScopeOrder.indexOf(dep.scope)) {
  157. this._addLoadError(`${registration.scope} fixture "${registration.name}" cannot depend on a ${dep.scope} fixture "${name}" defined in ${(0, _util.formatLocation)(dep.location)}.`, registration.location);
  158. continue;
  159. }
  160. if (!markers.has(dep)) {
  161. visit(dep);
  162. } else if (markers.get(dep) === 'visiting') {
  163. const index = stack.indexOf(dep);
  164. const regs = stack.slice(index, stack.length);
  165. const names = regs.map(r => `"${r.name}"`);
  166. this._addLoadError(`Fixtures ${names.join(' -> ')} -> "${dep.name}" form a dependency cycle: ${regs.map(r => (0, _util.formatLocation)(r.location)).join(' -> ')}`, dep.location);
  167. continue;
  168. }
  169. }
  170. markers.set(registration, 'visited');
  171. stack.pop();
  172. };
  173. const hash = crypto.createHash('sha1');
  174. const names = Array.from(this.registrations.keys()).sort();
  175. for (const name of names) {
  176. const registration = this.registrations.get(name);
  177. visit(registration);
  178. if (registration.scope === 'worker') hash.update(registration.id + ';');
  179. }
  180. return hash.digest('hex');
  181. }
  182. validateFunction(fn, prefix, location) {
  183. for (const name of fixtureParameterNames(fn, location, e => this._onLoadError(e))) {
  184. const registration = this.registrations.get(name);
  185. if (!registration) this._addLoadError(`${prefix} has unknown parameter "${name}".`, location);
  186. }
  187. }
  188. resolveDependency(registration, name) {
  189. if (name === registration.name) return registration.super;
  190. return this.registrations.get(name);
  191. }
  192. _addLoadError(message, location) {
  193. this._onLoadError({
  194. message,
  195. location
  196. });
  197. }
  198. }
  199. exports.FixturePool = FixturePool;
  200. const signatureSymbol = Symbol('signature');
  201. function fixtureParameterNames(fn, location, onError) {
  202. if (typeof fn !== 'function') return [];
  203. if (!fn[signatureSymbol]) fn[signatureSymbol] = innerFixtureParameterNames(fn, location, onError);
  204. return fn[signatureSymbol];
  205. }
  206. function innerFixtureParameterNames(fn, location, onError) {
  207. const text = filterOutComments(fn.toString());
  208. const match = text.match(/(?:async)?(?:\s+function)?[^(]*\(([^)]*)/);
  209. if (!match) return [];
  210. const trimmedParams = match[1].trim();
  211. if (!trimmedParams) return [];
  212. const [firstParam] = splitByComma(trimmedParams);
  213. if (firstParam[0] !== '{' || firstParam[firstParam.length - 1] !== '}') {
  214. onError({
  215. message: 'First argument must use the object destructuring pattern: ' + firstParam,
  216. location
  217. });
  218. return [];
  219. }
  220. const props = splitByComma(firstParam.substring(1, firstParam.length - 1)).map(prop => {
  221. const colon = prop.indexOf(':');
  222. return colon === -1 ? prop.trim() : prop.substring(0, colon).trim();
  223. });
  224. const restProperty = props.find(prop => prop.startsWith('...'));
  225. if (restProperty) {
  226. onError({
  227. message: `Rest property "${restProperty}" is not supported. List all used fixtures explicitly, separated by comma.`,
  228. location
  229. });
  230. return [];
  231. }
  232. return props;
  233. }
  234. function filterOutComments(s) {
  235. const result = [];
  236. let commentState = 'none';
  237. for (let i = 0; i < s.length; ++i) {
  238. if (commentState === 'singleline') {
  239. if (s[i] === '\n') commentState = 'none';
  240. } else if (commentState === 'multiline') {
  241. if (s[i - 1] === '*' && s[i] === '/') commentState = 'none';
  242. } else if (commentState === 'none') {
  243. if (s[i] === '/' && s[i + 1] === '/') {
  244. commentState = 'singleline';
  245. } else if (s[i] === '/' && s[i + 1] === '*') {
  246. commentState = 'multiline';
  247. i += 2;
  248. } else {
  249. result.push(s[i]);
  250. }
  251. }
  252. }
  253. return result.join('');
  254. }
  255. function splitByComma(s) {
  256. const result = [];
  257. const stack = [];
  258. let start = 0;
  259. for (let i = 0; i < s.length; i++) {
  260. if (s[i] === '{' || s[i] === '[') {
  261. stack.push(s[i] === '{' ? '}' : ']');
  262. } else if (s[i] === stack[stack.length - 1]) {
  263. stack.pop();
  264. } else if (!stack.length && s[i] === ',') {
  265. const token = s.substring(start, i).trim();
  266. if (token) result.push(token);
  267. start = i + 1;
  268. }
  269. }
  270. const lastToken = s.substring(start).trim();
  271. if (lastToken) result.push(lastToken);
  272. return result;
  273. }
  274. // name + superId, fn -> id
  275. const registrationIdMap = new Map();
  276. let lastId = 0;
  277. function registrationId(registration) {
  278. if (registration.id) return registration.id;
  279. const key = registration.name + '@@@' + (registration.super ? registrationId(registration.super) : '');
  280. let map = registrationIdMap.get(key);
  281. if (!map) {
  282. map = new Map();
  283. registrationIdMap.set(key, map);
  284. }
  285. if (!map.has(registration.fn)) map.set(registration.fn, String(lastId++));
  286. registration.id = map.get(registration.fn);
  287. return registration.id;
  288. }