Skip to content

Commit 6b56306

Browse files
tchsskraphael
authored andcommitted
Use build.Default.GOPATH instead of env GOPATH (goadesign#1930)
* Use build.Default.GOPATH instead of env GOPATH * Specify package name for some packages
1 parent e6e9ecb commit 6b56306

30 files changed

+108
-69
lines changed

design/random.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99

1010
"github.com/manveru/faker"
11-
"github.com/satori/go.uuid"
11+
uuid "github.com/satori/go.uuid"
1212
)
1313

1414
// RandomGenerator generates consistent random values of different types given a seed.

design/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/goadesign/goa/dslengine"
21-
"github.com/satori/go.uuid"
21+
uuid "github.com/satori/go.uuid"
2222
)
2323

2424
// DefaultView is the name of the default view.

goagen/codegen/helpers.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package codegen
33
import (
44
"bytes"
55
"fmt"
6+
"go/build"
67
"os"
78
"path/filepath"
89
"strings"
@@ -35,7 +36,7 @@ func CommandLine() string {
3536

3637
if len(os.Args) > 1 {
3738
args := make([]string, len(os.Args)-1)
38-
gopaths := filepath.SplitList(os.Getenv("GOPATH"))
39+
gopaths := filepath.SplitList(envOr("GOPATH", build.Default.GOPATH))
3940
for i, a := range os.Args[1:] {
4041
for _, p := range gopaths {
4142
p = "=" + p
@@ -59,6 +60,15 @@ func CommandLine() string {
5960
return strings.Replace(cmd, " --", "\n\t--", -1)
6061
}
6162

63+
// Copied from go/build/build.go
64+
func envOr(name, def string) string {
65+
s := os.Getenv(name)
66+
if s == "" {
67+
return def
68+
}
69+
return s
70+
}
71+
6272
// Comment produces line comments by concatenating the given strings and producing 80 characters
6373
// long lines starting with "//"
6474
func Comment(elems ...string) string {

goagen/codegen/helpers_test.go

+67-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package codegen_test
22

33
import (
4+
"fmt"
5+
"go/build"
46
"os"
7+
"path/filepath"
8+
"runtime"
59

610
"github.com/goadesign/goa/goagen/codegen"
711

@@ -32,31 +36,76 @@ var _ = Describe("Helpers", func() {
3236
})
3337

3438
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+
})
4348

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"}
4651

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+
})
4960

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"}
5263

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+
})
5466
})
5567

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+
})
5877

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+
})
6089
})
6190
})
6291
})
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+
}

