| 
 | 1 | +import XCTest  | 
 | 2 | +import class Foundation.Bundle  | 
 | 3 | + | 
 | 4 | +final class example_swiftTests: XCTestCase {  | 
 | 5 | +    func testExample() throws {  | 
 | 6 | +        // This is an example of a functional test case.  | 
 | 7 | +        // Use XCTAssert and related functions to verify your tests produce the correct  | 
 | 8 | +        // results.  | 
 | 9 | + | 
 | 10 | +        // Some of the APIs that we use below are available in macOS 10.13 and above.  | 
 | 11 | +        guard #available(macOS 10.13, *) else {  | 
 | 12 | +            return  | 
 | 13 | +        }  | 
 | 14 | + | 
 | 15 | +        let fooBinary = productsDirectory.appendingPathComponent("example-swift")  | 
 | 16 | + | 
 | 17 | +        let process = Process()  | 
 | 18 | +        process.executableURL = fooBinary  | 
 | 19 | + | 
 | 20 | +        let pipe = Pipe()  | 
 | 21 | +        process.standardOutput = pipe  | 
 | 22 | + | 
 | 23 | +        try process.run()  | 
 | 24 | +        process.waitUntilExit()  | 
 | 25 | + | 
 | 26 | +        let data = pipe.fileHandleForReading.readDataToEndOfFile()  | 
 | 27 | +        let output = String(data: data, encoding: .utf8)  | 
 | 28 | + | 
 | 29 | +        XCTAssertEqual(output, "Hello, world!\n")  | 
 | 30 | +    }  | 
 | 31 | + | 
 | 32 | +    /// Returns path to the built products directory.  | 
 | 33 | +    var productsDirectory: URL {  | 
 | 34 | +      #if os(macOS)  | 
 | 35 | +        for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {  | 
 | 36 | +            return bundle.bundleURL.deletingLastPathComponent()  | 
 | 37 | +        }  | 
 | 38 | +        fatalError("couldn't find the products directory")  | 
 | 39 | +      #else  | 
 | 40 | +        return Bundle.main.bundleURL  | 
 | 41 | +      #endif  | 
 | 42 | +    }  | 
 | 43 | + | 
 | 44 | +    static var allTests = [  | 
 | 45 | +        ("testExample", testExample),  | 
 | 46 | +    ]  | 
 | 47 | +}  | 
0 commit comments