Skip to content

Commit b94ec32

Browse files
committed
Fixes SR-822, support tests directly under Tests directory
1 parent 235f5dd commit b94ec32

File tree

18 files changed

+224
-2
lines changed

18 files changed

+224
-2
lines changed

Fixtures/InvalidLayouts/DirectTestsWithModules1/Package.swift

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo {
2+
var bar: Int = 0
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import XCTest
2+
3+
@testable import ModuleA
4+
5+
class FooTests: XCTestCase {
6+
func testSuccess() {
7+
}
8+
}
9+
10+
#if os(Linux)
11+
extension FooTests: XCTestCaseProvider {
12+
var allTests: [(String, () throws -> Void)] {
13+
return [
14+
("testSuccess", testSuccess),
15+
]
16+
}
17+
}
18+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import XCTest
2+
3+
@testable import DirectTestsWithModules1
4+
@testable import ModuleAtest
5+
6+
XCTMain([
7+
FooTests(),
8+
BarTests(),
9+
])
10+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import XCTest
2+
3+
@testable import ModuleA
4+
5+
class BarTests: XCTestCase {
6+
func testSuccess() {
7+
}
8+
}
9+
10+
#if os(Linux)
11+
extension BarTests: XCTestCaseProvider {
12+
var allTests: [(String, () throws -> Void)] {
13+
return [
14+
("testSuccess", testSuccess),
15+
]
16+
}
17+
}
18+
#endif
19+

Fixtures/InvalidLayouts/DirectTestsWithModules2/Package.swift

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo {
2+
var bar: Int = 0
3+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import XCTest
2+
3+
@testable import DirectTestsWithModules2
4+
5+
class BarTests: XCTestCase {
6+
func testSuccess() {
7+
}
8+
}
9+
10+
#if os(Linux)
11+
extension BarTests: XCTestCaseProvider {
12+
var allTests: [(String, () throws -> Void)] {
13+
return [
14+
("testSuccess", testSuccess),
15+
]
16+
}
17+
}
18+
#endif
19+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import XCTest
2+
3+
class FooTests: XCTestCase {
4+
func testSuccess() {
5+
}
6+
}
7+
8+
#if os(Linux)
9+
extension FooTests: XCTestCaseProvider {
10+
var allTests: [(String, () throws -> Void)] {
11+
return [
12+
("testSuccess", testSuccess),
13+
]
14+
}
15+
}
16+
#endif
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import XCTest
2+
3+
@testable import DirectTests
4+
@testable import DirectTestsWithModules2
5+
6+
XCTMain([
7+
FooTests(),
8+
BarTests(),
9+
])
10+

Fixtures/ValidLayouts/SingleModule/DirectTests/Package.swift

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo {
2+
var bar: Int = 0
3+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import XCTest
2+
3+
class FooTests: XCTestCase {
4+
func testSuccess() {
5+
}
6+
}
7+
8+
#if os(Linux)
9+
extension FooTests: XCTestCaseProvider {
10+
var allTests: [(String, () throws -> Void)] {
11+
return [
12+
("testSuccess", testSuccess),
13+
]
14+
}
15+
}
16+
#endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import XCTest
2+
3+
@testable import DirectTests
4+
5+
XCTMain([
6+
FooTests(),
7+
])
8+

Sources/Transmute/Package+testModules.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,26 @@ import Utility
1313

1414
extension Package {
1515
func testModules() throws -> [TestModule] {
16-
return try walk(path, "Tests", recursively: false).filter{ $0.isDirectory }.map { dir in
17-
return TestModule(basename: dir.basename, sources: try self.sourcify(dir))
16+
let (directories, files) = walk(path, "Tests", recursively: false).partition{ $0.isDirectory }
17+
18+
let testDirectories = directories.filter{ !excludes.contains($0) }
19+
let rootTestFiles = files.filter {
20+
!$0.hasSuffix("LinuxMain.swift") && isValidSource($0) && !excludes.contains($0)
21+
}
22+
23+
if (testDirectories.count > 0 && rootTestFiles.count > 0) {
24+
throw ModuleError.InvalidLayout(.InvalidLayout)
25+
} else if (testDirectories.count > 0) {
26+
return try testDirectories.map {
27+
TestModule(basename: $0.basename, sources: try self.sourcify($0))
28+
}
29+
} else {
30+
if (rootTestFiles.count > 0) {
31+
let rootTestSource = Sources(paths: rootTestFiles, root: path)
32+
return [TestModule(basename: name, sources: rootTestSource)]
33+
}
1834
}
35+
36+
return []
1937
}
2038
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
extension SequenceType {
12+
@warn_unused_result
13+
public func partition(@noescape include:(Generator.Element)->Bool) -> ([Generator.Element], [Generator.Element]) {
14+
var left = Array<Generator.Element>()
15+
var right = Array<Generator.Element>()
16+
17+
for element in self {
18+
if include(element) {
19+
left.append(element)
20+
} else {
21+
right.append(element)
22+
}
23+
}
24+
25+
return (left, right)
26+
}
27+
}
28+

Tests/Functional/TestInvalidLayouts.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import XCTest
1212
import func POSIX.rmdir
1313
import func POSIX.unlink
14+
import func POSIX.rename
1415

1516
class InvalidLayoutsTestCase: XCTestCase {
1617

@@ -111,4 +112,46 @@ class InvalidLayoutsTestCase: XCTestCase {
111112
XCTAssertBuilds(prefix)
112113
}
113114
}
115+
116+
func testDirectTestsWithModules1() {
117+
/*
118+
Package
119+
├── Sources
120+
│ └── ModuleA
121+
│ └── Foo.swift
122+
└── Tests
123+
├── FooTests.swift <-- Invalid
124+
└── ModuleA
125+
└── ModuleATests.swift
126+
*/
127+
128+
fixture(name: "InvalidLayouts/DirectTestsWithModules1") { prefix in
129+
130+
XCTAssertBuildFails(prefix)
131+
132+
try POSIX.unlink("\(prefix)/Tests/FooTests.swift")
133+
XCTAssertBuilds(prefix)
134+
}
135+
}
136+
137+
func testDirectTestsWithModules2() {
138+
/*
139+
Package
140+
├── Sources
141+
│ └── Foo.swift
142+
└── Tests
143+
├── FooTests.swift <-- Invalid
144+
└── ImplicitModuleName
145+
└── ModuleTests.swift
146+
*/
147+
148+
fixture(name: "InvalidLayouts/DirectTestsWithModules1") { prefix in
149+
150+
XCTAssertBuildFails(prefix)
151+
152+
try POSIX.unlink("\(prefix)/Tests/FooTests.swift")
153+
XCTAssertBuilds(prefix)
154+
}
155+
}
156+
114157
}

Tests/Functional/TestValidLayouts.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ class ValidLayoutsTestCase: XCTestCase {
5050
}
5151
}
5252

53+
func testEmptyPackageWithDirectTests() {
54+
fixture(name: "ValidLayouts/SingleModule/DirectTests", file: #file, line: #line) { prefix in
55+
XCTAssertBuilds(prefix)
56+
XCTAssertFileExists(prefix, ".build", "debug", "DirectTests.swiftmodule")
57+
}
58+
}
59+
5360
func testMultipleModulesLibraries() {
5461
runLayoutFixture(name: "MultipleModules/Libraries") { prefix in
5562
XCTAssertBuilds(prefix)
@@ -189,6 +196,7 @@ extension ValidLayoutsTestCase {
189196
("testSingleModuleExecutable", testSingleModuleExecutable),
190197
("testSingleModuleCustomizedName", testSingleModuleCustomizedName),
191198
("testSingleModuleSubfolderWithSwiftSuffix", testSingleModuleSubfolderWithSwiftSuffix),
199+
("testEmptyPackageWithDirectTests", testEmptyPackageWithDirectTests),
192200
("testMultipleModulesLibraries", testMultipleModulesLibraries),
193201
("testMultipleModulesExecutables", testMultipleModulesExecutables),
194202
("testPackageIdentifiers", testPackageIdentifiers),

0 commit comments

Comments
 (0)