goagen/codegen/workspace.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func NewWorkspace(prefix string) (*Workspace, error) {
8989
os.MkdirAll(filepath.Join(dir, "bin"), 0755)
9090

9191
// setup GOPATH
92-
gopath := os.Getenv("GOPATH")
92+
gopath := envOr("GOPATH", build.Default.GOPATH)
9393
os.Setenv("GOPATH", fmt.Sprintf("%s%c%s", dir, os.PathListSeparator, gopath))
9494

9595
// we're done
@@ -98,7 +98,7 @@ func NewWorkspace(prefix string) (*Workspace, error) {
9898

9999
// WorkspaceFor returns the Go workspace for the given Go source file.
100100
func WorkspaceFor(source string) (*Workspace, error) {
101-
gopaths := os.Getenv("GOPATH")
101+
gopaths := envOr("GOPATH", build.Default.GOPATH)
102102
// We use absolute paths so that in particular on Windows the case gets normalized
103103
sourcePath, err := filepath.Abs(source)
104104
if err != nil {
@@ -316,7 +316,7 @@ func PackagePath(path string) (string, error) {
316316
if err != nil {
317317
absPath = path
318318
}
319-
gopaths := filepath.SplitList(os.Getenv("GOPATH"))
319+
gopaths := filepath.SplitList(envOr("GOPATH", build.Default.GOPATH))
320320
for _, gopath := range gopaths {
321321
if gp, err := filepath.Abs(gopath); err == nil {
322322
gopath = gp
@@ -333,7 +333,7 @@ func PackagePath(path string) (string, error) {
333333
// PackageSourcePath returns the absolute path to the given package source.
334334
func PackageSourcePath(pkg string) (string, error) {
335335
buildCtx := build.Default
336-
buildCtx.GOPATH = os.Getenv("GOPATH") // Reevaluate each time to be nice to tests
336+
buildCtx.GOPATH = envOr("GOPATH", build.Default.GOPATH) // Reevaluate each time to be nice to tests
337337
wd, err := os.Getwd()
338338
if err != nil {
339339
wd = "."

goagen/gen_app/encoding_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package genapp_test
22

33
import (
44
"github.com/goadesign/goa/design"
5-
"github.com/goadesign/goa/goagen/gen_app"
5+
genapp "github.com/goadesign/goa/goagen/gen_app"
66
. "github.com/onsi/ginkgo"
77
. "github.com/onsi/gomega"
88
)

goagen/gen_app/generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/goadesign/goa/design"
1212
"github.com/goadesign/goa/dslengine"
1313
"github.com/goadesign/goa/goagen/codegen"
14-
"github.com/goadesign/goa/goagen/gen_app"
14+
genapp "github.com/goadesign/goa/goagen/gen_app"
1515
"github.com/goadesign/goa/version"
1616
. "github.com/onsi/ginkgo"
1717
. "github.com/onsi/gomega"

goagen/gen_app/test_generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/goadesign/goa/design"
1010
"github.com/goadesign/goa/dslengine"
1111
"github.com/goadesign/goa/goagen/codegen"
12-
"github.com/goadesign/goa/goagen/gen_app"
12+
genapp "github.com/goadesign/goa/goagen/gen_app"
1313
"github.com/goadesign/goa/version"
1414
. "github.com/onsi/ginkgo"
1515
. "github.com/onsi/gomega"

goagen/gen_app/writers_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/goadesign/goa/design/apidsl"
99
"github.com/goadesign/goa/dslengine"
1010
"github.com/goadesign/goa/goagen/codegen"
11-
"github.com/goadesign/goa/goagen/gen_app"
11+
genapp "github.com/goadesign/goa/goagen/gen_app"
1212
. "github.com/onsi/ginkgo"
1313
. "github.com/onsi/gomega"
1414
)

goagen/gen_client/cli_generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/goadesign/goa/design"
1212
"github.com/goadesign/goa/dslengine"
1313
"github.com/goadesign/goa/goagen/codegen"
14-
"github.com/goadesign/goa/goagen/gen_client"
14+
genclient "github.com/goadesign/goa/goagen/gen_client"
1515
"github.com/goadesign/goa/version"
1616
. "github.com/onsi/ginkgo"
1717
. "github.com/onsi/gomega"

goagen/gen_client/generator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/goadesign/goa/design"
1414
"github.com/goadesign/goa/dslengine"
1515
"github.com/goadesign/goa/goagen/codegen"
16-
"github.com/goadesign/goa/goagen/gen_app"
16+
genapp "github.com/goadesign/goa/goagen/gen_app"
1717
"github.com/goadesign/goa/goagen/utils"
1818
)
1919

goagen/gen_client/generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/goadesign/goa/design"
1212
"github.com/goadesign/goa/dslengine"
1313
"github.com/goadesign/goa/goagen/codegen"
14-
"github.com/goadesign/goa/goagen/gen_client"
14+
genclient "github.com/goadesign/goa/goagen/gen_client"
1515
"github.com/goadesign/goa/version"
1616
. "github.com/onsi/ginkgo"
1717
. "github.com/onsi/gomega"

goagen/gen_controller/generator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
"github.com/goadesign/goa/design"
1212
"github.com/goadesign/goa/goagen/codegen"
13-
"github.com/goadesign/goa/goagen/gen_main"
13+
genmain "github.com/goadesign/goa/goagen/gen_main"
1414
"github.com/goadesign/goa/goagen/utils"
1515
)
1616

goagen/gen_controller/generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/goadesign/goa/design"
1010
"github.com/goadesign/goa/goagen/codegen"
11-
"github.com/goadesign/goa/goagen/gen_controller"
11+
gencontroller "github.com/goadesign/goa/goagen/gen_controller"
1212
"github.com/goadesign/goa/version"
1313
. "github.com/onsi/ginkgo"
1414
. "github.com/onsi/gomega"

goagen/gen_js/generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
"github.com/goadesign/goa/design"
12-
"github.com/goadesign/goa/goagen/gen_js"
12+
genjs "github.com/goadesign/goa/goagen/gen_js"
1313
"github.com/goadesign/goa/version"
1414
. "github.com/onsi/ginkgo"
1515
. "github.com/onsi/gomega"

goagen/gen_main/generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"strings"
99

1010
"github.com/goadesign/goa/design"
11-
"github.com/goadesign/goa/goagen/gen_main"
11+
genmain "github.com/goadesign/goa/goagen/gen_main"
1212
"github.com/goadesign/goa/version"
1313
. "github.com/onsi/ginkgo"
1414
. "github.com/onsi/gomega"

goagen/gen_schema/generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/goadesign/goa/design/apidsl"
1212
"github.com/goadesign/goa/dslengine"
1313
"github.com/goadesign/goa/goagen/codegen"
14-
"github.com/goadesign/goa/goagen/gen_schema"
14+
genschema "github.com/goadesign/goa/goagen/gen_schema"
1515
"github.com/goadesign/goa/version"
1616
. "github.com/onsi/ginkgo"
1717
. "github.com/onsi/gomega"

goagen/gen_schema/json_schema_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"github.com/goadesign/goa/design"
55
. "github.com/goadesign/goa/design/apidsl"
66
"github.com/goadesign/goa/dslengine"
7-
"github.com/goadesign/goa/goagen/gen_schema"
7+
genschema "github.com/goadesign/goa/goagen/gen_schema"
88
. "github.com/onsi/ginkgo"
99
. "github.com/onsi/gomega"
1010
)

goagen/gen_swagger/generator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010

11-
"gopkg.in/yaml.v2"
11+
yaml "gopkg.in/yaml.v2"
1212

1313
"github.com/goadesign/goa/design"
1414
"github.com/goadesign/goa/goagen/codegen"

goagen/gen_swagger/generator_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package genswagger_test
22

33
import (
44
"github.com/goadesign/goa/design"
5-
"github.com/goadesign/goa/goagen/gen_swagger"
5+
genswagger "github.com/goadesign/goa/goagen/gen_swagger"
66
. "github.com/onsi/ginkgo"
77
. "github.com/onsi/gomega"
88
)

goagen/gen_swagger/swagger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
"github.com/goadesign/goa/design"
1111
"github.com/goadesign/goa/dslengine"
12-
"github.com/goadesign/goa/goagen/gen_schema"
12+
genschema "github.com/goadesign/goa/goagen/gen_schema"
1313
)
1414

1515
type (

goagen/gen_swagger/swagger_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
. "github.com/goadesign/goa/design"
1010
. "github.com/goadesign/goa/design/apidsl"
1111
"github.com/goadesign/goa/dslengine"
12-
"github.com/goadesign/goa/goagen/gen_schema"
13-
"github.com/goadesign/goa/goagen/gen_swagger"
12+
genschema "github.com/goadesign/goa/goagen/gen_schema"
13+
genswagger "github.com/goadesign/goa/goagen/gen_swagger"
1414
. "github.com/onsi/ginkgo"
1515
. "github.com/onsi/gomega"
1616
)

goagen/meta/generator.go

-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ func NewGenerator(genfunc string, imports []*codegen.ImportSpec, flags map[strin
8282
// Generate compiles and runs the generator and returns the generated filenames.
8383
func (m *Generator) Generate() ([]string, error) {
8484
// Sanity checks
85-
if os.Getenv("GOPATH") == "" {
86-
return nil, fmt.Errorf("GOPATH not set")
87-
}
8885
if m.OutDir == "" {
8986
return nil, fmt.Errorf("missing output directory flag")
9087
}

goagen/meta/generator_test.go

-17
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,6 @@ var _ = Describe("Run", func() {
8989
genWorkspace.Delete()
9090
})
9191

92-
Context("with no GOPATH environment variable", func() {
93-
var gopath string
94-
95-
BeforeEach(func() {
96-
gopath = os.Getenv("GOPATH")
97-
os.Setenv("GOPATH", "")
98-
})
99-
100-
AfterEach(func() {
101-
os.Setenv("GOPATH", gopath)
102-
})
103-
104-
It("fails with a useful error message", func() {
105-
Ω(compileError).Should(MatchError("GOPATH not set"))
106-
})
107-
})
108-
10992
Context("with an invalid GOPATH environment variable", func() {
11093
var gopath string
11194
const invalidPath = "DOES NOT EXIST"

logging/kit/adapter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/go-kit/kit/log"
77
"github.com/goadesign/goa"
8-
"github.com/goadesign/goa/logging/kit"
8+
goakit "github.com/goadesign/goa/logging/kit"
99
. "github.com/onsi/ginkgo"
1010
. "github.com/onsi/gomega"
1111
)

0 commit comments

Comments
 (0)