Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
testwill committed Jun 10, 2024
1 parent 3d9a2bf commit 04f3751
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 35 deletions.
9 changes: 4 additions & 5 deletions c-bindings/c-bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path"
"unsafe"
Expand Down Expand Up @@ -238,7 +237,7 @@ func jsonnet_evaluate_snippet_multi(vmRef *C.struct_JsonnetVm, filename *C.char,
//export jsonnet_evaluate_file
func jsonnet_evaluate_file(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int) *C.char {
f := C.GoString(filename)
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
*e = 1
return C.CString(fmt.Sprintf("Failed to read input file: %s: %s", f, err.Error()))
Expand All @@ -249,7 +248,7 @@ func jsonnet_evaluate_file(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int
//export jsonnet_evaluate_file_stream
func jsonnet_evaluate_file_stream(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int) *C.char {
f := C.GoString(filename)
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
*e = 1
return C.CString(fmt.Sprintf("Failed to read input file: %s: %s", f, err.Error()))
Expand All @@ -260,7 +259,7 @@ func jsonnet_evaluate_file_stream(vmRef *C.struct_JsonnetVm, filename *C.char, e
//export jsonnet_evaluate_file_multi
func jsonnet_evaluate_file_multi(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int) *C.char {
f := C.GoString(filename)
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
*e = 1
return C.CString(fmt.Sprintf("Failed to read input file: %s: %s", f, err.Error()))
Expand Down Expand Up @@ -622,7 +621,7 @@ func jsonnet_fmt_snippet(vmRef *C.struct_JsonnetVm, filename *C.char, code *C.ch
//export jsonnet_fmt_file
func jsonnet_fmt_file(vmRef *C.struct_JsonnetVm, filename *C.char, e *C.int) *C.char {
f := C.GoString(filename)
data, err := ioutil.ReadFile(f)
data, err := os.ReadFile(f)
if err != nil {
*e = 1
return C.CString(fmt.Sprintf("Failed to read input file: %s: %s", f, err.Error()))
Expand Down
3 changes: 1 addition & 2 deletions cmd/dumpstdlibast/dumpstdlibast.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -29,7 +28,7 @@ func main() {
fmt.Fprintf(os.Stderr, "usage: %s <file>\n", filepath.Base(os.Args[0]))
os.Exit(2)
}
buf, err := ioutil.ReadFile(os.Args[1])
buf, err := os.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/internal/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package cmd

import (
"fmt"
"io/ioutil"
"io"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -81,12 +81,12 @@ func ReadInput(filenameIsCode bool, filename *string) (input string, err error)
*filename = "<cmdline>"
} else if *filename == "-" {
var bytes []byte
bytes, err = ioutil.ReadAll(os.Stdin)
bytes, err = io.ReadAll(os.Stdin)
input = string(bytes)
*filename = "<stdin>"
} else {
var bytes []byte
bytes, err = ioutil.ReadFile(*filename)
bytes, err = os.ReadFile(*filename)
input = string(bytes)
}
return
Expand Down
3 changes: 1 addition & 2 deletions cmd/jsonnet-lint/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -166,7 +165,7 @@ func main() {
if err != nil {
die(err)
}
data, err := ioutil.ReadAll(f)
data, err := io.ReadAll(f)
if err != nil {
die(err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/jsonnet/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -327,7 +326,7 @@ func writeMultiOutputFiles(output map[string]string, outputDir, outputFile strin
}

if _, err := os.Stat(filename); !os.IsNotExist(err) {
existingContent, err := ioutil.ReadFile(filename)
existingContent, err := os.ReadFile(filename)
if err != nil {
return err
}
Expand All @@ -344,7 +343,7 @@ func writeMultiOutputFiles(output map[string]string, outputDir, outputFile strin
}
}

err = ioutil.WriteFile(filename, []byte(newContent), 0666)
err = os.WriteFile(filename, []byte(newContent), 0666)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package jsonnet

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"unsafe"
Expand Down Expand Up @@ -241,7 +240,7 @@ func (importer *FileImporter) tryPath(dir, importedPath string) (found bool, con
if cacheEntry, isCached := importer.fsCache[absPath]; isCached {
entry = cacheEntry
} else {
contentBytes, err := ioutil.ReadFile(absPath)
contentBytes, err := os.ReadFile(absPath)
if err != nil {
if os.IsNotExist(err) {
entry = &fsCacheEntry{
Expand Down
5 changes: 2 additions & 3 deletions internal/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testutils

import (
"bytes"
"io/ioutil"
"os"

"github.com/sergi/go-diff/diffmatchpatch"
Expand Down Expand Up @@ -31,15 +30,15 @@ func CompareWithGolden(result string, golden []byte) (string, bool) {
// are actually different from what is already there. It returns whether or not
// the overwrite was performed (i.e. the desired content was different than actual).
func UpdateGoldenFile(path string, content []byte, mode os.FileMode) (changed bool, err error) {
old, err := ioutil.ReadFile(path)
old, err := os.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
return false, err
}
// If it exists and already has the right content, do nothing,
if bytes.Equal(old, content) && !os.IsNotExist(err) {
return false, nil
}
if err := ioutil.WriteFile(path, content, mode); err != nil {
if err := os.WriteFile(path, content, mode); err != nil {
return false, err
}
return true, nil
Expand Down
8 changes: 4 additions & 4 deletions linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package linter
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
Expand All @@ -27,7 +27,7 @@ type ChangedGoldensList struct {

func runTest(t *testing.T, test *linterTest, changedGoldensList *ChangedGoldensList) {
read := func(file string) []byte {
bytz, err := ioutil.ReadFile(file)
bytz, err := os.ReadFile(file)
if err != nil {
t.Fatalf("reading file: %s: %v", file, err)
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func runTest(t *testing.T, test *linterTest, changedGoldensList *ChangedGoldensL
changedGoldensList.changedGoldens = append(changedGoldensList.changedGoldens, test.output)
}
} else {
golden, err := ioutil.ReadFile(test.output)
golden, err := os.ReadFile(test.output)
if err != nil {
t.Error(err)
return
Expand All @@ -74,7 +74,7 @@ func runTest(t *testing.T, test *linterTest, changedGoldensList *ChangedGoldensL

func runTests(t *testing.T, tests []*linterTest) {
read := func(file string) []byte {
bytz, err := ioutil.ReadFile(file)
bytz, err := os.ReadFile(file)
if err != nil {
t.Fatalf("reading file: %s: %v", file, err)
}
Expand Down
21 changes: 10 additions & 11 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -229,7 +228,7 @@ func compareSingleGolden(path string, result jsonnetResult) []error {
if result.outputMulti != nil {
return []error{fmt.Errorf("outputMulti is populated in a single-file test for %v", path)}
}
golden, err := ioutil.ReadFile(path)
golden, err := os.ReadFile(path)
if err != nil {
return []error{fmt.Errorf("reading file %s: %v", path, err)}
}
Expand All @@ -254,14 +253,14 @@ func updateSingleGolden(path string, result jsonnetResult) (updated []string, er
}

func compareMultifileGolden(path string, result jsonnetResult) []error {
expectFiles, err := ioutil.ReadDir(path)
expectFiles, err := os.ReadDir(path)
if err != nil {
return []error{fmt.Errorf("reading golden dir %v: %v", path, err)}
}
goldenContent := map[string][]byte{}
var errs []error
for _, f := range expectFiles {
golden, err := ioutil.ReadFile(filepath.Join(path, f.Name()))
golden, err := os.ReadFile(filepath.Join(path, f.Name()))
if err != nil {
return []error{fmt.Errorf("reading file %s: %v", f.Name(), err)}
}
Expand All @@ -284,7 +283,7 @@ func compareMultifileGolden(path string, result jsonnetResult) []error {
}

func updateMultifileGolden(path string, result jsonnetResult) ([]string, error) {
expectFiles, err := ioutil.ReadDir(path)
expectFiles, err := os.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("reading golden directory %v: %v", path, err)
}
Expand Down Expand Up @@ -312,7 +311,7 @@ func updateMultifileGolden(path string, result jsonnetResult) ([]string, error)

func runTest(t *testing.T, test *mainTest) {
read := func(file string) []byte {
bytz, err := ioutil.ReadFile(file)
bytz, err := os.ReadFile(file)
if err != nil {
t.Fatalf("reading file: %s: %v", file, err)
}
Expand Down Expand Up @@ -413,7 +412,7 @@ func TestEvalUnusualFilenames(t *testing.T) {
dir := os.Getenv("TEST_TMPDIR")
if len(dir) == 0 {
var err error
if dir, err = ioutil.TempDir("", "jsonnet"); err != nil {
if dir, err = os.MkdirTemp("", "jsonnet"); err != nil {
t.Fatal(err)
}
defer func() {
Expand All @@ -425,11 +424,11 @@ func TestEvalUnusualFilenames(t *testing.T) {
}

copySmallFile := func(t *testing.T, dst, src string) {
b, err := ioutil.ReadFile(src)
b, err := os.ReadFile(src)
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(dst, b, 0444); err != nil {
if err := os.WriteFile(dst, b, 0444); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -502,10 +501,10 @@ func TestEvalUnusualFilenames(t *testing.T) {
`),
},
} {
if err := ioutil.WriteFile(f.name+".jsonnet", f.content, 0444); err != nil {
if err := os.WriteFile(f.name+".jsonnet", f.content, 0444); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(f.name+".golden", f.golden, 0444); err != nil {
if err := os.WriteFile(f.name+".golden", f.golden, 0444); err != nil {
t.Fatal(err)
}
t.Run(f.name, func(t *testing.T) {
Expand Down

0 comments on commit 04f3751

Please sign in to comment.