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

Conversation

bhargavg
Copy link
Contributor

@bhargavg bhargavg commented Mar 1, 2016

Added support for test files directly under Tests directory.

Sample:

Foo
|
- Package.swift
|
- Sources
|   - Bar.swift
- Tests
    - BarTests.swift

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

@kostiakoval
Copy link
Contributor

PR-158 is merged now, please update your tests.

@bhargavg
Copy link
Contributor Author

bhargavg commented Mar 2, 2016

@aciidb0mb3r @kostiakoval Made the changes and rebased. Please check.

if (rootTestFiles.count > 0) {
let rootTestSource = Sources(paths: rootTestFiles, root: path)
return [TestModule(basename: name, sources: rootTestSource)]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this might look better with guard

guard !(testDirectories.count > 0 && rootTestFiles.count > 0) else { throw ModuleError.InvalidLayout(.InvalidLayout) } 

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Done

@bhargavg
Copy link
Contributor Author

bhargavg commented Mar 3, 2016

Could someone please trigger CI tests on this PR?

@mxcl
Copy link
Contributor

mxcl commented Mar 4, 2016

@swift-ci please test

@bhargavg
Copy link
Contributor Author

bhargavg commented Mar 5, 2016

Fixed issue that caused build to fail.
@mxcl Could you please re-trigger CI?

@kostiakoval
Copy link
Contributor

I'm not sure I 100% agree with that implementation. Why can't we support both tests files in root Tests folder and subfolders with tests at the same time?

Just by simple adding 1 test file in the root, you can break your project. I think that could be confusing for people.

We could simple support both of them by implementing it like that.

extension Package {
    func testModules() throws -> [TestModule] {
        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)
        }
        var res: [TestModule] = []

        if (testDirectories.count > 0) {
            res + try testDirectories.map {
                TestModule(basename: $0.basename, sources: try self.sourcify($0)) 
            }
        }
        if (rootTestFiles.count > 0) {
            let rootTestSource = Sources(paths: rootTestFiles, root: path)
            res.append(TestModule(basename: name, sources: rootTestSource))
        }

        return res
    }
}

What do you think?

@bhargavg
Copy link
Contributor Author

bhargavg commented Mar 5, 2016

Yes @kostiakoval, I thought about the same. In fact, in swift-build-dev mailing list, I've asked whether we could support the following layouts.

layout-1
├── Package.swift
└── Sources
    └── ModuleA
        ├── Bar.swift
        ├── Foo.swift
        └── Tests
            └── ModuleATests.swift

Current Status: Building - Tests Failing


layout-2
├── Foo.swift
├── Package.swift
└── Tests
    └── FooTests.swift

Current Status: Building - Tests Failing


layout-3
├── Package.swift
├── Sources
│   ├── Foo.swift
│   └── ModuleA
│       ├── Bar.swift
│       └── Tests
│           └── BarTests.swift
└── Tests
    └── FooTests.swift

Current Status: Build Failing - Invalid Layout


layout-4
├── Package.swift
├── Sources
│   ├── Foo.swift
│   ├── ModuleA
│   │   └── Bar.swift
│   └── Tests
│       └── FooTests.swift
└── Tests
    └── ModuleA
        └── BarTests.swift

Current Status: Build Failing - Invalid Layout

Max Howell, told that he is drafting a proposal that will make folder layouts to be more flexible. That's why I haven't added support for tests directly under Tests directory along with sub directories.

So, what do you think? Shall we wait for the proposal to come out? or can we merge this one and refactor once the proposal is out?

@kostiakoval
Copy link
Contributor

Yes, I've seen the mail, and If I understood Max correctly

called “No More Magic”

we should include all test that are in a Root folder for tests (the Tests folder)
I think @mxcl is the correct person to answer that question.

@mxcl
Copy link
Contributor

mxcl commented Mar 8, 2016

Of course, we cannot actually test a single module executable, so we should probably address that separately.

@kostiakoval
Copy link
Contributor

This is still a valid issue. Can we resurrect work on it? @bhargavg @mxcl

swift test produce very bad error if I try to run it with test in Tests folder

fatal error: unexpectedly found nil while unwrapping an Optional value: file   
apple/swift/stdlib/public/core/Optional.swift, line 136

@bhargavg
Copy link
Contributor Author

@kostiakoval I'm still waiting for "No More Magic" proposal to come out which should draw the line between supported and unsupported layouts.

For now, as you might've already guessed, you can create folder inside Tests with module name to prevent this error.

@kostiakoval
Copy link
Contributor

@bhargavg I think it would be better to throw an exception with diagnostics info that this Test folder layout is not supported for now and give tips to users how to fix it.
It's way better than having fatal error:

@mxcl any thoughts?

@bhargavg
Copy link
Contributor Author

@kostiakoval Agreed. Raised PR #301 making swift test to throw an error instead of failing with a cryptic error message.

@mxcl
Copy link
Contributor

mxcl commented May 2, 2016

I'm torn between:

  1. Insisting Tests/ have subdirs
  2. Being consistent with Sources/

So maybe someone else can weigh in.

I like 1. as things are then less ambiguous in how our code works and what the user will get when experimenting. But the reality is we already support Sources/ without subdirs.

@ddunbar
Copy link
Contributor

ddunbar commented May 5, 2016

My vote is consistency with Sources, I am surprised when it isn't.

@ddunbar
Copy link
Contributor

ddunbar commented Jul 1, 2016

I'm closing this due to age, I think we are going to probably end up going in the direction of not accepting this to enforce consistency with a more scalable convention (subdirectories). In any case, we should resolve it in a proposal or bug, not decide in this PR.

@ddunbar ddunbar closed this Jul 1, 2016
aciidgh pushed a commit to aciidgh/swift-package-manager that referenced this pull request Jan 11, 2019
…emoval

<rdar://problem/30961839> Implement support for stale file removal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants