From e11001aabf30078c005226410ac19df5d0af5750 Mon Sep 17 00:00:00 2001 From: Sam Deane Date: Wed, 7 Mar 2018 17:44:56 +0000 Subject: [PATCH 1/3] Re-enable the tests under SwiftPM. Since we can't use a bundle to find the test data, we fall back on a relative path from the built executable. This is a bit skanky, but it's a useable workaround. --- DocoptTests/DocoptTestCasesTests.swift | 13 ++++++++++++- Package.swift | 14 +++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/DocoptTests/DocoptTestCasesTests.swift b/DocoptTests/DocoptTestCasesTests.swift index ddaf89f..16f9027 100644 --- a/DocoptTests/DocoptTestCasesTests.swift +++ b/DocoptTests/DocoptTestCasesTests.swift @@ -61,7 +61,18 @@ class DocoptTestCasesTests: XCTestCase { 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 { + // SwiftPM currently doesn't support bundles, so if the tests are run with it, + // we'll fail to find the bundle path here. + // As a temporary workaround, we can fall back on a relative path. This is fragile + // as it relies on the assumption that we know where SwiftPM will put the + // executable, and where the testcases file lives relative to it, but it's + // better than just disabling all the tests... + let url = testBundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("DocoptTests").appendingPathComponent("testcases.docopt") + return url.path + } + + return path } private func fixturesFileContents() -> String { diff --git a/Package.swift b/Package.swift index 4c38955..c038980 100644 --- a/Package.swift +++ b/Package.swift @@ -12,12 +12,12 @@ let package = Package( name: "Docopt", path: "Sources" ) - // Commented out until SPM supports resources - //, - // .testTarget( - // name: "DocoptTests", - // dependencies: ["Docopt"], - // path: "DocoptTests" - // ) +// Commented out until SPM supports resources + , + .testTarget( + name: "DocoptTests", + dependencies: ["Docopt"], + path: "DocoptTests" + ) ] ) From 5ecdb80f3b6efb550bbad476412ed1807e11065d Mon Sep 17 00:00:00 2001 From: Sam Deane Date: Wed, 7 Mar 2018 17:47:15 +0000 Subject: [PATCH 2/3] Removed obsolete comment. --- Package.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Package.swift b/Package.swift index c038980..6a07509 100644 --- a/Package.swift +++ b/Package.swift @@ -11,9 +11,7 @@ let package = Package( .target( name: "Docopt", path: "Sources" - ) -// Commented out until SPM supports resources - , + ), .testTarget( name: "DocoptTests", dependencies: ["Docopt"], From f456fc336fa37dae1d78f3b6f76e3082cd7c9269 Mon Sep 17 00:00:00 2001 From: Sam Deane Date: Sat, 10 Mar 2018 13:55:45 +0000 Subject: [PATCH 3/3] moved fallbackFilePath into a separate method --- DocoptTests/DocoptTestCasesTests.swift | 31 ++++++++++++++------------ 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/DocoptTests/DocoptTestCasesTests.swift b/DocoptTests/DocoptTestCasesTests.swift index 16f9027..76e9dfb 100644 --- a/DocoptTests/DocoptTestCasesTests.swift +++ b/DocoptTests/DocoptTestCasesTests.swift @@ -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 @@ -58,23 +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)) guard let path = testBundle.path(forResource: "testcases", ofType: "docopt") else { - // SwiftPM currently doesn't support bundles, so if the tests are run with it, - // we'll fail to find the bundle path here. - // As a temporary workaround, we can fall back on a relative path. This is fragile - // as it relies on the assumption that we know where SwiftPM will put the - // executable, and where the testcases file lives relative to it, but it's - // better than just disabling all the tests... - let url = testBundle.bundleURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("DocoptTests").appendingPathComponent("testcases.docopt") - return url.path + 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)