Skip to content

Commit 445bbd8

Browse files
committed
Refactor EndToEndTests
Create helper methods on Process and Bundle
1 parent 222b738 commit 445bbd8

File tree

5 files changed

+91
-45
lines changed

5 files changed

+91
-45
lines changed

Tests/EndToEndTests/EndToEndTests.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import class Foundation.Bundle
2+
import XCTest
3+
4+
final class GenerateSubcommandTests: XCTestCase {
5+
func testHTMLGeneration() throws {
6+
let command = Bundle.productsDirectory.appendingPathComponent("swift-doc")
7+
let outputDirectory = try temporaryDirectory()
8+
9+
defer { try? FileManager.default.removeItem(at: outputDirectory) }
10+
try Process.run(command: command,
11+
arguments: [
12+
"generate",
13+
"--module-name", "SwiftDoc",
14+
"--format", "html",
15+
"--output", outputDirectory.path,
16+
"Sources"
17+
]
18+
) { result in
19+
XCTAssertEqual(result.terminationStatus, EXIT_SUCCESS)
20+
XCTAssertEqual(result.output, "")
21+
XCTAssertEqual(result.error, "")
22+
23+
do {
24+
let html = try String(contentsOf: outputDirectory.appendingPathComponent("index.html"))
25+
XCTAssertTrue(html.contains("<!DOCTYPE html>"))
26+
}
27+
28+
do {
29+
let css = try String(contentsOf: outputDirectory.appendingPathComponent("all.css"))
30+
XCTAssertTrue(css.contains(":root"))
31+
}
32+
}
33+
}
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
3+
extension Bundle {
4+
static var productsDirectory: URL = {
5+
#if os(macOS)
6+
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
7+
return bundle.bundleURL.deletingLastPathComponent()
8+
}
9+
fatalError("couldn't find the products directory")
10+
#else
11+
return Bundle.main.bundleURL
12+
#endif
13+
}()
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Foundation
2+
3+
extension Process {
4+
typealias Result = (terminationStatus: Int32, output: String?, error: String?)
5+
6+
static func run(command executableURL: URL, arguments: [String] = [], completion: (Result) throws -> Void) throws {
7+
let process = Process()
8+
if #available(OSX 10.13, *) {
9+
process.executableURL = executableURL
10+
} else {
11+
process.launchPath = executableURL.path
12+
}
13+
process.arguments = arguments
14+
15+
let standardOutput = Pipe()
16+
process.standardOutput = standardOutput
17+
18+
let standardError = Pipe()
19+
process.standardError = standardError
20+
21+
if #available(OSX 10.13, *) {
22+
try process.run()
23+
} else {
24+
process.launch()
25+
}
26+
process.waitUntilExit()
27+
28+
let output = String(data: standardOutput.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
29+
let error = String(data: standardError.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8)
30+
31+
try completion((numericCast(process.terminationStatus), output, error))
32+
}
33+
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Foundation
2+
3+
func temporaryDirectory() throws -> URL {
4+
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString)
5+
try FileManager.default.createDirectory(at: temporaryDirectoryURL, withIntermediateDirectories: true)
6+
7+
return temporaryDirectoryURL
8+
}
9+

0 commit comments

Comments
 (0)