Skip to content

Commit fd06eaf

Browse files
authored
Fix abs dest bug (#45)
1 parent 767ac61 commit fd06eaf

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func rootSetup(cmd *cobra.Command) rootInfo {
7272
logs.Panic("Could not get working directory", err)
7373
}
7474

75-
stateFile, _ := os.LookupEnv(stateFileEnvVar)
75+
stateFile := os.Getenv(stateFileEnvVar)
7676

7777
logs.Debugf("Moqueries root info,"+
7878
" bulk processing state file: %s,"+

generator/moqgen.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,25 @@ func (g *MoqGenerator) Generate(req GenerateRequest) (*dst.File, string, error)
149149
return file, destPath, nil
150150
}
151151

152+
func (g *MoqGenerator) relativePath(workingDir string) (string, error) {
153+
wd, err := g.getwdFn()
154+
if err != nil {
155+
return "", fmt.Errorf("error getting current working directory: %w", err)
156+
}
157+
158+
if workingDir == wd || workingDir == "" {
159+
return ".", nil
160+
}
161+
162+
relPath, err := filepath.Rel(wd, workingDir)
163+
if err != nil {
164+
return "", fmt.Errorf("error getting relative import path from %s to %s: %w",
165+
wd, workingDir, err)
166+
}
167+
168+
return relPath, err
169+
}
170+
152171
func (g *MoqGenerator) outPackagePath(req GenerateRequest, relPath string) (string, error) {
153172
destDir := path.Join(relPath, req.DestinationDir, req.Destination)
154173
if strings.HasSuffix(destDir, ".go") {
@@ -182,25 +201,6 @@ func initializeFile(pkg string) *dst.File {
182201
return file
183202
}
184203

185-
func (g *MoqGenerator) relativePath(workingDir string) (string, error) {
186-
wd, err := g.getwdFn()
187-
if err != nil {
188-
return "", fmt.Errorf("error getting current working directory: %w", err)
189-
}
190-
191-
if workingDir == wd || workingDir == "" {
192-
return ".", nil
193-
}
194-
195-
relPath, err := filepath.Rel(wd, workingDir)
196-
if err != nil {
197-
return "", fmt.Errorf("error getting relative import path from %s to %s: %w",
198-
wd, workingDir, err)
199-
}
200-
201-
return relPath, err
202-
}
203-
204204
func importPath(imp, relPath string) string {
205205
if imp != "." && !strings.HasPrefix(imp, "./") {
206206
return imp
@@ -367,5 +367,13 @@ func destinationPath(req GenerateRequest, relPath string) (string, error) {
367367
destPath += ".go"
368368
}
369369

370+
if filepath.IsAbs(destPath) {
371+
return destPath, nil
372+
}
373+
374+
if filepath.IsAbs(req.DestinationDir) {
375+
return filepath.Join(req.DestinationDir, destPath), nil
376+
}
377+
370378
return filepath.Join(relPath, req.DestinationDir, destPath), nil
371379
}

generator/moqgen_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,40 @@ func TestMoqGenerator(t *testing.T) {
348348
typePath: ".",
349349
destPath: "file_test.go",
350350
},
351+
"gives a sane path when destination is an absolute path": {
352+
request: generator.GenerateRequest{
353+
Types: types,
354+
Export: false,
355+
Destination: "/exactly-here/file_test.go",
356+
Package: "",
357+
Import: ".",
358+
TestImport: false,
359+
WorkingDir: "/some-nice-path",
360+
},
361+
findPkgDir: "exactly-here",
362+
findPkgOut: "thispkg",
363+
outPkgPath: "thispkg_test",
364+
getwdDir: "/some-nice-path",
365+
typePath: ".",
366+
destPath: "/exactly-here/file_test.go",
367+
},
368+
"gives a sane path when destination dir is an absolute path": {
369+
request: generator.GenerateRequest{
370+
Types: types,
371+
Export: false,
372+
DestinationDir: "/exactly-here",
373+
Package: "",
374+
Import: ".",
375+
TestImport: false,
376+
WorkingDir: "/some-nice-path",
377+
},
378+
findPkgDir: "exactly-here",
379+
findPkgOut: "thispkg",
380+
outPkgPath: "thispkg_test",
381+
getwdDir: "/some-nice-path",
382+
typePath: ".",
383+
destPath: "/exactly-here/moq_publicinterface_privateinterface_test.go",
384+
},
351385
"req working dir not set": {
352386
request: generator.GenerateRequest{
353387
Types: types,

0 commit comments

Comments
 (0)