Skip to content

Commit 5c8f16b

Browse files
authored
Merge pull request #1678 from ArangoGutierrez/ioutil_depre
Deprecation of package ioutil in Go 1.16
2 parents 4559ae9 + 0a5e5da commit 5c8f16b

13 files changed

+62
-71
lines changed

altsrc/json_command_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package altsrc
22

33
import (
44
"flag"
5-
"io/ioutil"
65
"os"
76
"testing"
87

@@ -318,7 +317,7 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *tes
318317
}
319318

320319
func writeTempFile(t *testing.T, name string, content string) func() {
321-
if err := ioutil.WriteFile(name, []byte(content), 0666); err != nil {
320+
if err := os.WriteFile(name, []byte(content), 0666); err != nil {
322321
t.Fatalf("cannot write %q: %v", name, err)
323322
}
324323
return func() {

altsrc/json_source_context.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"strings"
98
"time"
109

@@ -40,7 +39,7 @@ func NewJSONSourceFromFile(f string) (InputSourceContext, error) {
4039
// NewJSONSourceFromReader returns an InputSourceContext suitable for
4140
// retrieving config variables from an io.Reader that returns JSON data.
4241
func NewJSONSourceFromReader(r io.Reader) (InputSourceContext, error) {
43-
data, err := ioutil.ReadAll(r)
42+
data, err := io.ReadAll(r)
4443
if err != nil {
4544
return nil, err
4645
}

altsrc/toml_command_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package altsrc
22

33
import (
44
"flag"
5-
"io/ioutil"
65
"os"
76
"testing"
87

@@ -12,7 +11,7 @@ import (
1211
func TestCommandTomFileTest(t *testing.T) {
1312
app := &cli.App{}
1413
set := flag.NewFlagSet("test", 0)
15-
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
14+
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
1615
defer os.Remove("current.toml")
1716
test := []string{"test-cmd", "--load", "current.toml"}
1817
_ = set.Parse(test)
@@ -42,7 +41,7 @@ func TestCommandTomFileTest(t *testing.T) {
4241
func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
4342
app := &cli.App{}
4443
set := flag.NewFlagSet("test", 0)
45-
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
44+
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
4645
defer os.Remove("current.toml")
4746

4847
_ = os.Setenv("THE_TEST", "10")
@@ -76,7 +75,7 @@ func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
7675
func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
7776
app := &cli.App{}
7877
set := flag.NewFlagSet("test", 0)
79-
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
78+
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
8079
defer os.Remove("current.toml")
8180

8281
_ = os.Setenv("THE_TEST", "10")
@@ -110,7 +109,7 @@ func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
110109
func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
111110
app := &cli.App{}
112111
set := flag.NewFlagSet("test", 0)
113-
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
112+
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
114113
defer os.Remove("current.toml")
115114

116115
test := []string{"test-cmd", "--load", "current.toml", "--test", "7"}
@@ -142,7 +141,7 @@ func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
142141
func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
143142
app := &cli.App{}
144143
set := flag.NewFlagSet("test", 0)
145-
_ = ioutil.WriteFile("current.toml", []byte(`[top]
144+
_ = os.WriteFile("current.toml", []byte(`[top]
146145
test = 15`), 0666)
147146
defer os.Remove("current.toml")
148147

@@ -175,7 +174,7 @@ func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
175174
func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
176175
app := &cli.App{}
177176
set := flag.NewFlagSet("test", 0)
178-
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
177+
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
179178
defer os.Remove("current.toml")
180179

181180
test := []string{"test-cmd", "--load", "current.toml"}
@@ -207,7 +206,7 @@ func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
207206
func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
208207
app := &cli.App{}
209208
set := flag.NewFlagSet("test", 0)
210-
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
209+
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
211210
defer os.Remove("current.toml")
212211

213212
test := []string{"test-cmd", "--load", "current.toml"}
@@ -239,7 +238,7 @@ func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
239238
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T) {
240239
app := &cli.App{}
241240
set := flag.NewFlagSet("test", 0)
242-
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
241+
_ = os.WriteFile("current.toml", []byte("test = 15"), 0666)
243242
defer os.Remove("current.toml")
244243

245244
_ = os.Setenv("THE_TEST", "11")
@@ -273,7 +272,7 @@ func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T
273272
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *testing.T) {
274273
app := &cli.App{}
275274
set := flag.NewFlagSet("test", 0)
276-
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
275+
_ = os.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
277276
defer os.Remove("current.toml")
278277

279278
_ = os.Setenv("THE_TEST", "11")

altsrc/yaml_command_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package altsrc
22

33
import (
44
"flag"
5-
"io/ioutil"
65
"os"
76
"testing"
87

@@ -12,7 +11,7 @@ import (
1211
func TestCommandYamlFileTest(t *testing.T) {
1312
app := &cli.App{}
1413
set := flag.NewFlagSet("test", 0)
15-
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
14+
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
1615
defer os.Remove("current.yaml")
1716
test := []string{"test-cmd", "--load", "current.yaml"}
1817
_ = set.Parse(test)
@@ -42,7 +41,7 @@ func TestCommandYamlFileTest(t *testing.T) {
4241
func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
4342
app := &cli.App{}
4443
set := flag.NewFlagSet("test", 0)
45-
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
44+
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
4645
defer os.Remove("current.yaml")
4746

4847
_ = os.Setenv("THE_TEST", "10")
@@ -76,7 +75,7 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
7675
func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
7776
app := &cli.App{}
7877
set := flag.NewFlagSet("test", 0)
79-
_ = ioutil.WriteFile("current.yaml", []byte(`top:
78+
_ = os.WriteFile("current.yaml", []byte(`top:
8079
test: 15`), 0666)
8180
defer os.Remove("current.yaml")
8281

@@ -111,7 +110,7 @@ func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
111110
func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
112111
app := &cli.App{}
113112
set := flag.NewFlagSet("test", 0)
114-
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
113+
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
115114
defer os.Remove("current.yaml")
116115

117116
test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"}
@@ -143,7 +142,7 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
143142
func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
144143
app := &cli.App{}
145144
set := flag.NewFlagSet("test", 0)
146-
_ = ioutil.WriteFile("current.yaml", []byte(`top:
145+
_ = os.WriteFile("current.yaml", []byte(`top:
147146
test: 15`), 0666)
148147
defer os.Remove("current.yaml")
149148

@@ -176,7 +175,7 @@ func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
176175
func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
177176
app := &cli.App{}
178177
set := flag.NewFlagSet("test", 0)
179-
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
178+
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
180179
defer os.Remove("current.yaml")
181180

182181
test := []string{"test-cmd", "--load", "current.yaml"}
@@ -208,7 +207,7 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
208207
func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
209208
app := &cli.App{}
210209
set := flag.NewFlagSet("test", 0)
211-
_ = ioutil.WriteFile("current.yaml", []byte(`top:
210+
_ = os.WriteFile("current.yaml", []byte(`top:
212211
test: 15`), 0666)
213212
defer os.Remove("current.yaml")
214213

@@ -241,7 +240,7 @@ func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
241240
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) {
242241
app := &cli.App{}
243242
set := flag.NewFlagSet("test", 0)
244-
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
243+
_ = os.WriteFile("current.yaml", []byte("test: 15"), 0666)
245244
defer os.Remove("current.yaml")
246245

247246
_ = os.Setenv("THE_TEST", "11")
@@ -275,7 +274,7 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T
275274
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *testing.T) {
276275
app := &cli.App{}
277276
set := flag.NewFlagSet("test", 0)
278-
_ = ioutil.WriteFile("current.yaml", []byte(`top:
277+
_ = os.WriteFile("current.yaml", []byte(`top:
279278
test: 15`), 0666)
280279
defer os.Remove("current.yaml")
281280

altsrc/yaml_file_loader.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package altsrc
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/url"
88
"os"
@@ -68,21 +68,21 @@ func loadDataFrom(filePath string) ([]byte, error) {
6868
if err != nil {
6969
return nil, err
7070
}
71-
return ioutil.ReadAll(res.Body)
71+
return io.ReadAll(res.Body)
7272
default:
7373
return nil, fmt.Errorf("scheme of %s is unsupported", filePath)
7474
}
7575
} else if u.Path != "" { // i dont have a host, but I have a path. I am a local file.
7676
if _, notFoundFileErr := os.Stat(filePath); notFoundFileErr != nil {
7777
return nil, fmt.Errorf("Cannot read from file: '%s' because it does not exist.", filePath)
7878
}
79-
return ioutil.ReadFile(filePath)
79+
return os.ReadFile(filePath)
8080
} else if runtime.GOOS == "windows" && strings.Contains(u.String(), "\\") {
8181
// on Windows systems u.Path is always empty, so we need to check the string directly.
8282
if _, notFoundFileErr := os.Stat(filePath); notFoundFileErr != nil {
8383
return nil, fmt.Errorf("Cannot read from file: '%s' because it does not exist.", filePath)
8484
}
85-
return ioutil.ReadFile(filePath)
85+
return os.ReadFile(filePath)
8686
}
8787

8888
return nil, fmt.Errorf("unable to determine how to load from path %s", filePath)

app_test.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"flag"
77
"fmt"
88
"io"
9-
"io/ioutil"
109
"os"
1110
"reflect"
1211
"strconv"
@@ -1278,7 +1277,7 @@ func TestApp_BeforeFunc(t *testing.T) {
12781277
Flags: []Flag{
12791278
&StringFlag{Name: "opt"},
12801279
},
1281-
Writer: ioutil.Discard,
1280+
Writer: io.Discard,
12821281
}
12831282

12841283
// run with the Before() func succeeding
@@ -1369,7 +1368,7 @@ func TestApp_BeforeAfterFuncShellCompletion(t *testing.T) {
13691368
Flags: []Flag{
13701369
&StringFlag{Name: "opt"},
13711370
},
1372-
Writer: ioutil.Discard,
1371+
Writer: io.Discard,
13731372
}
13741373

13751374
// run with the Before() func succeeding
@@ -1487,7 +1486,7 @@ func TestAppNoHelpFlag(t *testing.T) {
14871486

14881487
HelpFlag = nil
14891488

1490-
app := &App{Writer: ioutil.Discard}
1489+
app := &App{Writer: io.Discard}
14911490
err := app.Run([]string{"test", "-h"})
14921491

14931492
if err != flag.ErrHelp {
@@ -1718,7 +1717,7 @@ func TestApp_OrderOfOperations(t *testing.T) {
17181717
counts.OnUsageError = counts.Total
17191718
return errors.New("hay OnUsageError")
17201719
},
1721-
Writer: ioutil.Discard,
1720+
Writer: io.Discard,
17221721
}
17231722

17241723
beforeNoError := func(c *Context) error {
@@ -2308,7 +2307,7 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
23082307
Action: func(c *Context) error { return nil },
23092308
Before: func(c *Context) error { return fmt.Errorf("before error") },
23102309
After: func(c *Context) error { return fmt.Errorf("after error") },
2311-
Writer: ioutil.Discard,
2310+
Writer: io.Discard,
23122311
}
23132312

23142313
err := app.Run([]string{"foo"})
@@ -2458,7 +2457,7 @@ func (c *customBoolFlag) IsSet() bool {
24582457
func TestCustomFlagsUnused(t *testing.T) {
24592458
app := &App{
24602459
Flags: []Flag{&customBoolFlag{"custom"}},
2461-
Writer: ioutil.Discard,
2460+
Writer: io.Discard,
24622461
}
24632462

24642463
err := app.Run([]string{"foo"})
@@ -2470,7 +2469,7 @@ func TestCustomFlagsUnused(t *testing.T) {
24702469
func TestCustomFlagsUsed(t *testing.T) {
24712470
app := &App{
24722471
Flags: []Flag{&customBoolFlag{"custom"}},
2473-
Writer: ioutil.Discard,
2472+
Writer: io.Discard,
24742473
}
24752474

24762475
err := app.Run([]string{"foo", "--custom=bar"})
@@ -2481,7 +2480,7 @@ func TestCustomFlagsUsed(t *testing.T) {
24812480

24822481
func TestCustomHelpVersionFlags(t *testing.T) {
24832482
app := &App{
2484-
Writer: ioutil.Discard,
2483+
Writer: io.Discard,
24852484
}
24862485

24872486
// Be sure to reset the global flags
@@ -2573,7 +2572,7 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
25732572
Action: func(ctx *Context) error {
25742573
return fmt.Errorf("should not get here")
25752574
},
2576-
Writer: ioutil.Discard,
2575+
Writer: io.Discard,
25772576
}
25782577
err := app.Run([]string{"", "--test-completion", "--" + "generate-bash-completion"})
25792578
if err != nil {
@@ -2636,7 +2635,7 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
26362635

26372636
func newTestApp() *App {
26382637
a := NewApp()
2639-
a.Writer = ioutil.Discard
2638+
a.Writer = io.Discard
26402639
return a
26412640
}
26422641

0 commit comments

Comments
 (0)