Skip to content

Commit 1c16b69

Browse files
authored
Merge pull request #667 from aciidb0mb3r/update-tests
Remove dependency from Packages/ dir
2 parents dd677d0 + 5ff1145 commit 1c16b69

File tree

7 files changed

+96
-25
lines changed

7 files changed

+96
-25
lines changed

Sources/TestSupport/SwiftPMProduct.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import Utility
1818
import class Foundation.Bundle
1919
#endif
2020

21+
enum SwiftPMProductError: Swift.Error {
22+
case packagePathNotFound
23+
}
24+
2125
/// Defines the executables used by SwiftPM.
2226
/// Contains path to the currently built executable and
2327
/// helper method to execute them.
@@ -65,6 +69,10 @@ public enum SwiftPMProduct {
6569
public func execute(_ args: [String], chdir: AbsolutePath? = nil, env: [String: String] = [:], printIfError: Bool = false) throws -> String {
6670
var out = ""
6771
var completeArgs = [path.asString]
72+
// FIXME: Eliminate this when we switch to the new resolver.
73+
if SwiftPMProduct.enableNewResolver && self != .XCTestHelper {
74+
completeArgs += ["--enable-new-resolver"]
75+
}
6876
if let chdir = chdir {
6977
completeArgs += ["--chdir", chdir.asString]
7078
}
@@ -84,4 +92,24 @@ public enum SwiftPMProduct {
8492
throw error
8593
}
8694
}
95+
96+
/// Set this to true to run tests with new resolver.
97+
public static var enableNewResolver = false
98+
99+
public static func packagePath(for packageName: String, packageRoot: AbsolutePath) throws -> AbsolutePath {
100+
// FIXME: The directory paths are hard coded right now and should be replaced by --get-package-path
101+
// whenever we design that. https://bugs.swift.org/browse/SR-2753
102+
let packagesPath: AbsolutePath
103+
if enableNewResolver {
104+
packagesPath = packageRoot.appending(components: ".build", "checkouts")
105+
} else {
106+
packagesPath = packageRoot.appending(component: "Packages")
107+
}
108+
for name in try localFileSystem.getDirectoryContents(packagesPath) {
109+
if name.hasPrefix(packageName) {
110+
return packagesPath.appending(RelativePath(name))
111+
}
112+
}
113+
throw SwiftPMProductError.packagePathNotFound
114+
}
87115
}

Tests/CommandsTests/BuildToolTests.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ final class BuildToolTests: XCTestCase {
4444
// Clean, and check for removal.
4545
_ = try execute(["--clean"], chdir: packageRoot)
4646
XCTAssert(!isFile(packageRoot.appending(components: ".build", "debug", "Foo")))
47-
XCTAssert(!isDirectory(packageRoot.appending(component: ".build")))
47+
// We don't delete the build folder in new resolver.
48+
// FIXME: Eliminate this once we switch to new resolver.
49+
if !SwiftPMProduct.enableNewResolver {
50+
XCTAssert(!isDirectory(packageRoot.appending(component: ".build")))
51+
}
4852

4953
// Clean again to ensure we get no error.
5054
_ = try execute(["--clean"], chdir: packageRoot)
@@ -59,17 +63,28 @@ final class BuildToolTests: XCTestCase {
5963
XCTAssertBuilds(packageRoot)
6064
XCTAssertFileExists(packageRoot.appending(components: ".build", "debug", "Bar"))
6165
XCTAssert(isDirectory(packageRoot.appending(component: ".build")))
62-
XCTAssert(isDirectory(packageRoot.appending(component: "Packages")))
66+
// FIXME: Eliminate this.
67+
if !SwiftPMProduct.enableNewResolver {
68+
XCTAssert(isDirectory(packageRoot.appending(component: "Packages")))
69+
}
6370

6471
// Clean, and check for removal of the build directory but not Packages.
6572
_ = try execute(["--clean"], chdir: packageRoot)
66-
XCTAssert(!isDirectory(packageRoot.appending(component: ".build")))
67-
XCTAssert(isDirectory(packageRoot.appending(component: "Packages")))
73+
XCTAssert(!exists(packageRoot.appending(components: ".build", "debug", "Bar")))
74+
// We don't delete the build folder in new resolver.
75+
// FIXME: Eliminate this once we switch to new resolver.
76+
if !SwiftPMProduct.enableNewResolver {
77+
XCTAssert(!isDirectory(packageRoot.appending(component: ".build")))
78+
XCTAssert(isDirectory(packageRoot.appending(component: "Packages")))
79+
}
6880

6981
// Fully clean, and check for removal of both.
7082
_ = try execute(["--clean=dist"], chdir: packageRoot)
7183
XCTAssert(!isDirectory(packageRoot.appending(component: ".build")))
72-
XCTAssert(!isDirectory(packageRoot.appending(component: "Packages")))
84+
// FIXME: Eliminate this.
85+
if !SwiftPMProduct.enableNewResolver {
86+
XCTAssert(!isDirectory(packageRoot.appending(component: "Packages")))
87+
}
7388
}
7489
}
7590

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import XCTest
1313
import TestSupport
1414
import Basic
1515
import Commands
16+
import SourceControl
1617

