From c82f3c3cbfabf1106ae4282f48676589af2b950f Mon Sep 17 00:00:00 2001 From: Patryk Orwat Date: Sat, 2 Jul 2022 18:35:48 +0800 Subject: [PATCH] removed unused package import logic --- pkg/generator.go | 70 +------------------------------------- pkg/generator_test.go | 78 ------------------------------------------- 2 files changed, 1 insertion(+), 147 deletions(-) diff --git a/pkg/generator.go b/pkg/generator.go index 74422a6e..96f7e5bd 100644 --- a/pkg/generator.go +++ b/pkg/generator.go @@ -6,11 +6,9 @@ import ( "errors" "fmt" "go/ast" - "go/build" "go/types" "io" "os" - "path/filepath" "regexp" "sort" "strings" @@ -40,19 +38,10 @@ type Generator struct { localizationCache map[string]string packagePathToName map[string]string nameToPackagePath map[string]string - - packageRoots []string } // NewGenerator builds a Generator. func NewGenerator(ctx context.Context, c config.Config, iface *Interface, pkg string) *Generator { - - var roots []string - - for _, root := range filepath.SplitList(build.Default.GOPATH) { - roots = append(roots, filepath.Join(root, "src")) - } - g := &Generator{ Config: c, iface: iface, @@ -60,7 +49,6 @@ func NewGenerator(ctx context.Context, c config.Config, iface *Interface, pkg st localizationCache: make(map[string]string), packagePathToName: make(map[string]string), nameToPackagePath: make(map[string]string), - packageRoots: roots, } g.addPackageImportWithName(ctx, "github.com/stretchr/testify/mock", "mock") @@ -121,7 +109,6 @@ func (g *Generator) addPackageImport(ctx context.Context, pkg *types.Package) st } func (g *Generator) addPackageImportWithName(ctx context.Context, path, name string) string { - path = g.getLocalizedPath(ctx, path) if existingName, pathExists := g.packagePathToName[path]; pathExists { return existingName } @@ -138,8 +125,7 @@ func (g *Generator) getNonConflictingName(path, name string) string { return name } - // The path will always contain '/' because it is enforced in getLocalizedPath - // regardless of OS. + // The path will always contain '/' because it is enforced by Go import system directories := strings.Split(path, "/") cleanedDirectories := make([]string, 0, len(directories)) @@ -172,60 +158,6 @@ func (g *Generator) importNameExists(name string) bool { return nameExists } -func calculateImport(ctx context.Context, set []string, path string) string { - log := zerolog.Ctx(ctx).With().Str(logging.LogKeyPath, path).Logger() - ctx = log.WithContext(ctx) - - for _, root := range set { - if strings.HasPrefix(path, root) { - packagePath, err := filepath.Rel(root, path) - if err == nil { - return packagePath - } - log.Err(err).Msgf("Unable to localize path") - } - } - return path -} - -// getLocalizedPath, given a path to a file or an importable URL, -// returns the proper string needed to import the package. See tests -// for specific examples of what this should return. -func (g *Generator) getLocalizedPath(ctx context.Context, path string) string { - log := zerolog.Ctx(ctx).With().Str(logging.LogKeyPath, path).Logger() - ctx = log.WithContext(ctx) - - if strings.HasSuffix(path, ".go") { - path, _ = filepath.Split(path) - } - if localized, ok := g.localizationCache[path]; ok { - return localized - } - directories := strings.Split(path, string(filepath.Separator)) - numDirectories := len(directories) - vendorIndex := -1 - for i := 1; i <= numDirectories; i++ { - dir := directories[numDirectories-i] - if dir == "vendor" { - vendorIndex = numDirectories - i - break - } - } - - toReturn := path - if vendorIndex >= 0 { - toReturn = filepath.Join(directories[vendorIndex+1:]...) - } else if filepath.IsAbs(path) { - toReturn = calculateImport(ctx, g.packageRoots, path) - } - - // Enforce '/' slashes for import paths in every OS. - toReturn = filepath.ToSlash(toReturn) - - g.localizationCache[path] = toReturn - return toReturn -} - func (g *Generator) maybeMakeNameExported(name string, export bool) string { if export && !ast.IsExported(name) { return g.makeNameExported(name) diff --git a/pkg/generator_test.go b/pkg/generator_test.go index d480bef1..572f30dc 100644 --- a/pkg/generator_test.go +++ b/pkg/generator_test.go @@ -2,7 +2,6 @@ package pkg import ( "bufio" - "bytes" "context" "go/format" "io" @@ -117,14 +116,6 @@ func (s *GeneratorSuite) checkPrologueGeneration( ) } -func (s *GeneratorSuite) TestCalculateImport() { - gp := []string{"a/src", "b/src"} - - s.Equal("c", calculateImport(ctx, gp, "a/src/c")) - s.Equal("c", calculateImport(ctx, gp, "b/src/c")) - s.Equal("d/src/c", calculateImport(ctx, gp, "d/src/c")) -} - func (s *GeneratorSuite) TestGenerator() { expected := `// Requester is an autogenerated mock type for the Requester type type Requester struct { @@ -2621,72 +2612,3 @@ func TestGeneratorSuite(t *testing.T) { generatorSuite := new(GeneratorSuite) suite.Run(t, generatorSuite) } - -func TestGenerator_getLocalizedPath(t *testing.T) { - type fields struct { - Config config.Config - buf bytes.Buffer - iface *Interface - pkg string - localizationCache map[string]string - packagePathToName map[string]string - nameToPackagePath map[string]string - packageRoots []string - } - type args struct { - ctx context.Context - path string - } - tests := []struct { - name string - fields fields - args args - want string - }{ - { - name: "test on-disk", - fields: fields{localizationCache: make(map[string]string)}, - args: args{ctx: context.Background(), path: "path/to/file.go"}, - want: "path/to/", - }, - { - name: "test vendored", - fields: fields{localizationCache: make(map[string]string)}, - args: args{ctx: context.Background(), path: "vendor/path/to/file.go"}, - want: "path/to", - }, - { - name: "test URL", - fields: fields{localizationCache: make(map[string]string)}, - args: args{ctx: context.Background(), path: "github.com/vektra/mockery"}, - want: "github.com/vektra/mockery", - }, - { - // NOTE: This test is currently testing for behavior that _should_ be - // fixed. This is a special case where the import path ends in .go, - // but we should be getting the full importable URL here. - // https://github.com/vektra/mockery/pull/487 - name: "test nats.go", - fields: fields{localizationCache: make(map[string]string)}, - args: args{ctx: context.Background(), path: "github.com/nats-io/nats.go"}, - want: "github.com/nats-io/", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - g := &Generator{ - Config: tt.fields.Config, - buf: tt.fields.buf, - iface: tt.fields.iface, - pkg: tt.fields.pkg, - localizationCache: tt.fields.localizationCache, - packagePathToName: tt.fields.packagePathToName, - nameToPackagePath: tt.fields.nameToPackagePath, - packageRoots: tt.fields.packageRoots, - } - if got := g.getLocalizedPath(tt.args.ctx, tt.args.path); got != tt.want { - t.Errorf("Generator.getLocalizedPath() = %v, want %v", got, tt.want) - } - }) - } -}