Skip to content

Commit

Permalink
Use build description to check for unified testing binary (#1004)
Browse files Browse the repository at this point in the history
We were checking the `mtime` of the `<ProductName>.swift-testing` binary
to determine if it was produced by the latest build. The issue with this
is that when you run tests over and over without making any code changes
the binary is never rewritten because it is already up to date.

Instead of checking the `mtime` of the .swift-testing binary in the build
folder to determine if it was produced, use the build `description.json`
and check the builtTestProducts list to see if the legacy
`.swift-testing` binary was produced by the latest build.
  • Loading branch information
plemarquand authored Aug 8, 2024
1 parent 51d5be1 commit 9f8cf10
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
4 changes: 0 additions & 4 deletions src/TestExplorer/TestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ export class TestRunner {
fifoPipePath,
this.testKind,
this.testArgs.swiftTestArgs,
new Date(),
true
);

Expand Down Expand Up @@ -700,8 +699,6 @@ export class TestRunner {
});
subscriptions.push(buildTask);

const buildStartTime = new Date();

// Perform a build all before the tests are run. We want to avoid the "Debug Anyway" dialog
// since choosing that with no prior build, when debugging swift-testing tests, will cause
// the extension to start listening to the fifo pipe when no results will be produced,
Expand Down Expand Up @@ -729,7 +726,6 @@ export class TestRunner {
fifoPipePath,
this.testKind,
this.testArgs.swiftTestArgs,
buildStartTime,
true
);

Expand Down
31 changes: 18 additions & 13 deletions src/debugger/buildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class TestingDebugConfigurationFactory {
fifoPipePath: string,
testKind: TestKind,
testList: string[],
buildStartTime: Date,
expandEnvVariables = false
): Promise<vscode.DebugConfiguration | null> {
return new TestingDebugConfigurationFactory(
Expand All @@ -47,7 +46,6 @@ export class TestingDebugConfigurationFactory {
testKind,
TestLibrary.swiftTesting,
testList,
buildStartTime,
expandEnvVariables
).build();
}
Expand All @@ -64,7 +62,6 @@ export class TestingDebugConfigurationFactory {
testKind,
TestLibrary.xctest,
testList,
null,
expandEnvVariables
).build();
}
Expand All @@ -80,7 +77,6 @@ export class TestingDebugConfigurationFactory {
testKind,
testLibrary,
[],
null,
true
).testExecutableOutputPath();
}
Expand All @@ -91,7 +87,6 @@ export class TestingDebugConfigurationFactory {
private testKind: TestKind,
private testLibrary: TestLibrary,
private testList: string[],
private buildStartTime: Date | null,
private expandEnvVariables = false
) {}

Expand Down Expand Up @@ -454,23 +449,33 @@ export class TestingDebugConfigurationFactory {
);
}

private buildDescriptionPath(): string {
return path.join(this.buildDirectory, this.artifactFolderForTestKind, "description.json");
}

private async isUnifiedTestingBinary(): Promise<boolean> {
// Toolchains that contain https://github.com/swiftlang/swift-package-manager/commit/844bd137070dcd18d0f46dd95885ef7907ea0697
// no longer produce a .swift-testing binary, instead we want to use `unifiedTestingOutputPath`.
// In order to determine if we're working with a unified binary we need to check if the .swift-testing
// binary _doesn't_ exist, and if it does exist we need to check that it wasn't built by an old toolchain
// and is still in the .build directory. We do this by checking its mtime and seeing if it is after
// the `buildStartTime`.
// binary was produced by the latest build. If it was then we are not using a unified binary.

// TODO: When Swift 6 is released and enough time has passed that we're sure no one is building the .swift-testing
// binary anymore this fs.stat workaround can be removed and `swiftTestingPath` can be returned. The
// `buildStartTime` argument can be removed and the build config generation can be made synchronous again.
// binary anymore this workaround can be removed and `swiftTestingPath` can be returned, and the build config
// generation can be made synchronous again.

try {
const stat = await fs.stat(this.swiftTestingOutputPath());
return !this.buildStartTime || stat.mtime.getTime() < this.buildStartTime.getTime();
const buildDescriptionStr = await fs.readFile(this.buildDescriptionPath(), "utf-8");
const buildDescription = JSON.parse(buildDescriptionStr);
const testProducts = buildDescription.builtTestProducts as { binaryPath: string }[];
if (!testProducts) {
return false;
}
const testBinaryPaths = testProducts.map(({ binaryPath }) => binaryPath);
const swiftTestingBinaryRealPath = await fs.realpath(this.swiftTestingOutputPath());
return !testBinaryPaths.includes(swiftTestingBinaryRealPath);
} catch {
// If the .swift-testing binary doesn't exist then swift-testing tests are in the unified binary.
// If the .swift-testing binary wasn't produced by the latest build then we assume the
// swift-testing tests are in the unified binary.
return true;
}
}
Expand Down

0 comments on commit 9f8cf10

Please sign in to comment.