From a6f359d2243a87b080413440aa54c8ee29c30aa4 Mon Sep 17 00:00:00 2001 From: Mark Ryan Date: Wed, 15 Jun 2016 16:04:32 +0100 Subject: [PATCH 1/2] ciao-vendor: Add test commmand The test command can be used to run the unit tests of a given version of a given package. This is to simplify the process of updating a vendored dependency. For example go run ciao-vendor/ciao-vendor.go test gopkg.in/yaml.v2 b667914 -v Fixes #202 Signed-off-by: Mark Ryan --- ciao-vendor/ciao-vendor.go | 70 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/ciao-vendor/ciao-vendor.go b/ciao-vendor/ciao-vendor.go index 657e74caf..01a65582c 100644 --- a/ciao-vendor/ciao-vendor.go +++ b/ciao-vendor/ciao-vendor.go @@ -313,6 +313,28 @@ func updateNonVendoredDeps(deps piList, projectRoot string) error { return nil } +func getCurrentBranch(repo string) (string, error) { + cmd := exec.Command("git", "symbolic-ref", "HEAD") + cmd.Dir = repo + output, err := cmd.Output() + if err != nil { + return "", err + } + scanner := bufio.NewScanner(bytes.NewBuffer(output)) + if !scanner.Scan() { + return "", fmt.Errorf("Unable to determine current branch of %s", + repo) + } + branch := strings.TrimSpace(scanner.Text()) + const prefix = "refs/heads/" + if !strings.HasPrefix(branch, prefix) { + return "", fmt.Errorf("Unable to determine current branch of %s", + repo) + } + + return branch[len(prefix):], nil +} + func checkoutVersion(sourceRoot string) { for k, v := range repos { cmd := exec.Command("git", "checkout", v.version) @@ -885,6 +907,44 @@ func updates(sourceRoot, projectRoot string) error { return nil } +func test(sourceRoot, projectRoot, pkg, version string, goTestFlags []string) error { + fmt.Printf("Go getting %s\n", pkg) + cmd := exec.Command("go", "get", "-t", "-u", pkg) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + return fmt.Errorf("Unable to go get %s", pkg) + } + + branch, err := getCurrentBranch(path.Join(sourceRoot, pkg)) + if err != nil { + return fmt.Errorf("Unable to determine current branch of %s: %v", pkg, err) + } + cmd = exec.Command("git", "checkout", version) + cmd.Dir = path.Join(sourceRoot, pkg) + err = cmd.Run() + if err != nil { + return fmt.Errorf("Unable to checkout version %s of %s: %v", + version, pkg, err) + } + + args := []string{"test"} + args = append(args, goTestFlags...) + args = append(args, pkg) + cmd = exec.Command("go", args...) + cmd.Dir = path.Join(sourceRoot, pkg) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err = cmd.Run() + + cmd = exec.Command("git", "checkout", branch) + cmd.Dir = path.Join(sourceRoot, pkg) + _ = cmd.Run() + + return err +} + func runCommand(cwd, sourceRoot string, args []string) error { var err error @@ -910,6 +970,8 @@ func runCommand(cwd, sourceRoot string, args []string) error { err = uses(fs.Args()[0], projectRoot, direct) case "updates": err = updates(sourceRoot, projectRoot) + case "test": + err = test(sourceRoot, projectRoot, args[2], args[3], args[4:]) } return err @@ -918,8 +980,12 @@ func runCommand(cwd, sourceRoot string, args []string) error { func main() { if !((len(os.Args) == 2 && (os.Args[1] == "vendor" || os.Args[1] == "check" || os.Args[1] == "deps" || - os.Args[1] == "packages" || os.Args[1] == "updates")) || (len(os.Args) >= 3 && os.Args[1] == "uses")) { - fmt.Fprintln(os.Stderr, "Usage: ciao-vendor vendor|check|deps|packages") + os.Args[1] == "packages" || os.Args[1] == "updates")) || + (len(os.Args) >= 3 && (os.Args[1] == "uses")) || + (len(os.Args) >= 4 && (os.Args[1] == "test"))) { + fmt.Fprintln(os.Stderr, "Usage: ciao-vendor vendor|check|deps|packages|updates") + fmt.Fprintln(os.Stderr, "Usage: ciao-vendor uses [-d] package") + fmt.Fprintln(os.Stderr, "Usage: ciao-vendor test package version [go-test flags]") os.Exit(1) } From fd638101eb79084890a29b8f0b698f3af0dc3d30 Mon Sep 17 00:00:00 2001 From: Mark Ryan Date: Wed, 15 Jun 2016 16:41:10 +0100 Subject: [PATCH 2/2] ciao-vendor: Add -s option to test -s can be used to run test cases as a super user. This is needed for some tests, e.g., github.com/vishvananda/netns. Signed-off-by: Mark Ryan --- ciao-vendor/ciao-vendor.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/ciao-vendor/ciao-vendor.go b/ciao-vendor/ciao-vendor.go index 01a65582c..bc6c4d910 100644 --- a/ciao-vendor/ciao-vendor.go +++ b/ciao-vendor/ciao-vendor.go @@ -907,7 +907,7 @@ func updates(sourceRoot, projectRoot string) error { return nil } -func test(sourceRoot, projectRoot, pkg, version string, goTestFlags []string) error { +func test(sudo bool, sourceRoot, projectRoot, pkg, version string, goTestFlags []string) error { fmt.Printf("Go getting %s\n", pkg) cmd := exec.Command("go", "get", "-t", "-u", pkg) cmd.Stdout = os.Stdout @@ -929,13 +929,24 @@ func test(sourceRoot, projectRoot, pkg, version string, goTestFlags []string) er version, pkg, err) } - args := []string{"test"} + var args []string + var command string + if sudo { + command = "sudo" + args = []string{"-E", "go"} + } else { + command = "go" + } + args = append(args, "test") args = append(args, goTestFlags...) args = append(args, pkg) - cmd = exec.Command("go", args...) + cmd = exec.Command(command, args...) cmd.Dir = path.Join(sourceRoot, pkg) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + if sudo { + cmd.Stdin = os.Stdin + } err = cmd.Run() cmd = exec.Command("git", "checkout", branch) @@ -971,7 +982,16 @@ func runCommand(cwd, sourceRoot string, args []string) error { case "updates": err = updates(sourceRoot, projectRoot) case "test": - err = test(sourceRoot, projectRoot, args[2], args[3], args[4:]) + fs := flag.NewFlagSet("test", flag.ExitOnError) + sudo := false + fs.BoolVar(&sudo, "s", false, "run tests with sudo") + + if err := fs.Parse(args[2:]); err != nil { + return err + } + + args = fs.Args() + err = test(sudo, sourceRoot, projectRoot, args[0], args[1], args[2:]) } return err