( dir: string, targetUri?: string, filePattern?: string, namePattern?: string, )
| 7 | import { fromYamlStep, YamlStep } from "../appdistribution/yaml_helper"; |
| 8 | |
| 9 | export async function parseTestFiles( |
| 10 | dir: string, |
| 11 | targetUri?: string, |
| 12 | filePattern?: string, |
| 13 | namePattern?: string, |
| 14 | ): Promise<TestCaseInvocation[]> { |
| 15 | if (targetUri) { |
| 16 | try { |
| 17 | new URL(targetUri); |
| 18 | } catch (ex) { |
| 19 | const errMsg = |
| 20 | "Invalid URL" + (targetUri.startsWith("http") ? "" : " (must include protocol)"); |
| 21 | throw new FirebaseError(errMsg, { original: getError(ex) }); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | const files = await parseTestFilesRecursive({ testDir: dir, targetUri }); |
| 26 | const idToInvocation = files |
| 27 | .flatMap((file) => file.invocations) |
| 28 | .reduce( |
| 29 | (accumulator, invocation) => { |
| 30 | if (invocation.testCase.id) { |
| 31 | accumulator[invocation.testCase.id] = invocation; |
| 32 | } |
| 33 | return accumulator; |
| 34 | }, |
| 35 | {} as Record<string, TestCaseInvocation>, |
| 36 | ); |
| 37 | |
| 38 | const fileFilterFn = createFilter(filePattern, "file pattern"); |
| 39 | const nameFilterFn = createFilter(namePattern, "test name pattern"); |
| 40 | const filteredInvocations = files |
| 41 | .filter((file) => fileFilterFn(file.path)) |
| 42 | .flatMap((file) => file.invocations) |
| 43 | .filter((invocation) => nameFilterFn(invocation.testCase.displayName)); |
| 44 | |
| 45 | return filteredInvocations.map((invocation) => { |
| 46 | let prerequisiteTestCaseId = invocation.testCase.prerequisiteTestCaseId; |
| 47 | if (prerequisiteTestCaseId === undefined) { |
| 48 | return invocation; |
| 49 | } |
| 50 | |
| 51 | const prerequisiteSteps: TestStep[] = []; |
| 52 | const previousTestCaseIds = new Set<string>(); |
| 53 | while (prerequisiteTestCaseId) { |
| 54 | if (previousTestCaseIds.has(prerequisiteTestCaseId)) { |
| 55 | throw new FirebaseError(`Detected a cycle in prerequisite test cases.`); |
| 56 | } |
| 57 | previousTestCaseIds.add(prerequisiteTestCaseId); |
| 58 | const prerequisiteTestCaseInvocation: TestCaseInvocation | undefined = |
| 59 | idToInvocation[prerequisiteTestCaseId]; |
| 60 | if (prerequisiteTestCaseInvocation === undefined) { |
| 61 | throw new FirebaseError( |
| 62 | `Invalid prerequisiteTestCaseId. There is no test case with id ${prerequisiteTestCaseId}`, |
| 63 | ); |
| 64 | } |
| 65 | prerequisiteSteps.unshift(...prerequisiteTestCaseInvocation.testCase.steps); |
| 66 | prerequisiteTestCaseId = prerequisiteTestCaseInvocation.testCase.prerequisiteTestCaseId; |
searching dependent graphs…