1718
final class PackageToolTests: XCTestCase {
1819
private func execute(_ args: [String], chdir: AbsolutePath? = nil) throws -> String {
@@ -30,27 +31,30 @@ final class PackageToolTests: XCTestCase {
3031
func testFetch() throws {
3132
fixture(name: "DependencyResolution/External/Simple") { prefix in
3233
let packageRoot = prefix.appending(component: "Bar")
33-
let packagesPath = packageRoot.appending(component: "Packages")
3434

3535
// Check that `fetch` works.
3636
_ = try execute(["fetch"], chdir: packageRoot)
37-
XCTAssertEqual(try localFileSystem.getDirectoryContents(packagesPath), ["Foo-1.2.3"])
37+
let path = try SwiftPMProduct.packagePath(for: "Foo", packageRoot: packageRoot)
38+
XCTAssertEqual(GitRepository(path: path).tags, ["1.2.3"])
3839
}
3940
}
4041

4142
func testUpdate() throws {
4243
fixture(name: "DependencyResolution/External/Simple") { prefix in
4344
let packageRoot = prefix.appending(component: "Bar")
44-
let packagesPath = packageRoot.appending(component: "Packages")
4545

4646
// Perform an initial fetch.
4747
_ = try execute(["fetch"], chdir: packageRoot)
48-
XCTAssertEqual(try localFileSystem.getDirectoryContents(packagesPath), ["Foo-1.2.3"])
48+
var path = try SwiftPMProduct.packagePath(for: "Foo", packageRoot: packageRoot)
49+
XCTAssertEqual(GitRepository(path: path).tags, ["1.2.3"])
4950

5051
// Retag the dependency, and update.
5152
try tagGitRepo(prefix.appending(component: "Foo"), tag: "1.2.4")
5253
_ = try execute(["update"], chdir: packageRoot)
53-
XCTAssertEqual(try localFileSystem.getDirectoryContents(packagesPath), ["Foo-1.2.4"])
54+
55+
// We shouldn't assume package path will be same after an update so ask again for it.
56+
path = try SwiftPMProduct.packagePath(for: "Foo", packageRoot: packageRoot)
57+
XCTAssertEqual(GitRepository(path: path).tags, ["1.2.3", "1.2.4"])
5458
}
5559
}
5660

Tests/FunctionalTests/ClangModuleTests.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import XCTest
1313
import TestSupport
1414
import Basic
1515
import PackageModel
16+
import SourceControl
1617
import Utility
1718

1819
extension String {
@@ -54,11 +55,13 @@ class ClangModulesTestCase: XCTestCase {
5455

5556
func testExternalSimpleCDep() {
5657
fixture(name: "DependencyResolution/External/SimpleCDep") { prefix in
57-
XCTAssertBuilds(prefix.appending(component: "Bar"))
58+
let packageRoot = prefix.appending(component: "Bar")
59+
XCTAssertBuilds(packageRoot)
5860
let debugPath = prefix.appending(components: "Bar", ".build", "debug")
5961
XCTAssertFileExists(debugPath.appending(component: "Bar"))
6062
XCTAssertFileExists(debugPath.appending(component: "Foo".soname))
61-
XCTAssertDirectoryExists(prefix.appending(components: "Bar", "Packages", "Foo-1.2.3"))
63+
let path = try SwiftPMProduct.packagePath(for: "Foo", packageRoot: packageRoot)
64+
XCTAssertEqual(GitRepository(path: path).tags, ["1.2.3"])
6265
}
6366
}
6467

@@ -74,10 +77,12 @@ class ClangModulesTestCase: XCTestCase {
7477

7578
func testCUsingCDep() {
7679
fixture(name: "DependencyResolution/External/CUsingCDep") { prefix in
77-
XCTAssertBuilds(prefix.appending(component: "Bar"))
80+
let packageRoot = prefix.appending(component: "Bar")
81+
XCTAssertBuilds(packageRoot)
7882
let debugPath = prefix.appending(components: "Bar", ".build", "debug")
7983
XCTAssertFileExists(debugPath.appending(component: "Foo".soname))
80-
XCTAssertDirectoryExists(prefix.appending(components: "Bar", "Packages", "Foo-1.2.3"))
84+
let path = try SwiftPMProduct.packagePath(for: "Foo", packageRoot: packageRoot)
85+
XCTAssertEqual(GitRepository(path: path).tags, ["1.2.3"])
8186
}
8287
}
8388

@@ -94,10 +99,12 @@ class ClangModulesTestCase: XCTestCase {
9499
func testCUsingCDep2() {
95100
//The C dependency "Foo" has different layout
96101
fixture(name: "DependencyResolution/External/CUsingCDep2") { prefix in
97-
XCTAssertBuilds(prefix.appending(component: "Bar"))
102+
let packageRoot = prefix.appending(component: "Bar")
103+
XCTAssertBuilds(packageRoot)
98104
let debugPath = prefix.appending(components: "Bar", ".build", "debug")
99105
XCTAssertFileExists(debugPath.appending(component: "Foo".soname))
100-
XCTAssertDirectoryExists(prefix.appending(components: "Bar", "Packages", "Foo-1.2.3"))
106+
let path = try SwiftPMProduct.packagePath(for: "Foo", packageRoot: packageRoot)
107+
XCTAssertEqual(GitRepository(path: path).tags, ["1.2.3"])
101108
}
102109
}
103110

Tests/FunctionalTests/DependencyResolutionTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Basic
1515
import func POSIX.popen
1616

1717
import TestSupport
18+
import SourceControl
1819

1920
class DependencyResolutionTests: XCTestCase {
2021
func testInternalSimple() {
@@ -50,9 +51,11 @@ class DependencyResolutionTests: XCTestCase {
5051
try tagGitRepo(prefix.appending(components: "Foo"), tag: tag)
5152
}
5253

53-
XCTAssertBuilds(prefix.appending(component: "Bar"))
54+
let packageRoot = prefix.appending(component: "Bar")
55+
XCTAssertBuilds(packageRoot)
5456
XCTAssertFileExists(prefix.appending(components: "Bar", ".build", "debug", "Bar"))
55-
XCTAssertDirectoryExists(prefix.appending(components: "Bar", "Packages", "Foo-1.2.3"))
57+
let path = try SwiftPMProduct.packagePath(for: "Foo", packageRoot: packageRoot)
58+
XCTAssert(GitRepository(path: path).tags.contains("1.2.3"))
5659
}
5760
}
5861

Tests/FunctionalTests/MiscellaneousTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,17 @@ class MiscellaneousTestCase: XCTestCase {
264264
fixture(name: "DependencyResolution/External/Complex") { prefix in
265265
let execpath = [prefix.appending(components: "app", ".build", "debug", "Dealer").asString]
266266

267-
XCTAssertBuilds(prefix.appending(component: "app"))
267+
let packageRoot = prefix.appending(component: "app")
268+
XCTAssertBuilds(packageRoot)
268269
var output = try popen(execpath)
269270
XCTAssertEqual(output, "♣︎K\n♣︎Q\n♣︎J\n♣︎10\n♣︎9\n♣︎8\n♣︎7\n♣︎6\n♣︎5\n♣︎4\n")
270271

271272
// we need to sleep at least one second otherwise
272273
// llbuild does not realize the file has changed
273274
sleep(1)
274275

275-
try localFileSystem.writeFileContents(prefix.appending(components: "app", "Packages", "FisherYates-1.2.3", "src", "Fisher-Yates_Shuffle.swift"), bytes: "public extension Collection{ func shuffle() -> [Iterator.Element] {return []} }\n\npublic extension MutableCollection where Index == Int { mutating func shuffleInPlace() { for (i, _) in enumerated() { self[i] = self[0] } }}\n\npublic let shuffle = true")
276+
let path = try SwiftPMProduct.packagePath(for: "FisherYates", packageRoot: packageRoot)
277+
try localFileSystem.writeFileContents(path.appending(components: "src", "Fisher-Yates_Shuffle.swift"), bytes: "public extension Collection{ func shuffle() -> [Iterator.Element] {return []} }\n\npublic extension MutableCollection where Index == Int { mutating func shuffleInPlace() { for (i, _) in enumerated() { self[i] = self[0] } }}\n\npublic let shuffle = true")
276278

277279
XCTAssertBuilds(prefix.appending(component: "app"))
278280
output = try popen(execpath)
@@ -288,6 +290,7 @@ class MiscellaneousTestCase: XCTestCase {
288290
fixture(name: "Miscellaneous/DependencyEdges/External") { prefix in
289291
let execpath = [prefix.appending(components: "root", ".build", "debug", "dep2").asString]
290292

293+
let packageRoot = prefix.appending(component: "root")
291294
XCTAssertBuilds(prefix.appending(component: "root"))
292295
var output = try popen(execpath)
293296
XCTAssertEqual(output, "Hello\n")
@@ -296,7 +299,8 @@ class MiscellaneousTestCase: XCTestCase {
296299
// llbuild does not realize the file has changed
297300
sleep(1)
298301

299-
try localFileSystem.writeFileContents(prefix.appending(components: "root", "Packages", "dep1-1.2.3", "Foo.swift"), bytes: "public let foo = \"Goodbye\"")
302+
let path = try SwiftPMProduct.packagePath(for: "dep1", packageRoot: packageRoot)
303+
try localFileSystem.writeFileContents(path.appending(components: "Foo.swift"), bytes: "public let foo = \"Goodbye\"")
300304

301305
XCTAssertBuilds(prefix.appending(component: "root"))
302306
output = try popen(execpath)

Tests/FunctionalTests/ValidLayoutTests.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import XCTest
1313
import TestSupport
1414
import Basic
1515
import Utility
16+
import SourceControl
1617

1718
import func POSIX.rename
1819
import func POSIX.popen
@@ -73,10 +74,19 @@ class ValidLayoutsTests: XCTestCase {
7374
#endif
7475

7576
fixture(name: "DependencyResolution/External/Complex", tags: tags) { prefix in
76-
XCTAssertBuilds(prefix.appending(component: "app"), configurations: [.Debug])
77-
XCTAssertDirectoryExists(prefix.appending(RelativePath("app/Packages/DeckOfPlayingCards-1.2.3-beta5")))
78-
XCTAssertDirectoryExists(prefix.appending(RelativePath("app/Packages/FisherYates-1.3.4-alpha.beta.gamma1")))
79-
XCTAssertDirectoryExists(prefix.appending(RelativePath("app/Packages/PlayingCard-1.2.3+24")))
77+
let packageRoot = prefix.appending(component: "app")
78+
XCTAssertBuilds(packageRoot, configurations: [.Debug])
79+
80+
// FIXME: Elminate this.
81+
let deckOfPlayingCards = SwiftPMProduct.enableNewResolver ? "deck-of-playing-cards" : "DeckOfPlayingCards"
82+
var path = try SwiftPMProduct.packagePath(for: deckOfPlayingCards, packageRoot: packageRoot)
83+
XCTAssertEqual(GitRepository(path: path).tags, ["1.2.3-beta5"])
84+
85+
path = try SwiftPMProduct.packagePath(for: "FisherYates", packageRoot: packageRoot)
86+
XCTAssertEqual(GitRepository(path: path).tags, ["1.3.4-alpha.beta.gamma1"])
87+
88+
path = try SwiftPMProduct.packagePath(for: "PlayingCard", packageRoot: packageRoot)
89+
XCTAssertEqual(GitRepository(path: path).tags, ["1.2.3+24"])
8090
}
8191
}
8292

0 commit comments

Comments
 (0)