Skip to content

Commit ca789b2

Browse files
committed
Add functional tests for binary with moved resources
1 parent 0c9c8d5 commit ca789b2

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version:5.3
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "Resources",
6+
targets: [
7+
.target(
8+
name: "SwiftyResource",
9+
resources: [
10+
.copy("foo.txt"),
11+
]
12+
),
13+
14+
.target(
15+
name: "SeaResource",
16+
resources: [
17+
.copy("foo.txt"),
18+
]
19+
),
20+
]
21+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#import <Foundation/Foundation.h>
2+
3+
int main(int argc, const char * argv[]) {
4+
@autoreleasepool {
5+
NSBundle *bundle = SWIFTPM_MODULE_BUNDLE;
6+
NSString *foo = [bundle pathForResource:@"foo" ofType:@"txt"];
7+
NSData *data = [NSFileManager.defaultManager contentsAtPath:foo];
8+
NSString *contents = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
9+
printf("%s", contents.UTF8String);
10+
}
11+
return 0;
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
3+
let bundle = Bundle.module
4+
5+
let foo = bundle.path(forResource: "foo", ofType: "txt")!
6+
let contents = FileManager.default.contents(atPath: foo)!
7+
print(String(data: contents, encoding: .utf8)!, terminator: "")

Tests/FunctionalTests/ResourcesTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,48 @@ class ResourcesTests: XCTestCase {
4545
""")
4646
}
4747
}
48+
49+
func testMovedBinaryResources() {
50+
fixture(name: "Resources/Moved") { prefix in
51+
var executables = ["SwiftyResource"]
52+
53+
// Objective-C module requires macOS
54+
#if os(macOS)
55+
executables.append("SeaResource")
56+
#endif
57+
58+
// Both executables are built in one go since
59+
// adding "--target execName" will not produce a binary
60+
_ = try executeSwiftBuild(prefix, configuration: .Release)
61+
62+
let binPath = try AbsolutePath(
63+
executeSwiftBuild(prefix, configuration: .Release, extraArgs: ["--show-bin-path"]).stdout
64+
.trimmingCharacters(in: .whitespacesAndNewlines)
65+
)
66+
67+
for execName in executables {
68+
try withTemporaryDirectory(prefix: execName) { tmpDirPath in
69+
defer {
70+
// Unblock and remove the tmp dir on deinit.
71+
try? localFileSystem.chmod(.userWritable, path: tmpDirPath, options: [.recursive])
72+
try? localFileSystem.removeFileTree(tmpDirPath)
73+
}
74+
75+
let destBinPath = tmpDirPath.appending(component: execName)
76+
// Copy the binary
77+
try systemQuietly("cp", "-R", "-H",
78+
binPath.appending(component: execName).pathString,
79+
destBinPath.pathString)
80+
// Copy the resources
81+
try Process.checkNonZeroExit(args: "find", binPath.pathString, "(", "-name", "*.bundle", "-or", "-name", "*.resources", ")")
82+
.split(whereSeparator: { $0.isNewline })
83+
.filter { !$0.isEmpty }
84+
.forEach { try systemQuietly("cp", "-R", "-H", String($0), tmpDirPath.pathString) }
85+
// Run the binary
86+
let output = try Process.checkNonZeroExit(args: destBinPath.pathString)
87+
XCTAssertTrue(output.contains("foo"))
88+
}
89+
}
90+
}
91+
}
4892
}

0 commit comments

Comments
 (0)