github.com/tcncloud/wollemi@v0.8.1/adapters/golang/importer_test.go (about) 1 package golang_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 11 "github.com/tcncloud/wollemi/adapters/golang" 12 ) 13 14 func TestImporter_ImportDir(t *testing.T) { 15 if dir, ok := os.LookupEnv("PKG_DIR"); ok { 16 require.NoError(t, os.MkdirAll(dir, os.FileMode(0700))) 17 require.NoError(t, os.Chdir(dir)) 18 } 19 20 t.Run("errors when package dir does not exist", func(t *testing.T) { 21 importer := golang.NewImporter() 22 23 pkg, err := importer.ImportDir("foo/bar", []string{"baz.go"}) 24 require.EqualError(t, err, "open foo/bar/baz.go: no such file or directory") 25 require.Nil(t, pkg) 26 }) 27 28 t.Run("gets package info for import directory", func(t *testing.T) { 29 tmp, err := ioutil.TempDir("", "importer_test") 30 require.NoError(t, err) 31 32 defer os.RemoveAll(tmp) 33 34 for _, x := range []struct { 35 Path string 36 Data []byte 37 }{{ 38 Path: "adder/adder.go", 39 Data: []byte(adder), 40 }, { 41 Path: "adder/adder_test.go", 42 Data: []byte(adderTest), 43 }, { 44 Path: "multiplier/multiplier.go", 45 Data: []byte(multiplier), 46 }, { 47 Path: "multiplier/multiplier_test.go", 48 Data: []byte(multiplierTest), 49 }} { 50 path := filepath.Join(tmp, x.Path) 51 52 require.NoError(t, os.MkdirAll(filepath.Dir(path), os.FileMode(0700))) 53 require.NoError(t, ioutil.WriteFile(path, x.Data, os.FileMode(0644))) 54 } 55 56 importer := golang.NewImporter() 57 58 // --------------------------------------------------------------------- 59 60 have, err := importer.ImportDir(filepath.Join(tmp, "adder"), []string{ 61 "adder_test.go", 62 "adder.go", 63 }) 64 65 require.NoError(t, err) 66 67 want := &golang.Package{ 68 Name: "adder", 69 Imports: []string{ 70 "database/sql", 71 "fmt", 72 "github.com/spf13/viper", 73 }, 74 XTestImports: []string{ 75 "github.com/stretchr/testify/require", 76 "testing", 77 }, 78 GoFiles: []string{ 79 "adder.go", 80 }, 81 XTestGoFiles: []string{ 82 "adder_test.go", 83 }, 84 GoFileImports: map[string][]string{ 85 "adder_test.go": []string{ 86 "testing", 87 "github.com/stretchr/testify/require", 88 }, 89 "adder.go": []string{ 90 "database/sql", 91 "fmt", 92 "github.com/spf13/viper", 93 }, 94 }, 95 } 96 97 require.Equal(t, want, have) 98 99 // --------------------------------------------------------------------- 100 101 have, err = importer.ImportDir(filepath.Join(tmp, "multiplier"), []string{ 102 "multiplier_test.go", 103 "multiplier.go", 104 }) 105 106 require.NoError(t, err) 107 108 want = &golang.Package{ 109 Name: "multiplier", 110 Imports: []string{ 111 "github.com/coreos/rkt/pkg/lock", 112 "github.com/wollemi_test/project/proto", 113 "github.com/wollemi_test/project/service/routes/async", 114 "github.com/wollemi_test/project/service/routes/client", 115 "github.com/wollemi_test/project/service/routes/server", 116 "go/ast", 117 "go/build", 118 }, 119 TestImports: []string{ 120 "encoding/json", 121 "fmt", 122 "github.com/golang/mock/gomock", 123 "github.com/stretchr/testify/require", 124 "testing", 125 }, 126 GoFiles: []string{ 127 "multiplier.go", 128 }, 129 TestGoFiles: []string{ 130 "multiplier_test.go", 131 }, 132 GoFileImports: map[string][]string{ 133 "multiplier_test.go": []string{ 134 "encoding/json", 135 "fmt", 136 "testing", 137 "github.com/stretchr/testify/require", 138 "github.com/golang/mock/gomock", 139 }, 140 "multiplier.go": []string{ 141 "go/build", 142 "go/ast", 143 "github.com/coreos/rkt/pkg/lock", 144 "github.com/wollemi_test/project/proto", 145 "github.com/wollemi_test/project/service/routes/async", 146 "github.com/wollemi_test/project/service/routes/client", 147 "github.com/wollemi_test/project/service/routes/server", 148 }, 149 }, 150 } 151 152 require.Equal(t, want, have) 153 }) 154 } 155 156 const multiplier = ` 157 package multiplier 158 159 import ( 160 "go/build" 161 "go/ast" 162 163 "github.com/coreos/rkt/pkg/lock" 164 "github.com/wollemi_test/project/proto" 165 "github.com/wollemi_test/project/service/routes/async" 166 "github.com/wollemi_test/project/service/routes/client" 167 "github.com/wollemi_test/project/service/routes/server" 168 ) 169 170 func Multiply(x, y int) int { 171 return x * y 172 } 173 ` 174 175 const multiplierTest = ` 176 package multiplier 177 178 import ( 179 "encoding/json" 180 "fmt" 181 "testing" 182 183 "github.com/stretchr/testify/require" 184 "github.com/golang/mock/gomock" 185 ) 186 187 func TestMultiply(t*testing.T) { 188 t.Run("multiples two integers", func(t*testing.T) { 189 require.Equal(t, 14, multiply(7, 2)) 190 }) 191 } 192 ` 193 194 const adder = ` 195 package adder 196 197 import ( 198 "database/sql" 199 "fmt" 200 201 "github.com/spf13/viper" 202 ) 203 204 func NewAdder() *Adder { 205 return &Adder{} 206 } 207 208 type Adder struct{} 209 210 func (*Adder) Add(x, y int) int { 211 return x + y 212 } 213 ` 214 215 const adderTest = ` 216 package adder_test 217 218 import ( 219 "testing" 220 221 "github.com/stretchr/testify/require" 222 ) 223 224 func TestAdder_Add(t *testing.T) { 225 t.Run("adds two integers", func(t *testing.T) { 226 res := adder.New().Add(1, 2) 227 require.Equal(t, 3, res) 228 }) 229 } 230 `