Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
better getCommit() for tests that doesn't require access to github AP…
Browse files Browse the repository at this point in the history
…I or network

Signed-off-by: Jess Frazelle <[email protected]>
  • Loading branch information
jessfraz committed Jan 13, 2017
1 parent 3d1d5d1 commit 65e2c63
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 31 deletions.
3 changes: 1 addition & 2 deletions ensure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
11 changes: 11 additions & 0 deletions hoard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
28 changes: 1 addition & 27 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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": [
{
Expand Down Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down

0 comments on commit 65e2c63

Please sign in to comment.