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

Commit

Permalink
Added test for depfile dependant build (#622)
Browse files Browse the repository at this point in the history
* Added test for depfile dependant build

* fix error spotted in windows build
  • Loading branch information
davecheney authored Jun 26, 2016
1 parent 2303071 commit a6ff047
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cmd/gb/depfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ func fetchIfMissing(root, prefix, version string) {
fatalf("unable to construct gzip reader: %v", err)
}

parent, _ := filepath.Split(dest)
parent, pkg := filepath.Split(dest)
mkdirall(parent)
tmpdir := tempdir(parent)
defer os.RemoveAll(tmpdir)

tmpdir = filepath.Join(tmpdir, pkg)

if err := untar.Untar(tmpdir, gzr); err != nil {
fatalf("unable to untar: %v", err)
}
defer os.RemoveAll(tmpdir)

dents, err := ioutil.ReadDir(tmpdir)
if err != nil {
Expand Down
41 changes: 40 additions & 1 deletion cmd/gb/depfile_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main_test

import "testing"
import (
"path/filepath"
"runtime"
"testing"
)

func TestMissingDepfile(t *testing.T) {
gb := T{T: t}
Expand All @@ -21,3 +25,38 @@ func main() {
gb.runFail("build")
gb.grepStderr(`FATAL: command "build" failed:.+import "github.com/a/b": not found`, `import "github.com/a/b": not found`)
}

func TestDepfileFetchMissing(t *testing.T) {
if testing.Short() {
t.Skip("skipping test during -short")
}

gb := T{T: t}
defer gb.cleanup()

gb.tempDir("src/github.com/user/proj/a/")
gb.tempFile("src/github.com/user/proj/a/main.go", `package main
import "github.com/pkg/profile"
func main() {
defer profile.Start().Stop()
}
`)
gb.tempFile("depfile", `
github.com/pkg/profile version=1.1.0
`)

gb.cd(gb.tempdir)
gbhome := gb.tempDir(".gb")
gb.setenv("GB_HOME", gbhome)
gb.setenv("DEBUG", ".")
gb.run("build")
name := "a"
if runtime.GOOS == "windows" {
name += ".exe"
}
gb.wantExecutable(gb.path("bin", name), "expected $PROJECT/bin/"+name)
gb.grepStdout("^fetching github.com/pkg/profile", "fetching pkg/profile not found")
gb.mustExist(filepath.Join(gbhome, "cache", "8fd41ea4fa48cd8435005bad56faeefdc57a25d6", "src", "github.com", "pkg", "profile", "profile.go"))
}
8 changes: 8 additions & 0 deletions cmd/gb/internal/untar/untar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
// Untar extracts the contents of r to the destination dest.
// dest must not aleady exist.
func Untar(dest string, r io.Reader) error {
if exists(dest) {
return errors.Errorf("%q must not exist", dest)
}
parent, _ := filepath.Split(dest)
tmpdir, err := ioutil.TempDir(parent, ".untar")
if err != nil {
Expand Down Expand Up @@ -84,3 +87,8 @@ func writefile(path string, r io.Reader, mode os.FileMode) error {
}
return w.Close()
}

func exists(path string) bool {
_, err := os.Stat(path)
return err == nil || !os.IsNotExist(err)
}

0 comments on commit a6ff047

Please sign in to comment.