Skip to content

Add test for Test module. #143

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

Closed
wants to merge 3 commits into from
Closed
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
Empty file.
3 changes: 3 additions & 0 deletions Fixtures/SwiftTesting/SingleTarget/Sources/Foo/Foo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func ten() -> Int {
return 10
}
17 changes: 17 additions & 0 deletions Fixtures/SwiftTesting/SingleTarget/Tests/Foo/Test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@testable import Foo
import XCTest

class SimpleGetTests: XCTestCase {

func testGetRequestStatusCode() {
XCTAssertEqual(ten(), 10)
}
}

extension SimpleGetTests {
static var allTests : [(String, SimpleGetTests -> () throws -> Void)] {
return [
("testGetRequestStatusCode", testGetRequestStatusCode),
]
}
}
7 changes: 7 additions & 0 deletions Fixtures/SwiftTesting/SingleTarget/Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

@testable import FooTestSuite

XCTMain([
testCase(SimpleGetTests.allTests),
])
14 changes: 14 additions & 0 deletions Tests/Functional/TestMiscellaneous.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,18 @@ class MiscellaneousTestCase: XCTestCase {
XCTAssertFileExists(prefix, ".build/debug/libBar.a")
}
}

func testModuleWithTests() {
fixture(name: "SwiftTesting/SingleTarget") { prefix in
XCTAssertBuilds(prefix)
XCTAssertFileExists(prefix, ".build", "debug", "Foo.swiftmodule")
XCTAssertTests(prefix)
#if os(OSX)
XCTAssertDirectoryExists(prefix, ".build", "debug", "Package.xctest")
#else
XCTAssertFileExists(prefix, ".build", "debug", "test-Package")
#endif

}
}
}
1 change: 1 addition & 0 deletions Tests/Functional/TestValidLayouts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ extension MiscellaneousTestCase {
("testInternalDependencyEdges", testInternalDependencyEdges),
("testExternalDependencyEdges1", testExternalDependencyEdges1),
("testExternalDependencyEdges2", testExternalDependencyEdges2),
("testModuleWithTests", testModuleWithTests),
]
}
}
Expand Down
47 changes: 47 additions & 0 deletions Tests/Functional/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,43 @@ func executeSwiftBuild(chdir: String, configuration: Configuration = .Debug, pri
}
}

//FIXME: rafactor to use a common dir for swift-build and swift-test
func swiftTestPath() -> String {
#if os(OSX)
for bundle in NSBundle.allBundles() where bundle.bundlePath.hasSuffix(".xctest") {
return Path.join(bundle.bundlePath.parentDirectory, "swift-test")
}
fatalError()
#else
return Path.join(try! Process.arguments.first!.abspath().parentDirectory, "swift-test")
#endif
}

//TODO: refactor swift-build and swift-test tools runing
func executeSwiftTest(dir: String, printIfError: Bool = false) throws -> String {
let toolPath = swiftTestPath()
let env = [String:String]()
let args = [toolPath]

var out = ""
do {
// FIXME: this probably should be refactored to --chdir arg in swift-test
let cwd = try getcwd()
defer { _ = try? POSIX.chdir(cwd) }
try POSIX.chdir(dir)

try popen(args, redirectStandardError: true, environment: env) {
out += $0
}
return out
} catch {
if printIfError {
print(out)
}
throw error
}
}

func mktmpdir(file: StaticString = #file, line: UInt = #line, @noescape body: (String) throws -> Void) {
do {
try POSIX.mkdtemp("spm-tests") { dir in
Expand Down Expand Up @@ -171,6 +208,16 @@ func XCTAssertBuildFails(paths: String..., file: StaticString = #file, line: UIn
}
}

func XCTAssertTests(paths: String..., file: StaticString = #file, line: UInt = #line) {
let prefix = Path.join(paths)
do {
print(" Testing")
try executeSwiftTest(prefix, printIfError: true)
} catch {
XCTFail("swift-test failed:\n\n\(error)\n", file: file, line: line)
}
}

func XCTAssertFileExists(paths: String..., file: StaticString = #file, line: UInt = #line) {
let path = Path.join(paths)
if !path.isFile {
Expand Down