-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparser_extra_test.go
More file actions
193 lines (173 loc) · 5.64 KB
/
Copy pathparser_extra_test.go
File metadata and controls
193 lines (173 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package publiccode
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
// errReader always returns an error when Read is called.
type errReader struct{}
func (e errReader) Read(_ []byte) (int, error) {
return 0, errors.New("read error")
}
func TestNewParserBaseURL(t *testing.T) {
p, err := NewParser(ParserConfig{BaseURL: "https://example.com"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if p.baseURL == nil {
t.Error("expected baseURL to be set")
}
if p.baseURL.Host != "example.com" {
t.Errorf("unexpected host: %s", p.baseURL.Host)
}
}
func TestNewParserDisableExternalChecksImpliesDisableNetwork(t *testing.T) {
p, err := NewParser(ParserConfig{DisableExternalChecks: true})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !p.disableNetwork {
t.Error("expected disableNetwork to be true when DisableExternalChecks is true")
}
}
func TestParseNonexistentFile(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
_, err := p.Parse("/nonexistent/path/file.yml")
if err == nil {
t.Error("expected error for nonexistent file")
}
if !strings.Contains(err.Error(), "can't open file") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestParseHTTPError(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
// Port 1 is not listening, so this should fail with a connection error.
_, err := p.Parse("http://localhost:1/publiccode.yml")
if err == nil {
t.Error("expected error for unreachable HTTP server")
}
if !strings.Contains(err.Error(), "can't GET") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseStreamInvalidUTF8(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
// Invalid UTF-8 bytes.
invalid := []byte{0xff, 0xfe, 0x00}
_, err := p.ParseStream(bytes.NewReader(invalid))
if err == nil {
t.Fatal("expected error for invalid UTF-8")
}
if !strings.Contains(err.Error(), "Invalid UTF-8") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseStreamMultiDoc(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
yaml := "---\na: 1\n---\nb: 2\n"
_, err := p.ParseStream(strings.NewReader(yaml))
if err == nil {
t.Fatal("expected error for multi-doc YAML")
}
if !strings.Contains(err.Error(), "multiple YAML documents") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseStreamEmptyYAML(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
_, err := p.ParseStream(strings.NewReader(""))
if err == nil {
t.Fatal("expected error for empty YAML")
}
if !strings.Contains(err.Error(), "publiccodeYmlVersion") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseStreamVersionNotString(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
yaml := "publiccodeYmlVersion: 123\n"
_, err := p.ParseStream(strings.NewReader(yaml))
if err == nil {
t.Fatal("expected error for non-string version")
}
if !strings.Contains(err.Error(), "publiccodeYmlVersion") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseStreamUnsupportedVersion(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
yaml := "publiccodeYmlVersion: \"99.99\"\n"
_, err := p.ParseStream(strings.NewReader(yaml))
if err == nil {
t.Fatal("expected error for unsupported version")
}
if !strings.Contains(err.Error(), "unsupported version") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseStreamSyntaxError(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
// Malformed YAML.
yaml := "key: [\n"
_, err := p.ParseStream(strings.NewReader(yaml))
if err == nil {
t.Fatal("expected error for malformed YAML")
}
}
func TestParseStreamReaderError(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
_, err := p.ParseStream(errReader{})
if err == nil {
t.Fatal("expected error from reader failure")
}
if !strings.Contains(err.Error(), "Can't read the stream") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseStreamMissingVersion(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
// Valid YAML but no publiccodeYmlVersion key.
yaml := "name: test\n"
_, err := p.ParseStream(strings.NewReader(yaml))
if err == nil {
t.Fatal("expected error for missing publiccodeYmlVersion")
}
if !strings.Contains(err.Error(), "publiccodeYmlVersion") {
t.Errorf("unexpected error: %v", err)
}
}
func TestParseStreamWithBaseURL(t *testing.T) {
// A parser with BaseURL set exercises the p.baseURL != nil branch in parseStream.
p, _ := NewParser(ParserConfig{
BaseURL: "https://example.com/org/repo/",
DisableNetwork: true,
})
// Minimal YAML: just a version so we get past early returns into base-URL computation.
_, _ = p.ParseStream(strings.NewReader("publiccodeYmlVersion: \"0\"\n"))
}
func TestParseStreamNetworkURLUnknownVCS(t *testing.T) {
// With DisableNetwork=false, no BaseURL, and a URL from an unknown VCS host,
// vcsurl.GetRawRoot fails and parseStream returns early before any network checks.
p, _ := NewParser(ParserConfig{DisableNetwork: false})
yaml := "publiccodeYmlVersion: \"0\"\nurl: \"https://example.com/org/repo.git\"\n"
_, _ = p.ParseStream(strings.NewReader(yaml))
}
func TestParseHTTPServer(t *testing.T) {
content, err := os.ReadFile("testdata/v0/valid/valid.yml")
if err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/yaml")
_, _ = w.Write(content)
}))
defer srv.Close()
p, _ := NewParser(ParserConfig{DisableNetwork: true})
// Should successfully parse the YAML served over HTTP.
_, _ = p.Parse(srv.URL + "/publiccode.yml")
}