Skip to content

Commit 51b2ffd

Browse files
authored
Add functional test for binary with moved resources (#3472)
* Add functional tests for binary with moved resources * Remove .gitignore for resources fixtures * Use local file system for file operations * Build each product separately
1 parent caf6d54 commit 51b2ffd

File tree

7 files changed

+82
-5
lines changed

7 files changed

+82
-5
lines changed
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: "")

Fixtures/Resources/Simple/.gitignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

Tests/FunctionalTests/ResourcesTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,44 @@ 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+
let binPath = try AbsolutePath(
59+
executeSwiftBuild(prefix, configuration: .Release, extraArgs: ["--show-bin-path"]).stdout
60+
.trimmingCharacters(in: .whitespacesAndNewlines)
61+
)
62+
63+
for execName in executables {
64+
_ = try executeSwiftBuild(prefix, configuration: .Release, extraArgs: ["--product", execName])
65+
66+
try withTemporaryDirectory(prefix: execName) { tmpDirPath in
67+
defer {
68+
// Unblock and remove the tmp dir on deinit.
69+
try? localFileSystem.chmod(.userWritable, path: tmpDirPath, options: [.recursive])
70+
try? localFileSystem.removeFileTree(tmpDirPath)
71+
}
72+
73+
let destBinPath = tmpDirPath.appending(component: execName)
74+
// Move the binary
75+
try localFileSystem.move(from: binPath.appending(component: execName), to: destBinPath)
76+
// Move the resources
77+
try localFileSystem
78+
.getDirectoryContents(binPath)
79+
.filter { $0.contains(execName) && $0.hasSuffix(".bundle") || $0.hasSuffix(".resources") }
80+
.forEach { try localFileSystem.move(from: binPath.appending(component: $0), to: tmpDirPath.appending(component: $0)) }
81+
// Run the binary
82+
let output = try Process.checkNonZeroExit(args: destBinPath.pathString)
83+
XCTAssertTrue(output.contains("foo"))
84+
}
85+
}
86+
}
87+
}
4888
}

0 commit comments

Comments
 (0)