Skip to content

Commit

Permalink
Merge pull request #1341 from testwill/ioutil
Browse files Browse the repository at this point in the history
chore: remove refs to deprecated io/ioutil
  • Loading branch information
denis256 committed Sep 19, 2023
2 parents f99fb38 + 9f19ac2 commit 15cff8c
Show file tree
Hide file tree
Showing 22 changed files with 45 additions and 64 deletions.
3 changes: 1 addition & 2 deletions modules/docker/build.go
@@ -1,7 +1,6 @@
package docker

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -139,7 +138,7 @@ func GitCloneAndBuildE(
path string,
dockerBuildOpts *BuildOptions,
) error {
workingDir, err := ioutil.TempDir("", "")
workingDir, err := os.MkdirTemp("", "")
if err != nil {
return err
}
Expand Down
15 changes: 7 additions & 8 deletions modules/files/files.go
Expand Up @@ -3,7 +3,6 @@ package files

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -110,7 +109,7 @@ func CopyFolderToDest(folderPath string, destRootFolder string, tempFolderPrefix
return "", DirNotFoundError{Directory: folderPath}
}

tmpDir, err := ioutil.TempDir(destRootFolder, tempFolderPrefix)
tmpDir, err := os.MkdirTemp(destRootFolder, tempFolderPrefix)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -149,27 +148,27 @@ func CopyFolderContents(source string, destination string) error {
// CopyFolderContentsWithFilter copies the files and folders within the given source folder that pass the given filter (return true) to the
// destination folder.
func CopyFolderContentsWithFilter(source string, destination string, filter func(path string) bool) error {
files, err := ioutil.ReadDir(source)
files, err := os.ReadDir(source)
if err != nil {
return err
}

for _, file := range files {
src := filepath.Join(source, file.Name())
dest := filepath.Join(destination, file.Name())

f, _ := file.Info()
if !filter(src) {
continue
} else if file.IsDir() {
if err := os.MkdirAll(dest, file.Mode()); err != nil {
if err := os.MkdirAll(dest, f.Mode()); err != nil {
return err
}

if err := CopyFolderContentsWithFilter(src, dest, filter); err != nil {
return err
}

} else if isSymLink(file) {
} else if isSymLink(f) {
if err := copySymLink(src, dest); err != nil {
return err
}
Expand Down Expand Up @@ -218,7 +217,7 @@ func PathIsTerraformLockFile(path string) bool {

// CopyFile copies a file from source to destination.
func CopyFile(source string, destination string) error {
contents, err := ioutil.ReadFile(source)
contents, err := os.ReadFile(source)
if err != nil {
return err
}
Expand All @@ -233,7 +232,7 @@ func WriteFileWithSamePermissions(source string, destination string, contents []
return err
}

return ioutil.WriteFile(destination, contents, fileInfo.Mode())
return os.WriteFile(destination, contents, fileInfo.Mode())
}

// isSymLink returns true if the given file is a symbolic link
Expand Down
3 changes: 1 addition & 2 deletions modules/helm/format_test.go
Expand Up @@ -2,7 +2,6 @@ package helm

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -189,7 +188,7 @@ func TestFormatValuesFilesAsArgs(t *testing.T) {
func createTempFiles(numFiles int) ([]string, error) {
paths := []string{}
for i := 0; i < numFiles; i++ {
tmpFile, err := ioutil.TempFile("", "")
tmpFile, err := os.CreateTemp("", "")
defer tmpFile.Close()
// We don't use require or t.Fatal here so that we give a chance to delete any temp files that were created
// before this error
Expand Down
5 changes: 2 additions & 3 deletions modules/http-helper/http_helper.go
Expand Up @@ -6,7 +6,6 @@ import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -75,7 +74,7 @@ func HttpGetWithOptionsE(t testing.TestingT, options HttpGetOptions) (int, strin
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)

if err != nil {
return -1, "", err
Expand Down Expand Up @@ -280,7 +279,7 @@ func HTTPDoWithOptionsE(
}

defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)

if err != nil {
return -1, "", err
Expand Down
12 changes: 6 additions & 6 deletions modules/http-helper/http_helper_test.go
Expand Up @@ -3,7 +3,7 @@ package http_helper
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"regexp"
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestErrorWithRetry(t *testing.T) {

func bodyCopyHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
body, _ := ioutil.ReadAll(r.Body)
body, _ := io.ReadAll(r.Body)
w.Write(body)
}

Expand All @@ -168,10 +168,10 @@ func retryHandler(w http.ResponseWriter, r *http.Request) {
if counter > 0 {
counter--
w.WriteHeader(http.StatusServiceUnavailable)
ioutil.ReadAll(r.Body)
io.ReadAll(r.Body)
} else {
w.WriteHeader(http.StatusOK)
bytes, _ := ioutil.ReadAll(r.Body)
bytes, _ := io.ReadAll(r.Body)
w.Write(bytes)
}
}
Expand All @@ -182,10 +182,10 @@ func failRetryHandler(w http.ResponseWriter, r *http.Request) {
if failCounter > 0 {
failCounter--
w.WriteHeader(http.StatusServiceUnavailable)
ioutil.ReadAll(r.Body)
io.ReadAll(r.Body)
} else {
w.WriteHeader(http.StatusOK)
bytes, _ := ioutil.ReadAll(r.Body)
bytes, _ := io.ReadAll(r.Body)
w.Write(bytes)
}
}
3 changes: 1 addition & 2 deletions modules/k8s/config.go
Expand Up @@ -2,7 +2,6 @@ package k8s

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -167,7 +166,7 @@ func CopyHomeKubeConfigToTempE(t testing.TestingT) (string, error) {
if err != nil {
return "", err
}
tmpConfig, err := ioutil.TempFile("", "")
tmpConfig, err := os.CreateTemp("", "")
if err != nil {
return "", gwErrors.WithStackTrace(err)
}
Expand Down
3 changes: 1 addition & 2 deletions modules/k8s/kubectl.go
@@ -1,7 +1,6 @@
package k8s

import (
"io/ioutil"
"net/url"
"os"

Expand Down Expand Up @@ -135,7 +134,7 @@ func StoreConfigToTempFile(t testing.TestingT, configData string) string {
// filename, or error.
func StoreConfigToTempFileE(t testing.TestingT, configData string) (string, error) {
escapedTestName := url.PathEscape(t.Name())
tmpfile, err := ioutil.TempFile("", escapedTestName)
tmpfile, err := os.CreateTemp("", escapedTestName)
if err != nil {
return "", err
}
Expand Down
3 changes: 1 addition & 2 deletions modules/k8s/tunnel.go
Expand Up @@ -8,7 +8,6 @@ package k8s
import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -92,7 +91,7 @@ func NewTunnelWithLogger(
logger logger.TestLogger,
) *Tunnel {
return &Tunnel{
out: ioutil.Discard,
out: io.Discard,
localPort: local,
remotePort: remote,
kubectlOptions: kubectlOptions,
Expand Down
7 changes: 3 additions & 4 deletions modules/logger/parser/store_test.go
@@ -1,7 +1,6 @@
package parser

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -153,10 +152,10 @@ func TestWriteLogWritesToCorrectLogFile(t *testing.T) {
err = logWriter.writeLog(logger, alternativeTestName, alternativeRandomString)
assert.Nil(t, err)

buf, err := ioutil.ReadFile(testFileName)
buf, err := os.ReadFile(testFileName)
assert.Nil(t, err)
assert.Equal(t, string(buf), randomString+"\n")
buf, err = ioutil.ReadFile(alternativeTestFileName)
buf, err = os.ReadFile(alternativeTestFileName)
assert.Nil(t, err)
assert.Equal(t, string(buf), alternativeRandomString+"\n")
}
Expand All @@ -175,7 +174,7 @@ func TestWriteLogCreatesLogFileIfNotExists(t *testing.T) {
assert.Nil(t, err)

assert.True(t, files.FileExists(testFileName))
buf, err := ioutil.ReadFile(testFileName)
buf, err := os.ReadFile(testFileName)
assert.Nil(t, err)
assert.Equal(t, string(buf), randomString+"\n")
}
3 changes: 1 addition & 2 deletions modules/opa/download_policy.go
@@ -1,7 +1,6 @@
package opa

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -58,7 +57,7 @@ func DownloadPolicyE(t testing.TestingT, rulePath string) (string, error) {
}

// Not downloaded, so use go-getter to download the remote source to a temp dir.
tempDir, err := ioutil.TempDir("", "terratest-opa-policy-*")
tempDir, err := os.MkdirTemp("", "terratest-opa-policy-*")
if err != nil {
return "", err
}
Expand Down
5 changes: 2 additions & 3 deletions modules/opa/download_policy_test.go
Expand Up @@ -2,7 +2,6 @@ package opa

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -53,9 +52,9 @@ func TestDownloadPolicyDownloadsRemote(t *testing.T) {
require.NoError(t, err)
assert.NotEqual(t, absPath, path)

localContents, err := ioutil.ReadFile(localPath)
localContents, err := os.ReadFile(localPath)
require.NoError(t, err)
remoteContents, err := ioutil.ReadFile(path)
remoteContents, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, localContents, remoteContents)
}
Expand Down
3 changes: 1 addition & 2 deletions modules/packer/packer.go
Expand Up @@ -4,7 +4,6 @@ package packer
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -106,7 +105,7 @@ func BuildArtifactE(t testing.TestingT, options *Options) (string, error) {
// The built-in env variable defining where plugins are downloaded
const packerPluginPathEnvVar = "PACKER_PLUGIN_PATH"
options.Logger.Logf(t, "Creating a temporary directory for Packer plugins")
pluginDir, err := ioutil.TempDir("", "terratest-packer-")
pluginDir, err := os.MkdirTemp("", "terratest-packer-")
require.NoError(t, err)
if len(options.Env) == 0 {
options.Env = make(map[string]string)
Expand Down
3 changes: 1 addition & 2 deletions modules/ssh/agent.go
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/x509"
"encoding/pem"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -115,7 +114,7 @@ func SshAgentWithKeyPairsE(t testing.TestingT, keyPairs []*KeyPair) (*SshAgent,
logger.Logf(t, "Generating SSH Agent with given KeyPair(s)")

// Instantiate a temporary SSH agent
socketDir, err := ioutil.TempDir("", "ssh-agent-")
socketDir, err := os.MkdirTemp("", "ssh-agent-")
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions modules/terraform/opa_check.go
@@ -1,7 +1,6 @@
package terraform

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -45,7 +44,7 @@ func OPAEvalE(
}

// Create a temporary dir to store all the json files
tmpDir, err := ioutil.TempDir("", "terratest-opa-hcl2json-*")
tmpDir, err := os.MkdirTemp("", "terratest-opa-hcl2json-*")
if err != nil {
return err
}
Expand Down Expand Up @@ -78,13 +77,13 @@ func OPAEvalE(
// HCLFileToJSONFile is a function that takes a path containing HCL code, and converts it to JSON representation and
// writes out the contents to the given path.
func HCLFileToJSONFile(hclPath, jsonOutPath string) error {
fileBytes, err := ioutil.ReadFile(hclPath)
fileBytes, err := os.ReadFile(hclPath)
if err != nil {
return err
}
converted, err := convert.Bytes(fileBytes, hclPath, convert.Options{})
if err != nil {
return err
}
return ioutil.WriteFile(jsonOutPath, converted, 0600)
return os.WriteFile(jsonOutPath, converted, 0600)
}
3 changes: 1 addition & 2 deletions modules/terraform/plan.go
Expand Up @@ -2,7 +2,6 @@ package terraform

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

"github.com/gruntwork-io/terratest/modules/logger"
Expand Down Expand Up @@ -70,7 +69,7 @@ func InitAndPlanAndShowWithStructNoLogTempPlanFile(t testing.TestingT, options *
options.Logger = logger.Discard
defer func() { options.Logger = oldLogger }()

tmpFile, err := ioutil.TempFile("", "terratest-plan-file-")
tmpFile, err := os.CreateTemp("", "terratest-plan-file-")
require.NoError(t, err)
require.NoError(t, tmpFile.Close())
defer require.NoError(t, os.Remove(tmpFile.Name()))
Expand Down
4 changes: 2 additions & 2 deletions modules/terraform/var-file.go
Expand Up @@ -3,7 +3,7 @@ package terraform
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"

Expand Down Expand Up @@ -127,7 +127,7 @@ func GetAllVariablesFromVarFile(t testing.TestingT, fileName string, out interfa
// the value pointed to by out. Returns an error if the specified file does not exist, the specified file is not
// readable, or the specified file cannot be decoded from HCL.
func GetAllVariablesFromVarFileE(t testing.TestingT, fileName string, out interface{}) error {
fileContents, err := ioutil.ReadFile(fileName)
fileContents, err := os.ReadFile(fileName)
if err != nil {
return err
}
Expand Down

0 comments on commit 15cff8c

Please sign in to comment.