Skip to content

Commit 344b524

Browse files
authored
Merge pull request #1511 from Scalingo/feat/no-more-gopath
feat(drop-gopath) Stop basing all generation on GOPATH which is not mandatory anymore
2 parents 95e14cd + c7cc02c commit 344b524

File tree

5 files changed

+53
-26
lines changed

5 files changed

+53
-26
lines changed

gomock_generator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## To be Released
44

5+
* feat(drop-gopath) Stop basing all generation on GOPATH which is not mandatory anymore
6+
57
## v1.4.2
68

79
* build(gomock_generator): update `github.com/urfave/cli` from v1 to v3

gomock_generator/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ COMMANDS:
1818
help, h Shows a list of commands or help for one command
1919
2020
GLOBAL OPTIONS:
21-
--mocks-filepath value Path to the JSON file containing the MockConfiguration. Location of this file is the base package. (default: "./mocks.json") [$MOCKS_FILEPATH]
22-
--signatures-filename value Filename of the signatures cache. Location of this file is the base package. (default: "mocks_sig.json") [$SIGNATURES_FILENAME]
21+
--mocks-filepath value Path to the JSON file containing the MockConfiguration. Its containing directory is the base package directory. (default: "./mocks.json") [$MOCKS_FILEPATH]
22+
--signatures-filename value Filename of the signatures cache, written in the base package directory. (default: "mocks_sig.json") [$SIGNATURES_FILENAME]
2323
--concurrent-goroutines value Concurrent amount of goroutines to generate mock. (default: 4) [$CONCURRENT_GOROUTINES]
2424
--debug Activate debug logs
2525
--help, -h show help
@@ -38,8 +38,7 @@ purpose of every attribute.
3838
## Installation
3939

4040
```shell
41-
cd $GOPATH/src/github.com/Scalingo/go-utils/gomock_generator
42-
go install
41+
go install github.com/Scalingo/go-utils/gomock_generator@latest
4342
```
4443

4544
## Release a new version of gomock_generator

gomock_generator/gomockgenerator/generate.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020

