From db9b3d07c851b66254c69938d1660ff4be8e5b97 Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Wed, 5 May 2021 08:52:15 +0200 Subject: [PATCH 1/4] Add functional tests for binary with moved resources --- Fixtures/Resources/Moved/.gitignore | 5 +++ Fixtures/Resources/Moved/Package.swift | 21 +++++++++ .../Moved/Sources/SeaResource/foo.txt | 1 + .../Moved/Sources/SeaResource/main.m | 12 +++++ .../Moved/Sources/SwiftyResource/foo.txt | 1 + .../Moved/Sources/SwiftyResource/main.swift | 7 +++ Tests/FunctionalTests/ResourcesTests.swift | 44 +++++++++++++++++++ 7 files changed, 91 insertions(+) create mode 100644 Fixtures/Resources/Moved/.gitignore create mode 100644 Fixtures/Resources/Moved/Package.swift create mode 100644 Fixtures/Resources/Moved/Sources/SeaResource/foo.txt create mode 100644 Fixtures/Resources/Moved/Sources/SeaResource/main.m create mode 100644 Fixtures/Resources/Moved/Sources/SwiftyResource/foo.txt create mode 100644 Fixtures/Resources/Moved/Sources/SwiftyResource/main.swift diff --git a/Fixtures/Resources/Moved/.gitignore b/Fixtures/Resources/Moved/.gitignore new file mode 100644 index 00000000000..95c4320919a --- /dev/null +++ b/Fixtures/Resources/Moved/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ 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/Tests/FunctionalTests/ResourcesTests.swift b/Tests/FunctionalTests/ResourcesTests.swift index e79c78f5acb..a4db7693e97 100644 --- a/Tests/FunctionalTests/ResourcesTests.swift +++ b/Tests/FunctionalTests/ResourcesTests.swift @@ -45,4 +45,48 @@ 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 + + // Both executables are built in one go since + // adding "--target execName" will not produce a binary + _ = try executeSwiftBuild(prefix, configuration: .Release) + + let binPath = try AbsolutePath( + executeSwiftBuild(prefix, configuration: .Release, extraArgs: ["--show-bin-path"]).stdout + .trimmingCharacters(in: .whitespacesAndNewlines) + ) + + for execName in executables { + 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) + // Copy the binary + try systemQuietly("cp", "-R", "-H", + binPath.appending(component: execName).pathString, + destBinPath.pathString) + // Copy the resources + try Process.checkNonZeroExit(args: "find", binPath.pathString, "(", "-name", "*.bundle", "-or", "-name", "*.resources", ")") + .split(whereSeparator: { $0.isNewline }) + .filter { !$0.isEmpty } + .forEach { try systemQuietly("cp", "-R", "-H", String($0), tmpDirPath.pathString) } + // Run the binary + let output = try Process.checkNonZeroExit(args: destBinPath.pathString) + XCTAssertTrue(output.contains("foo")) + } + } + } + } } From aa8667b959f729d96c54df154e6188be86fa442b Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Thu, 6 May 2021 10:44:37 +0200 Subject: [PATCH 2/4] Remove .gitignore for resources fixtures --- Fixtures/Resources/Moved/.gitignore | 5 ----- Fixtures/Resources/Simple/.gitignore | 5 ----- 2 files changed, 10 deletions(-) delete mode 100644 Fixtures/Resources/Moved/.gitignore delete mode 100644 Fixtures/Resources/Simple/.gitignore diff --git a/Fixtures/Resources/Moved/.gitignore b/Fixtures/Resources/Moved/.gitignore deleted file mode 100644 index 95c4320919a..00000000000 --- a/Fixtures/Resources/Moved/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -.DS_Store -/.build -/Packages -/*.xcodeproj -xcuserdata/ 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/ From 19b11c7e3fe1da375c519378dcc547dde7f05c98 Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Thu, 6 May 2021 11:11:06 +0200 Subject: [PATCH 3/4] Use local file system for file operations --- Tests/FunctionalTests/ResourcesTests.swift | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/Tests/FunctionalTests/ResourcesTests.swift b/Tests/FunctionalTests/ResourcesTests.swift index a4db7693e97..572bfc9c987 100644 --- a/Tests/FunctionalTests/ResourcesTests.swift +++ b/Tests/FunctionalTests/ResourcesTests.swift @@ -55,8 +55,7 @@ class ResourcesTests: XCTestCase { executables.append("SeaResource") #endif - // Both executables are built in one go since - // adding "--target execName" will not produce a binary + // Both executables are built in one go _ = try executeSwiftBuild(prefix, configuration: .Release) let binPath = try AbsolutePath( @@ -73,15 +72,13 @@ class ResourcesTests: XCTestCase { } let destBinPath = tmpDirPath.appending(component: execName) - // Copy the binary - try systemQuietly("cp", "-R", "-H", - binPath.appending(component: execName).pathString, - destBinPath.pathString) - // Copy the resources - try Process.checkNonZeroExit(args: "find", binPath.pathString, "(", "-name", "*.bundle", "-or", "-name", "*.resources", ")") - .split(whereSeparator: { $0.isNewline }) - .filter { !$0.isEmpty } - .forEach { try systemQuietly("cp", "-R", "-H", String($0), tmpDirPath.pathString) } + // 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")) From 16320dce6de8b6ea9d60f12d679635fa78a5b1c4 Mon Sep 17 00:00:00 2001 From: Florian Friedrich Date: Fri, 7 May 2021 07:48:27 +0200 Subject: [PATCH 4/4] Build each product separately --- Tests/FunctionalTests/ResourcesTests.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Tests/FunctionalTests/ResourcesTests.swift b/Tests/FunctionalTests/ResourcesTests.swift index 572bfc9c987..0a8e59f12d5 100644 --- a/Tests/FunctionalTests/ResourcesTests.swift +++ b/Tests/FunctionalTests/ResourcesTests.swift @@ -55,15 +55,14 @@ class ResourcesTests: XCTestCase { executables.append("SeaResource") #endif - // Both executables are built in one go - _ = try executeSwiftBuild(prefix, configuration: .Release) - 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.