|
1 | 1 | package codegen_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "go/build" |
4 | 6 | "os"
|
| 7 | + "path/filepath" |
| 8 | + "runtime" |
5 | 9 |
|
6 | 10 | "github.com/goadesign/goa/goagen/codegen"
|
7 | 11 |
|
@@ -32,31 +36,76 @@ var _ = Describe("Helpers", func() {
|
32 | 36 | })
|
33 | 37 |
|
34 | 38 | Describe("CommandLine", func() {
|
35 |
| - oldGOPATH, oldArgs := os.Getenv("GOPATH"), os.Args |
36 |
| - BeforeEach(func() { |
37 |
| - os.Setenv("GOPATH", "/xx") |
38 |
| - }) |
39 |
| - AfterEach(func() { |
40 |
| - os.Setenv("GOPATH", oldGOPATH) |
41 |
| - os.Args = oldArgs |
42 |
| - }) |
| 39 | + Context("with exported GOPATH", func() { |
| 40 | + oldGOPATH, oldArgs := build.Default.GOPATH, os.Args |
| 41 | + BeforeEach(func() { |
| 42 | + os.Setenv("GOPATH", "/xx") |
| 43 | + }) |
| 44 | + AfterEach(func() { |
| 45 | + os.Setenv("GOPATH", oldGOPATH) |
| 46 | + os.Args = oldArgs |
| 47 | + }) |
43 | 48 |
|
44 |
| - It("should not touch free arguments", func() { |
45 |
| - os.Args = []string{"foo", "/xx/bar/xx/42"} |
| 49 | + It("should not touch free arguments", func() { |
| 50 | + os.Args = []string{"foo", "/xx/bar/xx/42"} |
46 | 51 |
|
47 |
| - Expect(codegen.CommandLine()).To(Equal("$ foo /xx/bar/xx/42")) |
48 |
| - }) |
| 52 | + Expect(codegen.CommandLine()).To(Equal("$ foo /xx/bar/xx/42")) |
| 53 | + }) |
| 54 | + |
| 55 | + It("should replace GOPATH one match only in a long option", func() { |
| 56 | + os.Args = []string{"foo", "--opt=/xx/bar/xx/42"} |
| 57 | + |
| 58 | + Expect(codegen.CommandLine()).To(Equal("$ foo\n\t--opt=$(GOPATH)/bar/xx/42")) |
| 59 | + }) |
49 | 60 |
|
50 |
| - It("should replace GOPATH one match only in a long option", func() { |
51 |
| - os.Args = []string{"foo", "--opt=/xx/bar/xx/42"} |
| 61 | + It("should not replace GOPATH if a match is not at the beginning of a long option", func() { |
| 62 | + os.Args = []string{"foo", "--opt=/bar/xx/42"} |
52 | 63 |
|
53 |
| - Expect(codegen.CommandLine()).To(Equal("$ foo\n\t--opt=$(GOPATH)/bar/xx/42")) |
| 64 | + Expect(codegen.CommandLine()).To(Equal("$ foo\n\t--opt=/bar/xx/42")) |
| 65 | + }) |
54 | 66 | })
|
55 | 67 |
|
56 |
| - It("should not replace GOPATH if a match is not at the beginning of a long option", func() { |
57 |
| - os.Args = []string{"foo", "--opt=/bar/xx/42"} |
| 68 | + Context("with default GOPATH", func() { |
| 69 | + oldGOPATH, oldArgs := build.Default.GOPATH, os.Args |
| 70 | + BeforeEach(func() { |
| 71 | + os.Setenv("GOPATH", defaultGOPATH()) // Simulate a situation with no GOPATH exported. |
| 72 | + }) |
| 73 | + AfterEach(func() { |
| 74 | + os.Setenv("GOPATH", oldGOPATH) |
| 75 | + os.Args = oldArgs |
| 76 | + }) |
58 | 77 |
|
59 |
| - Expect(codegen.CommandLine()).To(Equal("$ foo\n\t--opt=/bar/xx/42")) |
| 78 | + It("should not touch free arguments", func() { |
| 79 | + os.Args = []string{"foo", "/xx/bar/xx/42"} |
| 80 | + |
| 81 | + Expect(codegen.CommandLine()).To(Equal("$ foo /xx/bar/xx/42")) |
| 82 | + }) |
| 83 | + |
| 84 | + It("should replace GOPATH one match only in a long option", func() { |
| 85 | + os.Args = []string{"foo", fmt.Sprintf("--opt=%s/bar/xx/42", defaultGOPATH())} |
| 86 | + |
| 87 | + Expect(codegen.CommandLine()).To(Equal("$ foo\n\t--opt=$(GOPATH)/bar/xx/42")) |
| 88 | + }) |
60 | 89 | })
|
61 | 90 | })
|
62 | 91 | })
|
| 92 | + |
| 93 | +// Copied from go/build/build.go |
| 94 | +func defaultGOPATH() string { |
| 95 | + env := "HOME" |
| 96 | + if runtime.GOOS == "windows" { |
| 97 | + env = "USERPROFILE" |
| 98 | + } else if runtime.GOOS == "plan9" { |
| 99 | + env = "home" |
| 100 | + } |
| 101 | + if home := os.Getenv(env); home != "" { |
| 102 | + def := filepath.Join(home, "go") |
| 103 | + if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) { |
| 104 | + // Don't set the default GOPATH to GOROOT, |
| 105 | + // as that will trigger warnings from the go tool. |
| 106 | + return "" |
| 107 | + } |
| 108 | + return def |
| 109 | + } |
| 110 | + return "" |
| 111 | +} |
0 commit comments