Skip to content

Commit f76b874

Browse files
committed
fix template path in generated code uses OS specific path separators hexdigest#75
1 parent e5b68ed commit f76b874

File tree

6 files changed

+46
-47
lines changed

6 files changed

+46
-47
lines changed

cmd_generate.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"path"
910
"path/filepath"
1011
"regexp"
1112
"strings"
@@ -46,7 +47,7 @@ func NewGenerateCommand(l remoteTemplateLoader) *GenerateCommand {
4647
},
4748
}
4849

49-
//this flagset loads flags values to the command fields
50+
// this flagset loads flags values to the command fields
5051
fs := &flag.FlagSet{}
5152
fs.BoolVar(&gc.noGenerate, "g", false, "don't put //go:generate instruction to the generated code")
5253
fs.StringVar(&gc.interfaceName, "i", "", `the source interface name, i.e. "Reader"`)
@@ -129,7 +130,7 @@ func (gc *GenerateCommand) getOptions() (*generator.Options, error) {
129130
HeaderTemplate: headerTemplate,
130131
HeaderVars: map[string]interface{}{
131132
"DisableGoGenerate": gc.noGenerate,
132-
"OutputFileName": filepath.Base(gc.outputFile),
133+
"OutputFileName": path.Base(gc.outputFile),
133134
"VarsArgs": varsToArgs(gc.vars),
134135
},
135136
Vars: gc.vars.toMap(),
@@ -289,8 +290,10 @@ func downFirst(s string) string {
289290
return ""
290291
}
291292

292-
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
293-
var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
293+
var (
294+
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
295+
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
296+
)
294297

295298
func toSnakeCase(str string) string {
296299
result := matchFirstCap.ReplaceAllString(str, "${1}_${2}")

cmd_generate_test.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package gowrap
33
import (
44
"io"
55
"os"
6-
"path/filepath"
6+
"path"
77
"testing"
88
"time"
99

@@ -18,18 +18,18 @@ func TestNewGenerateCommand(t *testing.T) {
1818
}
1919

2020
func Test_loader_Load(t *testing.T) {
21-
var unexpectedErr = errors.New("unexpected error")
21+
unexpectedErr := errors.New("unexpected error")
2222

2323
tests := []struct {
2424
name string
2525
init func(t minimock.Tester) loader
26-
inspect func(r loader, t *testing.T) //inspects loader after execution of Load
26+
inspect func(r loader, t *testing.T) // inspects loader after execution of Load
2727

2828
template string
2929
want1 []byte
3030
want2 string
3131
wantErr bool
32-
inspectErr func(err error, t *testing.T) //use for more precise error evaluation
32+
inspectErr func(err error, t *testing.T) // use for more precise error evaluation
3333
}{
3434
{
3535
name: "file read error",
@@ -100,23 +100,21 @@ func Test_loader_Load(t *testing.T) {
100100
} else {
101101
assert.NoError(t, err)
102102
}
103-
104103
})
105104
}
106105
}
107106

108107
func TestGenerateCommand_getOptions(t *testing.T) {
109-
var absError = errors.New("abs error")
108+
absError := errors.New("abs error")
110109

111110
tests := []struct {
112111
name string
113112
init func(t minimock.Tester) *GenerateCommand
114113

115114
want1 *generator.Options
116115
wantErr bool
117-
inspectErr func(err error, t *testing.T) //use for more precise error evaluation
116+
inspectErr func(err error, t *testing.T) // use for more precise error evaluation
118117
}{
119-
120118
{
121119
name: "unexisting output path",
122120
init: func(t minimock.Tester) *GenerateCommand {
@@ -149,7 +147,6 @@ func TestGenerateCommand_getOptions(t *testing.T) {
149147
} else {
150148
assert.NoError(t, err)
151149
}
152-
153150
})
154151
}
155152
}
@@ -158,12 +155,12 @@ func TestGenerateCommand_Run(t *testing.T) {
158155
tests := []struct {
159156
name string
160157
init func(t minimock.Tester) *GenerateCommand
161-
inspect func(r *GenerateCommand, t *testing.T) //inspects *GenerateCommand after execution of Run
158+
inspect func(r *GenerateCommand, t *testing.T) // inspects *GenerateCommand after execution of Run
162159

163160
args []string
164161

165162
wantErr bool
166-
inspectErr func(err error, t *testing.T) //use for more precise error evaluation
163+
inspectErr func(err error, t *testing.T) // use for more precise error evaluation
167164
}{
168165
{
169166
name: "parse args error",
@@ -244,7 +241,7 @@ func TestGenerateCommand_Run(t *testing.T) {
244241
)`), "local/file", nil)
245242

246243
cmd.filepath.WriteFile = func(filename string, data []byte, perm os.FileMode) error {
247-
cmd.outputFile = filepath.Join(t.TempDir(), cmd.outputFile)
244+
cmd.outputFile = path.Join(t.TempDir(), cmd.outputFile)
248245
return os.WriteFile(cmd.outputFile, data, perm)
249246
}
250247
return cmd
@@ -289,7 +286,6 @@ func TestGenerateCommand_Run(t *testing.T) {
289286
} else {
290287
assert.NoError(t, err)
291288
}
292-
293289
})
294290
}
295291
}
@@ -352,7 +348,7 @@ func TestVars_toMap(t *testing.T) {
352348
func TestVars_Set(t *testing.T) {
353349
tests := []struct {
354350
name string
355-
inspect func(r vars, t *testing.T) //inspects vars after execution of Set
351+
inspect func(r vars, t *testing.T) // inspects vars after execution of Set
356352
s string
357353
}{
358354
{

generator/generator.go

+24-23
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package generator
33
import (
44
"bytes"
55
"fmt"
6-
"path/filepath"
7-
"sort"
8-
"strings"
9-
106
"go/ast"
117
"go/token"
128
"io"
9+
"path"
10+
"sort"
11+
"strings"
1312
"text/template"
1413

1514
"github.com/pkg/errors"
@@ -95,38 +94,38 @@ type TemplateInputInterface struct {
9594

9695
// Options of the NewGenerator constructor
9796
type Options struct {
98-
//InterfaceName is a name of interface type
97+
// InterfaceName is a name of interface type
9998
InterfaceName string
10099

101-
//Imports from the file with interface definition
100+
// Imports from the file with interface definition
102101
Imports []string
103102

104-
//SourcePackage is an import path or a relative path of the package that contains the source interface
103+
// SourcePackage is an import path or a relative path of the package that contains the source interface
105104
SourcePackage string
106105

107-
//SourcePackageAlias is an import selector defauls is source package name
106+
// SourcePackageAlias is an import selector defauls is source package name
108107
SourcePackageAlias string
109108

110-
//OutputFile name which is used to detect destination package name and also to fix imports in the resulting source
109+
// OutputFile name which is used to detect destination package name and also to fix imports in the resulting source
111110
OutputFile string
112111

113-
//HeaderTemplate is used to generate package clause and comment over the generated source
112+
// HeaderTemplate is used to generate package clause and comment over the generated source
114113
HeaderTemplate string
115114

116-
//BodyTemplate generates import section, decorator constructor and methods
115+
// BodyTemplate generates import section, decorator constructor and methods
117116
BodyTemplate string
118117

119-
//Vars additional vars that are passed to the templates from the command line
118+
// Vars additional vars that are passed to the templates from the command line
120119
Vars map[string]interface{}
121120

122-
//HeaderVars header specific variables
121+
// HeaderVars header specific variables
123122
HeaderVars map[string]interface{}
124123

125-
//Funcs is a map of helper functions that can be used within a template
124+
// Funcs is a map of helper functions that can be used within a template
126125
Funcs template.FuncMap
127126

128-
//LocalPrefix is a comma-separated string of import path prefixes, which, if set, instructs Process to sort the import
129-
//paths with the given prefixes into another group after 3rd-party packages.
127+
// LocalPrefix is a comma-separated string of import path prefixes, which, if set, instructs Process to sort the import
128+
// paths with the given prefixes into another group after 3rd-party packages.
130129
LocalPrefix string
131130
}
132131

@@ -154,8 +153,10 @@ type processOutput struct {
154153
imports []*ast.ImportSpec
155154
}
156155

157-
var errEmptyInterface = errors.New("interface has no methods")
158-
var errUnexportedMethod = errors.New("unexported method")
156+
var (
157+
errEmptyInterface = errors.New("interface has no methods")
158+
errUnexportedMethod = errors.New("unexported method")
159+
)
159160

160161
// NewGenerator returns Generator initialized with options
161162
func NewGenerator(options Options) (*Generator, error) {
@@ -184,7 +185,7 @@ func NewGenerator(options Options) (*Generator, error) {
184185
return nil, errors.Wrap(err, "failed to load source package")
185186
}
186187

187-
dstPackagePath := filepath.Dir(options.OutputFile)
188+
dstPackagePath := path.Dir(options.OutputFile)
188189
if !strings.HasPrefix(dstPackagePath, "/") && !strings.HasPrefix(dstPackagePath, "./") {
189190
dstPackagePath = "./" + dstPackagePath
190191
}
@@ -265,7 +266,7 @@ func makeImports(imports []*ast.ImportSpec) []string {
265266
func loadDestinationPackage(path string) (*packages.Package, error) {
266267
dstPackage, err := pkg.Load(path)
267268
if err != nil {
268-
//using directory name as a package name
269+
// using directory name as a package name
269270
dstPackage, err = makePackage(path)
270271
}
271272

@@ -274,9 +275,9 @@ func loadDestinationPackage(path string) (*packages.Package, error) {
274275

275276
var errNoPackageName = errors.New("failed to determine the destination package name")
276277

277-
func makePackage(path string) (*packages.Package, error) {
278-
name := filepath.Base(path)
279-
if name == string(filepath.Separator) || name == "." {
278+
func makePackage(packagpath string) (*packages.Package, error) {
279+
name := path.Base(packagpath)
280+
if name == string("/") || name == "." {
280281
return nil, errNoPackageName
281282
}
282283

loader/gitpath_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package loader
22

33
import (
4-
"path/filepath"
4+
"path"
55
"testing"
66

77
"github.com/stretchr/testify/assert"
@@ -24,7 +24,7 @@ func Test_gitRootPath(t *testing.T) {
2424
got, err := gitRootPath()
2525
require.NoError(t, err)
2626

27-
assert.Equal(t, tt.want, filepath.Base(got))
27+
assert.Equal(t, tt.want, path.Base(got))
2828
})
2929
}
3030
}

loader/loader.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"net/http"
88
"os"
9-
"path/filepath"
109
"strings"
1110

1211
"github.com/pkg/errors"
@@ -123,7 +122,7 @@ func (l Loader) absGitPath(path string) (string, error) {
123122
return "", err
124123
}
125124

126-
return filepath.Join(gitRoot, path), nil
125+
return path.Join(gitRoot, path), nil
127126
}
128127

129128
var errTemplateNotFound = errors.New("remote template not found")

pkg/package.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"go/ast"
66
"go/parser"
77
"go/token"
8-
"path/filepath"
8+
"path"
99

1010
"golang.org/x/tools/go/packages"
1111
)
@@ -54,5 +54,5 @@ func Dir(p *packages.Package) string {
5454
return p.PkgPath
5555
}
5656

57-
return filepath.Dir(files[0])
57+
return path.Dir(files[0])
5858
}

0 commit comments

Comments
 (0)