Skip to content

Commit 8a01ecd

Browse files
committed
Update an integration test to store files on the filesystem instead of creating them in the test
1 parent 8eea6f3 commit 8a01ecd

File tree

6 files changed

+38
-89
lines changed

6 files changed

+38
-89
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// swift-tools-version:5.3
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "AwesomeResources",
6+
targets: [
7+
.target(name: "AwesomeResources", resources: [.copy("hello.txt")]),
8+
.testTarget(name: "AwesomeResourcesTest", dependencies: ["AwesomeResources"], resources: [.copy("world.txt")])
9+
]
10+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Foundation
2+
3+
public struct AwesomeResource {
4+
public init() {}
5+
public let hello = try! String(contentsOf: Bundle.module.url(forResource: "hello", withExtension: "txt")!)
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import XCTest
2+
import Foundation
3+
import AwesomeResources
4+
5+
final class MyTests: XCTestCase {
6+
func testFoo() {
7+
XCTAssertTrue(AwesomeResource().hello == "hello")
8+
}
9+
func testBar() {
10+
let world = try! String(contentsOf: Bundle.module.url(forResource: "world", withExtension: "txt")!)
11+
XCTAssertTrue(world == "world")
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
world

Tests/IntegrationTests/BasicTests.swift

Lines changed: 7 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -303,100 +303,18 @@ private struct BasicTests {
303303
}
304304

305305
@Test
306-
func testSwiftTestWithResources() throws {
307-
try withTemporaryDirectory { tempDir in
308-
let packagePath = tempDir.appending(component: "swiftTestResources")
309-
try localFileSystem.createDirectory(packagePath)
310-
try localFileSystem.writeFileContents(
311-
packagePath.appending(component: "Package.swift"),
312-
bytes: ByteString(
313-
encodingAsUTF8: """
314-
// swift-tools-version:5.3
315-
import PackageDescription
316-
317-
let package = Package(
318-
name: "AwesomeResources",
319-
targets: [
320-
.target(name: "AwesomeResources", resources: [.copy("hello.txt")]),
321-
.testTarget(name: "AwesomeResourcesTest", dependencies: ["AwesomeResources"], resources: [.copy("world.txt")])
322-
]
323-
)
324-
"""
325-
)
326-
)
327-
try localFileSystem.createDirectory(packagePath.appending(component: "Sources"))
328-
try localFileSystem.createDirectory(
329-
packagePath.appending(components: "Sources", "AwesomeResources")
330-
)
331-
try localFileSystem.writeFileContents(
332-
packagePath.appending(
333-
components: "Sources", "AwesomeResources", "AwesomeResource.swift"
334-
),
335-
bytes: ByteString(
336-
encodingAsUTF8: """
337-
import Foundation
338-
339-
public struct AwesomeResource {
340-
public init() {}
341-
public let hello = try! String(contentsOf: Bundle.module.url(forResource: "hello", withExtension: "txt")!)
342-
}
306+
func testSwiftTestWithResources() async throws {
307+
try await fixture(name: "Miscellaneous/PackageWithResource/") { packagePath in
343308

344-
"""
345-
)
346-
)
347-
348-
try localFileSystem.writeFileContents(
349-
packagePath.appending(components: "Sources", "AwesomeResources", "hello.txt"),
350-
bytes: ByteString(encodingAsUTF8: "hello")
351-
)
352-
353-
try localFileSystem.createDirectory(packagePath.appending(component: "Tests"))
354-
try localFileSystem.createDirectory(
355-
packagePath.appending(components: "Tests", "AwesomeResourcesTest")
356-
)
357-
358-
try localFileSystem.writeFileContents(
359-
packagePath.appending(components: "Tests", "AwesomeResourcesTest", "world.txt"),
360-
bytes: ByteString(encodingAsUTF8: "world")
361-
)
362-
363-
try localFileSystem.writeFileContents(
364-
packagePath.appending(components: "Tests", "AwesomeResourcesTest", "MyTests.swift"),
365-
bytes: ByteString(
366-
encodingAsUTF8: """
367-
import XCTest
368-
import Foundation
369-
import AwesomeResources
370-
371-
final class MyTests: XCTestCase {
372-
func testFoo() {
373-
XCTAssertTrue(AwesomeResource().hello == "hello")
374-
}
375-
func testBar() {
376-
let world = try! String(contentsOf: Bundle.module.url(forResource: "world", withExtension: "txt")!)
377-
XCTAssertTrue(world == "world")
378-
}
379-
}
380-
"""
381-
)
382-
)
383-
384-
let shOutput = try await executeSwiftTest(
309+
let result = try await executeSwiftTest(
385310
packagePath, extraArgs: ["--filter", "MyTests.*", "--vv"]
386311
)
387312

388313
// Check the test log.
389-
let checker = StringChecker(string: shOutput.stdout)
390-
let testSuitePassedCheck: String
391-
#if os(Windows)
392-
// CHORE: This is not the right solution
393-
testSuitePassedCheck = #"Test Suite \'MyTests\' passed"#
394-
#else
395-
testSuitePassedCheck = "Test Suite 'MyTests' passed"
396-
#endif
397-
#expect(checker.check(.contains("Test Suite 'MyTests' started")), "stdout: '\(shOutput.stdout)'\n stderr:'\(shOutput.stderr)'")
398-
#expect(checker.check(.contains(testSuitePassedCheck)), "stdout: '\(shOutput.stdout)'\n stderr:'\(shOutput.stderr)'")
399-
#expect(checker.check(.contains("Executed 2 tests, with 0 failures")), "stdout: '\(shOutput.stdout)'\n stderr:'\(shOutput.stderr)'")
314+
let checker = StringChecker(string: result.stdout)
315+
#expect(checker.check(.contains("Test Suite 'MyTests' started")), "stdout: '\(result.stdout)'\n stderr:'\(result.stderr)'")
316+
#expect(checker.check(.contains("Test Suite 'MyTests' passed")), "stdout: '\(result.stdout)'\n stderr:'\(result.stderr)'")
317+
#expect(checker.check(.contains("Executed 2 tests, with 0 failures")), "stdout: '\(result.stdout)'\n stderr:'\(result.stderr)'")
400318
}
401319
}
402320
}

0 commit comments

Comments
 (0)