Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 3dcdcb6

Browse files
authoredJan 20, 2020
fix not resolving go module major versions (#385)
This change now makes use of go list to check for package names. This tool is module aware and allows for better named imports. To test this change I needed to also add a small package to our mod file. To keep this import from disappearing from go.mod I made use of the tools file strategy. Note this change will change the import names in generated code. This should not be a breaking change in user code. Fixes #326
1 parent f165686 commit 3dcdcb6

File tree

9 files changed

+94
-12
lines changed

9 files changed

+94
-12
lines changed
 

Diff for: ‎go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module github.com/golang/mock
22

3-
require golang.org/x/tools v0.0.0-20190425150028-36563e24a262
3+
require (
4+
golang.org/x/tools v0.0.0-20190425150028-36563e24a262
5+
rsc.io/quote/v3 v3.1.0
6+
)
47

58
go 1.11

Diff for: ‎go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
22
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
33
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
44
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5+
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
57
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
68
golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg=
79
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
10+
rsc.io/quote/v3 v3.1.0 h1:9JKUTTIUgS6kzR9mK1YuGKv6Nl+DijDNIc0ghT58FaY=
11+
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
12+
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
13+
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

Diff for: ‎mockgen/internal/tests/custom_package_name/greeter/greeter_mock_test.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎mockgen/internal/tests/import_source/source_mock.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎mockgen/mockgen.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package main
2020

2121
import (
2222
"bytes"
23+
"encoding/json"
2324
"flag"
2425
"fmt"
2526
"go/build"
@@ -29,6 +30,7 @@ import (
2930
"io/ioutil"
3031
"log"
3132
"os"
33+
"os/exec"
3234
"path"
3335
"path/filepath"
3436
"sort"
@@ -287,7 +289,10 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
287289
g.packageMap = make(map[string]string, len(im))
288290
localNames := make(map[string]bool, len(im))
289291
for _, pth := range sortedPaths {
290-
base := sanitize(path.Base(pth))
292+
base, ok := lookupPackageName(pth)
293+
if !ok {
294+
base = sanitize(path.Base(pth))
295+
}
291296

292297
// Local names for an imported package can usually be the basename of the import path.
293298
// A couple of situations don't permit that, such as duplicate local names
@@ -598,3 +603,21 @@ func (g *generator) Output() []byte {
598603
}
599604
return src
600605
}
606+
607+
func lookupPackageName(importPath string) (string, bool) {
608+
var pkg struct {
609+
Name string
610+
}
611+
b := bytes.NewBuffer(nil)
612+
cmd := exec.Command("go", "list", "-json", importPath)
613+
cmd.Stdout = b
614+
err := cmd.Run()
615+
if err != nil {
616+
return "", false
617+
}
618+
err = json.Unmarshal(b.Bytes(), &pkg)
619+
if err != nil {
620+
return "", false
621+
}
622+
return pkg.Name, true
623+
}

Diff for: ‎mockgen/mockgen_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,31 @@ func TestGetArgNames(t *testing.T) {
334334
})
335335
}
336336
}
337+
338+
func Test_lookupPackageName(t *testing.T) {
339+
type args struct {
340+
importPath string
341+
}
342+
tests := []struct {
343+
name string
344+
importPath string
345+
wantPackageName string
346+
wantOK bool
347+
}{
348+
{"golang package", "context", "context", true},
349+
{"third party", "golang.org/x/tools/present", "present", true},
350+
{"modules", "rsc.io/quote/v3", "quote", true},
351+
{"fail", "this/should/not/work", "", false},
352+
}
353+
for _, tt := range tests {
354+
t.Run(tt.name, func(t *testing.T) {
355+
gotPackageName, gotOk := lookupPackageName(tt.importPath)
356+
if gotPackageName != tt.wantPackageName {
357+
t.Errorf("lookupPackageName() gotPackageName = %v, wantPackageName %v", gotPackageName, tt.wantPackageName)
358+
}
359+
if gotOk != tt.wantOK {
360+
t.Errorf("lookupPackageName() gotOk = %v, wantOK %v", gotOk, tt.wantOK)
361+
}
362+
})
363+
}
364+
}

Diff for: ‎mockgen/parse.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,15 @@ func importsOfFile(file *ast.File) (normalImports map[string]string, dotImports
453453
}
454454
pkgName = is.Name.Name
455455
} else {
456-
pkg, err := build.Import(importPath, "", 0)
457-
if err != nil {
456+
pkg, ok := lookupPackageName(importPath)
457+
if !ok {
458458
// Fallback to import path suffix. Note that this is uncertain.
459459
_, last := path.Split(importPath)
460460
// If the last path component has dots, the first dot-delimited
461461
// field is used as the name.
462462
pkgName = strings.SplitN(last, ".", 2)[0]
463463
} else {
464-
pkgName = pkg.Name
464+
pkgName = pkg
465465
}
466466
}
467467

Diff for: ‎sample/mock_user/mock_user.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎tools.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// +build tools
2+
3+
// Copyright 2020 Google Inc.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package mock
18+
19+
// These imports are included here so they don't disappear from go.mod file.
20+
import (
21+
_ "rsc.io/quote/v3"
22+
)

0 commit comments

Comments
 (0)
This repository has been archived.