Skip to content

Commit

Permalink
all: replace uses of deprecated ioutil APIs
Browse files Browse the repository at this point in the history
I had a few of these stashed locally, and they shouldn't cause
conflicts with any other ongoing work right now.

Note that TestMkdir was leaking an open file on Windows,
which is only caught by testing.T.TempDir checking RemoveAll errors.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I4dbf94cf4c5994210a55f409bfc5b5e116058736
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1177108
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed Feb 20, 2024
1 parent 88431ee commit d6ecc3d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 34 deletions.
3 changes: 1 addition & 2 deletions encoding/gocode/testdata/gen.go
Expand Up @@ -15,7 +15,6 @@
package main

import (
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -27,7 +26,7 @@ import (
)

func main() {
dirs, err := ioutil.ReadDir("testdata")
dirs, err := os.ReadDir("testdata")
if err != nil {
log.Fatal(err)
}
Expand Down
15 changes: 9 additions & 6 deletions internal/copy/copy.go
Expand Up @@ -18,7 +18,6 @@ package copy
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -68,7 +67,7 @@ func copyDir(info os.FileInfo, src, dst string) error {
}
}

entries, err := ioutil.ReadDir(src)
entries, err := os.ReadDir(src)
if err != nil {
return fmt.Errorf("reading dir %s: %v", src, err)
}
Expand All @@ -77,13 +76,17 @@ func copyDir(info os.FileInfo, src, dst string) error {
srcPath := filepath.Join(src, e.Name())
dstPath := filepath.Join(dst, e.Name())

switch mode := e.Mode(); mode & os.ModeType {
info, err := e.Info()
if err != nil {
return err
}
switch e.Type() {
case os.ModeSymlink:
err = copySymLink(e, srcPath, dstPath)
err = copySymLink(info, srcPath, dstPath)
case os.ModeDir:
err = copyDir(e, srcPath, dstPath)
err = copyDir(info, srcPath, dstPath)
default:
err = copyFile(e, srcPath, dstPath)
err = copyFile(info, srcPath, dstPath)
}
if err != nil {
return err
Expand Down
34 changes: 9 additions & 25 deletions pkg/tool/file/file_test.go
Expand Up @@ -16,7 +16,6 @@ package file

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -69,20 +68,14 @@ func TestRead(t *testing.T) {
}

func TestAppend(t *testing.T) {
f, err := ioutil.TempFile("", "filetest")
if err != nil {
t.Fatal(err)
}
name := f.Name()
defer os.Remove(name)
f.Close()
name := filepath.Join(t.TempDir(), "file")
name = filepath.ToSlash(name)

v := parse(t, "tool/file.Append", fmt.Sprintf(`{
filename: "%s"
contents: "This is a test."
}`, name))
_, err = (*cmdAppend).Run(nil, &task.Context{Obj: v})
_, err := (*cmdAppend).Run(nil, &task.Context{Obj: v})
if err != nil {
t.Fatal(err)
}
Expand All @@ -98,20 +91,14 @@ func TestAppend(t *testing.T) {
}

func TestCreate(t *testing.T) {
f, err := ioutil.TempFile("", "filetest")
if err != nil {
t.Fatal(err)
}
name := f.Name()
defer os.Remove(name)
f.Close()
name := filepath.Join(t.TempDir(), "file")
name = filepath.ToSlash(name)

v := parse(t, "tool/file.Create", fmt.Sprintf(`{
filename: "%s"
contents: "This is a test."
}`, name))
_, err = (*cmdCreate).Run(nil, &task.Context{Obj: v})
_, err := (*cmdCreate).Run(nil, &task.Context{Obj: v})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -140,16 +127,12 @@ func TestGlob(t *testing.T) {
}

func TestMkdir(t *testing.T) {
baseDir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(baseDir)
baseDir := t.TempDir()

// simple dir creation
d1 := filepath.Join(baseDir, "foo")
v := parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: #"%s"#}`, d1))
_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
_, err := (*cmdMkdir).Run(nil, &task.Context{Obj: v})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -185,10 +168,11 @@ func TestMkdir(t *testing.T) {
}

// file at same path
f, err := ioutil.TempFile(baseDir, "")
f, err := os.CreateTemp(baseDir, "")
if err != nil {
t.Fatal(err)
}
f.Close()
v = parse(t, "tool/file.Mkdir", fmt.Sprintf(`{path: #"%s"#}`, f.Name()))
_, err = (*cmdMkdir).Run(nil, &task.Context{Obj: v})
if err == nil {
Expand All @@ -207,7 +191,7 @@ func TestMkdirTemp(t *testing.T) {
t.Fatal("no directory path returned")
}
path := r.(map[string]interface{})["path"].(string)
defer os.RemoveAll(path)
t.Cleanup(func() { os.RemoveAll(path) })
fi, err := os.Stat(path)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/tool/http/http_test.go
Expand Up @@ -53,7 +53,7 @@ func parse(t *testing.T, kind, expr string) cue.Value {

func TestTLS(t *testing.T) {
s := newTLSServer()
defer s.Close()
t.Cleanup(s.Close)

v1 := parse(t, "tool/http.Get", fmt.Sprintf(`{url: "%s"}`, s.URL))
_, err := (*httpCmd).Run(nil, &task.Context{Obj: v1})
Expand Down

0 comments on commit d6ecc3d

Please sign in to comment.