Skip to content

Commit ddfed10

Browse files
committed
Merge pull request #112 from kostiakoval/hasVersion
Check if git has a version tag
2 parents 2e841e1 + a5c5fde commit ddfed10

File tree

7 files changed

+110
-4
lines changed

7 files changed

+110
-4
lines changed

Sources/dep/Error.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public enum Error: ErrorType {
1818
case UpdateRequired(String)
1919

2020
case GitCloneFailure(String, String)
21+
case GitVersionTagRequired(String)
22+
2123
}
2224

2325

@@ -40,6 +42,8 @@ extension Error: CustomStringConvertible {
4042
return "The dependency graph could not be satisfied because an update to `\(package)' is required"
4143
case .GitCloneFailure(let url, let dstdir):
4244
return "Failed to clone \(url) to \(dstdir)"
45+
case .GitVersionTagRequired(let package):
46+
return "No version tag found in (\(package)) package. Add a version tag with \"git tag\" command. Example: \"git tag 0.1.0\""
4347
}
4448
}
4549
}

Sources/dep/Git.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class Git {
6060
}
6161
}
6262

63+
/// Check if repo contains a version tag
64+
var hasVersion: Bool {
65+
return !versions.isEmpty
66+
}
67+
6368
/**
6469
- Returns: true if the package versions in this repository
6570
are all prefixed with "v", otherwise false. If there are

Sources/dep/get.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ extension Sandbox: Fetcher {
102102
let dstdir = Path.join(prefix, Package.name(forURL: url))
103103
if let repo = Git.Repo(root: dstdir) where repo.origin == url {
104104
//TODO need to canonicalize the URL need URL struct
105-
return RawClone(path: dstdir)
105+
return try RawClone(path: dstdir)
106106
}
107107

108108
// fetch as well, clone does not fetch all tags, only tags on the master branch
109109
try Git.clone(url, to: dstdir).fetch()
110110

111-
return RawClone(path: dstdir)
111+
return try RawClone(path: dstdir)
112112
}
113113

114114
func finalize(fetchable: Fetchable) throws -> Package {
@@ -147,8 +147,11 @@ extension Sandbox: Fetcher {
147147
}
148148
private var _manifest: Manifest?
149149

150-
init(path: String) {
150+
init(path: String) throws {
151151
self.path = path
152+
if !repo.hasVersion {
153+
throw Error.GitVersionTagRequired(path)
154+
}
152155
}
153156

154157
var repo: Git.Repo {

Support/swiftpm.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
63FDF0DA1C29EF4C00B3904D /* TestDependencyResolution.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63FDF0D91C29EF4C00B3904D /* TestDependencyResolution.swift */; };
2626
63FDF0F01C29F34D00B3904D /* TestMiscellaneous.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63FDF0EF1C29F34D00B3904D /* TestMiscellaneous.swift */; };
2727
A1A815631C17A8F000BD6927 /* GetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1A815621C17A8E700BD6927 /* GetTests.swift */; };
28+
B59359861C429F5200AD4547 /* GitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59359851C429F5200AD4547 /* GitTests.swift */; };
2829
DA3C2B1C1C1B4D7600893759 /* XCTestCaseProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA3C2AE11C1B48C700893759 /* XCTestCaseProvider.swift */; };
2930
E136D9421BD5F3DD000DFCE6 /* TOMLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E136D9411BD5F35D000DFCE6 /* TOMLTests.swift */; };
3031
E1E286BA1BD72D700015F0C5 /* ManifestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E286B91BD72D700015F0C5 /* ManifestTests.swift */; };
@@ -245,6 +246,7 @@
245246
63FDF0D91C29EF4C00B3904D /* TestDependencyResolution.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestDependencyResolution.swift; path = ../Tests/Functional/TestDependencyResolution.swift; sourceTree = "<group>"; };
246247
63FDF0EF1C29F34D00B3904D /* TestMiscellaneous.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestMiscellaneous.swift; path = ../Tests/Functional/TestMiscellaneous.swift; sourceTree = "<group>"; };
247248
A1A815621C17A8E700BD6927 /* GetTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = GetTests.swift; path = ../Tests/dep/GetTests.swift; sourceTree = "<group>"; };
249+
B59359851C429F5200AD4547 /* GitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = GitTests.swift; path = ../Tests/dep/GitTests.swift; sourceTree = "<group>"; };
248250
DA3C2AE11C1B48C700893759 /* XCTestCaseProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XCTestCaseProvider.swift; sourceTree = "<group>"; };
249251
DA3C2B141C1B4D4A00893759 /* XCTestCaseProvider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = XCTestCaseProvider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
250252
E1066B6E1BC5931C00B892CE /* llbuild.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = llbuild.xcodeproj; path = ../../llbuild/llbuild.xcodeproj; sourceTree = "<group>"; };
@@ -367,6 +369,7 @@
367369
isa = PBXGroup;
368370
children = (
369371
A1A815621C17A8E700BD6927 /* GetTests.swift */,
372+
B59359851C429F5200AD4547 /* GitTests.swift */,
370373
63D2BC171BFD6287006E395C /* PackageTests.swift */,
371374
6305480D1BD708B30001290A /* DependencyGraphTests.swift */,
372375
635C147C1BCDA0A900D4C4A9 /* TargetTests.swift */,
@@ -789,6 +792,7 @@
789792
634D97011C2874670056FAAA /* Utilities.swift in Sources */,
790793
63353A5B1BB4C4BC00D6C4B0 /* VersionTests.swift in Sources */,
791794
63D2BC181BFD6287006E395C /* PackageTests.swift in Sources */,
795+
B59359861C429F5200AD4547 /* GitTests.swift in Sources */,
792796
635C147D1BCDA0A900D4C4A9 /* TargetTests.swift in Sources */,
793797
);
794798
runOnlyForDeploymentPostprocessing = 0;

