-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add a Bundle.testTarget
property.
#848
Open
grynspan
wants to merge
3
commits into
main
Choose a base branch
from
jgrynspan/bundle-testcontent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Sources/Overlays/_Testing_Foundation/Support/Additions/BundleAdditions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
// | ||
|
||
#if canImport(Foundation) | ||
@_spi(ForSwiftTestingOnly) private import Testing | ||
public import Foundation | ||
|
||
extension Bundle { | ||
#if SWT_TARGET_OS_APPLE && !SWT_NO_DYNAMIC_LINKING && !SWT_NO_FILE_IO | ||
/// Storage for ``testTarget``. | ||
/// | ||
/// On Apple platforms, the bundle containing test content is a loadable | ||
/// XCTest bundle. By the time this property is read, the bundle should have | ||
/// already been loaded. | ||
private static let _testTarget: Bundle? = { | ||
Test.testBundlePath.flatMap { imagePath in | ||
// Construct a lazy sequence of URLs corresponding to the directories that | ||
// contain the loaded image. | ||
let imageURL = URL(fileURLWithFileSystemRepresentation: imagePath, isDirectory: false, relativeTo: nil) | ||
let containingDirectoryURLs = sequence(first: imageURL) { url in | ||
try? url.resourceValues(forKeys: [.parentDirectoryURLKey]).parentDirectory | ||
} | ||
|
||
// Find the directory most likely to contain our test content and return it. | ||
return containingDirectoryURLs.lazy | ||
.filter { $0.pathExtension.caseInsensitiveCompare("xctest") == .orderedSame } | ||
.compactMap(Bundle.init(url:)) | ||
.first { _ in true } | ||
} | ||
}() | ||
#endif | ||
|
||
/// A bundle representing the currently-running test target. | ||
/// | ||
/// On Apple platforms, this bundle represents the test bundle built by Xcode | ||
/// or Swift Package Manager. On other platforms, it is equal to the main | ||
/// bundle and represents the test executable built by Swift Package Manager. | ||
/// | ||
/// If more than one test bundle has been loaded into the current process, the | ||
/// value of this property represents the first test bundle found by the | ||
/// testing library at runtime. | ||
/// | ||
/// - Note: This property accesses the file system the first time it is used. | ||
@_spi(Experimental) | ||
public static var testTarget: Bundle { | ||
#if SWT_TARGET_OS_APPLE && !SWT_NO_DYNAMIC_LINKING && !SWT_NO_FILE_IO | ||
_testTarget ?? main | ||
#else | ||
// On other platforms, the main executable contains test content. | ||
main | ||
#endif | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think we should extend Foundation this way from Testing. The correct way would be implementation in Foundation itself, following the process there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a prototype only. 🙂 We can certainly follow the Foundation review process for this feature (in whatever form it takes) although due to layering it may need to live here in the overlay.
I have no plans to merge this without your team's signoff!