Skip to content

Commit 50e9b91

Browse files
committed
fix(ios): make swift tests self-contained
1 parent 5967c3e commit 50e9b91

2 files changed

Lines changed: 58 additions & 9 deletions

File tree

packages/react-native-brownfield/ios/Package.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ let package = Package(
4242
name: "BrownfieldBundleSupportTests",
4343
dependencies: ["BrownfieldBundleSupport"],
4444
path: "Tests",
45-
resources: [
46-
.copy("Fixtures/main.jsbundle"),
47-
]
45+
exclude: ["Fixtures"]
4846
),
4947
]
5048
)

packages/react-native-brownfield/ios/Tests/BrownfieldBundleURLResolverTests.swift

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import XCTest
44
final class BrownfieldBundleURLResolverTests: XCTestCase {
55
func test_debugResolutionPrefersBundledResourceWhenEnabled() throws {
66
let metroURL = URL(string: "http://localhost:8081/index.bundle?platform=ios")!
7+
let bundle = try makeFixtureBundle()
78

89
let resolvedURL = try BrownfieldBundleURLResolver.resolve(
910
isDebug: true,
1011
preferBundledBundleInDebug: true,
1112
bundlePath: "main.jsbundle",
12-
bundle: .module,
13+
bundle: bundle,
1314
bundleURLOverride: nil,
1415
metroURL: { metroURL }
1516
)
@@ -21,12 +22,13 @@ final class BrownfieldBundleURLResolverTests: XCTestCase {
2122

2223
func test_debugResolutionUsesMetroByDefault() throws {
2324
let metroURL = URL(string: "http://localhost:8081/index.bundle?platform=ios")!
25+
let bundle = try makeFixtureBundle()
2426

2527
let resolvedURL = try BrownfieldBundleURLResolver.resolve(
2628
isDebug: true,
2729
preferBundledBundleInDebug: false,
2830
bundlePath: "main.jsbundle",
29-
bundle: .module,
31+
bundle: bundle,
3032
bundleURLOverride: nil,
3133
metroURL: { metroURL }
3234
)
@@ -36,12 +38,13 @@ final class BrownfieldBundleURLResolverTests: XCTestCase {
3638

3739
func test_releaseResolutionUsesBundledResource() throws {
3840
let metroURL = URL(string: "http://localhost:8081/index.bundle?platform=ios")!
41+
let bundle = try makeFixtureBundle()
3942

4043
let resolvedURL = try BrownfieldBundleURLResolver.resolve(
4144
isDebug: false,
4245
preferBundledBundleInDebug: false,
4346
bundlePath: "main.jsbundle",
44-
bundle: .module,
47+
bundle: bundle,
4548
bundleURLOverride: nil,
4649
metroURL: { metroURL }
4750
)
@@ -54,12 +57,13 @@ final class BrownfieldBundleURLResolverTests: XCTestCase {
5457
func test_bundleURLOverrideTakesPrecedenceWhenItReturnsAURL() throws {
5558
let metroURL = URL(string: "http://localhost:8081/index.bundle?platform=ios")!
5659
let overrideURL = URL(string: "https://example.com/custom.bundle")!
60+
let bundle = try makeFixtureBundle()
5761

5862
let resolvedURL = try BrownfieldBundleURLResolver.resolve(
5963
isDebug: true,
6064
preferBundledBundleInDebug: false,
6165
bundlePath: "main.jsbundle",
62-
bundle: .module,
66+
bundle: bundle,
6367
bundleURLOverride: { overrideURL },
6468
metroURL: { metroURL }
6569
)
@@ -69,12 +73,13 @@ final class BrownfieldBundleURLResolverTests: XCTestCase {
6973

7074
func test_bundleURLOverrideFallsBackWhenItReturnsNil() throws {
7175
let metroURL = URL(string: "http://localhost:8081/index.bundle?platform=ios")!
76+
let bundle = try makeFixtureBundle()
7277

7378
let resolvedURL = try BrownfieldBundleURLResolver.resolve(
7479
isDebug: true,
7580
preferBundledBundleInDebug: true,
7681
bundlePath: "main.jsbundle",
77-
bundle: .module,
82+
bundle: bundle,
7883
bundleURLOverride: { nil },
7984
metroURL: { metroURL }
8085
)
@@ -90,7 +95,7 @@ final class BrownfieldBundleURLResolverTests: XCTestCase {
9095
isDebug: false,
9196
preferBundledBundleInDebug: false,
9297
bundlePath: "mainjsbundle",
93-
bundle: .module,
98+
bundle: Bundle(for: Self.self),
9499
bundleURLOverride: nil,
95100
metroURL: { nil }
96101
)
@@ -102,4 +107,50 @@ final class BrownfieldBundleURLResolverTests: XCTestCase {
102107
XCTAssertEqual(bundlePath, "mainjsbundle")
103108
}
104109
}
110+
111+
private func makeFixtureBundle() throws -> Bundle {
112+
let fileManager = FileManager.default
113+
let bundleURL = fileManager.temporaryDirectory
114+
.appendingPathComponent("BrownfieldBundleFixture-\(UUID().uuidString).bundle")
115+
let contentsURL = bundleURL.appendingPathComponent("Contents")
116+
let resourcesURL = contentsURL.appendingPathComponent("Resources")
117+
let plistURL = contentsURL.appendingPathComponent("Info.plist")
118+
let fixtureURL = resourcesURL.appendingPathComponent("main.jsbundle")
119+
120+
try fileManager.createDirectory(at: resourcesURL, withIntermediateDirectories: true)
121+
122+
let plist = """
123+
<?xml version="1.0" encoding="UTF-8"?>
124+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
125+
<plist version="1.0">
126+
<dict>
127+
<key>CFBundleIdentifier</key>
128+
<string>com.callstack.BrownfieldBundleFixture</string>
129+
<key>CFBundleName</key>
130+
<string>BrownfieldBundleFixture</string>
131+
<key>CFBundlePackageType</key>
132+
<string>BNDL</string>
133+
<key>CFBundleVersion</key>
134+
<string>1</string>
135+
</dict>
136+
</plist>
137+
"""
138+
139+
try plist.write(to: plistURL, atomically: true, encoding: .utf8)
140+
try "console.log(\"fixture\");".write(to: fixtureURL, atomically: true, encoding: .utf8)
141+
142+
addTeardownBlock {
143+
try? fileManager.removeItem(at: bundleURL)
144+
}
145+
146+
guard let bundle = Bundle(url: bundleURL) else {
147+
throw NSError(
148+
domain: "BrownfieldBundleURLResolverTests",
149+
code: 1,
150+
userInfo: [NSLocalizedDescriptionKey: "Failed to create fixture bundle"]
151+
)
152+
}
153+
154+
return bundle
155+
}
105156
}

0 commit comments

Comments
 (0)