Skip to content

Commit eb63266

Browse files
committedFeb 23, 2022
replace uses of os/ioutil, and use test.TempDir() in tests
The os/ioutil package is now deprecated, so replace with os. While at it, also replace its used for test.TempDir() in tests, which is easier to use as it is automatically cleaned up. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
·
v0.10.0v0.4.0
1 parent 77df2af commit eb63266

File tree

5 files changed

+15
-32
lines changed

5 files changed

+15
-32
lines changed
 

‎clidocstool_md.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package clidocstool
1717
import (
1818
"bytes"
1919
"fmt"
20-
"io/ioutil"
2120
"log"
2221
"os"
2322
"path/filepath"
@@ -82,14 +81,14 @@ func (c *Client) GenMarkdownTree(cmd *cobra.Command) error {
8281
}); err != nil {
8382
return err
8483
}
85-
if err = ioutil.WriteFile(targetPath, icBuf.Bytes(), 0644); err != nil {
84+
if err = os.WriteFile(targetPath, icBuf.Bytes(), 0644); err != nil {
8685
return err
8786
}
8887
} else if err := copyFile(sourcePath, targetPath); err != nil {
8988
return err
9089
}
9190

92-
content, err := ioutil.ReadFile(targetPath)
91+
content, err := os.ReadFile(targetPath)
9392
if err != nil {
9493
return err
9594
}
@@ -116,7 +115,7 @@ func (c *Client) GenMarkdownTree(cmd *cobra.Command) error {
116115
if err != nil {
117116
return err
118117
}
119-
if err = ioutil.WriteFile(targetPath, []byte(cont), fi.Mode()); err != nil {
118+
if err = os.WriteFile(targetPath, []byte(cont), fi.Mode()); err != nil {
120119
return fmt.Errorf("failed to write %s: %w", targetPath, err)
121120
}
122121

‎clidocstool_md_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package clidocstool
1616

1717
import (
18-
"io/ioutil"
1918
"os"
2019
"path"
2120
"path/filepath"
@@ -27,11 +26,9 @@ import (
2726

2827
//nolint:errcheck
2928
func TestGenMarkdownTree(t *testing.T) {
30-
tmpdir, err := ioutil.TempDir("", "test-gen-markdown-tree")
31-
require.NoError(t, err)
32-
defer os.RemoveAll(tmpdir)
29+
tmpdir := t.TempDir()
3330

34-
err = copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
31+
err := copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
3532
require.NoError(t, err)
3633

3734
c, err := New(Options{
@@ -45,12 +42,10 @@ func TestGenMarkdownTree(t *testing.T) {
4542
for _, tt := range []string{"buildx.md", "buildx_build.md", "buildx_stop.md"} {
4643
tt := tt
4744
t.Run(tt, func(t *testing.T) {
48-
fres := filepath.Join(tmpdir, tt)
49-
require.FileExists(t, fres)
50-
bres, err := ioutil.ReadFile(fres)
45+
bres, err := os.ReadFile(filepath.Join(tmpdir, tt))
5146
require.NoError(t, err)
5247

53-
bexc, err := ioutil.ReadFile(path.Join("fixtures", tt))
48+
bexc, err := os.ReadFile(path.Join("fixtures", tt))
5449
require.NoError(t, err)
5550
assert.Equal(t, string(bexc), string(bres))
5651
})

‎clidocstool_test.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package clidocstool
1616

1717
import (
18-
"io/ioutil"
1918
"os"
2019
"path"
2120
"path/filepath"
@@ -175,11 +174,9 @@ func init() {
175174

176175
//nolint:errcheck
177176
func TestGenAllTree(t *testing.T) {
178-
tmpdir, err := ioutil.TempDir("", "test-gen-all-tree")
179-
require.NoError(t, err)
180-
defer os.RemoveAll(tmpdir)
177+
tmpdir := t.TempDir()
181178

182-
err = copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
179+
err := copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
183180
require.NoError(t, err)
184181

185182
c, err := New(Options{
@@ -193,12 +190,10 @@ func TestGenAllTree(t *testing.T) {
193190
for _, tt := range []string{"buildx.md", "buildx_build.md", "buildx_stop.md", "docker_buildx.yaml", "docker_buildx_build.yaml", "docker_buildx_stop.yaml"} {
194191
tt := tt
195192
t.Run(tt, func(t *testing.T) {
196-
fres := filepath.Join(tmpdir, tt)
197-
require.FileExists(t, fres)
198-
bres, err := ioutil.ReadFile(fres)
193+
bres, err := os.ReadFile(filepath.Join(tmpdir, tt))
199194
require.NoError(t, err)
200195

201-
bexc, err := ioutil.ReadFile(path.Join("fixtures", tt))
196+
bexc, err := os.ReadFile(path.Join("fixtures", tt))
202197
require.NoError(t, err)
203198
assert.Equal(t, string(bexc), string(bres))
204199
})

‎clidocstool_yaml.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package clidocstool
1717
import (
1818
"fmt"
1919
"io"
20-
"io/ioutil"
2120
"log"
2221
"os"
2322
"path/filepath"
@@ -376,7 +375,7 @@ func (c *Client) loadLongDescription(parentCmd *cobra.Command) error {
376375
}
377376
mdFile := strings.ReplaceAll(name, " ", "_") + ".md"
378377
sourcePath := filepath.Join(c.source, mdFile)
379-
content, err := ioutil.ReadFile(sourcePath)
378+
content, err := os.ReadFile(sourcePath)
380379
if os.IsNotExist(err) {
381380
log.Printf("WARN: %s does not exist, skipping Markdown examples for YAML doc\n", mdFile)
382381
continue

‎clidocstool_yaml_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package clidocstool
1616

1717
import (
18-
"io/ioutil"
1918
"os"
2019
"path"
2120
"path/filepath"
@@ -27,9 +26,7 @@ import (
2726

2827
//nolint:errcheck
2928
func TestGenYamlTree(t *testing.T) {
30-
tmpdir, err := ioutil.TempDir("", "test-gen-yaml-tree")
31-
require.NoError(t, err)
32-
defer os.RemoveAll(tmpdir)
29+
tmpdir := t.TempDir()
3330

3431
c, err := New(Options{
3532
Root: buildxCmd,
@@ -42,12 +39,10 @@ func TestGenYamlTree(t *testing.T) {
4239
for _, tt := range []string{"docker_buildx.yaml", "docker_buildx_build.yaml", "docker_buildx_stop.yaml"} {
4340
tt := tt
4441
t.Run(tt, func(t *testing.T) {
45-
fres := filepath.Join(tmpdir, tt)
46-
require.FileExists(t, fres)
47-
bres, err := ioutil.ReadFile(fres)
42+
bres, err := os.ReadFile(filepath.Join(tmpdir, tt))
4843
require.NoError(t, err)
4944

50-
bexc, err := ioutil.ReadFile(path.Join("fixtures", tt))
45+
bexc, err := os.ReadFile(path.Join("fixtures", tt))
5146
require.NoError(t, err)
5247
assert.Equal(t, string(bexc), string(bres))
5348
})

0 commit comments

Comments
 (0)
Please sign in to comment.