From 1ec2d106daffac0f0a2e1b6e72878fb9ad79d953 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 24 Jun 2022 00:46:11 +0200 Subject: [PATCH] refactor: simplify --- go.mod | 2 -- go.sum | 4 ---- mocktail.go | 37 +++++++++++++------------------------ mod.go | 33 ++++++++------------------------- 4 files changed, 21 insertions(+), 55 deletions(-) diff --git a/go.mod b/go.mod index d39bf70..4e7dcf5 100644 --- a/go.mod +++ b/go.mod @@ -6,12 +6,10 @@ require ( github.com/ettle/strcase v0.1.1 github.com/hexops/gotextdiff v1.0.3 github.com/stretchr/testify v1.7.4 - golang.org/x/mod v0.5.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 8b5e76b..3a334e3 100644 --- a/go.sum +++ b/go.sum @@ -13,10 +13,6 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM= github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= -golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/mocktail.go b/mocktail.go index 1fd6d66..b560fe0 100644 --- a/mocktail.go +++ b/mocktail.go @@ -40,28 +40,19 @@ type InterfaceDesc struct { } func main() { - modulePath, err := getModulePath(os.Getenv("MOCKTAIL_TEST_PATH")) + info, err := getModuleInfo(os.Getenv("MOCKTAIL_TEST_PATH")) if err != nil { log.Fatal("get module path", err) } - moduleName, err := getModuleName(modulePath) - if err != nil { - log.Fatal("get module name", err) - } - - root := filepath.Dir(modulePath) - - // moduleName: github.com/traefik/mocktail - // modulePath: /home/ldez/go/src/github.com/traefik/mocktail/go.mod - // root: /home/ldez/go/src/github.com/traefik/mocktail + root := info.Dir err = os.Chdir(root) if err != nil { log.Fatalf("Chdir: %v", err) } - model, err := walk(root, moduleName) + model, err := walk(root, info.Path) if err != nil { log.Fatalf("walk: %v", err) } @@ -120,21 +111,19 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) { interfaceName := line[i+len(commentTagPattern):] - filePkgName, err := filepath.Rel(root, filepath.Dir(fp)) - if err != nil { - return err - } - - var pkgName string + var importPath string if index := strings.LastIndex(interfaceName, "."); index > 0 { - pkgName = interfaceName[:index] + importPath = path.Join(moduleName, interfaceName[:index]) + interfaceName = interfaceName[index+1:] } else { - pkgName = filePkgName - } + filePkgName, err := filepath.Rel(root, filepath.Dir(fp)) + if err != nil { + return err + } - importPathFile := path.Join(moduleName, filePkgName) - importPath := path.Join(moduleName, pkgName) + importPath = path.Join(moduleName, filePkgName) + } pkg, err := importR.Import(importPath) if err != nil { @@ -161,7 +150,7 @@ func walk(root, moduleName string) (map[string]PackageDesc, error) { interfaceDesc.Methods = append(interfaceDesc.Methods, method) - for _, imp := range getMethodImports(method, importPathFile) { + for _, imp := range getMethodImports(method, packageDesc.PkgPath) { packageDesc.Imports[imp] = struct{}{} } } diff --git a/mod.go b/mod.go index 01e4bde..11f58b3 100644 --- a/mod.go +++ b/mod.go @@ -5,21 +5,18 @@ import ( "encoding/json" "errors" "fmt" - "os" "os/exec" - - "golang.org/x/mod/modfile" ) type modInfo struct { - Path string `json:"Path"` - Dir string `json:"Dir"` - GoMod string `json:"GoMod"` + Path string `json:"Path"` // module name + Dir string `json:"Dir"` // absolute path to the module + GoMod string `json:"GoMod"` // absolute path to the go.mod GoVersion string `json:"GoVersion"` Main bool `json:"Main"` } -func getModulePath(dir string) (string, error) { +func getModuleInfo(dir string) (modInfo, error) { // https://github.com/golang/go/issues/44753#issuecomment-790089020 cmd := exec.Command("go", "list", "-m", "-json") if dir != "" { @@ -28,32 +25,18 @@ func getModulePath(dir string) (string, error) { raw, err := cmd.CombinedOutput() if err != nil { - return "", fmt.Errorf("command go list: %w: %s", err, string(raw)) + return modInfo{}, fmt.Errorf("command go list: %w: %s", err, string(raw)) } var v modInfo err = json.NewDecoder(bytes.NewBuffer(raw)).Decode(&v) if err != nil { - return "", fmt.Errorf("unmarshaling error: %w: %s", err, string(raw)) + return modInfo{}, fmt.Errorf("unmarshaling error: %w: %s", err, string(raw)) } if v.GoMod == "" { - return "", errors.New("working directory is not part of a module") - } - - return v.GoMod, nil -} - -func getModuleName(modulePath string) (string, error) { - raw, err := os.ReadFile(modulePath) - if err != nil { - return "", fmt.Errorf("reading go.mod file: %w", err) - } - - modData, err := modfile.Parse("go.mod", raw, nil) - if err != nil { - return "", fmt.Errorf("parsing go.mod file: %w", err) + return modInfo{}, errors.New("working directory is not part of a module") } - return modData.Module.Mod.String(), nil + return v, nil }