Skip to content
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

Using Package Manager Resources in Swift PM Unit Tests #192

Closed
karimhm opened this issue Apr 30, 2020 · 6 comments · Fixed by #307
Closed

Using Package Manager Resources in Swift PM Unit Tests #192

karimhm opened this issue Apr 30, 2020 · 6 comments · Fixed by #307

Comments

@karimhm
Copy link
Contributor

karimhm commented Apr 30, 2020

Currently, in order to have successful tests when using the Swift Package Manager the gettysburgaddress.txt file is copied manually to the unit test bundle resources folder.

#if SWIFT_PACKAGE
// Swift Pacage Manager does not support resources distribution currently,
// therefore the "gettysburgaddress.txt" file must be copied manually to the test bundle.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *gettysburg = @""\
"Four score and seven years ago our fathers brought forth on this continent, a"\
"new nation, conceived in liberty, and dedicated to the proposition that all men"\
"are created equal.";
NSString * resourcePath = [testBundle resourcePath];
XCTAssertNotNil(resourcePath);
if (![[NSFileManager defaultManager] fileExistsAtPath:resourcePath]) {
NSError *createDirectoryError = nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:resourcePath
withIntermediateDirectories:true
attributes:nil
error:&createDirectoryError]) {
XCTFail("Failed to create the Resources directory, error: %@", createDirectoryError);
}
}
NSString *gettysburgPath = [resourcePath stringByAppendingPathComponent:@"gettysburgaddress.txt"];
NSError *writeError = nil;
if (![gettysburg writeToFile:gettysburgPath
atomically:true
encoding:NSUTF8StringEncoding
error:&writeError]) {
XCTFail("Failed to write `gettysburgaddress.txt` bundle Resource, error: %@", writeError);
}
});
#endif

Swift 5.3 introduced a new feature called Package Manager Resources. Once this gets rolled into production, it must be used to copy resources rather than doing it manually.

@thomasvl
Copy link
Member

I have an attempt at this in https://github.com/thomasvl/gtm-session-fetcher/tree/swiftpm_updates.

  • For some reason, one has to add excludes for lots of things even when one has sources or resources entry. Sorta feels odd to have to list what's both in and out, instead of having to do one or the other.
  • The tests fail from the command line. If I open the Package.swift Xcode is good, but swift test fails with the resource bundle apparently always being nil.

@karimhm
Copy link
Contributor Author

karimhm commented Sep 17, 2020

The tests might be failing due to SR-12912. The following PR (#2905) should solve it.

@thomasvl
Copy link
Member

I also posted on the Swift Forums about this and they also pointed at that PR, but I'm not sure if it is the same problem. That fix shows the old code path calling fatalError if it can't make the Bundle, but I'm not crashing, I'm getting back nil in the ObjC code.

@karimhm
Copy link
Contributor Author

karimhm commented Sep 17, 2020

We should file a new bug then. XMPPFramework #1177 suffer from the same issue.

@thomasvl
Copy link
Member

Opened https://bugs.swift.org/browse/SR-13560 also.

thomasvl added a commit to thomasvl/gtm-session-fetcher that referenced this issue Sep 28, 2020
Document the resources support that isn't working in SwiftPM 5.3, and create the
version specific files that will be needed to eventually use it.

Progress on google#192
thomasvl added a commit that referenced this issue Sep 28, 2020
Document the resources support that isn't working in SwiftPM 5.3, and create the
version specific files that will be needed to eventually use it.

Progress on #192
@karimhm
Copy link
Contributor Author

karimhm commented Jan 1, 2021

@thomasvl One workaround iI recently knew about is to use xcodebuild to test Swift packages rather than the swift tool. The resources issue (SR-13560) seem to be present inside swift tool only.

Example:

xcodebuild -scheme GTMSessionFetcher-Package -destination "name=iPhone 11 Pro" test

thomasvl added a commit to thomasvl/gtm-session-fetcher that referenced this issue Apr 26, 2022
thomasvl added a commit to thomasvl/gtm-session-fetcher that referenced this issue Apr 26, 2022
thomasvl added a commit to thomasvl/gtm-session-fetcher that referenced this issue Apr 27, 2022
- Provide a map of documents to the http server used in testing, removes the
  need for resources in the testing.
- Prune a few apis that are based around things that appear to hit the disk.

Fixes google#192
thomasvl added a commit that referenced this issue Apr 27, 2022
- Provide a map of documents to the http server used in testing, removes the
  need for resources in the testing.
- Prune a few apis that are based around things that appear to hit the disk.

Fixes #192
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants