Skip to content

Commit 2a4b91c

Browse files
authored
Merge pull request #626 from abertelrud/SR-2270
Validate that there are no loose source files in the `Tests` directory
2 parents 9ce9abf + 82b37b3 commit 2a4b91c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,19 @@ public struct PackageBuilder {
558558
return []
559559
}
560560

561+
// Get the contents of the Tests directory.
562+
let testsDirContents = try directoryContents(testsPath)
563+
564+
// Check that the Tests directory doesn't contain any loose source files.
565+
// FIXME: Right now we just check for source files. We need to decide whether we should check for other kinds of files too.
566+
// FIXME: We should factor out the checking for the `LinuxMain.swift` source file. So ugly...
567+
let looseSourceFiles = testsDirContents.filter(isValidSource).filter({ $0.basename.lowercased() != "linuxmain.swift" })
568+
guard looseSourceFiles.isEmpty else {
569+
throw ModuleError.invalidLayout(.unexpectedSourceFiles(looseSourceFiles.map{ $0.asString }))
570+
}
571+
561572
// Create the test modules
562-
return try directoryContents(testsPath).filter(shouldConsiderDirectory).flatMap { dir in
573+
return try testsDirContents.filter(shouldConsiderDirectory).flatMap { dir in
563574
return [try createModule(dir, name: dir.basename, isTest: true)]
564575
}
565576
}

Tests/PackageLoadingTests/ConventionTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,16 @@ class ConventionTests: XCTestCase {
554554
}
555555
}
556556

557+
func testLooseSourceFileInTestsDir() throws {
558+
// Loose source file in Tests/
559+
let fs = InMemoryFileSystem(emptyFiles:
560+
"/Sources/main.swift",
561+
"/Tests/source.swift")
562+
PackageBuilderTester("LooseSourceFileInTestsDir", in: fs) { result in
563+
result.checkDiagnostic("the package has an unsupported layout, unexpected source file(s) found: /Tests/source.swift fix: move the file(s) inside a module")
564+
}
565+
}
566+
557567
func testManifestTargetDeclErrors() throws {
558568
// Reference a target which doesn't exist.
559569
var fs = InMemoryFileSystem(emptyFiles:
@@ -857,6 +867,7 @@ class ConventionTests: XCTestCase {
857867
("testCustomTargetDependencies", testCustomTargetDependencies),
858868
("testTestTargetDependencies", testTestTargetDependencies),
859869
("testInvalidTestTargets", testInvalidTestTargets),
870+
("testLooseSourceFileInTestsDir", testLooseSourceFileInTestsDir),
860871
("testManifestTargetDeclErrors", testManifestTargetDeclErrors),
861872
("testProducts", testProducts),
862873
("testBadProducts", testBadProducts),

0 commit comments

Comments
 (0)