Skip to content

Commit

Permalink
Merge pull request #488 from vektra/getLocalizedPath_test
Browse files Browse the repository at this point in the history
Add test for getLocalizedPath
  • Loading branch information
LandonTClipp authored Jun 27, 2022
2 parents 519a84f + 8582bd6 commit 2ca0b83
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ func calculateImport(ctx context.Context, set []string, path string) string {
return path
}

// TODO(@IvanMalison): Is there not a better way to get the actual
// import path of a package?
// 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)
Expand Down
71 changes: 70 additions & 1 deletion pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"bufio"
"bytes"
"context"
"go/format"
"io"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

mocks "github.com/vektra/mockery/v2/mocks/pkg/fixtures"
"github.com/vektra/mockery/v2/pkg/config"
)
Expand Down Expand Up @@ -2437,3 +2437,72 @@ 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)
}
})
}
}

0 comments on commit 2ca0b83

Please sign in to comment.