github.com/ManabuSeki/goa-v1@v1.4.3/goagen/codegen/helpers_test.go (about) 1 package codegen_test 2 3 import ( 4 "fmt" 5 "go/build" 6 "os" 7 "path/filepath" 8 "runtime" 9 10 "github.com/goadesign/goa/goagen/codegen" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Helpers", func() { 17 Describe("KebabCase", func() { 18 It("should change uppercase letters to lowercase letters", func() { 19 Expect(codegen.KebabCase("test-B")).To(Equal("test-b")) 20 Expect(codegen.KebabCase("teste")).To(Equal("teste")) 21 }) 22 23 It("should not add a dash before an abbreviation or acronym", func() { 24 Expect(codegen.KebabCase("testABC")).To(Equal("testabc")) 25 }) 26 27 It("should add a dash before a title", func() { 28 Expect(codegen.KebabCase("testAa")).To(Equal("test-aa")) 29 Expect(codegen.KebabCase("testAbc")).To(Equal("test-abc")) 30 }) 31 32 It("should replace underscores to dashes", func() { 33 Expect(codegen.KebabCase("test_cA")).To(Equal("test-ca")) 34 Expect(codegen.KebabCase("test_D")).To(Equal("test-d")) 35 }) 36 }) 37 38 Describe("CommandLine", func() { 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 }) 48 49 It("should not touch free arguments", func() { 50 os.Args = []string{"foo", "/xx/bar/xx/42"} 51 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 }) 60 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"} 63 64 Expect(codegen.CommandLine()).To(Equal("$ foo\n\t--opt=/bar/xx/42")) 65 }) 66 }) 67 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 }) 77 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 }) 89 }) 90 }) 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 }