github.com/vektra/mockery/v2@v2.43.1/cmd/mockery_test.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/chigopher/pathlib" 10 "github.com/spf13/viper" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestNewRootCmd(t *testing.T) { 16 cmd := NewRootCmd() 17 assert.Equal(t, "mockery", cmd.Name()) 18 } 19 20 func Test_initConfig(t *testing.T) { 21 tests := []struct { 22 name string 23 base_path string 24 configPath string 25 }{ 26 { 27 name: "test config at base directory", 28 base_path: "1/2/3/4", 29 configPath: "1/2/3/4/.mockery.yaml", 30 }, 31 { 32 name: "test config at upper directory", 33 base_path: "1/2/3/4", 34 configPath: "1/.mockery.yaml", 35 }, 36 { 37 name: "no config file found", 38 base_path: "1/2/3/4", 39 }, 40 } 41 for _, tt := range tests { 42 t.Run(tt.name, func(t *testing.T) { 43 tmpDir := pathlib.NewPath(t.TempDir()) 44 baseDir := tmpDir.Join(strings.Split(tt.base_path, "/")...) 45 require.NoError(t, baseDir.MkdirAll()) 46 47 configPath := pathlib.NewPath("") 48 if tt.configPath != "" { 49 configPath = tmpDir.Join(strings.Split(tt.configPath, "/")...) 50 require.NoError(t, configPath.WriteFile([]byte("all: True"))) 51 } 52 53 viperObj := viper.New() 54 55 initConfig(baseDir, viperObj, nil) 56 57 assert.Equal(t, configPath.String(), viperObj.ConfigFileUsed()) 58 }) 59 } 60 } 61 62 type Writer interface { 63 Foo() 64 } 65 66 func TestRunLegacyGenerationNonExistent(t *testing.T) { 67 tmpDir := t.TempDir() 68 config := ` 69 name: Foo 70 ` 71 configPath := pathlib.NewPath(tmpDir).Join("config.yaml") 72 require.NoError(t, configPath.WriteFile([]byte(config))) 73 74 v := viper.New() 75 initConfig(nil, v, configPath) 76 app, err := GetRootAppFromViper(v) 77 require.NoError(t, err) 78 assert.Error(t, app.Run()) 79 } 80 81 func newViper(tmpDir string) *viper.Viper { 82 v := viper.New() 83 v.Set("dir", tmpDir) 84 return v 85 } 86 87 func TestRunPackagesGenerationGlobalDefaults(t *testing.T) { 88 tmpDir := t.TempDir() 89 configFmt := ` 90 log-level: info 91 filename: "hello_{{.InterfaceName}}.go" 92 packages: 93 io: 94 config: 95 outpkg: mock_io 96 dir: %s 97 interfaces: 98 Writer:` 99 config := fmt.Sprintf(configFmt, tmpDir) 100 configPath := pathlib.NewPath(tmpDir).Join("config.yaml") 101 require.NoError(t, configPath.WriteFile([]byte(config))) 102 mockPath := pathlib.NewPath(tmpDir).Join("hello_Writer.go") 103 104 v := newViper(tmpDir) 105 initConfig(nil, v, configPath) 106 app, err := GetRootAppFromViper(v) 107 require.NoError(t, err) 108 require.NoError(t, app.Run()) 109 110 exists, err := mockPath.Exists() 111 require.NoError(t, err) 112 assert.True(t, exists) 113 } 114 115 func TestRunPackagesGeneration(t *testing.T) { 116 tmpDir := t.TempDir() 117 configFmt := ` 118 with-expecter: true 119 log-level: info 120 packages: 121 io: 122 config: 123 outpkg: mock_io 124 dir: %s 125 interfaces: 126 Writer:` 127 config := fmt.Sprintf(configFmt, tmpDir) 128 configPath := pathlib.NewPath(tmpDir).Join("config.yaml") 129 require.NoError(t, configPath.WriteFile([]byte(config))) 130 mockPath := pathlib.NewPath(tmpDir).Join("mock_Writer.go") 131 132 v := newViper(tmpDir) 133 initConfig(nil, v, configPath) 134 app, err := GetRootAppFromViper(v) 135 require.NoError(t, err) 136 require.NoError(t, app.Run()) 137 138 exists, err := mockPath.Exists() 139 require.NoError(t, err) 140 assert.True(t, exists) 141 } 142 143 func TestIssue565(t *testing.T) { 144 // An issue was posed in https://github.com/vektra/mockery/issues/565 145 // where mockery wasn't entering the `packages` config section. I think 146 // this is some kind of bug with viper. We should instead parse the yaml 147 // directly instead of relying on the struct unmarshalling from viper, 148 // which is kind of buggy. 149 tmpDir := t.TempDir() 150 config := ` 151 with-expecter: True 152 inpackage: True 153 testonly: True 154 log-level: debug 155 packages: 156 github.com/testuser/testpackage/internal/foopkg: 157 interfaces: 158 FooInterface: 159 ` 160 //config := fmt.Sprintf(configFmt, tmpDir) 161 configPath := pathlib.NewPath(tmpDir).Join("config.yaml") 162 require.NoError(t, configPath.WriteFile([]byte(config))) 163 164 goModPath := pathlib.NewPath(tmpDir).Join("go.mod") 165 err := goModPath.WriteFile([]byte(` 166 module github.com/testuser/testpackage 167 168 go 1.20`)) 169 require.NoError(t, err) 170 171 interfacePath := pathlib.NewPath(tmpDir).Join("internal", "foopkg", "interface.go") 172 require.NoError(t, interfacePath.Parent().MkdirAll()) 173 require.NoError(t, interfacePath.WriteFile([]byte(` 174 package foopkg 175 176 type FooInterface interface { 177 Foo() 178 Bar() 179 }`))) 180 181 mockPath := pathlib.NewPath(tmpDir).Join( 182 "mocks", 183 "github.com", 184 "testuser", 185 "testpackage", 186 "internal", 187 "foopkg", 188 "mock_FooInterface.go") 189 190 require.NoError(t, os.Chdir(tmpDir)) 191 192 v := viper.New() 193 initConfig(nil, v, configPath) 194 app, err := GetRootAppFromViper(v) 195 require.NoError(t, err) 196 require.NoError(t, app.Run()) 197 198 exists, err := mockPath.Exists() 199 require.NoError(t, err) 200 assert.True(t, exists) 201 } 202 203 func TestRunLegacyNoConfig(t *testing.T) { 204 tmpDir := pathlib.NewPath(t.TempDir()) 205 206 mockPath := tmpDir.Join("Foo.go") 207 codePath := tmpDir.Join("foo.go") 208 require.NoError(t, codePath.WriteFile([]byte(` 209 package test 210 211 type Foo interface { 212 Get(str string) string 213 }`))) 214 215 v := viper.New() 216 v.Set("log-level", "debug") 217 v.Set("outpkg", "foobar") 218 v.Set("name", "Foo") 219 v.Set("output", tmpDir.String()) 220 v.Set("disable-config-search", true) 221 require.NoError(t, os.Chdir(tmpDir.String())) 222 223 initConfig(nil, v, nil) 224 app, err := GetRootAppFromViper(v) 225 require.NoError(t, err) 226 require.NoError(t, app.Run()) 227 228 exists, err := mockPath.Exists() 229 require.NoError(t, err) 230 assert.True(t, exists) 231 } 232 233 func TestRunLegacyNoConfigDirSet(t *testing.T) { 234 tmpDir := pathlib.NewPath(t.TempDir()) 235 236 subdir := tmpDir.Join("subdir") 237 require.NoError(t, subdir.MkdirAll()) 238 239 mockPath := subdir.Join("Foo.go") 240 codePath := subdir.Join("foo.go") 241 242 err := codePath.WriteFile([]byte(` 243 package test 244 245 type Foo interface { 246 Get(str string) string 247 }`)) 248 require.NoError(t, err, "failed to write go file") 249 250 v := viper.New() 251 v.Set("log-level", "debug") 252 v.Set("outpkg", "foobar") 253 v.Set("name", "Foo") 254 v.Set("output", subdir.String()) 255 v.Set("disable-config-search", true) 256 v.Set("dir", subdir.String()) 257 v.Set("recursive", true) 258 require.NoError(t, os.Chdir(tmpDir.String())) 259 260 initConfig(nil, v, nil) 261 app, err := GetRootAppFromViper(v) 262 require.NoError(t, err) 263 require.NoError(t, app.Run()) 264 265 exists, err := mockPath.Exists() 266 require.NoError(t, err) 267 assert.True(t, exists) 268 }