From 65e2c637decf391479847ea0ba5f575dc1766ba9 Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Fri, 13 Jan 2017 15:44:23 -0800 Subject: [PATCH] better getCommit() for tests that doesn't require access to github API or network Signed-off-by: Jess Frazelle --- ensure_test.go | 3 +-- hoard_test.go | 11 +++++++++++ init_test.go | 28 +--------------------------- remove_test.go | 3 +-- 4 files changed, 14 insertions(+), 31 deletions(-) diff --git a/ensure_test.go b/ensure_test.go index d7372ad1dc..19c1a42540 100644 --- a/ensure_test.go +++ b/ensure_test.go @@ -52,8 +52,7 @@ func main() { t.Fatalf("expected %s, got %s", expectedManifest, manifest) } - sysCommit, err := getRepoLatestCommit("golang/sys") - tg.must(err) + sysCommit := tg.getCommit("go.googlesource.com/sys") expectedLock := `{ "memo": "595716d270828e763c811ef79c9c41f85b1d1bfbdfe85280036405c03772206c", "projects": [ diff --git a/hoard_test.go b/hoard_test.go index 2d537c62a6..dd804e4af3 100644 --- a/hoard_test.go +++ b/hoard_test.go @@ -502,3 +502,14 @@ func (tg *testgoData) readLock() string { tg.must(err) return string(f) } + +func (tg *testgoData) getCommit(repo string) string { + repoPath := tg.path("pkg/hoard/sources/https---" + strings.Replace(repo, "/", "-", -1)) + cmd := exec.Command("git", "rev-parse", "HEAD") + cmd.Dir = repoPath + out, err := cmd.CombinedOutput() + if err != nil { + tg.t.Fatalf("git commit failed: out -> %s err -> %v", string(out), err) + } + return strings.TrimSpace(string(out)) +} diff --git a/init_test.go b/init_test.go index 174a97bd13..545ddfcea2 100644 --- a/init_test.go +++ b/init_test.go @@ -1,10 +1,6 @@ package main import ( - "encoding/json" - "errors" - "fmt" - "net/http" "os" "path/filepath" "regexp" @@ -169,8 +165,7 @@ const Qux = "yo yo!" t.Fatalf("expected %s, got %s", expectedManifest, manifest) } - sysCommit, err := getRepoLatestCommit("golang/sys") - tg.must(err) + sysCommit := tg.getCommit("go.googlesource.com/sys") expectedLock := `{ "projects": [ { @@ -210,24 +205,3 @@ var memoRE = regexp.MustCompile(`\s+"memo": "[a-z0-9]+",`) func wipeMemo(s string) string { return memoRE.ReplaceAllString(s, "") } - -type commit struct { - Sha string `json:"sha"` -} - -func getRepoLatestCommit(repo string) (string, error) { - resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/commits?per_page=1", repo)) - if err != nil { - return "", err - } - - var commits []commit - if err := json.NewDecoder(resp.Body).Decode(&commits); err != nil { - return "", err - } - - if len(commits) < 1 { - return "", errors.New("got no commits") - } - return commits[0].Sha, nil -} diff --git a/remove_test.go b/remove_test.go index 72142eb426..dba16812fa 100644 --- a/remove_test.go +++ b/remove_test.go @@ -121,8 +121,7 @@ func main() { t.Fatalf("expected %s, got %s", expectedManifest, manifest) } - sysCommit, err := getRepoLatestCommit("golang/sys") - tg.must(err) + sysCommit := tg.getCommit("go.googlesource.com/sys") expectedLock := `{ "projects": [ {