Skip to content

Commit 30bae18

Browse files
authored
replace io/ioutil with io, os package (#199)
1 parent 892b4f3 commit 30bae18

3 files changed

Lines changed: 65 additions & 41 deletions

File tree

cli_test.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"fmt"
2222
"io"
23-
"io/ioutil"
2423
"strings"
2524
"testing"
2625
"time"
@@ -46,15 +45,18 @@ func TestBuildCommands(t *testing.T) {
4645
}{
4746
{Input: `SELECT * FROM t1;`, Expected: []*command{{&SelectStatement{"SELECT * FROM t1"}, false}}},
4847
{Input: `CREATE TABLE t1;`, Expected: []*command{{&BulkDdlStatement{[]string{"CREATE TABLE t1"}}, false}}},
49-
{Input: `CREATE TABLE t1(pk INT64) PRIMARY KEY(pk); ALTER TABLE t1 ADD COLUMN col INT64; CREATE INDEX i1 ON t1(col); DROP INDEX i1; DROP TABLE t1;`,
48+
{
49+
Input: `CREATE TABLE t1(pk INT64) PRIMARY KEY(pk); ALTER TABLE t1 ADD COLUMN col INT64; CREATE INDEX i1 ON t1(col); DROP INDEX i1; DROP TABLE t1;`,
5050
Expected: []*command{{&BulkDdlStatement{[]string{
5151
"CREATE TABLE t1(pk INT64) PRIMARY KEY(pk)",
5252
"ALTER TABLE t1 ADD COLUMN col INT64",
5353
"CREATE INDEX i1 ON t1(col)",
5454
"DROP INDEX i1",
5555
"DROP TABLE t1",
56-
}}, false}}},
57-
{Input: `CREATE TABLE t1(pk INT64) PRIMARY KEY(pk);
56+
}}, false}},
57+
},
58+
{
59+
Input: `CREATE TABLE t1(pk INT64) PRIMARY KEY(pk);
5860
CREATE TABLE t2(pk INT64) PRIMARY KEY(pk);
5961
SELECT * FROM t1\G
6062
DROP TABLE t1;
@@ -65,7 +67,8 @@ func TestBuildCommands(t *testing.T) {
6567
{&SelectStatement{"SELECT * FROM t1"}, true},
6668
{&BulkDdlStatement{[]string{"DROP TABLE t1", "DROP TABLE t2"}}, false},
6769
{&SelectStatement{"SELECT 1"}, false},
68-
}},
70+
},
71+
},
6972
{
7073
Input: `
7174
CREATE TABLE t1(pk INT64 /* NOT NULL*/, col INT64) PRIMARY KEY(pk);
@@ -79,7 +82,8 @@ func TestBuildCommands(t *testing.T) {
7982
{&DmlStatement{"UPDATE t1 SET col = /* pk + */ col + 1 WHERE TRUE"}, false},
8083
{&DmlStatement{"DELETE t1 WHERE TRUE /* AND pk = 1 */"}, false},
8184
{&SelectStatement{"SELECT 0x1/**/A"}, false},
82-
}},
85+
},
86+
},
8387
{
8488
// spanner-cli don't permit empty statements.
8589
Input: `SELECT 1; /* comment */; SELECT 2`,
@@ -94,7 +98,8 @@ func TestBuildCommands(t *testing.T) {
9498
Input: `SELECT 1; /* comment */`,
9599
Expected: []*command{
96100
{&SelectStatement{"SELECT 1"}, false},
97-
}},
101+
},
102+
},
98103
}
99104

100105
for _, test := range tests {
@@ -164,9 +169,9 @@ func TestReadInteractiveInput(t *testing.T) {
164169
} {
165170
t.Run(tt.desc, func(t *testing.T) {
166171
rl, err := readline.NewEx(&readline.Config{
167-
Stdin: ioutil.NopCloser(strings.NewReader(tt.input)),
168-
Stdout: ioutil.Discard,
169-
Stderr: ioutil.Discard,
172+
Stdin: io.NopCloser(strings.NewReader(tt.input)),
173+
Stdout: io.Discard,
174+
Stderr: io.Discard,
170175
})
171176
if err != nil {
172177
t.Fatalf("unexpected readline.NewEx() error: %v", err)
@@ -189,8 +194,8 @@ func TestPrintResult(t *testing.T) {
189194
result := &Result{
190195
ColumnNames: []string{"foo", "bar"},
191196
Rows: []Row{
192-
Row{[]string{"1", "2"}},
193-
Row{[]string{"3", "4"}},
197+
{[]string{"1", "2"}},
198+
{[]string{"3", "4"}},
194199
},
195200
IsMutation: false,
196201
}
@@ -216,8 +221,8 @@ func TestPrintResult(t *testing.T) {
216221
result := &Result{
217222
ColumnNames: []string{"foo", "bar"},
218223
Rows: []Row{
219-
Row{[]string{"1", "2"}},
220-
Row{[]string{"3", "4"}},
224+
{[]string{"1", "2"}},
225+
{[]string{"3", "4"}},
221226
},
222227
IsMutation: false,
223228
}
@@ -243,8 +248,8 @@ bar: 4
243248
result := &Result{
244249
ColumnNames: []string{"foo", "bar"},
245250
Rows: []Row{
246-
Row{[]string{"1", "2"}},
247-
Row{[]string{"3", "4"}},
251+
{[]string{"1", "2"}},
252+
{[]string{"3", "4"}},
248253
},
249254
IsMutation: false,
250255
}

main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package main
1919

2020
import (
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323
"os"
2424
"os/user"
2525
"path/filepath"
@@ -119,13 +119,13 @@ func main() {
119119
if opts.Execute != "" {
120120
input = opts.Execute
121121
} else if opts.File == "-" {
122-
b, err := ioutil.ReadAll(os.Stdin)
122+
b, err := io.ReadAll(os.Stdin)
123123
if err != nil {
124124
exitf("Read from stdin failed: %v", err)
125125
}
126126
input = string(b)
127127
} else if opts.File != "" {
128-
b, err := ioutil.ReadFile(opts.File)
128+
b, err := os.ReadFile(opts.File)
129129
if err != nil {
130130
exitf("Read from file %v failed: %v", opts.File, err)
131131
}
@@ -182,13 +182,13 @@ func readCredentialFile(filepath string) ([]byte, error) {
182182
if err != nil {
183183
return nil, err
184184
}
185-
return ioutil.ReadAll(f)
185+
return io.ReadAll(f)
186186
}
187187

188188
func readStdin() (string, error) {
189189
stat, _ := os.Stdin.Stat()
190190
if (stat.Mode() & os.ModeCharDevice) == 0 {
191-
b, err := ioutil.ReadAll(os.Stdin)
191+
b, err := io.ReadAll(os.Stdin)
192192
if err != nil {
193193
return "", err
194194
}

query_plan_test.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"io/ioutil"
4+
"os"
55
"testing"
66

77
"github.com/google/go-cmp/cmp"
@@ -67,7 +67,8 @@ func TestRenderTreeUsingTestdataPlans(t *testing.T) {
6767
ID: 7,
6868
Text: " +- Index Scan (Index: SingersByFirstLastName)",
6969
},
70-
}},
70+
},
71+
},
7172
{
7273
/*
7374
Original Query:
@@ -107,7 +108,8 @@ func TestRenderTreeUsingTestdataPlans(t *testing.T) {
107108
ID: 9,
108109
Text: " +- Index Scan (Full scan: true, Index: SongsBySingerAlbumSongNameDesc)",
109110
},
110-
}},
111+
},
112+
},
111113
{
112114
/*
113115
Original Query: https://cloud.google.com/spanner/docs/query-execution-operators?hl=en#array_subqueries
@@ -158,7 +160,8 @@ func TestRenderTreeUsingTestdataPlans(t *testing.T) {
158160
ID: 11,
159161
Text: " +- Index Scan (Index: ConcertsBySingerId)",
160162
},
161-
}},
163+
},
164+
},
162165
{
163166
/*
164167
Original Query: https://cloud.google.com/spanner/docs/query-execution-operators?hl=en#scalar_subqueries
@@ -219,7 +222,8 @@ func TestRenderTreeUsingTestdataPlans(t *testing.T) {
219222
ID: 16,
220223
Text: " +- Table Scan (Full scan: true, Table: Songs)",
221224
},
222-
}},
225+
},
226+
},
223227
{
224228
/*
225229
Original Query:
@@ -367,7 +371,7 @@ func TestRenderTreeUsingTestdataPlans(t *testing.T) {
367371
},
368372
} {
369373
t.Run(test.title, func(t *testing.T) {
370-
b, err := ioutil.ReadFile(test.file)
374+
b, err := os.ReadFile(test.file)
371375
if err != nil {
372376
t.Fatal(err)
373377
}
@@ -480,7 +484,8 @@ func TestRenderTreeWithStats(t *testing.T) {
480484
Execution: "1",
481485
LatencyTotal: "1 msec",
482486
},
483-
}},
487+
},
488+
},
484489
} {
485490
t.Run(test.title, func(t *testing.T) {
486491
tree := BuildQueryPlanTree(test.plan, 0)
@@ -494,13 +499,15 @@ func TestRenderTreeWithStats(t *testing.T) {
494499
})
495500
}
496501
}
502+
497503
func TestNodeString(t *testing.T) {
498504
for _, test := range []struct {
499505
title string
500506
node *Node
501507
want string
502508
}{
503-
{"Distributed Union with call_type=Local",
509+
{
510+
"Distributed Union with call_type=Local",
504511
&Node{PlanNode: &spanner.PlanNode{
505512
DisplayName: "Distributed Union",
506513
Metadata: mustNewStruct(map[string]interface{}{
@@ -509,52 +516,64 @@ func TestNodeString(t *testing.T) {
509516
}),
510517
}}, "Local Distributed Union",
511518
},
512-
{"Scan with scan_type=IndexScan and Full scan=true",
519+
{
520+
"Scan with scan_type=IndexScan and Full scan=true",
513521
&Node{PlanNode: &spanner.PlanNode{
514522
DisplayName: "Scan",
515523
Metadata: mustNewStruct(map[string]interface{}{
516524
"scan_type": "IndexScan",
517525
"scan_target": "SongsBySongName",
518526
"Full scan": "true",
519527
}),
520-
}}, "Index Scan (Full scan: true, Index: SongsBySongName)"},
521-
{"Scan with scan_type=TableScan",
528+
}}, "Index Scan (Full scan: true, Index: SongsBySongName)",
529+
},
530+
{
531+
"Scan with scan_type=TableScan",
522532
&Node{PlanNode: &spanner.PlanNode{
523533
DisplayName: "Scan",
524534
Metadata: mustNewStruct(map[string]interface{}{
525535
"scan_type": "TableScan",
526536
"scan_target": "Songs",
527537
}),
528-
}}, "Table Scan (Table: Songs)"},
529-
{"Scan with scan_type=BatchScan",
538+
}}, "Table Scan (Table: Songs)",
539+
},
540+
{
541+
"Scan with scan_type=BatchScan",
530542
&Node{PlanNode: &spanner.PlanNode{
531543
DisplayName: "Scan",
532544
Metadata: mustNewStruct(map[string]interface{}{
533545
"scan_type": "BatchScan",
534546
"scan_target": "$v2",
535547
}),
536-
}}, "Batch Scan (Batch: $v2)"},
537-
{"Sort Limit with call_type=Local",
548+
}}, "Batch Scan (Batch: $v2)",
549+
},
550+
{
551+
"Sort Limit with call_type=Local",
538552
&Node{PlanNode: &spanner.PlanNode{
539553
DisplayName: "Sort Limit",
540554
Metadata: mustNewStruct(map[string]interface{}{
541555
"call_type": "Local",
542556
}),
543-
}}, "Local Sort Limit"},
544-
{"Sort Limit with call_type=Global",
557+
}}, "Local Sort Limit",
558+
},
559+
{
560+
"Sort Limit with call_type=Global",
545561
&Node{PlanNode: &spanner.PlanNode{
546562
DisplayName: "Sort Limit",
547563
Metadata: mustNewStruct(map[string]interface{}{
548564
"call_type": "Global",
549565
}),
550-
}}, "Global Sort Limit"},
551-
{"Aggregate with iterator_type=Stream",
566+
}}, "Global Sort Limit",
567+
},
568+
{
569+
"Aggregate with iterator_type=Stream",
552570
&Node{PlanNode: &spanner.PlanNode{
553571
DisplayName: "Aggregate",
554572
Metadata: mustNewStruct(map[string]interface{}{
555573
"iterator_type": "Stream",
556574
}),
557-
}}, "Stream Aggregate"},
575+
}}, "Stream Aggregate",
576+
},
558577
} {
559578
if got := test.node.String(); got != test.want {
560579
t.Errorf("%s: node.String() = %q but want %q", test.title, got, test.want)

0 commit comments

Comments
 (0)