fix In goctl new api, occur error invalid character 'A' looking for beginning of value (#3357)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package ctx
|
package ctx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -94,8 +95,11 @@ func getRealModule(workDir string, execRun execx.RunFunc) (*Module, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func decodePackages(rc io.Reader) ([]Module, error) {
|
func decodePackages(rc io.Reader) ([]Module, error) {
|
||||||
|
r := bufio.NewReader(rc)
|
||||||
|
_, _ = r.ReadSlice('{')
|
||||||
|
_ = r.UnreadByte()
|
||||||
var modules []Module
|
var modules []Module
|
||||||
decoder := json.NewDecoder(rc)
|
decoder := json.NewDecoder(r)
|
||||||
for decoder.More() {
|
for decoder.More() {
|
||||||
var m Module
|
var m Module
|
||||||
if err := decoder.Decode(&m); err != nil {
|
if err := decoder.Decode(&m); err != nil {
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ package ctx
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"go/build"
|
"go/build"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -110,3 +112,90 @@ func Test_getRealModule(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecodePackages(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
data io.Reader
|
||||||
|
want []Module
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "single module",
|
||||||
|
data: strings.NewReader(`{
|
||||||
|
"Path":"foo",
|
||||||
|
"Dir":"/home/foo",
|
||||||
|
"GoMod":"/home/foo/go.mod",
|
||||||
|
"GoVersion":"go1.16"}`),
|
||||||
|
want: []Module{
|
||||||
|
{
|
||||||
|
Path: "foo",
|
||||||
|
Dir: "/home/foo",
|
||||||
|
GoMod: "/home/foo/go.mod",
|
||||||
|
GoVersion: "go1.16",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "go work multiple modules",
|
||||||
|
data: strings.NewReader(`
|
||||||
|
{
|
||||||
|
"Path":"foo",
|
||||||
|
"Dir":"/home/foo",
|
||||||
|
"GoMod":"/home/foo/go.mod",
|
||||||
|
"GoVersion":"go1.18"
|
||||||
|
}
|
||||||
|
{
|
||||||
|
"Path":"bar",
|
||||||
|
"Dir":"/home/bar",
|
||||||
|
"GoMod":"/home/bar/go.mod",
|
||||||
|
"GoVersion":"go1.18"
|
||||||
|
}`),
|
||||||
|
want: []Module{
|
||||||
|
{
|
||||||
|
Path: "foo",
|
||||||
|
Dir: "/home/foo",
|
||||||
|
GoMod: "/home/foo/go.mod",
|
||||||
|
GoVersion: "go1.18",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Path: "bar",
|
||||||
|
Dir: "/home/bar",
|
||||||
|
GoMod: "/home/bar/go.mod",
|
||||||
|
GoVersion: "go1.18",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "There are extra characters at the beginning",
|
||||||
|
data: strings.NewReader(`Active code page: 65001
|
||||||
|
{
|
||||||
|
"Path":"foo",
|
||||||
|
"Dir":"/home/foo",
|
||||||
|
"GoMod":"/home/foo/go.mod",
|
||||||
|
"GoVersion":"go1.18"
|
||||||
|
}`),
|
||||||
|
want: []Module{
|
||||||
|
{
|
||||||
|
Path: "foo",
|
||||||
|
Dir: "/home/foo",
|
||||||
|
GoMod: "/home/foo/go.mod",
|
||||||
|
GoVersion: "go1.18",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
result, err := decodePackages(tt.data)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("decodePackages() error %v,wantErr = %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(result, tt.want) {
|
||||||
|
t.Errorf("decodePackages() = %v,want %v", result, tt.want)
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user