Skip to content

Commit

Permalink
Merge pull request #822 from 99designs/windows-import-path-loop
Browse files Browse the repository at this point in the history
fix for windows infinite loop
  • Loading branch information
vektah authored Aug 8, 2019
2 parents 12893fa + a861aa5 commit 5628169
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
8 changes: 6 additions & 2 deletions client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (p *Client) WebsocketWithPayload(query string, initPayload map[string]inter
url := strings.Replace(p.url, "http://", "ws://", -1)
url = strings.Replace(url, "https://", "wss://", -1)

c, _, err := websocket.DefaultDialer.Dial(url, nil)
c, resp, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
return errorSubscription(fmt.Errorf("dial: %s", err.Error()))
}
Expand Down Expand Up @@ -92,7 +92,11 @@ func (p *Client) WebsocketWithPayload(query string, initPayload map[string]inter
}

return &Subscription{
Close: c.Close,
Close: func() error {
c.Close()
resp.Body.Close()
return nil
},
Next: func(response interface{}) error {
var op operationMessage
err := c.ReadJSON(&op)
Expand Down
4 changes: 3 additions & 1 deletion handler/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ func TestWebsocketInitFunc(t *testing.T) {
}

func wsConnect(url string) *websocket.Conn {
c, _, err := websocket.DefaultDialer.Dial(strings.Replace(url, "http://", "ws://", -1), nil)
c, resp, err := websocket.DefaultDialer.Dial(strings.Replace(url, "http://", "ws://", -1), nil)
if err != nil {
panic(err)
}
_ = resp.Body.Close()

return c
}

Expand Down
9 changes: 5 additions & 4 deletions internal/code/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,23 @@ 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 {
// Walked all the way to the root and didnt find anything :'(
break
}
modDir = parentDir
}

for _, gopath := range gopaths {
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 5628169

Please sign in to comment.