Skip to content

Commit d34d7f1

Browse files
committed
[Get] Update getRepositoryVersion for SE-0135.
- This also adds an end-to-end function test of the behavior.
1 parent f48e1cf commit d34d7f1

File tree

6 files changed

+121
-6
lines changed

6 files changed

+121
-6
lines changed

Sources/Get/RawClone.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ class RawClone: Fetchable {
3232
if branch.hasPrefix("v") {
3333
branch = String(branch.characters.dropFirst())
3434
}
35+
if branch.contains("@") {
36+
branch = branch.components(separatedBy: "@").first!
37+
}
3538
return Version(branch)
3639
}
3740

Sources/TestSupport/misc.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ public func tagGitRepo(_ dir: AbsolutePath, tag: String) throws {
109109
try systemQuietly([Git.tool, "-C", dir.asString, "tag", tag])
110110
}
111111

112+
public func removeTagGitRepo(_ dir: AbsolutePath, tag: String) throws {
113+
try systemQuietly([Git.tool, "-C", dir.asString, "tag", "-d", tag])
114+
}
115+
116+
public func addGitRepo(_ dir: AbsolutePath, file path: RelativePath) throws {
117+
try systemQuietly([Git.tool, "-C", dir.asString, "add", path.asString])
118+
}
119+
120+
public func commitGitRepo(_ dir: AbsolutePath, message: String = "Test commit") throws {
121+
try systemQuietly([Git.tool, "-C", dir.asString, "commit", "-m", message])
122+
}
123+
112124
public enum Configuration {
113125
case Debug
114126
case Release
@@ -158,3 +170,16 @@ public func systemQuietly(_ args: [String]) throws {
158170
public func systemQuietly(_ args: String...) throws {
159171
try systemQuietly(args)
160172
}
173+
174+
public extension FileSystem {
175+
/// Write to a file from a stream producer.
176+
//
177+
// FIXME: This is copy-paste from Commands/init.swift, maybe it is reasonable to lift it to Basic?
178+
mutating func writeFileContents(_ path: AbsolutePath, body: (OutputByteStream) -> ()) throws {
179+
let contents = BufferedOutputByteStream()
180+
body(contents)
181+
try createDirectory(path.parentDirectory, recursive: true)
182+
try writeFileContents(path, bytes: contents.bytes)
183+
}
184+
}
185+

Tests/FunctionalTests/DependencyResolutionTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
*/
1010

1111
import XCTest
12-
import TestSupport
12+
1313
import Basic
14+
1415
import func POSIX.popen
1516

16-
class DependencyResolutionTestCase: XCTestCase {
17+
import TestSupport
18+
19+
class DependencyResolutionTests: XCTestCase {
1720
func testInternalSimple() {
1821
fixture(name: "DependencyResolution/Internal/Simple") { prefix in
1922
XCTAssertBuilds(prefix)

Tests/FunctionalTests/ValidLayoutTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Utility
1717
import func POSIX.rename
1818
import func POSIX.popen
1919

20-
class ValidLayoutsTestCase: XCTestCase {
20+
class ValidLayoutsTests: XCTestCase {
2121

2222
func testSingleModuleLibrary() {
2323
runLayoutFixture(name: "SingleModule/Library") { prefix in
@@ -113,7 +113,7 @@ class ValidLayoutsTestCase: XCTestCase {
113113

114114
// MARK: Utility
115115

116-
extension ValidLayoutsTestCase {
116+
extension ValidLayoutsTests {
117117
func runLayoutFixture(name: String, line: UInt = #line, body: (AbsolutePath) throws -> Void) {
118118
let name = "ValidLayouts/\(name)"
119119

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import XCTest
12+
13+
import Basic
14+
import Utility
15+
16+
import TestSupport
17+
18+
class VersionSpecificTests: XCTestCase {
19+
/// Functional tests of end-to-end support for version specific dependency resolution.
20+
func testEndToEndResolution() throws {
21+
mktmpdir { path in
22+
var fs = localFileSystem
23+
24+
// Create a repo for the dependency to test against.
25+
let depPath = path.appending(component: "Dep")
26+
try fs.createDirectory(depPath)
27+
initGitRepo(depPath)
28+
29+
// Create the initial version (works, but empty).
30+
try fs.writeFileContents(depPath.appending(component: "Package.swift")) {
31+
$0 <<< "import PackageDescription\n"
32+
$0 <<< "let package = Package(name: \"Dep\")\n"
33+
}
34+
try addGitRepo(depPath, file: RelativePath("Package.swift"))
35+
try commitGitRepo(depPath, message: "Initial v1.0.0")
36+
try tagGitRepo(depPath, tag: "1.0.0")
37+
38+
// Create the version to test against.
39+
try fs.writeFileContents(depPath.appending(component: "Package.swift")) {
40+
$0 <<< "NOT_A_VALID_PACKAGE"
41+
}
42+
try fs.writeFileContents(depPath.appending(component: "foo.swift")) {
43+
$0 <<< "public func foo() { print(\"foo\\n\") }\n"
44+
}
45+
try addGitRepo(depPath, file: RelativePath("Package.swift"))
46+
try addGitRepo(depPath, file: RelativePath("foo.swift"))
47+
try commitGitRepo(depPath, message: "Bogus v1.1.0")
48+
try tagGitRepo(depPath, tag: "1.1.0")
49+
50+
// Create the primary repository.
51+
let primaryPath = path.appending(component: "Primary")
52+
try fs.writeFileContents(primaryPath.appending(component: "Package.swift")) {
53+
$0 <<< "import PackageDescription\n"
54+
$0 <<< "let package = Package(name: \"Primary\", dependencies: [.Package(url: \"../Dep\", majorVersion: 1)])\n"
55+
}
56+
// This build should fail, because of the invalid package.
57+
XCTAssertBuildFails(primaryPath)
58+
59+
// Create a file which requires a version 1.1.0 resolution.
60+
try fs.writeFileContents(primaryPath.appending(component: "main.swift")) {
61+
$0 <<< "import Dep\n"
62+
$0 <<< "Dep.foo()\n"
63+
}
64+
65+
// Create a version-specific tag, which should work.
66+
try fs.writeFileContents(depPath.appending(component: "Package.swift")) {
67+
$0 <<< "import PackageDescription\n"
68+
$0 <<< "let package = Package(name: \"Dep\")\n"
69+
}
70+
try addGitRepo(depPath, file: RelativePath("Package.swift"))
71+
try commitGitRepo(depPath, message: "OK v1.1.0")
72+
try tagGitRepo(depPath, tag: "1.1.0@swift-\(Versioning.currentVersion.major)")
73+
74+
// The build should work now.
75+
try removeFileTree(primaryPath.appending(component: "Packages"))
76+
XCTAssertBuilds(primaryPath)
77+
}
78+
}
79+
80+
static var allTests = [
81+
("testEndToEndResolution", testEndToEndResolution),
82+
]
83+
}

Tests/FunctionalTests/XCTestManifests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import XCTest
1414
public func allTests() -> [XCTestCaseEntry] {
1515
return [
1616
testCase(ClangModulesTestCase.allTests),
17-
testCase(DependencyResolutionTestCase.allTests),
17+
testCase(DependencyResolutionTests.allTests),
1818
testCase(MiscellaneousTestCase.allTests),
1919
testCase(ModuleMapsTestCase.allTests),
2020
testCase(SwiftPMXCTestHelperTests.allTests),
21-
testCase(ValidLayoutsTestCase.allTests),
21+
testCase(ValidLayoutsTests.allTests),
22+
testCase(VersionSpecificTests.allTests),
2223
]
2324
}
2425
#endif

0 commit comments

Comments
 (0)