Skip to content

Commit

Permalink
Fix installing test bundles from streams
Browse files Browse the repository at this point in the history
Summary:
This diff fixes S437790 which blocks debugging unit tests in iOS ODs.

The fixed bug happens when an .xctest bundle is installed by idb_companion through a stream. idb_companion decompresses the archive sent through the stream to a temporary directory, then creates a symlink to the bundle in the temp directory to install it in the simulator. Before actually running the test, the temporary directory is cleaned up, leaving the symlink pointing at nothing.

This diff will *move* the test bundle from the temp directory to install it if the bundle comes from an archive, and will use symlinks for all other cases.

Differential Revision: D60975841

fbshipit-source-id: 342768bb5a34d092128b3c9ab30c5bee05329aba
  • Loading branch information
fbgerrit authored and facebook-github-bot committed Aug 9, 2024
1 parent 10bb62a commit 8aa8b0c
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions CompanionLib/Utility/FBIDBStorageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ - (BOOL)checkArchitecture:(FBBundleDescriptor *)bundle error:(NSError **)error

- (FBFuture<FBInstalledArtifact *> *)saveBundle:(FBBundleDescriptor *)bundle
{
return [self saveBundle:bundle skipSigningBundles:NO];
return [self saveBundle:bundle usingSymlink:YES skipSigningBundles:NO];
}

- (FBFuture<FBInstalledArtifact *> *)saveBundle:(FBBundleDescriptor *)bundle skipSigningBundles:(BOOL)skipSigningBundles
- (FBFuture<FBInstalledArtifact *> *)saveBundle:(FBBundleDescriptor *)bundle usingSymlink:(BOOL)useSymlink skipSigningBundles:(BOOL)skipSigningBundles
{
// Check that the bundle matches the architecture of the target.
NSError *error = nil;
Expand All @@ -172,12 +172,21 @@ - (BOOL)checkArchitecture:(FBBundleDescriptor *)bundle error:(NSError **)error
return [FBFuture futureWithError:error];
}

// Copy over bundle
NSURL *sourceBundlePath = [NSURL fileURLWithPath:bundle.path];
NSURL *destinationBundlePath = [storageDirectory URLByAppendingPathComponent:sourceBundlePath.lastPathComponent];
[self.logger logFormat:@"Symlink %@ to %@", bundle.identifier, destinationBundlePath];
if (![NSFileManager.defaultManager createSymbolicLinkAtURL:destinationBundlePath withDestinationURL:sourceBundlePath error:&error]) {
return [FBFuture futureWithError:error];
if (useSymlink) {
// Symlink the bundle
[self.logger logFormat:@"Symlink %@ to %@", bundle.identifier, destinationBundlePath];
if (![NSFileManager.defaultManager createSymbolicLinkAtURL:destinationBundlePath withDestinationURL:sourceBundlePath error:&error]) {
return [FBFuture futureWithError:error];
}
} else {
// Move the bundle
[self.logger logFormat:@"Moving %@ to %@", bundle.identifier, destinationBundlePath];
if (![NSFileManager.defaultManager moveItemAtURL:sourceBundlePath toURL:destinationBundlePath error:&error]) {
return [FBFuture futureWithError:error];
}
[self.logger logFormat:@"Moved %@", bundle.identifier];
}

FBInstalledArtifact *artifact = [[FBInstalledArtifact alloc] initWithName:bundle.identifier uuid:bundle.binary.uuid path:destinationBundlePath];
Expand Down Expand Up @@ -291,7 +300,7 @@ @implementation FBXCTestBundleStorage
}

if (xctestBundleURL) {
return [self saveTestBundle:xctestBundleURL skipSigningBundles:skipSigningBundles];
return [self saveTestBundle:xctestBundleURL usingSymlink:NO skipSigningBundles:skipSigningBundles];
}
if (xctestrunURL) {
return [self saveTestRun:xctestrunURL];
Expand All @@ -305,7 +314,7 @@ @implementation FBXCTestBundleStorage
{
// save .xctest or .xctestrun
if ([filePath.pathExtension isEqualToString:XctestExtension]) {
return [self saveTestBundle:filePath skipSigningBundles:skipSigningBundles];
return [self saveTestBundle:filePath usingSymlink:YES skipSigningBundles:skipSigningBundles];
}
if ([filePath.pathExtension isEqualToString:XctestRunExtension]) {
return [self saveTestRun:filePath];
Expand Down Expand Up @@ -509,15 +518,15 @@ - (NSURL *)xctestBundleWithID:(NSString *)bundleID error:(NSError **)error
return [[FBXCodebuildTestRunDescriptor alloc] initWithURL:xctestrunURL name:testTarget testBundle:testBundle testHostBundle:testHostBundle];
}

- (FBFuture<FBInstalledArtifact *> *)saveTestBundle:(NSURL *)testBundleURL skipSigningBundles:(BOOL)skipSigningBundles
- (FBFuture<FBInstalledArtifact *> *)saveTestBundle:(NSURL *)testBundleURL usingSymlink:(BOOL)useSymlink skipSigningBundles:(BOOL)skipSigningBundles
{
// Test Bundles don't always have a bundle id, so fallback to another name if it's not there.
NSError *error = nil;
FBBundleDescriptor *bundle = [FBBundleDescriptor bundleWithFallbackIdentifierFromPath:testBundleURL.path error:&error];
if (!bundle) {
return [FBFuture futureWithError:error];
}
return [self saveBundle:bundle skipSigningBundles:skipSigningBundles];
return [self saveBundle:bundle usingSymlink:useSymlink skipSigningBundles:skipSigningBundles];
}

- (FBFuture<FBInstalledArtifact *> *)saveTestRun:(NSURL *)XCTestRunURL
Expand Down

0 comments on commit 8aa8b0c

Please sign in to comment.