Skip to content

Commit

Permalink
chore: remove refs to deprecated io/ioutil
Browse files Browse the repository at this point in the history
Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
  • Loading branch information
testwill committed Jul 19, 2023
1 parent a5ad2b8 commit 8d43694
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 36 deletions.
8 changes: 4 additions & 4 deletions pkg/cmd/template/regular_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -66,7 +66,7 @@ organization=Acme Widgets Inc.`)
files.MustNewFileFromSource(files.NewBytesSource("foo.yaml", yamlData)),
}

outputDir, err := ioutil.TempDir(os.TempDir(), "fakedir")
outputDir, err := os.MkdirTemp(os.TempDir(), "fakedir")
require.NoError(t, err)
defer os.RemoveAll(outputDir)

Expand Down Expand Up @@ -252,12 +252,12 @@ func TestFileMarkMultipleExcludes(t *testing.T) {
}

func assertStdoutAndStderr(t *testing.T, stdout *bytes.Buffer, stderr *bytes.Buffer, expectedStdOut string, expectedStdErr string) {
stdoutOutput, err := ioutil.ReadAll(stdout)
stdoutOutput, err := io.ReadAll(stdout)
require.NoError(t, err, "reading stdout")

require.Equal(t, expectedStdOut, string(stdoutOutput), "comparing stdout")

stderrOutput, err := ioutil.ReadAll(stderr)
stderrOutput, err := io.ReadAll(stderr)
require.NoError(t, err, "reading stdout")

require.Equal(t, expectedStdErr, string(stderrOutput), "comparing stderr")
Expand Down
6 changes: 3 additions & 3 deletions pkg/files/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package files

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -81,7 +81,7 @@ func (s LocalSource) RelativePath() (string, error) {
return "", fmt.Errorf("unknown relative path for %s", s.path)
}

func (s LocalSource) Bytes() ([]byte, error) { return ioutil.ReadFile(s.path) }
func (s LocalSource) Bytes() ([]byte, error) { return os.ReadFile(s.path) }

Check warning on line 84 in pkg/files/sources.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method LocalSource.Bytes should have comment or be unexported (revive)

type HTTPSource struct {
url string
Expand Down Expand Up @@ -109,7 +109,7 @@ func (s HTTPSource) Bytes() ([]byte, error) {
return nil, fmt.Errorf("Requesting URL '%s': %s", s.url, resp.Status)
}

result, err := ioutil.ReadAll(resp.Body)
result, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Reading URL '%s': %s", s.url, err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/files/sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package files_test
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -23,7 +23,7 @@ func TestHTTPFileSources(t *testing.T) {
return &http.Response{
StatusCode: http.StatusOK,
// Send response to be tested
Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)),
Body: io.NopCloser(bytes.NewBufferString(`OK`)),
// Must be set to non-nil value or it panics
Header: make(http.Header),
}
Expand All @@ -41,7 +41,7 @@ func TestHTTPFileSources(t *testing.T) {
require.Equal(t, req.URL.String(), url)
return &http.Response{
StatusCode: http.StatusIMUsed,
Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)),
Body: io.NopCloser(bytes.NewBufferString(`OK`)),
Header: make(http.Header),
}
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/files/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package files

import (
"fmt"
"io/ioutil"
"io"
"os"
)

Expand All @@ -17,5 +17,5 @@ func ReadStdin() ([]byte, error) {
return nil, fmt.Errorf("Standard input has already been read, has the '-' argument been used in more than one flag?")
}
hasStdinBeenRead = true
return ioutil.ReadAll(os.Stdin)
return io.ReadAll(os.Stdin)
}
5 changes: 2 additions & 3 deletions pkg/texttemplate/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package texttemplate_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand All @@ -24,7 +23,7 @@ var (
)

func TestTextTemplate(t *testing.T) {
files, err := ioutil.ReadDir("filetests")
files, err := os.ReadDir("filetests")
if err != nil {
t.Fatal(err)
}
Expand All @@ -45,7 +44,7 @@ func TestTextTemplate(t *testing.T) {
testDesc := fmt.Sprintf("checking %s ...\n", file.Name())
fmt.Printf("%s", testDesc)

contents, err := ioutil.ReadFile(filePath)
contents, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/website/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package website
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -115,7 +115,7 @@ func (s *Server) examplesHandler(w http.ResponseWriter, r *http.Request) {
}

func (s *Server) templateHandler(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err != nil {
s.logError(w, err)
return
Expand Down
3 changes: 1 addition & 2 deletions pkg/yamlfmt/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package yamlfmt_test

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -51,7 +50,7 @@ func TestYAMLFmt(t *testing.T) {
testDesc := fmt.Sprintf("checking %s ...\n", filePath)
fmt.Printf("%s", testDesc)

contents, err := ioutil.ReadFile(filePath)
contents, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
Expand Down
29 changes: 14 additions & 15 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -111,7 +110,7 @@ float: 123.123
}
actualOutput := runYtt(t, testInputFiles{testDir + "/config"}, "", flags, nil)

expectedOutput, err := ioutil.ReadFile(filepath.Join(testDir, "expected.txt"))
expectedOutput, err := os.ReadFile(filepath.Join(testDir, "expected.txt"))
require.NoError(t, err)

require.Equal(t, string(expectedOutput), actualOutput)
Expand All @@ -138,7 +137,7 @@ float: 0

})
t.Run("can be 'required'", func(t *testing.T) {
expectedFileOutput, err := ioutil.ReadFile("../../examples/data-values-required/expected.txt")
expectedFileOutput, err := os.ReadFile("../../examples/data-values-required/expected.txt")
require.NoError(t, err)

dirs := []string{
Expand Down Expand Up @@ -212,7 +211,7 @@ func TestSchema(t *testing.T) {
dirPath := fmt.Sprintf("../../examples/%s", dir)
actualOutput := runYtt(t, testInputFiles{dirPath}, "", nil, nil)

expectedOutput, err := ioutil.ReadFile(filepath.Join(dirPath, "expected.txt"))
expectedOutput, err := os.ReadFile(filepath.Join(dirPath, "expected.txt"))
require.NoError(t, err)

require.Equal(t, string(expectedOutput), actualOutput)
Expand All @@ -232,7 +231,7 @@ func TestOverlays(t *testing.T) {
dirPath := fmt.Sprintf("../../examples/%s", dir)
actualOutput := runYtt(t, testInputFiles{dirPath}, "", nil, nil)

expectedOutput, err := ioutil.ReadFile(filepath.Join(dirPath, "expected.txt"))
expectedOutput, err := os.ReadFile(filepath.Join(dirPath, "expected.txt"))
require.NoError(t, err)

require.Equal(t, string(expectedOutput), actualOutput)
Expand Down Expand Up @@ -264,7 +263,7 @@ func TestReadingFromStandardIn(t *testing.T) {
t.Run("through --file", func(t *testing.T) {
actualOutput := runYtt(t, []string{"-", "../../examples/eirini/input.yml"}, "../../examples/eirini/config.yml", nil, nil)

expectedFileOutput, err := ioutil.ReadFile("../../examples/eirini/config-result.yml")
expectedFileOutput, err := os.ReadFile("../../examples/eirini/config-result.yml")
require.NoError(t, err)
require.Equal(t, string(expectedFileOutput), actualOutput)
})
Expand Down Expand Up @@ -294,16 +293,16 @@ new_thing: new
}

func TestCheckDirectoryOutput(t *testing.T) {
tempOutputDir, err := ioutil.TempDir(os.TempDir(), "ytt-check-dir")
tempOutputDir, err := os.MkdirTemp(os.TempDir(), "ytt-check-dir")
require.NoError(t, err)
defer os.Remove(tempOutputDir)
flags := yttFlags{{fmt.Sprintf("--dangerous-emptied-output-directory=%s", tempOutputDir): ""}}
runYtt(t, []string{"../../examples/eirini/"}, "", flags, nil)

expectedFileOutput, err := ioutil.ReadFile("../../examples/eirini/config-result.yml")
expectedFileOutput, err := os.ReadFile("../../examples/eirini/config-result.yml")
require.NoError(t, err)

actualOutput, err := ioutil.ReadFile(filepath.Join(tempOutputDir, "config-result.yml"))
actualOutput, err := os.ReadFile(filepath.Join(tempOutputDir, "config-result.yml"))
require.NoError(t, err)

require.Equal(t, string(expectedFileOutput), string(actualOutput))
Expand Down Expand Up @@ -371,7 +370,7 @@ func TestApplicationSpecificScenarios(t *testing.T) {
dirPath := fmt.Sprintf("../../examples/%s", dir)
actualOutput := runYtt(t, testInputFiles{dirPath}, "", nil, nil)

expectedOutput, err := ioutil.ReadFile(filepath.Join(dirPath, "expected.txt"))
expectedOutput, err := os.ReadFile(filepath.Join(dirPath, "expected.txt"))
require.NoError(t, err)

require.Equal(t, string(expectedOutput), actualOutput)
Expand All @@ -385,7 +384,7 @@ func TestApplicationSpecificScenarios(t *testing.T) {
dirPath := fmt.Sprintf("../../examples/%s", dir)
actualOutput := runYtt(t, testInputFiles{dirPath}, "", nil, nil)

expectedOutput, err := ioutil.ReadFile(filepath.Join(dirPath, "expected.txt"))
expectedOutput, err := os.ReadFile(filepath.Join(dirPath, "expected.txt"))
require.NoError(t, err)

require.Equal(t, string(expectedOutput), actualOutput)
Expand All @@ -396,14 +395,14 @@ func TestApplicationSpecificScenarios(t *testing.T) {
t.Run("config.yml", func(t *testing.T) {
actualOutput := runYtt(t, []string{"../../examples/eirini/config.yml", "../../examples/eirini/input.yml"}, "", nil, nil)

expectedFileOutput, err := ioutil.ReadFile("../../examples/eirini/config-result.yml")
expectedFileOutput, err := os.ReadFile("../../examples/eirini/config-result.yml")
require.NoError(t, err)
require.Equal(t, string(expectedFileOutput), actualOutput)
})
t.Run("config-alt1.yml", func(t *testing.T) {
actualOutput := runYtt(t, []string{"../../examples/eirini/config-alt1.yml", "../../examples/eirini/input.yml"}, "", nil, nil)

expectedFileOutput, err := ioutil.ReadFile("../../examples/eirini/config-result.yml")
expectedFileOutput, err := os.ReadFile("../../examples/eirini/config-result.yml")
require.NoError(t, err)
require.Equal(t, string(expectedFileOutput), actualOutput)
})
Expand All @@ -416,7 +415,7 @@ func TestApplicationSpecificScenarios(t *testing.T) {
"../../examples/eirini/input.yml"},
"", nil, nil)

expectedFileOutput, err := ioutil.ReadFile("../../examples/eirini/config-result.yml")
expectedFileOutput, err := os.ReadFile("../../examples/eirini/config-result.yml")
require.NoError(t, err)
require.Equal(t, string(expectedFileOutput), actualOutput)
})
Expand All @@ -434,7 +433,7 @@ func TestRemainingExamples(t *testing.T) {
dirPath := fmt.Sprintf("../../examples/%s", dir)
actualOutput := runYtt(t, testInputFiles{dirPath}, "", nil, nil)

expectedOutput, err := ioutil.ReadFile(filepath.Join(dirPath, "expected.txt"))
expectedOutput, err := os.ReadFile(filepath.Join(dirPath, "expected.txt"))
require.NoError(t, err)

require.Equal(t, string(expectedOutput), actualOutput)
Expand Down
3 changes: 1 addition & 2 deletions test/filetests/filetests.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package filetests
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -86,7 +85,7 @@ func (f FileTests) Run(t *testing.T) {

for _, filePath := range files {
t.Run(filePath, func(t *testing.T) {
contents, err := ioutil.ReadFile(filePath)
contents, err := os.ReadFile(filePath)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 8d43694

Please sign in to comment.