2121
// GenerationConfiguration lets you configure the generation of mocks for your project
2222
type GenerationConfiguration struct {
23-
// MocksFilePath is the path to the JSON file containing the mock configuration. Location of this file is the base package.
23+
// MocksFilePath is the path to the JSON file containing the mock configuration.
24+
// The directory containing this file is considered as the base package directory.
2425
MocksFilePath string
25-
// SignaturesFilename is the filename of the signatures cache. Location of this file is the base package.
26+
// SignaturesFilename is the filename of the signatures cache, written in the base package directory.
2627
SignaturesFilename string
2728
// ConcurrentGoroutines specifies the concurrent amount of goroutines which can execute
2829
ConcurrentGoroutines int
@@ -64,22 +65,29 @@ func GenerateMocks(ctx context.Context, gcfg GenerationConfiguration, mocksCfg M
6465
if err != nil {
6566
return errors.Wrap(ctx, err, "fail to get current directory")
6667
}
68+
absMocksFilePath, err := filepath.Abs(gcfg.MocksFilePath)
69+
if err != nil {
70+
return errors.Wrap(ctx, err, "fail to resolve absolute path of the mocks file")
71+
}
72+
basePackageDirectory := filepath.Dir(absMocksFilePath)
6773
if mocksCfg.BaseDirectory == "" {
68-
mocksCfg.BaseDirectory = mocksCfg.BasePackage
74+
mocksCfg.BaseDirectory = "."
6975
}
70-
err = os.Chdir(path.Join(os.Getenv("GOPATH"), "src", mocksCfg.BaseDirectory))
76+
packageRootDirectory := filepath.Join(basePackageDirectory, mocksCfg.BaseDirectory)
77+
err = os.Chdir(packageRootDirectory)
7178
if err != nil {
7279
return errors.Wrap(ctx, err, "fail to move to base package directory")
7380
}
7481
defer os.Chdir(cwd)
7582

7683
ctx, log := logger.WithFieldToCtx(ctx, "nb_mocks", len(mocksCfg.Mocks))
7784
log.WithFields(logrus.Fields{
78-
"base_package": mocksCfg.BasePackage,
85+
"base_package": mocksCfg.BasePackage,
86+
"base_package_directory": packageRootDirectory,
7987
}).Infof("Generating %v mocks", len(mocksCfg.Mocks))
8088

8189
var mockSigs map[string]string
82-
mockSigsPath := path.Join(os.Getenv("GOPATH"), "src", mocksCfg.BaseDirectory, gcfg.SignaturesFilename)
90+
mockSigsPath := filepath.Join(packageRootDirectory, gcfg.SignaturesFilename)
8391

8492
sigs, err := os.ReadFile(mockSigsPath)
8593
if os.IsNotExist(err) {
@@ -105,7 +113,7 @@ func GenerateMocks(ctx context.Context, gcfg GenerationConfiguration, mocksCfg M
105113
<-sem
106114
}()
107115
sem <- true
108-
path, sig, err := generateMock(ctx, gcfg, mocksCfg.BaseDirectory, mocksCfg.BasePackage, mock, mockSigs)
116+
path, sig, err := generateMock(ctx, gcfg, packageRootDirectory, mocksCfg.BasePackage, mock, mockSigs)
109117
if err != nil {
110118
log.Error(err)
111119
return
@@ -128,7 +136,7 @@ func GenerateMocks(ctx context.Context, gcfg GenerationConfiguration, mocksCfg M
128136
return nil
129137
}
130138

131-
func generateMock(ctx context.Context, gcfg GenerationConfiguration, baseDirectory, basePackage string, mock MockConfiguration, sigs map[string]string) (string, string, error) {
139+
func generateMock(ctx context.Context, gcfg GenerationConfiguration, basePackageDirectory, basePackage string, mock MockConfiguration, sigs map[string]string) (string, string, error) {
132140
log := logger.Get(ctx)
133141

134142
if !mock.External {
@@ -164,11 +172,12 @@ func generateMock(ctx context.Context, gcfg GenerationConfiguration, baseDirecto
164172
mock.DstPackage = dst
165173
}
166174

175+
localSrcPackage := mock.SrcPackage
167176
if !mock.External {
168-
mock.SrcPackage = path.Join(basePackage, mock.SrcPackage)
177+
mock.SrcPackage = path.Join(basePackage, localSrcPackage)
169178
}
170179

171-
mockPath := filepath.Join(os.Getenv("GOPATH"), "src", baseDirectory, mock.MockFile)
180+
mockPath := filepath.Join(basePackageDirectory, mock.MockFile)
172181
log = log.WithFields(logrus.Fields{
173182
"mock_file": mock.MockFile,
174183
"interface": mock.Interface,
@@ -195,10 +204,15 @@ func generateMock(ctx context.Context, gcfg GenerationConfiguration, baseDirecto
195204
mock.DstPackage = filepath.Base(mock.SrcPackage)
196205
}
197206

198-
mockSrcPath := strings.Replace(mock.SrcPackage, basePackage, baseDirectory, -1)
207+
hashSourcePath := mock.SrcPackage
208+
hashKeyPackage := mock.SrcPackage
209+
if !mock.External {
210+
hashSourcePath = filepath.Join(basePackageDirectory, localSrcPackage)
211+
hashKeyPackage = localSrcPackage
212+
}
199213

200-
hashKey := fmt.Sprintf("%s.%s", mockSrcPath, mock.Interface)
201-
hash, err := interfaceHash(mockSrcPath, mock.Interface)
214+
hashKey := fmt.Sprintf("%s.%s", hashKeyPackage, mock.Interface)
215+
hash, err := interfaceHash(hashSourcePath, mock.Interface)
202216
if err != nil {
203217
return "", "", errors.Wrapf(ctx, err, "fail to get interface hash of %v:%v", mock.SrcPackage, mock.Interface)
204218
}
@@ -224,7 +238,7 @@ func generateMock(ctx context.Context, gcfg GenerationConfiguration, baseDirecto
224238

225239
vendorDir := path.Join(basePackage, "vendor")
226240
cmd := fmt.Sprintf(
227-
"mockgen %s -destination %s %s -package %s %s %s && sed -i s,%s,, %s && goimports -w %s",
241+
"mockgen -write_command_comment=false %s -destination %s %s -package %s %s %s && sed -i s,%s,, %s && goimports -w %s",
228242
gomod, mockPath, selfPackage, mock.DstPackage, mock.SrcPackage, mock.Interface,
229243
vendorDir, mockPath, mockPath,
230244
)

gomock_generator/gomockgenerator/package.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"go/parser"
88
"go/token"
99
"os"
10-
"path"
1110
"path/filepath"
1211
"strings"
1312

@@ -31,10 +30,18 @@ func interfaceSignature(pkg, iName string) (string, error) {
3130
if err != nil {
3231
return "", errors.Wrap(err, "fail to get current working directory")
3332
}
34-
fullPath := path.Join(os.Getenv("GOPATH"), "src", pkg)
35-
vendoredPkg := filepath.Join(cwd, "vendor", pkg)
36-
if _, err := os.Stat(vendoredPkg); err == nil {
37-
fullPath = vendoredPkg
33+
fullPath := pkg
34+
if !filepath.IsAbs(fullPath) {
35+
localPkg := filepath.Join(cwd, fullPath)
36+
vendoredPkg := filepath.Join(cwd, "vendor", fullPath)
37+
switch {
38+
case isDir(localPkg):
39+
fullPath = localPkg
40+
case isDir(vendoredPkg):
41+
fullPath = vendoredPkg
42+
default:
43+
fullPath = localPkg
44+
}
3845
}
3946
fileSet := token.NewFileSet()
4047
packages, err := parser.ParseDir(fileSet, fullPath, func(info os.FileInfo) bool {
@@ -131,3 +138,8 @@ func fieldToString(field ast.Expr) string {
131138

132139
return fmt.Sprintf("%v", field)
133140
}
141+
142+
func isDir(path string) bool {
143+
info, err := os.Stat(path)
144+
return err == nil && info.IsDir()
145+
}

gomock_generator/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func main() {
2323
2424
%s --concurrent-goroutine 8 --mocks-filename mymocks.json --signatures-filename sigs.json
2525
26-
Reads the mymocks.json file from the current directory and generates the mocks, 8 goroutines at a time. The signatures of the mocks are stored in sigs.json, in the folder designated by the base package written in mymocks.json.
26+
Reads the mymocks.json file from the current directory and generates the mocks, 8 goroutines at a time. The signatures of the mocks are stored in sigs.json in the same directory as mymocks.json.
2727
2828
`, cli.RootCommandHelpTemplate, os.Args[0])
2929

@@ -35,8 +35,8 @@ func main() {
3535
Usage: "Highly parallelized generator of gomock mocks",
3636
Version: version,
3737
Flags: []cli.Flag{
38-
&cli.StringFlag{Name: "mocks-filepath", Value: "./mocks.json", Usage: "Path to the JSON file containing the MockConfiguration. Location of this file is the base package.", Sources: cli.EnvVars("MOCKS_FILEPATH")},
39-
&cli.StringFlag{Name: "signatures-filename", Value: "mocks_sig.json", Usage: "Filename of the signatures cache. Location of this file is the base package.", Sources: cli.EnvVars("SIGNATURES_FILENAME")},
38+
&cli.StringFlag{Name: "mocks-filepath", Value: "./mocks.json", Usage: "Path to the JSON file containing the MockConfiguration. Its containing directory is the base package directory.", Sources: cli.EnvVars("MOCKS_FILEPATH")},
39+
&cli.StringFlag{Name: "signatures-filename", Value: "mocks_sig.json", Usage: "Filename of the signatures cache, written in the base package directory.", Sources: cli.EnvVars("SIGNATURES_FILENAME")},
4040
&cli.IntFlag{Name: "concurrent-goroutines", Value: 4, Usage: "Concurrent amount of goroutines to generate mock.", Sources: cli.EnvVars("CONCURRENT_GOROUTINES")},
4141
&cli.BoolFlag{Name: "debug", Usage: "Activate debug logs"},
4242
},

0 commit comments

Comments
 (0)