|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VSCode Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2024 the VSCode Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VSCode Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import * as vscode from "vscode"; |
| 16 | + |
| 17 | +type ProcessResult = { |
| 18 | + testItems: vscode.TestItem[]; |
| 19 | + xcTestArgs: string[]; |
| 20 | + swiftTestArgs: string[]; |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Given a `TestRunRequest`, produces the lists of |
| 25 | + * XCTests and swift-testing tests to run. |
| 26 | + */ |
| 27 | +export class TestRunArguments { |
| 28 | + public testItems: vscode.TestItem[]; |
| 29 | + public xcTestArgs: string[]; |
| 30 | + public swiftTestArgs: string[]; |
| 31 | + |
| 32 | + constructor(request: vscode.TestRunRequest) { |
| 33 | + const { testItems, xcTestArgs, swiftTestArgs } = this.createTestLists(request); |
| 34 | + this.testItems = testItems; |
| 35 | + this.xcTestArgs = xcTestArgs; |
| 36 | + this.swiftTestArgs = swiftTestArgs; |
| 37 | + } |
| 38 | + |
| 39 | + public get hasXCTests(): boolean { |
| 40 | + return this.xcTestArgs.length > 0; |
| 41 | + } |
| 42 | + |
| 43 | + public get hasSwiftTestingTests(): boolean { |
| 44 | + return this.swiftTestArgs.length > 0; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Construct test item list from TestRequest |
| 49 | + * @returns list of test items to run and list of test for XCTest arguments |
| 50 | + */ |
| 51 | + private createTestLists(request: vscode.TestRunRequest): { |
| 52 | + testItems: vscode.TestItem[]; |
| 53 | + xcTestArgs: string[]; |
| 54 | + swiftTestArgs: string[]; |
| 55 | + } { |
| 56 | + const includes = request.include ?? []; |
| 57 | + return includes.reduce(this.createTestItemReducer(request.include, request.exclude), { |
| 58 | + testItems: [], |
| 59 | + xcTestArgs: [], |
| 60 | + swiftTestArgs: [], |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private createTestItemReducer( |
| 65 | + include: readonly vscode.TestItem[] | undefined, |
| 66 | + exclude: readonly vscode.TestItem[] | undefined |
| 67 | + ): (previousValue: ProcessResult, testItem: vscode.TestItem) => ProcessResult { |
| 68 | + return (previousValue, testItem) => { |
| 69 | + const { testItems, swiftTestArgs, xcTestArgs } = this.processTestItem( |
| 70 | + testItem, |
| 71 | + include, |
| 72 | + exclude |
| 73 | + ); |
| 74 | + |
| 75 | + // If no children were added we can skip adding this parent. |
| 76 | + if (xcTestArgs.length + swiftTestArgs.length === 0) { |
| 77 | + return previousValue; |
| 78 | + } else if (xcTestArgs.length + swiftTestArgs.length === testItem.children.size) { |
| 79 | + // If we've added all the children to the list of arguments, just add |
| 80 | + // the parent instead of each individual child. This crafts a minimal set |
| 81 | + // of test/suites that run all the test cases requested with the smallest list |
| 82 | + // of arguments. |
| 83 | + const isXCTest = !!testItem.tags.find(tag => tag.id === "XCTest"); |
| 84 | + return { |
| 85 | + testItems: [...previousValue.testItems, ...testItems], |
| 86 | + swiftTestArgs: [ |
| 87 | + ...previousValue.swiftTestArgs, |
| 88 | + ...(!isXCTest ? [testItem.id] : []), |
| 89 | + ], |
| 90 | + xcTestArgs: [...previousValue.xcTestArgs, ...(isXCTest ? [testItem.id] : [])], |
| 91 | + }; |
| 92 | + } else { |
| 93 | + // If we've only added some of the children the append to our test list |
| 94 | + return { |
| 95 | + testItems: [...previousValue.testItems, ...testItems], |
| 96 | + swiftTestArgs: [...previousValue.swiftTestArgs, ...swiftTestArgs], |
| 97 | + xcTestArgs: [...previousValue.xcTestArgs, ...xcTestArgs], |
| 98 | + }; |
| 99 | + } |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + private processTestItem( |
| 104 | + testItem: vscode.TestItem, |
| 105 | + include?: readonly vscode.TestItem[], |
| 106 | + exclude?: readonly vscode.TestItem[] |
| 107 | + ): ProcessResult { |
| 108 | + // Skip tests the user asked to exclude |
| 109 | + if (exclude?.includes(testItem)) { |
| 110 | + return { |
| 111 | + testItems: [], |
| 112 | + xcTestArgs: [], |
| 113 | + swiftTestArgs: [], |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + const testItems: vscode.TestItem[] = []; |
| 118 | + const xcTestArgs: string[] = []; |
| 119 | + const swiftTestArgs: string[] = []; |
| 120 | + |
| 121 | + // If this test item is included or we are including everything |
| 122 | + if (include?.includes(testItem) || !include) { |
| 123 | + testItems.push(testItem); |
| 124 | + |
| 125 | + // Only add leaf items to testArgs |
| 126 | + if (testItem.children.size === 0) { |
| 127 | + if (testItem.tags.find(tag => tag.id === "XCTest")) { |
| 128 | + xcTestArgs.push(testItem.id); |
| 129 | + } else { |
| 130 | + swiftTestArgs.push(testItem.id); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return this.reduceTestItemChildren( |
| 136 | + testItem.children, |
| 137 | + this.createTestItemReducer(undefined, exclude), |
| 138 | + { |
| 139 | + testItems, |
| 140 | + xcTestArgs, |
| 141 | + swiftTestArgs, |
| 142 | + } |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + private reduceTestItemChildren<U>( |
| 147 | + array: vscode.TestItemCollection, |
| 148 | + callback: (accumulator: U, currentValue: vscode.TestItem) => U, |
| 149 | + initialValue: U |
| 150 | + ): U { |
| 151 | + let accumulator = initialValue; |
| 152 | + array.forEach(currentValue => { |
| 153 | + accumulator = callback(accumulator, currentValue); |
| 154 | + }); |
| 155 | + return accumulator; |
| 156 | + } |
| 157 | +} |
0 commit comments