diff --git a/Fixtures/Resources/Moved/Package.swift b/Fixtures/Resources/Moved/Package.swift new file mode 100644 index 00000000000..f2a14c71ebe --- /dev/null +++ b/Fixtures/Resources/Moved/Package.swift @@ -0,0 +1,21 @@ +// swift-tools-version:5.3 +import PackageDescription + +let package = Package( + name: "Resources", + targets: [ + .target( + name: "SwiftyResource", + resources: [ + .copy("foo.txt"), + ] + ), + + .target( + name: "SeaResource", + resources: [ + .copy("foo.txt"), + ] + ), + ] +) diff --git a/Fixtures/Resources/Moved/Sources/SeaResource/foo.txt b/Fixtures/Resources/Moved/Sources/SeaResource/foo.txt new file mode 100644 index 00000000000..257cc5642cb --- /dev/null +++ b/Fixtures/Resources/Moved/Sources/SeaResource/foo.txt @@ -0,0 +1 @@ +foo diff --git a/Fixtures/Resources/Moved/Sources/SeaResource/main.m b/Fixtures/Resources/Moved/Sources/SeaResource/main.m new file mode 100644 index 00000000000..c31e758713c --- /dev/null +++ b/Fixtures/Resources/Moved/Sources/SeaResource/main.m @@ -0,0 +1,12 @@ +#import + +int main(int argc, const char * argv[]) { + @autoreleasepool { + NSBundle *bundle = SWIFTPM_MODULE_BUNDLE; + NSString *foo = [bundle pathForResource:@"foo" ofType:@"txt"]; + NSData *data = [NSFileManager.defaultManager contentsAtPath:foo]; + NSString *contents = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; + printf("%s", contents.UTF8String); + } + return 0; +} diff --git a/Fixtures/Resources/Moved/Sources/SwiftyResource/foo.txt b/Fixtures/Resources/Moved/Sources/SwiftyResource/foo.txt new file mode 100644 index 00000000000..257cc5642cb --- /dev/null +++ b/Fixtures/Resources/Moved/Sources/SwiftyResource/foo.txt @@ -0,0 +1 @@ +foo diff --git a/Fixtures/Resources/Moved/Sources/SwiftyResource/main.swift b/Fixtures/Resources/Moved/Sources/SwiftyResource/main.swift new file mode 100644 index 00000000000..44f717d8359 --- /dev/null +++ b/Fixtures/Resources/Moved/Sources/SwiftyResource/main.swift @@ -0,0 +1,7 @@ +import Foundation + +let bundle = Bundle.module + +let foo = bundle.path(forResource: "foo", ofType: "txt")! +let contents = FileManager.default.contents(atPath: foo)! +print(String(data: contents, encoding: .utf8)!, terminator: "") diff --git a/Fixtures/Resources/Simple/.gitignore b/Fixtures/Resources/Simple/.gitignore deleted file mode 100644 index 95c4320919a..00000000000 --- a/Fixtures/Resources/Simple/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.DS_Store -/.build -/Packages -/*.xcodeproj -xcuserdata/ diff --git a/Tests/FunctionalTests/ResourcesTests.swift b/Tests/FunctionalTests/ResourcesTests.swift index e79c78f5acb..0a8e59f12d5 100644 --- a/Tests/FunctionalTests/ResourcesTests.swift +++ b/Tests/FunctionalTests/ResourcesTests.swift @@ -45,4 +45,44 @@ class ResourcesTests: XCTestCase { """) } } + + func testMovedBinaryResources() { + fixture(name: "Resources/Moved") { prefix in + var executables = ["SwiftyResource"] + + // Objective-C module requires macOS + #if os(macOS) + executables.append("SeaResource") + #endif + + let binPath = try AbsolutePath( + executeSwiftBuild(prefix, configuration: .Release, extraArgs: ["--show-bin-path"]).stdout + .trimmingCharacters(in: .whitespacesAndNewlines) + ) + + for execName in executables { + _ = try executeSwiftBuild(prefix, configuration: .Release, extraArgs: ["--product", execName]) + + try withTemporaryDirectory(prefix: execName) { tmpDirPath in + defer { + // Unblock and remove the tmp dir on deinit. + try? localFileSystem.chmod(.userWritable, path: tmpDirPath, options: [.recursive]) + try? localFileSystem.removeFileTree(tmpDirPath) + } + + let destBinPath = tmpDirPath.appending(component: execName) + // Move the binary + try localFileSystem.move(from: binPath.appending(component: execName), to: destBinPath) + // Move the resources + try localFileSystem + .getDirectoryContents(binPath) + .filter { $0.contains(execName) && $0.hasSuffix(".bundle") || $0.hasSuffix(".resources") } + .forEach { try localFileSystem.move(from: binPath.appending(component: $0), to: tmpDirPath.appending(component: $0)) } + // Run the binary + let output = try Process.checkNonZeroExit(args: destBinPath.pathString) + XCTAssertTrue(output.contains("foo")) + } + } + } + } }