1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:54:42 -07:00

go/vcs: fix bug in remote package discovery

The parser was assuming it would find <body> or </head>.
If the entire response is just <meta> tags, it finds EOF and
treats that as an error. It's not.

This is the same change as in https://golang.org/cl/68520044.

Fixes #9556.

Change-Id: If51ed36e7364c15788311039caf8323eb5fe9a6c
Reviewed-on: https://go-review.googlesource.com/2650
Reviewed-by: Minux Ma <minux@golang.org>
This commit is contained in:
Dmitri Shuralyov 2015-01-09 23:26:36 -08:00 committed by Minux Ma
parent 9468e480c8
commit c10e262955
2 changed files with 47 additions and 0 deletions

View File

@ -36,6 +36,9 @@ func parseMetaGoImports(r io.Reader) (imports []metaImport, err error) {
for {
t, err = d.Token()
if err != nil {
if err == io.EOF {
err = nil
}
return
}
if e, ok := t.(xml.StartElement); ok && strings.EqualFold(e.Name.Local, "body") {

View File

@ -8,6 +8,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
@ -84,3 +86,45 @@ func TestFromDir(t *testing.T) {
os.RemoveAll(test.path)
}
}
var parseMetaGoImportsTests = []struct {
in string
out []metaImport
}{
{
`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">`,
[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
},
{
`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
<meta name="go-import" content="baz/quux git http://github.com/rsc/baz/quux">`,
[]metaImport{
{"foo/bar", "git", "https://github.com/rsc/foo/bar"},
{"baz/quux", "git", "http://github.com/rsc/baz/quux"},
},
},
{
`<head>
<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
</head>`,
[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
},
{
`<meta name="go-import" content="foo/bar git https://github.com/rsc/foo/bar">
<body>`,
[]metaImport{{"foo/bar", "git", "https://github.com/rsc/foo/bar"}},
},
}
func TestParseMetaGoImports(t *testing.T) {
for i, tt := range parseMetaGoImportsTests {
out, err := parseMetaGoImports(strings.NewReader(tt.in))
if err != nil {
t.Errorf("test#%d: %v", i, err)
continue
}
if !reflect.DeepEqual(out, tt.out) {
t.Errorf("test#%d:\n\thave %q\n\twant %q", i, out, tt.out)
}
}
}