Skip to content

Commit 28f4e76

Browse files
kssilveiratxtpbfmt-copybara-robot
authored andcommitted
Avoid importing cmp in a non-testing context.
PiperOrigin-RevId: 775598019
1 parent c917e96 commit 28f4e76

2 files changed

Lines changed: 106 additions & 5 deletions

File tree

cmd/txtpbfmt/fmt.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"bufio"
77
"bytes"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"os"
1111
"strings"
1212

@@ -39,9 +39,9 @@ const stdinPlaceholderPath = "<stdin>"
3939

4040
func read(path string) ([]byte, error) {
4141
if path == stdinPlaceholderPath {
42-
return ioutil.ReadAll(bufio.NewReader(os.Stdin))
42+
return io.ReadAll(bufio.NewReader(os.Stdin))
4343
}
44-
return ioutil.ReadFile(path)
44+
return os.ReadFile(path)
4545
}
4646

4747
func errorf(format string, args ...interface{}) {
@@ -119,7 +119,7 @@ func write(path string, content, newContent []byte) error {
119119
fmt.Println(string(newContent))
120120
return nil
121121
}
122-
if err := ioutil.WriteFile(path, newContent, 0664); err != nil {
122+
if err := os.WriteFile(path, newContent, 0664); err != nil {
123123
return err
124124
}
125125
return nil

unquote/unquote_test.go

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,62 @@ func TestErrorHandling(t *testing.T) {
8888
in: "`foo`",
8989
wantErr: "invalid quote character `",
9090
},
91+
{
92+
in: `"foo\x"`,
93+
wantErr: `\x requires 2 following digits`,
94+
},
95+
{
96+
in: `"foo\xH"`,
97+
wantErr: `\x requires 2 following digits`,
98+
},
99+
{
100+
in: `"foo\xHH"`,
101+
wantErr: `\xHH contains non-hexadecimal digits`,
102+
},
103+
{
104+
in: `"foo\u"`,
105+
wantErr: `\u requires 4 following digits`,
106+
},
107+
{
108+
in: `"foo\uH"`,
109+
wantErr: `\u requires 4 following digits`,
110+
},
111+
{
112+
in: `"foo\uHHHH"`,
113+
wantErr: `\uHHHH contains non-hexadecimal digits`,
114+
},
115+
{
116+
in: `"foo\U"`,
117+
wantErr: `\U requires 8 following digits`,
118+
},
119+
{
120+
in: `"foo\UH"`,
121+
wantErr: `\U requires 8 following digits`,
122+
},
123+
{
124+
in: `"foo\UHHHHHHHH"`,
125+
wantErr: `\UHHHHHHHH contains non-hexadecimal digits`,
126+
},
127+
{
128+
in: `"foo\UFFFFFFFF"`,
129+
wantErr: `\UFFFFFFFF is not a valid Unicode code point`,
130+
},
131+
{
132+
in: `"foo\0"`,
133+
wantErr: `\0 requires 2 following digits`,
134+
},
135+
{
136+
in: `"foo\0H"`,
137+
wantErr: `\0 requires 2 following digits`,
138+
},
139+
{
140+
in: `"foo\0HH"`,
141+
wantErr: `\0HH contains non-octal digits`,
142+
},
143+
{
144+
in: `"foo\y"`,
145+
wantErr: `unknown escape \y`,
146+
},
91147
}
92148

93149
for _, input := range inputs {
@@ -97,10 +153,55 @@ func TestErrorHandling(t *testing.T) {
97153
if err == nil || !strings.Contains(err.Error(), input.wantErr) {
98154
t.Errorf("Unquote(%s) got %v, want err to contain %q", input.in, err, input.wantErr)
99155
}
156+
}
157+
}
100158

101-
_, _, err = Raw(node)
159+
func TestErrorHandlingRaw(t *testing.T) {
160+
inputs := []struct {
161+
in string
162+
wantErr string
163+
}{
164+
{
165+
in: `"value`,
166+
wantErr: "unmatched quote",
167+
},
168+
{
169+
in: `"`,
170+
wantErr: "not a quoted string",
171+
},
172+
{
173+
in: "`foo`",
174+
wantErr: "invalid quote character `",
175+
},
176+
}
177+
178+
for _, input := range inputs {
179+
node := &ast.Node{Name: "name", Values: []*ast.Value{{Value: input.in}}}
180+
181+
_, _, err := Raw(node)
102182
if err == nil || !strings.Contains(err.Error(), input.wantErr) {
103183
t.Errorf("Raw(%s) got %v, want err to contain %q", input.in, err, input.wantErr)
104184
}
105185
}
106186
}
187+
188+
func TestErrorHandlingMisc(t *testing.T) {
189+
inputs := []struct {
190+
in string
191+
wantErr string
192+
}{
193+
{
194+
in: `"value`,
195+
wantErr: "unmatched quote",
196+
},
197+
}
198+
199+
for _, input := range inputs {
200+
node := &ast.Node{Name: "name", Values: []*ast.Value{{Value: input.in}}}
201+
202+
_, _, err := Unquote(node)
203+
if err == nil || !strings.Contains(err.Error(), input.wantErr) {
204+
t.Errorf("Unquote(%s) got %v, want err to contain %q", input.in, err, input.wantErr)
205+
}
206+
}
207+
}

0 commit comments

Comments
 (0)