Skip to content

Commit

Permalink
fix for windows infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Aug 8, 2019
1 parent 5fafe79 commit d0e002d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 5 additions & 4 deletions internal/code/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,22 @@ func ImportPathForDir(dir string) (res string) {
modDir := dir
assumedPart := ""
for {
f, err := ioutil.ReadFile(filepath.Join(modDir, "/", "go.mod"))
f, err := ioutil.ReadFile(filepath.Join(modDir, "go.mod"))
if err == nil {
// found it, stop searching
return string(modregex.FindSubmatch(f)[1]) + assumedPart
}

assumedPart = "/" + filepath.Base(modDir) + assumedPart
modDir, err = filepath.Abs(filepath.Join(modDir, ".."))
parentDir, err := filepath.Abs(filepath.Join(modDir, ".."))
if err != nil {
panic(err)
}

// Walked all the way to the root and didnt find anything :'(
if modDir == "/" {
if parentDir == modDir {
break
} else {
modDir = parentDir
}
}

Expand Down
7 changes: 7 additions & 0 deletions internal/code/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package code
import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -21,6 +22,12 @@ func TestImportPathForDir(t *testing.T) {

// directory does not exist
assert.Equal(t, "github.com/99designs/gqlgen/dos", ImportPathForDir(filepath.Join(wd, "..", "..", "dos")))

if runtime.GOOS == "windows" {
assert.Equal(t, "", ImportPathForDir("C:/doesnotexist"))
} else {
assert.Equal(t, "", ImportPathForDir("/doesnotexist"))
}
}

func TestNameForPackage(t *testing.T) {
Expand Down

0 comments on commit d0e002d

Please sign in to comment.