Tests/dep/GetTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class GetTests: XCTestCase, XCTestCaseProvider {
3030
try system("git", "-C", path, "rm", "Package.swift")
3131
try system("git", "-C", path, "commit", "-mwip")
3232

33-
let rawClone = Sandbox.RawClone(path: path)
33+
let rawClone = try Sandbox.RawClone(path: path)
3434
XCTAssertEqual(rawClone.dependencies.count, 0)
3535
}
3636
}

Tests/dep/GitTests.swift

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 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+
import XCTest
11+
import XCTestCaseProvider
12+
13+
import struct sys.Path
14+
@testable import dep
15+
import func POSIX.popen
16+
17+
class GitTests: XCTestCase, XCTestCaseProvider {
18+
19+
var allTests : [(String, () -> Void)] {
20+
return [
21+
("testHasVersion", testHasVersion),
22+
("testHasNoVersion", testHasNoVersion),
23+
("testCloneShouldNotCrashWihoutTags", testCloneShouldNotCrashWihoutTags),
24+
("testCloneShouldCrashWihoutTags", testCloneShouldCrashWihoutTags),
25+
26+
]
27+
}
28+
29+
func testHasVersion() {
30+
mktmpdir { path in
31+
let gitRepo = makeGitRepo(path, tag: "0.1.0")!
32+
XCTAssertTrue(gitRepo.hasVersion)
33+
}
34+
}
35+
36+
func testHasNoVersion() {
37+
mktmpdir { path in
38+
let gitRepo = makeGitRepo(path, tag: nil)!
39+
XCTAssertFalse(gitRepo.hasVersion)
40+
}
41+
}
42+
43+
func testCloneShouldNotCrashWihoutTags() {
44+
tryCloningRepoWithTag("0.1.0", shouldCrash: false)
45+
}
46+
47+
func testCloneShouldCrashWihoutTags() {
48+
tryCloningRepoWithTag(nil, shouldCrash: true)
49+
}
50+
}
51+
52+
//MARK: - Helpers
53+
54+
private func makeGitRepo(dstdir: String, tag: String?, file: StaticString = __FILE__, line: UInt = __LINE__) -> Git.Repo? {
55+
do {
56+
let file = Path.join(dstdir, "file.swift")
57+
try popen(["touch", file])
58+
try popen(["git", "-C", dstdir, "init"])
59+
try popen(["git", "-C", dstdir, "config", "user.email", "[email protected]"])
60+
try popen(["git", "-C", dstdir, "config", "user.name", "Example Example"])
61+
try popen(["git", "-C", dstdir, "add", "."])
62+
try popen(["git", "-C", dstdir, "commit", "-m", "msg"])
63+
if let tag = tag {
64+
try popen(["git", "-C", dstdir, "tag", tag])
65+
}
66+
return Git.Repo(root: dstdir)
67+
}
68+
catch {
69+
XCTFail(safeStringify(error), file: file, line: line)
70+
}
71+
return nil
72+
}
73+
74+
private func tryCloningRepoWithTag(tag: String?, shouldCrash: Bool) {
75+
var done = !shouldCrash
76+
mktmpdir { path in
77+
makeGitRepo(path, tag: tag)!
78+
do {
79+
_ = try Sandbox.RawClone(path: path)
80+
} catch Error.GitVersionTagRequired {
81+
done = shouldCrash
82+
} catch {
83+
XCTFail()
84+
}
85+
XCTAssertTrue(done)
86+
}
87+
}

Tests/dep/main.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ XCTMain([
2222

2323
// GetTests.swift
2424
GetTests(),
25+
26+
// GitTests.swift
27+
GitTests(),
2528
])

0 commit comments

Comments
 (0)