This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Add end-to-end tests for command-line interface #199
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5351ee8
Test that `all.css` contains a valid CSS selector
MaxDesiatov 254741b
Update `CHANGELOG.md`
MaxDesiatov 2e03529
Install graphviz to avoid warnings on macOS
MaxDesiatov d0df58a
Add EndToEndTests subclassing XCTest
MaxDesiatov 07c2906
Apply suggestions from code review
MaxDesiatov 059652b
Remove stray comment leftover from review
mattt 222b738
Set macOS 10.13 as minimum platform target
mattt 445bbd8
Refactor EndToEndTests
mattt d3c6cec
Test whether generated HTML output contains subdirectories
mattt 72ecb06
Add test for CommonMark output
mattt c837de6
Add tests for coverage subcommand
mattt c78cfe2
Add tests for diagram subcommand
mattt 51b5927
Update Changelog entry for #199
mattt 08498bb
Work around unavailability of hasDirectoryPath
mattt 62d02ef
Update Changelog.md
mattt 0774eae
Update Changelog entry for #199
mattt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import XCTest | ||
|
||
final class CoverageSubcommandTests: XCTestCase { | ||
func testStandardOutput() throws { | ||
let command = Bundle.productsDirectory.appendingPathComponent("swift-doc") | ||
|
||
let outputDirectory = try temporaryDirectory() | ||
defer { try? FileManager.default.removeItem(at: outputDirectory) } | ||
|
||
try Process.run(command: command, | ||
arguments: [ | ||
"coverage", | ||
"Sources" | ||
] | ||
) { result in | ||
XCTAssertEqual(result.terminationStatus, EXIT_SUCCESS) | ||
XCTAssertEqual(result.output?.starts(with: "Total"), true) | ||
XCTAssertEqual(result.error, "") | ||
} | ||
} | ||
|
||
func testFileOutput() throws { | ||
let command = Bundle.productsDirectory.appendingPathComponent("swift-doc") | ||
|
||
let outputDirectory = try temporaryDirectory() | ||
let outputFile = outputDirectory.appendingPathComponent("report.json") | ||
defer { try? FileManager.default.removeItem(at: outputDirectory) } | ||
|
||
try Process.run(command: command, | ||
arguments: [ | ||
"coverage", | ||
"--output", outputFile.path, | ||
"Sources" | ||
] | ||
) { result in | ||
XCTAssertEqual(result.terminationStatus, EXIT_SUCCESS) | ||
XCTAssertEqual(result.output, "") | ||
XCTAssertEqual(result.error, "") | ||
|
||
do { | ||
let data = try Data(contentsOf: outputFile) | ||
let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] | ||
XCTAssertEqual(json?["type"] as? String, "org.dcov.report.json.export") | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import XCTest | ||
|
||
final class DiagramSubcommandTests: XCTestCase { | ||
func testStandardOutput() throws { | ||
let command = Bundle.productsDirectory.appendingPathComponent("swift-doc") | ||
|
||
let outputDirectory = try temporaryDirectory() | ||
defer { try? FileManager.default.removeItem(at: outputDirectory) } | ||
|
||
try Process.run(command: command, | ||
arguments: [ | ||
"diagram", | ||
"Sources" | ||
] | ||
) { result in | ||
XCTAssertEqual(result.terminationStatus, EXIT_SUCCESS) | ||
XCTAssertEqual(result.output?.starts(with: "digraph {"), true) | ||
XCTAssertEqual(result.error, "") | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import XCTest | ||
|
||
final class GenerateSubcommandTests: XCTestCase { | ||
func testCommonMark() throws { | ||
let command = Bundle.productsDirectory.appendingPathComponent("swift-doc") | ||
|
||
let outputDirectory = try temporaryDirectory() | ||
defer { try? FileManager.default.removeItem(at: outputDirectory) } | ||
|
||
try Process.run(command: command, | ||
arguments: [ | ||
"generate", | ||
"--module-name", "SwiftDoc", | ||
"--format", "commonmark", | ||
"--output", outputDirectory.path, | ||
"Sources" | ||
] | ||
) { result in | ||
XCTAssertEqual(result.terminationStatus, EXIT_SUCCESS) | ||
XCTAssertEqual(result.output, "") | ||
XCTAssertEqual(result.error, "") | ||
|
||
do { | ||
let commonmark = try String(contentsOf: outputDirectory.appendingPathComponent("Home.md")) | ||
XCTAssertTrue(commonmark.contains("# Types")) | ||
} | ||
|
||
do { | ||
let commonmark = try String(contentsOf: outputDirectory.appendingPathComponent("_Sidebar.md")) | ||
XCTAssertTrue(commonmark.contains("<summary>Types</summary>")) | ||
} | ||
|
||
do { | ||
let commonmark = try String(contentsOf: outputDirectory.appendingPathComponent("_Footer.md")) | ||
XCTAssertTrue(commonmark.contains("[swift-doc](https://github.com/SwiftDocOrg/swift-doc)")) | ||
} | ||
|
||
do { | ||
let contents = try FileManager.default.contentsOfDirectory(at: outputDirectory, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles]) | ||
let subdirectories = contents.filter { $0.hasDirectoryPath } | ||
XCTAssertEqual(subdirectories.count, 0, "output should not contain any subdirectories") | ||
} | ||
} | ||
} | ||
|
||
func testHTML() throws { | ||
let command = Bundle.productsDirectory.appendingPathComponent("swift-doc") | ||
let outputDirectory = try temporaryDirectory() | ||
|
||
defer { try? FileManager.default.removeItem(at: outputDirectory) } | ||
try Process.run(command: command, | ||
arguments: [ | ||
"generate", | ||
"--module-name", "SwiftDoc", | ||
"--format", "html", | ||
"--output", outputDirectory.path, | ||
"Sources" | ||
] | ||
) { result in | ||
XCTAssertEqual(result.terminationStatus, EXIT_SUCCESS) | ||
XCTAssertEqual(result.output, "") | ||
XCTAssertEqual(result.error, "") | ||
|
||
do { | ||
let html = try String(contentsOf: outputDirectory.appendingPathComponent("index.html")) | ||
XCTAssertTrue(html.contains("<!DOCTYPE html>")) | ||
} | ||
|
||
do { | ||
let css = try String(contentsOf: outputDirectory.appendingPathComponent("all.css")) | ||
XCTAssertTrue(css.contains(":root")) | ||
} | ||
|
||
do { | ||
let contents = try FileManager.default.contentsOfDirectory(at: outputDirectory, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles]) | ||
let subdirectories = contents.filter { $0.hasDirectoryPath } | ||
.filter { FileManager.default.fileExists(atPath: $0.appendingPathComponent("index.html").path) } | ||
XCTAssertGreaterThanOrEqual(subdirectories.count, 1, "output should contain one or more subdirectories containing index.html") | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
|
||
extension Bundle { | ||
static var productsDirectory: URL = { | ||
#if os(macOS) | ||
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { | ||
return bundle.bundleURL.deletingLastPathComponent() | ||
} | ||
fatalError("couldn't find the products directory") | ||
#else | ||
return Bundle.main.bundleURL | ||
#endif | ||
}() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Foundation | ||
|
||
extension Process { | ||
typealias Result = (terminationStatus: Int32, output: String?, error: String?) | ||
|
||
static func run(command executableURL: URL, arguments: [String] = [], completion: (Result) throws -> Void) throws { | ||
let process = Process() | ||
if #available(OSX 10.13, *) { | ||
process.executableURL = executableURL | ||
} else { | ||
process.launchPath = executableURL.path | ||
} | ||
process.arguments = arguments | ||
|
||
let standardOutput = Pipe() | ||
process.standardOutput = standardOutput | ||
|
||
let standardError = Pipe() | ||
process.standardError = standardError | ||
|
||
if #available(OSX 10.13, *) { | ||
try process.run() | ||
} else { | ||
process.launch() | ||
} | ||
process.waitUntilExit() | ||
|
||
let output = String(data: standardOutput.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) | ||
let error = String(data: standardError.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) | ||
|
||
try completion((numericCast(process.terminationStatus), output, error)) | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import Foundation | ||
|
||
func temporaryDirectory() throws -> URL { | ||
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString) | ||
try FileManager.default.createDirectory(at: temporaryDirectoryURL, withIntermediateDirectories: true) | ||
|
||
return temporaryDirectoryURL | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.