Skip to content

Handle multiple test items passed to runTestsMultipleTimes and runTestsUntilFailure #1724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Added code lenses to run suites/tests, configurable with the `swift.showTestCodeLenses` setting ([#1698](https://github.com/swiftlang/vscode-swift/pull/1698))
- New `swift.excludePathsFromActivation` setting to ignore specified sub-folders from being activated as projects ([#1693](https://github.com/swiftlang/vscode-swift/pull/1693))

### Fixed

- `Run multiple times...` and `Run until failure...` will now work when multiple tests are selected ([#1724](https://github.com/swiftlang/vscode-swift/pull/1724))

## 2.8.0 - 2025-07-14

### Added
Expand Down
62 changes: 54 additions & 8 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export enum Commands {
SHOW_NESTED_DEPENDENCIES_LIST = "swift.nestedDependenciesList",
UPDATE_DEPENDENCIES = "swift.updateDependencies",
RUN_TESTS_MULTIPLE_TIMES = "swift.runTestsMultipleTimes",
RUN_TESTS_UNTIL_FAILURE = "swift.runTestsUntilFailure",
RESET_PACKAGE = "swift.resetPackage",
USE_LOCAL_DEPENDENCY = "swift.useLocalDependency",
UNEDIT_DEPENDENCY = "swift.uneditDependency",
Expand Down Expand Up @@ -133,16 +134,24 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
async target => await debugBuild(ctx, ...unwrapTreeItem(target))
),
vscode.commands.registerCommand(Commands.CLEAN_BUILD, async () => await cleanBuild(ctx)),
vscode.commands.registerCommand(Commands.RUN_TESTS_MULTIPLE_TIMES, async (item, count) => {
if (ctx.currentFolder) {
return await runTestMultipleTimes(ctx.currentFolder, item, false, count);
vscode.commands.registerCommand(
Commands.RUN_TESTS_MULTIPLE_TIMES,
async (...args: (vscode.TestItem | number)[]) => {
const { testItems, count } = extractTestItemsAndCount(...args);
if (ctx.currentFolder) {
return await runTestMultipleTimes(ctx.currentFolder, testItems, false, count);
}
}
}),
vscode.commands.registerCommand("swift.runTestsUntilFailure", async (item, count) => {
if (ctx.currentFolder) {
return await runTestMultipleTimes(ctx.currentFolder, item, true, count);
),
vscode.commands.registerCommand(
Commands.RUN_TESTS_UNTIL_FAILURE,
async (...args: (vscode.TestItem | number)[]) => {
const { testItems, count } = extractTestItemsAndCount(...args);
if (ctx.currentFolder) {
return await runTestMultipleTimes(ctx.currentFolder, testItems, true, count);
}
}
}),
),
// Note: switchPlatform is only available on macOS and Swift 6.1 or later
// (gated in `package.json`) because it's the only OS and toolchain combination that
// has Darwin SDKs available and supports code editing with SourceKit-LSP
Expand Down Expand Up @@ -267,6 +276,43 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
];
}

/**
* Extracts an array of vscode.TestItem and count from the provided varargs. Effectively, this
* converts a varargs function from accepting both numbers and test items to:
*
* function (...testItems: vscode.TestItem[], count?: number): void;
*
* The VS Code testing view sends test items via varargs, but we have a couple testing commands that
* also accept a final count parameter. We have to find the count parameter ourselves since JavaScript
* only supports varargs at the end of an argument list.
*/
function extractTestItemsAndCount(...args: (vscode.TestItem | number)[]): {
testItems: vscode.TestItem[];
count?: number;
} {
const result = args.reduceRight<{
testItems: vscode.TestItem[];
count?: number;
}>(
(result, arg, index) => {
if (typeof arg === "number" && index === args.length - 1) {
result.count = arg;
return result;
} else if (typeof arg === "object") {
result.testItems.push(arg);
return result;
} else {
throw new Error(`Unexpected argument ${arg} at index ${index}`);
}
},
{ testItems: [] }
);
if (result.testItems.length === 0) {
throw new Error("At least one TestItem must be provided");
}
return result;
}

/**
* Certain commands can be called via a vscode TreeView, which will pass a {@link CommandNode} object.
* If the command is called via a command palette or other means, the target will be a string.
Expand Down
4 changes: 2 additions & 2 deletions src/commands/testMultipleTimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { FolderContext } from "../FolderContext";
*/
export async function runTestMultipleTimes(
currentFolder: FolderContext,
test: vscode.TestItem,
tests: vscode.TestItem[],
untilFailure: boolean,
count: number | undefined = undefined,
testRunner?: () => Promise<TestRunState>
Expand All @@ -52,7 +52,7 @@ export async function runTestMultipleTimes(
const testExplorer = currentFolder.testExplorer;
const runner = new TestRunner(
TestKind.standard,
new vscode.TestRunRequest([test]),
new vscode.TestRunRequest(tests),
currentFolder,
testExplorer.controller,
token.token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ suite("Test Multiple Times Command Test Suite", () => {
});

test("Runs successfully after testing 0 times", async () => {
const runState = await runTestMultipleTimes(folderContext, testItem, false, 0);
const runState = await runTestMultipleTimes(folderContext, [testItem], false, 0);
expect(runState).to.be.an("array").that.is.empty;
});

test("Runs successfully after testing 3 times", async () => {
const runState = await runTestMultipleTimes(folderContext, testItem, false, 3, () =>
const runState = await runTestMultipleTimes(folderContext, [testItem], false, 3, () =>
Promise.resolve(TestRunProxy.initialTestRunState())
);

Expand All @@ -61,7 +61,7 @@ suite("Test Multiple Times Command Test Suite", () => {
failed: [{ test: testItem, message: new vscode.TestMessage("oh no") }],
};
let ctr = 0;
const runState = await runTestMultipleTimes(folderContext, testItem, true, 3, () => {
const runState = await runTestMultipleTimes(folderContext, [testItem], true, 3, () => {
ctr += 1;
if (ctr === 2) {
return Promise.resolve(failure);
Expand Down