Skip to content

Commit f765001

Browse files
authored
Merge pull request #20 from thaJeztah/replace_ioutil
replace uses of os/ioutil, and use test.TempDir() in tests
2 parents 1b0effd + eb63266 commit f765001

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"
@@ -176,11 +175,9 @@ func init() {
176175

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

183-
err = copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
180+
err := copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
184181
require.NoError(t, err)
185182

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

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

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"
@@ -388,7 +387,7 @@ func (c *Client) loadLongDescription(parentCmd *cobra.Command) error {
388387
}
389388
mdFile := strings.ReplaceAll(name, " ", "_") + ".md"
390389
sourcePath := filepath.Join(c.source, mdFile)
391-
content, err := ioutil.ReadFile(sourcePath)
390+
content, err := os.ReadFile(sourcePath)
392391
if os.IsNotExist(err) {
393392
log.Printf("WARN: %s does not exist, skipping Markdown examples for YAML doc\n", mdFile)
394393
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)