Skip to content

Commit 852af2e

Browse files
committed
Refactor copy/pasta code
1 parent 40f2b10 commit 852af2e

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

Tests/Functional/SwiftTestTests.swift

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,17 @@ class SwiftTestTests: XCTestCase {
3434
ranFirstTest = exploded.contains("Test Case '-[SwiftTestModuleTestSuite.SwiftTestModuleTests testGiveNumber]' started.")
3535
ranSecondTest = exploded.contains("Test Case '-[SwiftTestModuleTestSuite.SwiftTestModuleTests testGiveNumber2]' started.")
3636
#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
37+
func outputContains(_ str: String) -> Bool {
38+
return !exploded.filter { $0.characters.starts(with: str.characters) }.isEmpty
39+
}
40+
ranFirstTest = outputContains("Test Case \'SwiftTestModuleTests.testGiveNumber\' started")
41+
ranSecondTest = outputContains("Test Case \'SwiftTestModuleTests.testGiveNumber2\' started")
3942
#endif
4043
}
4144
XCTAssertEqual(ranFirstTest, true, "First test did not run")
4245
XCTAssertEqual(ranSecondTest, false, "Second test ran")
4346
}
4447
}
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-
}
6148
}
6249

6350
extension SwiftTestTests {

Tests/Functional/Utilities.swift

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,28 @@ enum Configuration {
9898

9999
private var globalSymbolInMainBinary = 0
100100

101+
private enum SwiftExecutable: String {
102+
case Build = "swift-build"
103+
case Test = "swift-test"
104+
}
105+
106+
private func executablePath(exe: SwiftExecutable) -> String {
107+
#if os(OSX)
108+
for bundle in NSBundle.allBundles() where bundle.bundlePath.hasSuffix(".xctest") {
109+
return Path.join(bundle.bundlePath.parentDirectory, exe.rawValue)
110+
}
111+
fatalError()
112+
#else
113+
return Path.join(Process.arguments.first!.abspath().parentDirectory, exe.rawValue)
114+
#endif
115+
}
101116

102117
func swiftBuildPath() -> String {
103-
#if os(OSX)
104-
for bundle in NSBundle.allBundles() where bundle.bundlePath.hasSuffix(".xctest") {
105-
return Path.join(bundle.bundlePath.parentDirectory, "swift-build")
106-
}
107-
fatalError()
108-
#else
109-
return Path.join(Process.arguments.first!.abspath().parentDirectory, "swift-build")
110-
#endif
118+
return executablePath(exe: .Build)
119+
}
120+
121+
func swiftTestPath() -> String {
122+
return executablePath(exe: .Test)
111123
}
112124

113125

@@ -155,17 +167,6 @@ func executeSwiftBuild(_ chdir: String, configuration: Configuration = .Debug, p
155167
}
156168
}
157169

158-
func swiftTestPath() -> String {
159-
#if os(OSX)
160-
for bundle in NSBundle.allBundles() where bundle.bundlePath.hasSuffix(".xctest") {
161-
return Path.join(bundle.bundlePath.parentDirectory, "swift-test")
162-
}
163-
fatalError()
164-
#else
165-
return Path.join(Process.arguments.first!.abspath().parentDirectory, "swift-test")
166-
#endif
167-
}
168-
169170
func executeSwiftTest(_ chdir: String, printIfError: Bool = false, additionalArgs: [String] = []) throws -> String {
170171
let args = [swiftTestPath(), "--chdir", chdir] + additionalArgs
171172
var out = ""

0 commit comments

Comments
 (0)