UNPKG

3.34 kBJavaScriptView Raw
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.io/license
8 */
9var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 var desc = Object.getOwnPropertyDescriptor(m, k);
12 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
13 desc = { enumerable: true, get: function() { return m[k]; } };
14 }
15 Object.defineProperty(o, k2, desc);
16}) : (function(o, m, k, k2) {
17 if (k2 === undefined) k2 = k;
18 o[k2] = m[k];
19}));
20var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21 Object.defineProperty(o, "default", { enumerable: true, value: v });
22}) : function(o, v) {
23 o["default"] = v;
24});
25var __importStar = (this && this.__importStar) || function (mod) {
26 if (mod && mod.__esModule) return mod;
27 var result = {};
28 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29 __setModuleDefault(result, mod);
30 return result;
31};
32Object.defineProperty(exports, "__esModule", { value: true });
33exports.findTests = void 0;
34const fs_1 = require("fs");
35const glob = __importStar(require("glob"));
36const path_1 = require("path");
37const is_directory_1 = require("../../utils/is-directory");
38// go through all patterns and find unique list of files
39function findTests(patterns, cwd, workspaceRoot) {
40 return patterns.reduce((files, pattern) => {
41 const relativePathToMain = cwd.replace(workspaceRoot, '').substr(1); // remove leading slash
42 const tests = findMatchingTests(pattern, cwd, relativePathToMain);
43 tests.forEach((file) => {
44 if (!files.includes(file)) {
45 files.push(file);
46 }
47 });
48 return files;
49 }, []);
50}
51exports.findTests = findTests;
52function findMatchingTests(pattern, cwd, relativePathToMain) {
53 // normalize pattern, glob lib only accepts forward slashes
54 pattern = pattern.replace(/\\/g, '/');
55 relativePathToMain = relativePathToMain.replace(/\\/g, '/');
56 // remove relativePathToMain to support relative paths from root
57 // such paths are easy to get when running scripts via IDEs
58 if (pattern.startsWith(relativePathToMain + '/')) {
59 pattern = pattern.substr(relativePathToMain.length + 1); // +1 to include slash
60 }
61 // special logic when pattern does not look like a glob
62 if (!glob.hasMagic(pattern)) {
63 if ((0, is_directory_1.isDirectory)((0, path_1.join)(cwd, pattern))) {
64 pattern = `${pattern}/**/*.spec.@(ts|tsx)`;
65 }
66 else {
67 // see if matching spec file exists
68 const extension = (0, path_1.extname)(pattern);
69 const matchingSpec = `${(0, path_1.basename)(pattern, extension)}.spec${extension}`;
70 if ((0, fs_1.existsSync)((0, path_1.join)(cwd, (0, path_1.dirname)(pattern), matchingSpec))) {
71 pattern = (0, path_1.join)((0, path_1.dirname)(pattern), matchingSpec).replace(/\\/g, '/');
72 }
73 }
74 }
75 const files = glob.sync(pattern, {
76 cwd,
77 });
78 return files;
79}