Skip to content

Fixes SR-822, support tests directly under Tests directory #162

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 5 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo {
var bar: Int = 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import XCTest

@testable import ModuleA

class FooTests: XCTestCase {
func testSuccess() {
}
}

extension FooTests {
static var allTests: [(String, FooTests -> () throws -> Void)] {
return [
("testSuccess", testSuccess),
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

@testable import ModuleAtest

XCTMain([
testCase(BarTests.allTests)
])
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import XCTest

@testable import ModuleA

class BarTests: XCTestCase {
func testSuccess() {
}
}

extension BarTests {
static var allTests: [(String, BarTests -> () throws -> Void)] {
return [
("testSuccess", testSuccess),
]
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo {
var bar: Int = 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import XCTest

@testable import DirectTestsWithModules2

class BarTests: XCTestCase {
func testSuccess() {
}
}

extension BarTests {
static var allTests: [(String, BarTests -> () throws -> Void)] {
return [
("testSuccess", testSuccess),
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import XCTest

class FooTests: XCTestCase {
func testSuccess() {
}
}

extension FooTests {
static var allTests: [(String, FooTests -> () throws -> Void)] {
return [
("testSuccess", testSuccess),
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

@testable import DirectTestsWithModules2test

XCTMain([
testCase(BarTests.allTests)
])
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Foo {
var bar: Int = 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import XCTest

class FooTests: XCTestCase {
func testSuccess() {
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Linux support. It should be like

#if os(Linux)
extension FooTests: XCTestCaseProvider {
    var allTests: [(String, () throws -> Void)] {
        return [
            ("testSuccess", testSuccess),
        ]
    }
}
#endif


extension FooTests {
static var allTests: [(String, FooTests -> () throws -> Void)] {
return [
("testSuccess", testSuccess),
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

@testable import DirectTeststest

XCTMain([
testCase(FooTests.allTests)
])
21 changes: 19 additions & 2 deletions Sources/Transmute/Package+testModules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,25 @@ import Utility

extension Package {
func testModules() throws -> [TestModule] {
return try walk(path, "Tests", recursively: false).filter{ $0.isDirectory }.map { dir in
return TestModule(basename: dir.basename, sources: try self.sourcify(dir))
let (directories, files) = walk(path, "Tests", recursively: false).partition{ $0.isDirectory }

let testDirectories = directories.filter{ !excludes.contains($0) }
let rootTestFiles = files.filter {
!$0.hasSuffix("LinuxMain.swift") && isValidSource($0) && !excludes.contains($0)
}

if (testDirectories.count > 0) {
if (rootTestFiles.count > 0) {
throw ModuleError.InvalidLayout(.InvalidLayout)
}
return try testDirectories.map {
TestModule(basename: $0.basename, sources: try self.sourcify($0))
}
} else if (rootTestFiles.count > 0) {
let rootTestSource = Sources(paths: rootTestFiles, root: path)
return [TestModule(basename: name, sources: rootTestSource)]
}

return []
}
}
13 changes: 0 additions & 13 deletions Sources/Utility/ArrayExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,4 @@ extension Array {
}
return (t, u)
}

public func partition(body: (Element) -> Bool) -> ([Element], [Element]) {
var a = [Element]()
var b = [Element]()
for e in self {
if body(e) {
a.append(e)
} else {
b.append(e)
}
}
return (a, b)
}
}
28 changes: 28 additions & 0 deletions Sources/Utility/SequenceTypeExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
This source file is part of the Swift.org open source project

Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception

See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

extension SequenceType {
@warn_unused_result
public func partition(@noescape include: (Generator.Element) -> Bool) -> ([Generator.Element], [Generator.Element]) {
var left = Array<Generator.Element>()
var right = Array<Generator.Element>()

for element in self {
if include(element) {
left.append(element)
} else {
right.append(element)
}
}

return (left, right)
}
}

43 changes: 43 additions & 0 deletions Tests/Functional/TestInvalidLayouts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import XCTest
import func POSIX.rmdir
import func POSIX.unlink
import func POSIX.rename

class InvalidLayoutsTestCase: XCTestCase {

Expand Down Expand Up @@ -111,4 +112,46 @@ class InvalidLayoutsTestCase: XCTestCase {
XCTAssertBuilds(prefix)
}
}

func testDirectTestsWithModules1() {
/*
Package
├── Sources
│ └── ModuleA
│ └── Foo.swift
└── Tests
├── FooTests.swift <-- Invalid
└── ModuleA
└── ModuleATests.swift
*/

fixture(name: "InvalidLayouts/DirectTestsWithModules1") { prefix in

XCTAssertBuildFails(prefix)

try POSIX.unlink("\(prefix)/Tests/FooTests.swift")
XCTAssertBuilds(prefix)
}
}

func testDirectTestsWithModules2() {
/*
Package
├── Sources
│ └── Foo.swift
└── Tests
├── FooTests.swift <-- Invalid
└── ImplicitModuleName
└── ModuleTests.swift
*/

fixture(name: "InvalidLayouts/DirectTestsWithModules1") { prefix in

XCTAssertBuildFails(prefix)

try POSIX.unlink("\(prefix)/Tests/FooTests.swift")
XCTAssertBuilds(prefix)
}
}

}
10 changes: 10 additions & 0 deletions Tests/Functional/TestValidLayouts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class ValidLayoutsTestCase: XCTestCase {
}
}

func testEmptyPackageWithDirectTests() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add testEmptyPackageWithDirectTests in ValidLayoutsTestCase's XCTestCaseProvider conformance

fixture(name: "ValidLayouts/SingleModule/DirectTests", file: #file, line: #line) { prefix in
XCTAssertBuilds(prefix)
XCTAssertFileExists(prefix, ".build", "debug", "DirectTests.swiftmodule")
}
}

func testMultipleModulesLibraries() {
runLayoutFixture(name: "MultipleModules/Libraries") { prefix in
XCTAssertBuilds(prefix)
Expand Down Expand Up @@ -152,6 +159,8 @@ extension InvalidLayoutsTestCase {
("testInvalidLayout3", testInvalidLayout3),
("testInvalidLayout4", testInvalidLayout4),
("testInvalidLayout5", testInvalidLayout5),
("testDirectTestsWithModules1", testDirectTestsWithModules1),
("testDirectTestsWithModules2", testDirectTestsWithModules2),
]
}
}
Expand Down Expand Up @@ -189,6 +198,7 @@ extension ValidLayoutsTestCase {
("testSingleModuleExecutable", testSingleModuleExecutable),
("testSingleModuleCustomizedName", testSingleModuleCustomizedName),
("testSingleModuleSubfolderWithSwiftSuffix", testSingleModuleSubfolderWithSwiftSuffix),
("testEmptyPackageWithDirectTests", testEmptyPackageWithDirectTests),
("testMultipleModulesLibraries", testMultipleModulesLibraries),
("testMultipleModulesExecutables", testMultipleModulesExecutables),
("testPackageIdentifiers", testPackageIdentifiers),
Expand Down