|
| 1 | +// The check that keeps ios.yml's hand-written `-only-testing:` list honest is itself only |
| 2 | +// as good as its two parsers, and both of its inputs are files nobody edits with this check |
| 3 | +// in mind. So: the real tree must pass, and a planted typo in the real workflow text must |
| 4 | +// fail. Synthetic sources cover the shapes the real tree happens not to contain today. |
| 5 | + |
| 6 | +import fs from 'node:fs'; |
| 7 | +import path from 'node:path'; |
| 8 | +import { describe, expect, test } from 'vitest'; |
| 9 | +import { |
| 10 | + buildReport, |
| 11 | + formatSummary, |
| 12 | + loadReport, |
| 13 | + parseDeclaredTests, |
| 14 | + parseSelectedTests, |
| 15 | + PR_WORKFLOW_FILE, |
| 16 | + reportFailures, |
| 17 | + RUNNER_TESTS_DIR, |
| 18 | + readSwiftSources, |
| 19 | +} from '../check-xctest-selection.ts'; |
| 20 | + |
| 21 | +const repoRoot = path.resolve(import.meta.dirname, '..', '..'); |
| 22 | +const TARGET = 'AgentDeviceRunnerUITests'; |
| 23 | + |
| 24 | +function source(text: string) { |
| 25 | + return [{ file: 'RunnerTests+Fixture.swift', text }]; |
| 26 | +} |
| 27 | + |
| 28 | +describe('the real tree', () => { |
| 29 | + test('every `-only-testing:` entry in ios.yml names a declared test', () => { |
| 30 | + expect(reportFailures(loadReport(repoRoot))).toEqual([]); |
| 31 | + }); |
| 32 | + |
| 33 | + test('the PR lane selects a real subset — some tests run only in the nightly', () => { |
| 34 | + const report = loadReport(repoRoot); |
| 35 | + // Not pinned to today's 37/153: the point is that the list is a proper subset, so |
| 36 | + // neither "the filter is gone" nor "the scan found nothing" reads as healthy. |
| 37 | + expect(report.selected.length).toBeGreaterThan(0); |
| 38 | + expect(report.declared.length).toBeGreaterThan(report.selected.length); |
| 39 | + for (const entry of report.selected) { |
| 40 | + expect(entry.identifier.startsWith(`${TARGET}/`)).toBe(true); |
| 41 | + } |
| 42 | + // The ratio is the whole point of the output — a passing run has to report it, or |
| 43 | + // nobody reading CI logs can see the PR lane shrinking. |
| 44 | + expect(formatSummary(report)).toContain( |
| 45 | + `${report.selected.length} of ${report.declared.length}`, |
| 46 | + ); |
| 47 | + expect(formatSummary(report)).toContain( |
| 48 | + `the other ${report.declared.length - report.selected.length} run in`, |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + test('every declared test is addressable as the identifier a filter would use', () => { |
| 53 | + // The count is the load-bearing claim: it must equal the `func test` methods in the |
| 54 | + // sources, derived here the crude way the issue counted them. |
| 55 | + const directory = path.join(repoRoot, RUNNER_TESTS_DIR); |
| 56 | + const grepped = readSwiftSources(directory).reduce( |
| 57 | + (total, entry) => total + (entry.text.match(/^ {2}(?:[\w@]+ )*func test/gm)?.length ?? 0), |
| 58 | + 0, |
| 59 | + ); |
| 60 | + expect(loadReport(repoRoot).declared).toHaveLength(grepped); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('a planted typo', () => { |
| 65 | + test('a renamed test in the workflow list is reported with its line', () => { |
| 66 | + const workflow = fs.readFileSync(path.join(repoRoot, PR_WORKFLOW_FILE), 'utf8'); |
| 67 | + const first = parseSelectedTests(workflow)[0]; |
| 68 | + if (!first) throw new Error('ios.yml has no -only-testing entries to plant a typo in'); |
| 69 | + const typo = `${first.identifier}Renamed`; |
| 70 | + const report = buildReport( |
| 71 | + TARGET, |
| 72 | + readSwiftSources(path.join(repoRoot, RUNNER_TESTS_DIR)), |
| 73 | + workflow.replace(first.identifier, typo), |
| 74 | + ); |
| 75 | + |
| 76 | + expect(report.unknown).toEqual([{ identifier: typo, line: first.line }]); |
| 77 | + expect(reportFailures(report).join('\n')).toContain(typo); |
| 78 | + }); |
| 79 | + |
| 80 | + test('a deleted test is reported even though the surviving list still passes', () => { |
| 81 | + const swift = source( |
| 82 | + 'extension RunnerTests {\n func testKept() {}\n func testGone() {}\n}\n', |
| 83 | + ); |
| 84 | + const workflow = [ |
| 85 | + ` -only-testing:${TARGET}/RunnerTests/testKept \\`, |
| 86 | + ` -only-testing:${TARGET}/RunnerTests/testGone`, |
| 87 | + ].join('\n'); |
| 88 | + |
| 89 | + const before = buildReport(TARGET, swift, workflow); |
| 90 | + expect(before.unknown).toEqual([]); |
| 91 | + |
| 92 | + const after = buildReport( |
| 93 | + TARGET, |
| 94 | + source('extension RunnerTests {\n func testKept() {}\n}\n'), |
| 95 | + workflow, |
| 96 | + ); |
| 97 | + expect(after.unknown.map((entry) => entry.identifier)).toEqual([ |
| 98 | + `${TARGET}/RunnerTests/testGone`, |
| 99 | + ]); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('the declaration scan', () => { |
| 104 | + test('binds a method to the top-level type that encloses it', () => { |
| 105 | + expect( |
| 106 | + parseDeclaredTests( |
| 107 | + TARGET, |
| 108 | + source( |
| 109 | + 'final class RunnerTests: XCTestCase {\n func testInClass() {}\n}\n\n' + |
| 110 | + 'extension RunnerTests {\n func testInExtension() throws {}\n}\n\n' + |
| 111 | + 'final class OtherTests: XCTestCase {\n func testElsewhere() async throws {}\n}\n', |
| 112 | + ), |
| 113 | + ), |
| 114 | + ).toEqual([ |
| 115 | + `${TARGET}/OtherTests/testElsewhere`, |
| 116 | + `${TARGET}/RunnerTests/testInClass`, |
| 117 | + `${TARGET}/RunnerTests/testInExtension`, |
| 118 | + ]); |
| 119 | + }); |
| 120 | + |
| 121 | + test('ignores declarations no filter could address', () => { |
| 122 | + expect( |
| 123 | + parseDeclaredTests( |
| 124 | + TARGET, |
| 125 | + source( |
| 126 | + 'extension RunnerTests {\n' + |
| 127 | + // A helper type declared inside a test body must not capture the methods after |
| 128 | + // it, and `class func` must not read as a type declaration named `func`. |
| 129 | + ' class func makeHelper() {}\n' + |
| 130 | + ' func testWithNestedHelper() {\n' + |
| 131 | + ' final class ResultBox {}\n' + |
| 132 | + ' func testLocal() {}\n' + |
| 133 | + ' }\n' + |
| 134 | + ' // func testCommentedOut() {}\n' + |
| 135 | + ' func testAfterNesting() {}\n' + |
| 136 | + '}\n', |
| 137 | + ), |
| 138 | + ), |
| 139 | + ).toEqual([ |
| 140 | + `${TARGET}/RunnerTests/testAfterNesting`, |
| 141 | + `${TARGET}/RunnerTests/testWithNestedHelper`, |
| 142 | + ]); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('the workflow scan', () => { |
| 147 | + test('reads every entry on the multi-line xcodebuild invocation', () => { |
| 148 | + expect( |
| 149 | + parseSelectedTests( |
| 150 | + [ |
| 151 | + ' xcodebuild test-without-building \\', |
| 152 | + ' -xctestrun "$XCTESTRUN_PATH" \\', |
| 153 | + ` -only-testing:${TARGET}/RunnerTests/testOne \\`, |
| 154 | + ` -only-testing:${TARGET}/RunnerTests/testTwo`, |
| 155 | + ].join('\n'), |
| 156 | + ), |
| 157 | + ).toEqual([ |
| 158 | + { identifier: `${TARGET}/RunnerTests/testOne`, line: 3 }, |
| 159 | + { identifier: `${TARGET}/RunnerTests/testTwo`, line: 4 }, |
| 160 | + ]); |
| 161 | + }); |
| 162 | + |
| 163 | + test('leaves another target alone rather than guessing about sources it cannot see', () => { |
| 164 | + const report = buildReport( |
| 165 | + TARGET, |
| 166 | + source('extension RunnerTests {\n func testOne() {}\n}\n'), |
| 167 | + `-only-testing:SomeOtherTarget/OtherTests/testUnknown\n-only-testing:${TARGET}/RunnerTests/testOne`, |
| 168 | + ); |
| 169 | + expect(report.unknown).toEqual([]); |
| 170 | + expect(report.selected).toHaveLength(2); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +describe('the blind-parse guards', () => { |
| 175 | + test('an empty declaration scan fails instead of reporting a healthy list', () => { |
| 176 | + const report = buildReport(TARGET, source('// nothing here\n'), '-only-testing:a/b/c'); |
| 177 | + expect(reportFailures(report).join('\n')).toContain('declaration scan is broken'); |
| 178 | + }); |
| 179 | + |
| 180 | + test('an empty workflow scan fails instead of reporting a healthy list', () => { |
| 181 | + const report = buildReport( |
| 182 | + TARGET, |
| 183 | + source('extension RunnerTests {\n func testOne() {}\n}\n'), |
| 184 | + '', |
| 185 | + ); |
| 186 | + expect(reportFailures(report).join('\n')).toContain('stopped filtering'); |
| 187 | + }); |
| 188 | +}); |
0 commit comments