Skip to content

Commit

Permalink
Fixing Mercurial tests
Browse files Browse the repository at this point in the history
Bitbucket stopped serving Hg. This update provides testing and
detection for mercurial. It also removes mercurial testing
for Bitbucket that's no longer needed as the options have been
reduced to 1.

Signed-off-by: Matt Farina <[email protected]>
  • Loading branch information
mattfarina committed Mar 21, 2022
1 parent bb36dcd commit 922a512
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 58 deletions.
36 changes: 18 additions & 18 deletions hg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestHg(t *testing.T) {
}
}()

repo, err := NewHgRepo("https://bitbucket.org/mattfarina/testhgrepo", tempDir+"/testhgrepo")
repo, err := NewHgRepo("http://hg.code.sf.net/p/vcstesthgrepo/code", tempDir+"/testhgrepo")
if err != nil {
t.Error(err)
}
Expand All @@ -38,7 +38,7 @@ func TestHg(t *testing.T) {
}

// Check the basic getters.
if repo.Remote() != "https://bitbucket.org/mattfarina/testhgrepo" {
if repo.Remote() != "http://hg.code.sf.net/p/vcstesthgrepo/code" {
t.Error("Remote not set properly")
}
if repo.LocalPath() != tempDir+"/testhgrepo" {
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestHg(t *testing.T) {

// Test NewRepo on existing checkout. This should simply provide a working
// instance without error based on looking at the local directory.
nrepo, nrerr := NewRepo("https://bitbucket.org/mattfarina/testhgrepo", tempDir+"/testhgrepo")
nrepo, nrerr := NewRepo("http://hg.code.sf.net/p/vcstesthgrepo/code", tempDir+"/testhgrepo")
if nrerr != nil {
t.Error(nrerr)
}
Expand All @@ -87,14 +87,14 @@ func TestHg(t *testing.T) {
}

// Set the version using the short hash.
err = repo.UpdateVersion("a5494ba2177f")
err = repo.UpdateVersion("059e82823f3e")
if err != nil {
t.Errorf("Unable to update Hg repo version. Err was %s", err)
}

// Use Version to verify we are on the right version.
v, err = repo.Version()
if v != "a5494ba2177ff9ef26feb3c155dfecc350b1a8ef" {
if v != "059e82823f3ec23fceb532621ee7c15d5624f35f" {
t.Errorf("Error checking checked out Hg version: %s", v)
}
if err != nil {
Expand All @@ -105,7 +105,7 @@ func TestHg(t *testing.T) {
if err != nil {
t.Errorf("Error trying Hg Current for ref: %s", err)
}
if v != "a5494ba2177ff9ef26feb3c155dfecc350b1a8ef" {
if v != "059e82823f3ec23fceb532621ee7c15d5624f35f" {
t.Errorf("Current failed to detect Hg on ref of branch. Got version: %s", v)
}

Expand All @@ -114,7 +114,7 @@ func TestHg(t *testing.T) {
if err != nil {
t.Error(err)
}
if d.Format(longForm) != "2015-07-30 16:14:08 -0400" {
if d.Format(longForm) != "2022-03-21 15:53:47 -0400" {
t.Error("Error checking checked out Hg commit date. Got wrong date:", d)
}

Expand All @@ -125,7 +125,7 @@ func TestHg(t *testing.T) {
}

v, err = repo.Version()
if v != "9c6ccbca73e8a1351c834f33f57f1f7a0329ad35" {
if v != "c068171728d3cc343fef21ffdff43cdb14e3c716" {
t.Errorf("Error checking checked out Hg version: %s", v)
}
if err != nil {
Expand All @@ -140,15 +140,15 @@ func TestHg(t *testing.T) {
t.Error("Hg tags is not reporting the correct version")
}

tags, err = repo.TagsFromCommit("a5494ba2177f")
tags, err = repo.TagsFromCommit("059e82823f3e")
if err != nil {
t.Error(err)
}
if len(tags) != 0 {
t.Error("Hg is incorrectly returning tags for a commit")
}

tags, err = repo.TagsFromCommit("d680e82228d2")
tags, err = repo.TagsFromCommit("96379533a643")
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -181,21 +181,21 @@ func TestHg(t *testing.T) {
t.Error("Hg incorrectly reporting dirty")
}

ci, err := repo.CommitInfo("a5494ba2177f")
ci, err := repo.CommitInfo("72a363187366")
if err != nil {
t.Error(err)
}
if ci.Commit != "a5494ba2177ff9ef26feb3c155dfecc350b1a8ef" {
if ci.Commit != "72a3631873669f4bd4c41e4d9146104e1e55e767" {
t.Error("Hg.CommitInfo wrong commit id")
}
if ci.Author != "Matt Farina <[email protected]>" {
t.Error("Hg.CommitInfo wrong author")
}
if ci.Message != "A commit" {
if ci.Message != "Removing a" {
t.Error("Hg.CommitInfo wrong message")
}

ti := time.Unix(1438287248, 0)
ti := time.Unix(1647898961, 0)
if !ti.Equal(ci.Date) {
t.Error("Hg.CommitInfo wrong date")
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestHg(t *testing.T) {
t.Errorf("Unable to export Hg repo. Err was %s", err)
}

_, err = os.Stat(filepath.Join(exportDir, "Readme.md"))
_, err = os.Stat(filepath.Join(exportDir, "README.md"))
if err != nil {
t.Errorf("Error checking exported file in Hg: %s", err)
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestHgCheckLocal(t *testing.T) {

// Test NewRepo when there's no local. This should simply provide a working
// instance without error based on looking at the remote localtion.
_, nrerr := NewRepo("https://bitbucket.org/mattfarina/testhgrepo", tempDir+"/testhgrepo")
_, nrerr := NewRepo("http://hg.code.sf.net/p/vcstesthgrepo/code", tempDir+"/testhgrepo")
if nrerr != nil {
t.Error(nrerr)
}
Expand All @@ -277,7 +277,7 @@ func TestHgPing(t *testing.T) {
}
}()

repo, err := NewHgRepo("https://bitbucket.org/mattfarina/testhgrepo", tempDir)
repo, err := NewHgRepo("http://hg.code.sf.net/p/vcstesthgrepo/code", tempDir)
if err != nil {
t.Error(err)
}
Expand All @@ -287,7 +287,7 @@ func TestHgPing(t *testing.T) {
t.Error("Hg unable to ping working repo")
}

repo, err = NewHgRepo("https://bitbucket.org/mattfarina/ihopethisneverexistsbecauseitshouldnt", tempDir)
repo, err = NewHgRepo("http://bitbucket.org/mattfarina/ihopethisneverexistsbecauseitshouldnt", tempDir)
if err != nil {
t.Error(err)
}
Expand Down
52 changes: 16 additions & 36 deletions vcs_remote_lookup.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vcs

import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
Expand Down Expand Up @@ -33,7 +32,7 @@ var vcsList = []*vcsInfo{
{
host: "bitbucket.org",
pattern: `^(bitbucket\.org/(?P<name>[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+))(/[A-Za-z0-9_.\-]+)*$`,
addCheck: checkBitbucket,
vcs: Git,
},
{
host: "launchpad.net",
Expand All @@ -60,6 +59,21 @@ var vcsList = []*vcsInfo{
vcs: Git,
pattern: `^(git\.openstack\.org/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)$`,
},
{
host: "hg.code.sf.net",
pattern: `^(hg.code.sf.net/p/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)*$`,
vcs: Hg,
},
{
host: "git.code.sf.net",
pattern: `^(git.code.sf.net/p/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)*$`,
vcs: Git,
},
{
host: "svn.code.sf.net",
pattern: `^(svn.code.sf.net/p/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)*$`,
vcs: Svn,
},
// If none of the previous detect the type they will fall to this looking for the type in a generic sense
// by the extension to the path.
{
Expand Down Expand Up @@ -224,40 +238,6 @@ func detectVcsFromURL(vcsURL string) (Type, error) {
return "", ErrCannotDetectVCS
}

// Figure out the type for Bitbucket by the passed in information
// or via the public API.
func checkBitbucket(i map[string]string, ul *url.URL) (Type, error) {

// Fast path for ssh urls where we may not even be able to
// anonymously get details from the API.
if ul.User != nil {
un := ul.User.Username()
if un == "git" {
return Git, nil
} else if un == "hg" {
return Hg, nil
}
}

// The part of the response we care about.
var response struct {
SCM Type `json:"scm"`
}

u := expand(i, "https://api.bitbucket.org/2.0/repositories/{name}?fields=scm")
data, err := get(u)
if err != nil {
return "", err
}

if err := json.Unmarshal(data, &response); err != nil {
return "", fmt.Errorf("Decoding error %s: %v", u, err)
}

return response.SCM, nil

}

// Expect a type key on i with the exact type detected from the regex.
func checkURL(i map[string]string, u *url.URL) (Type, error) {
return Type(i["type"]), nil
Expand Down
9 changes: 5 additions & 4 deletions vcs_remote_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ func TestVCSLookup(t *testing.T) {
}{
"https://github.com/masterminds": {work: false, t: Git},
"https://github.com/Masterminds/VCSTestRepo": {work: true, t: Git},
"https://bitbucket.org/mattfarina/testhgrepo": {work: true, t: Hg},
"https://bitbucket.org/mattfarina/repo-does-not-exist": {work: false, t: Hg},
"https://bitbucket.org/mattfarina/private-repo-for-vcs-testing": {work: false, t: Hg},
"https://bitbucket.org/mattfarina/atest": {work: true, t: Git},
"https://launchpad.net/govcstestbzrrepo/trunk": {work: true, t: Bzr},
"https://launchpad.net/~mattfarina/+junk/mygovcstestbzrrepo": {work: true, t: Bzr},
"https://launchpad.net/~mattfarina/+junk/mygovcstestbzrrepo/trunk": {work: true, t: Bzr},
Expand All @@ -37,14 +35,17 @@ func TestVCSLookup(t *testing.T) {
"https://git.openstack.org/foo/bar": {work: true, t: Git},
"[email protected]:Masterminds/vcs.git": {work: true, t: Git},
"[email protected]:foo.git": {work: true, t: Git},
"ssh://hg@bitbucket.org/mattfarina/testhgrepo": {work: true, t: Hg},
"ssh://bitbucket.org/mattfarina/testhgrepo": {work: true, t: Git},
"[email protected]:mattfarina/glide-bitbucket-example.git": {work: true, t: Git},
"git+ssh://example.com/foo/bar": {work: true, t: Git},
"git://example.com/foo/bar": {work: true, t: Git},
"bzr+ssh://example.com/foo/bar": {work: true, t: Bzr},
"svn+ssh://example.com/foo/bar": {work: true, t: Svn},
"[email protected]:foo/bar": {work: true, t: Git},
"[email protected]:foo/bar": {work: true, t: Hg},
"http://hg.code.sf.net/p/masterminds/test": {work: true, t: Hg},
"http://git.code.sf.net/p/masterminds/test": {work: true, t: Git},
"http://svn.code.sf.net/p/masterminds/test": {work: true, t: Svn},
}

for u, c := range urlList {
Expand Down

0 comments on commit 922a512

Please sign in to comment.