mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 14:56:29 +00:00
build.go: allow running tests in temporary GOPATH
This commit is contained in:
parent
a37431e963
commit
59751645be
62
build.go
62
build.go
@ -18,6 +18,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
verbose bool
|
verbose bool
|
||||||
keepGopath bool
|
keepGopath bool
|
||||||
|
runTests bool
|
||||||
)
|
)
|
||||||
|
|
||||||
const timeFormat = "2006-01-02 15:04:05"
|
const timeFormat = "2006-01-02 15:04:05"
|
||||||
@ -32,6 +33,21 @@ func specialDir(name string) bool {
|
|||||||
return base[0] == '_' || base[0] == '.'
|
return base[0] == '_' || base[0] == '.'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// excludePath returns true if the file should not be copied to the new GOPATH.
|
||||||
|
func excludePath(name string) bool {
|
||||||
|
ext := path.Ext(name)
|
||||||
|
if ext == ".go" || ext == ".s" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
parentDir := filepath.Base(filepath.Dir(name))
|
||||||
|
if parentDir == "testdata" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// updateGopath builds a valid GOPATH at dst, with all Go files in src/ copied
|
// updateGopath builds a valid GOPATH at dst, with all Go files in src/ copied
|
||||||
// to dst/prefix/, so calling
|
// to dst/prefix/, so calling
|
||||||
//
|
//
|
||||||
@ -60,8 +76,7 @@ func updateGopath(dst, src, prefix string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ext := path.Ext(name)
|
if excludePath(name) {
|
||||||
if ext != ".go" && ext != ".s" {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,6 +149,9 @@ func showUsage(output io.Writer) {
|
|||||||
fmt.Fprintf(output, "\n")
|
fmt.Fprintf(output, "\n")
|
||||||
fmt.Fprintf(output, "OPTIONS:\n")
|
fmt.Fprintf(output, "OPTIONS:\n")
|
||||||
fmt.Fprintf(output, " -v --verbose output more messages\n")
|
fmt.Fprintf(output, " -v --verbose output more messages\n")
|
||||||
|
fmt.Fprintf(output, " -t --tags specify additional build tags\n")
|
||||||
|
fmt.Fprintf(output, " -k --keep-gopath do not remove the GOPATH after build\n")
|
||||||
|
fmt.Fprintf(output, " -T --test run tests\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func verbosePrintf(message string, args ...interface{}) {
|
func verbosePrintf(message string, args ...interface{}) {
|
||||||
@ -170,6 +188,18 @@ func build(gopath string, args ...string) error {
|
|||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test runs "go test args..." with GOPATH set to gopath.
|
||||||
|
func test(gopath string, args ...string) error {
|
||||||
|
args = append([]string{"test"}, args...)
|
||||||
|
cmd := exec.Command("go", args...)
|
||||||
|
cmd.Env = append(cleanEnv(), "GOPATH="+gopath)
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
verbosePrintf("go %s\n", args)
|
||||||
|
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
// getVersion returns a version string, either from the file VERSION in the
|
// getVersion returns a version string, either from the file VERSION in the
|
||||||
// current directory or from git.
|
// current directory or from git.
|
||||||
func getVersion() string {
|
func getVersion() string {
|
||||||
@ -218,6 +248,8 @@ func main() {
|
|||||||
case "-t", "-tags", "--tags":
|
case "-t", "-tags", "--tags":
|
||||||
skipNext = true
|
skipNext = true
|
||||||
buildTags = strings.Split(params[i+1], " ")
|
buildTags = strings.Split(params[i+1], " ")
|
||||||
|
case "-T", "--test":
|
||||||
|
runTests = true
|
||||||
case "-h":
|
case "-h":
|
||||||
showUsage(os.Stdout)
|
showUsage(os.Stdout)
|
||||||
default:
|
default:
|
||||||
@ -258,6 +290,17 @@ func main() {
|
|||||||
die("copying files from %v to %v failed: %v\n", root, gopath, err)
|
die("copying files from %v to %v failed: %v\n", root, gopath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if !keepGopath {
|
||||||
|
verbosePrintf("remove %v\n", gopath)
|
||||||
|
if err = os.RemoveAll(gopath); err != nil {
|
||||||
|
die("remove GOPATH at %s failed: %v\n", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Printf("leaving temporary GOPATH at %v\n", gopath)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
output := "restic"
|
output := "restic"
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
output = "restic.exe"
|
output = "restic.exe"
|
||||||
@ -277,14 +320,15 @@ func main() {
|
|||||||
err = build(gopath, args...)
|
err = build(gopath, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "build failed: %v\n", err)
|
fmt.Fprintf(os.Stderr, "build failed: %v\n", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !keepGopath {
|
if runTests {
|
||||||
verbosePrintf("remove %v\n", gopath)
|
verbosePrintf("running tests\n")
|
||||||
if err = os.RemoveAll(gopath); err != nil {
|
|
||||||
die("remove GOPATH at %s failed: %v\n", err)
|
err = test(gopath, "github.com/restic/restic/...")
|
||||||
}
|
if err != nil {
|
||||||
} else {
|
fmt.Fprintf(os.Stderr, "build failed: %v\n", err)
|
||||||
fmt.Printf("leaving temporary GOPATH at %v\n", gopath)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user