Skip to content

Commit 82b37b3

Browse files
committed
Check for loose source files in the Tests directory (it's a package
layout validation error). https://bugs.swift.org/browse/SR-2270 rdar://problem/28039111
1 parent 68fea29 commit 82b37b3

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
@@ -562,8 +562,19 @@ public struct PackageBuilder {
562562
return []
563563
}
564564

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

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)