Skip to content

Re-enable the tests under SwiftPM. #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions DocoptTests/DocoptTestCasesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class DocoptTestCasesTests: XCTestCase {
XCTAssertTrue(exists, "Fixtures file testcases.docopt does not exist in testing bundle")
}
}

func testFixturesFileCanBeOpened() {
XCTAssertNotEqual(fixturesFileContents(), "", "Could not read fixtures file")
}

func testTestCases() {
let rawTestCases = fixturesFileContents()
let parser = DocoptTestCaseParser(rawTestCases)

for testCase in parser.testCases {
let expectedOutput: AnyObject = testCase.expectedOutput
var result: AnyObject = "user-error" as AnyObject
Expand All @@ -58,12 +58,26 @@ class DocoptTestCasesTests: XCTestCase {
}
}
}


private func fallbackFilePath(from exeURL : URL) -> String? {
// SwiftPM currently doesn't support building bundles, so if the tests are run
// with SwiftPM we'll fail to find the bundle path here.
// As a temporary workaround, we can fall back on a relative path from the executable.
// This is fragile as it relies on the assumption that we know where SwiftPM will
// put the build products, and where the testcases file lives relative to them,
// but it's better than just disabling all the tests...
let path = exeURL.appendingPathComponent("../../../../Tests/DocoptTests/testcases.docopt").standardized.path
return path
}

private func fixturesFilePath() -> String? {
let testBundle: Bundle = Bundle(for: type(of: self))
return testBundle.path(forResource: "testcases", ofType: "docopt")
guard let path = testBundle.path(forResource: "testcases", ofType: "docopt") else {
return fallbackFilePath(from: testBundle.bundleURL)
}
return path
}

private func fixturesFileContents() -> String {
if let filePath = self.fixturesFilePath() {
let fileContents = try! String(contentsOfFile: filePath, encoding: String.Encoding.utf8)
Expand Down
14 changes: 6 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ let package = Package(
.target(
name: "Docopt",
path: "Sources"
)
// Commented out until SPM supports resources
//,
// .testTarget(
// name: "DocoptTests",
// dependencies: ["Docopt"],
// path: "DocoptTests"
// )
),
.testTarget(
name: "DocoptTests",
dependencies: ["Docopt"],
path: "DocoptTests"
)
]
)