Skip to content

Commit 32fdb7e

Browse files
committed
Add test for swift-test specifier
1 parent 7ccf628 commit 32fdb7e

File tree

8 files changed

+151
-4
lines changed

8 files changed

+151
-4
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import PackageDescription
2+
3+
let package = Package(
4+
name: "SwiftTestModule"
5+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public func giveNumber() -> Int {
2+
return 5
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
3+
@testable import SwiftTestModuleTestSuite
4+
5+
XCTMain([
6+
testCase(SwiftTestModuleTests.allTests),
7+
])
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@testable import SwiftTestModule
2+
import XCTest
3+
4+
class SwiftTestModuleTests: XCTestCase {
5+
func testGiveNumber() {
6+
XCTAssertEqual(giveNumber(), 5)
7+
}
8+
9+
func testGiveNumber2() {
10+
XCTAssertEqual(giveNumber(), 5)
11+
}
12+
}
13+
14+
extension SwiftTestModuleTests {
15+
static var allTests : [(String, SwiftTestModuleTests -> () throws -> Void)] {
16+
return [
17+
("testGiveNumber", testGiveNumber),
18+
("testGiveNumber2", testGiveNumber2),
19+
]
20+
}
21+
}

Sources/swift-test/usage.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func usage(_ print: (String) -> Void = { print($0) }) {
1818
print("USAGE: swift test [specifier] [options]")
1919
print("")
2020
print("SPECIFIER:")
21-
print(" TestModule.TestCase Run a test case subclass")
22-
print(" TestModule.TestCase/test1 Run a specific test method")
21+
print(" -s TestModule.TestCase Run a test case subclass")
22+
print(" -s TestModule.TestCase/test1 Run a specific test method")
2323
print("")
2424
print("OPTIONS:")
2525
print(" --chdir Change working directory before any other operation [-C]")
@@ -34,8 +34,11 @@ enum Mode: Argument, Equatable, CustomStringConvertible {
3434
switch argument {
3535
case "--help", "--usage", "-h":
3636
self = .Usage
37+
case "-s":
38+
guard let specifier = pop() else { throw OptionsParser.Error.ExpectedAssociatedValue(argument) }
39+
self = .Run(specifier)
3740
default:
38-
self = .Run(argument)
41+
return nil
3942
}
4043
}
4144

Tests/Functional/SwiftTestTests.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import XCTest
12+
13+
class SwiftTestTests: XCTestCase {
14+
15+
func testSwiftTestTests() {
16+
fixture(name: "ValidLayouts/SwiftTestModule") { prefix in
17+
XCTAssertBuilds(prefix)
18+
XCTAssertTests(prefix)
19+
}
20+
}
21+
22+
func testSwiftTestTestsSelectedTest() {
23+
// Here we check if specifer in swift test works.
24+
// Only one test method should execute.
25+
fixture(name: "ValidLayouts/SwiftTestModule") { prefix in
26+
XCTAssertBuilds(prefix)
27+
28+
var ranFirstTest = false
29+
var ranSecondTest = false
30+
31+
XCTAssertTests(prefix, additionalArgs: ["-s", "SwiftTestModuleTestSuite.SwiftTestModuleTests/testGiveNumber"]) { output in
32+
let exploded = output.characters.split(separator: "\n").map(String.init)
33+
#if os(OSX)
34+
ranFirstTest = exploded.contains("Test Case '-[SwiftTestModuleTestSuite.SwiftTestModuleTests testGiveNumber]' started.")
35+
ranSecondTest = exploded.contains("Test Case '-[SwiftTestModuleTestSuite.SwiftTestModuleTests testGiveNumber2]' started.")
36+
#else
37+
ranFirstTest = !exploded.filter { self.string($0, hasPrefix:"Test Case \'SwiftTestModuleTests.testGiveNumber\' started") }.isEmpty
38+
ranSecondTest = !exploded.filter { self.string($0, hasPrefix:"Test Case \'SwiftTestModuleTests.testGiveNumber2\' started") }.isEmpty
39+
#endif
40+
}
41+
XCTAssertEqual(ranFirstTest, true, "First test did not run")
42+
XCTAssertEqual(ranSecondTest, false, "Second test ran")
43+
}
44+
}
45+
46+
// FIXME: Copy pasted from libc/String+Linux.swift because swiftc errors out saying:
47+
// `ambiguous use of 'hasPrefix'` found in `Foundation.String` and `libc.String`.
48+
// This is probably because XCTest imports Foundation and libc is imported in other
49+
// parts of Funtional testsuite.
50+
private func string(_ prefix: String, hasPrefix str: String) -> Bool {
51+
if prefix.utf8.count < str.utf8.count {
52+
return false
53+
}
54+
for i in 0..<str.utf8.count {
55+
if prefix.utf8[prefix.utf8.startIndex.advanced(by: i)] != str.utf8[str.utf8.startIndex.advanced(by: i)] {
56+
return false
57+
}
58+
}
59+
return true
60+
}
61+
}
62+
63+
extension SwiftTestTests {
64+
static var allTests : [(String, SwiftTestTests -> () throws -> Void)] {
65+
return [
66+
("testSwiftTestTests", testSwiftTestTests),
67+
("testSwiftTestTestsSelectedTest", testSwiftTestTestsSelectedTest),
68+
]
69+
}
70+
}

Tests/Functional/Utilities.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ func swiftBuildPath() -> String {
110110
#endif
111111
}
112112

113-
114113
func executeSwiftBuild(_ chdir: String, configuration: Configuration = .Debug, printIfError: Bool = false, Xld: [String] = []) throws -> String {
115114
var env = [String:String]()
116115
#if Xcode
@@ -155,6 +154,44 @@ func executeSwiftBuild(_ chdir: String, configuration: Configuration = .Debug, p
155154
}
156155
}
157156

157+
func swiftTestPath() -> String {
158+
#if os(OSX)
159+
for bundle in NSBundle.allBundles() where bundle.bundlePath.hasSuffix(".xctest") {
160+
return Path.join(bundle.bundlePath.parentDirectory, "swift-test")
161+
}
162+
fatalError()
163+
#else
164+
return Path.join(Process.arguments.first!.abspath().parentDirectory, "swift-test")
165+
#endif
166+
}
167+
168+
func executeSwiftTest(_ chdir: String, printIfError: Bool = false, additionalArgs: [String] = []) throws -> String {
169+
let args = [swiftTestPath(), "--chdir", chdir] + additionalArgs
170+
var out = ""
171+
do {
172+
try popen(args, redirectStandardError: true, environment: [:]) {
173+
out += $0
174+
}
175+
return out
176+
} catch {
177+
if printIfError {
178+
print("output:", out)
179+
print("swift-test:", swiftTestPath())
180+
}
181+
throw error
182+
}
183+
}
184+
185+
func XCTAssertTests(_ paths: String..., file: StaticString = #file, line: UInt = #line, additionalArgs: [String] = [], output: ((String) -> Void)? = nil) {
186+
let prefix = Path.join(paths)
187+
do {
188+
let out = try executeSwiftTest(prefix, printIfError: true, additionalArgs: additionalArgs)
189+
output?(out)
190+
} catch {
191+
XCTFail("`swift test \(additionalArgs)' failed:\n\n\(error)\n", file: file, line: line)
192+
}
193+
}
194+
158195
func mktmpdir(_ file: StaticString = #file, line: UInt = #line, @noescape body: (String) throws -> Void) {
159196
do {
160197
try POSIX.mkdtemp("spm-tests") { dir in

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import XCTest
1616

1717
XCTMain([
1818
testCase(TestClangModulesTestCase.allTests),
19+
testCase(SwiftTestTests.allTests),
1920
testCase(DependencyResolutionTestCase.allTests),
2021
testCase(FileTests.allTests),
2122
testCase(GetTests.allTests),

0 commit comments

Comments
 (0)