Skip to content

Commit

Permalink
Fix #2886: replaces deprecated ioutils package. (#3137)
Browse files Browse the repository at this point in the history
Co-authored-by: Bobby Gryzynger <bobby.gryzynger@mediacurrent.com>
  • Loading branch information
rfay and Bobby Gryzynger committed Aug 3, 2021
1 parent 763e703 commit 827df16
Show file tree
Hide file tree
Showing 25 changed files with 100 additions and 107 deletions.
8 changes: 3 additions & 5 deletions cmd/ddev/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (

"fmt"

"io/ioutil"

"github.com/drud/ddev/pkg/ddevapp"
"github.com/drud/ddev/pkg/exec"
"github.com/drud/ddev/pkg/testcommon"
Expand Down Expand Up @@ -216,7 +214,7 @@ func TestConfigSetValues(t *testing.T) {
assert.NoError(err, "error running ddev %v: %v, output=%s", args, err, out)

configFile := filepath.Join(tmpdir, ".ddev", "config.yaml")
configContents, err := ioutil.ReadFile(configFile)
configContents, err := os.ReadFile(configFile)
if err != nil {
t.Errorf("Unable to read %s: %v", configFile, err)
}
Expand Down Expand Up @@ -271,7 +269,7 @@ func TestConfigSetValues(t *testing.T) {
_, err = exec.RunCommand(DdevBin, args)
assert.NoError(err)

configContents, err = ioutil.ReadFile(configFile)
configContents, err = os.ReadFile(configFile)
assert.NoError(err, "Unable to read %s: %v", configFile, err)

app = &ddevapp.DdevApp{}
Expand Down Expand Up @@ -306,7 +304,7 @@ func TestConfigSetValues(t *testing.T) {
_, err = exec.RunCommand(DdevBin, args)
assert.NoError(err)

configContents, err = ioutil.ReadFile(configFile)
configContents, err = os.ReadFile(configFile)
assert.NoError(err, "Unable to read %s: %v", configFile, err)

app = &ddevapp.DdevApp{}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ddev/cmd/debug-compose-config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

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

Expand Down Expand Up @@ -48,7 +48,7 @@ func TestComposeConfigCmd(t *testing.T) {

// Create a docker-compose.override.yaml
overrideFile := filepath.Join(tmpdir, ".ddev", "docker-compose.override.yaml")
err = ioutil.WriteFile(overrideFile, []byte(override), 0644)
err = os.WriteFile(overrideFile, []byte(override), 0644)
assert.NoError(t, err)

// Ensure ddev debug compose-config includes override values
Expand Down
4 changes: 2 additions & 2 deletions cmd/ddev/cmd/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/drud/ddev/pkg/ddevapp"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -92,7 +92,7 @@ func TestCmdExec(t *testing.T) {
assert.NoError(err)
require.FileExists(t, filepath.Join(site.Dir, filename))

content, err := ioutil.ReadFile(filepath.Join(site.Dir, filename))
content, err := os.ReadFile(filepath.Join(site.Dir, filename))
assert.NoError(err)
assert.Equal("This file was piped into ddev exec", string(content))
}
4 changes: 2 additions & 2 deletions cmd/ddev/cmd/share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
asrt "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"io"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestShareCmd(t *testing.T) {
}
//nolint: errcheck
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
assert.NoError(err)
assert.Contains(string(body), site.Safe200URIWithExpectation.Expect)
urlRead = true
Expand Down
3 changes: 1 addition & 2 deletions pkg/archive/archive_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package archive_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestUnarchive(t *testing.T) {
func TestArchiveTar(t *testing.T) {
assert := asrt.New(t)
pwd, _ := os.Getwd()
tarballFile, err := ioutil.TempFile("", t.Name())
tarballFile, err := os.CreateTemp("", t.Name())
assert.NoError(err)

err = archive.Tar(filepath.Join(pwd, "testdata", t.Name()), tarballFile.Name())
Expand Down
7 changes: 3 additions & 4 deletions pkg/ddevapp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/drud/ddev/pkg/dockerutil"
"github.com/drud/ddev/pkg/nodeps"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -209,7 +208,7 @@ func (app *DdevApp) WriteConfig() error {
cfgbytes = append(cfgbytes, []byte(ConfigInstructions)...)
cfgbytes = append(cfgbytes, appcopy.GetHookDefaultComments()...)

err = ioutil.WriteFile(appcopy.ConfigPath, cfgbytes, 0644)
err = os.WriteFile(appcopy.ConfigPath, cfgbytes, 0644)
if err != nil {
return err
}
Expand Down Expand Up @@ -317,7 +316,7 @@ func (app *DdevApp) ReadConfig(includeOverrides bool) ([]string, error) {

// LoadConfigYamlFile loads one config.yaml into app, overriding what might be there.
func (app *DdevApp) LoadConfigYamlFile(filePath string) error {
source, err := ioutil.ReadFile(filePath)
source, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("could not find an active ddev configuration at %s have you run 'ddev config'? %v", app.ConfigPath, err)
}
Expand Down Expand Up @@ -871,7 +870,7 @@ func WriteImageDockerfile(fullpath string, contents []byte) error {
if err != nil {
return err
}
err = ioutil.WriteFile(fullpath, contents, 0644)
err = os.WriteFile(fullpath, contents, 0644)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/ddevapp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/drud/ddev/pkg/nodeps"
"github.com/mitchellh/go-homedir"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestWriteDockerComposeYaml(t *testing.T) {
assert.False(fileinfo.IsDir())
assert.Equal(fileinfo.Name(), filepath.Base(app.DockerComposeYAMLPath()))

composeBytes, err := ioutil.ReadFile(app.DockerComposeYAMLPath())
composeBytes, err := os.ReadFile(app.DockerComposeYAMLPath())
assert.NoError(err)
contentString := string(composeBytes)
assert.Contains(contentString, app.Type)
Expand Down
15 changes: 12 additions & 3 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"io/fs"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -433,7 +433,7 @@ func (app *DdevApp) ImportDB(imPath string, extPath string, progress bool, noDro
targetDB = "db"
}
var extPathPrompt bool
dbPath, err := ioutil.TempDir(filepath.Dir(app.ConfigPath), ".importdb")
dbPath, err := os.MkdirTemp(filepath.Dir(app.ConfigPath), ".importdb")
//nolint: errcheck
defer os.RemoveAll(dbPath)
if err != nil {
Expand Down Expand Up @@ -1673,11 +1673,20 @@ func (app *DdevApp) ListSnapshots() ([]string, error) {
return snapshots, nil
}

files, err := ioutil.ReadDir(snapshotDir)
fileNames, err := fileutil.ListFilesInDir(snapshotDir)
if err != nil {
return snapshots, err
}

files := []fs.FileInfo{}
for _, n := range fileNames {
f, err := os.Stat(filepath.Join(snapshotDir, n))
if err != nil {
return snapshots, err
}
files = append(files, f)
}

// Sort snapshots by last modification time
// we need that to detect the latest snapshot
// first snapshot is the latest
Expand Down
23 changes: 11 additions & 12 deletions pkg/ddevapp/ddevapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ddevapp_test
import (
"bufio"
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -1877,15 +1876,15 @@ func TestDdevImportFilesDir(t *testing.T) {
app := &ddevapp.DdevApp{}

// Create a dummy directory to test non-archive imports
importDir, err := ioutil.TempDir("", t.Name())
importDir, err := os.MkdirTemp("", t.Name())
assert.NoError(err)
fileNames := make([]string, 0)
for i := 0; i < 5; i++ {
fileName := uuid.New().String()
fileNames = append(fileNames, fileName)

fullPath := filepath.Join(importDir, fileName)
err = ioutil.WriteFile(fullPath, []byte(fileName), 0644)
err = os.WriteFile(fullPath, []byte(fileName), 0644)
assert.NoError(err)
}

Expand All @@ -1909,12 +1908,12 @@ func TestDdevImportFilesDir(t *testing.T) {

// Confirm contents of destination dir after import
absUploadDir := filepath.Join(app.AppRoot, app.Docroot, app.GetUploadDir())
uploadedFiles, err := ioutil.ReadDir(absUploadDir)
uploadedFilesDirEntrySlice, err := os.ReadDir(absUploadDir)
assert.NoError(err)

uploadedFilesMap := map[string]bool{}
for _, uploadedFile := range uploadedFiles {
uploadedFilesMap[filepath.Base(uploadedFile.Name())] = true
for _, de := range uploadedFilesDirEntrySlice {
uploadedFilesMap[filepath.Base(de.Name())] = true
}

for _, expectedFile := range fileNames {
Expand Down Expand Up @@ -2004,9 +2003,9 @@ func TestDdevImportFilesCustomUploadDir(t *testing.T) {
assert.NoError(err)

// Ensure upload dir isn't empty
fileInfoSlice, err := ioutil.ReadDir(absUploadDir)
dirEntrySlice, err := os.ReadDir(absUploadDir)
assert.NoError(err)
assert.NotEmpty(fileInfoSlice)
assert.NotEmpty(dirEntrySlice)
}

if site.FilesZipballURL != "" {
Expand All @@ -2016,9 +2015,9 @@ func TestDdevImportFilesCustomUploadDir(t *testing.T) {
assert.NoError(err)

// Ensure upload dir isn't empty
fileInfoSlice, err := ioutil.ReadDir(absUploadDir)
dirEntrySlice, err := os.ReadDir(absUploadDir)
assert.NoError(err)
assert.NotEmpty(fileInfoSlice)
assert.NotEmpty(dirEntrySlice)
}

if site.FullSiteTarballURL != "" && site.FullSiteArchiveExtPath != "" {
Expand All @@ -2028,9 +2027,9 @@ func TestDdevImportFilesCustomUploadDir(t *testing.T) {
assert.NoError(err)

// Ensure upload dir isn't empty
fileInfoSlice, err := ioutil.ReadDir(absUploadDir)
dirEntrySlice, err := os.ReadDir(absUploadDir)
assert.NoError(err)
assert.NotEmpty(fileInfoSlice)
assert.NotEmpty(dirEntrySlice)
}

runTime()
Expand Down
7 changes: 3 additions & 4 deletions pkg/ddevapp/drupal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/drud/ddev/pkg/output"
"github.com/drud/ddev/pkg/util"

"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -249,7 +248,7 @@ func writeDrupalSettingsFile(filePath string, appType string) error {
}

// Create file
err = ioutil.WriteFile(filePath, content, 0755)
err = os.WriteFile(filePath, content, 0755)
if err != nil {
return err
}
Expand Down Expand Up @@ -497,7 +496,7 @@ if (getenv('IS_DDEV_PROJECT') == 'true') {
return err
}

err := ioutil.WriteFile(filePath, drushContents, 0666)
err := os.WriteFile(filePath, drushContents, 0666)
if err != nil {
return err
}
Expand Down Expand Up @@ -713,7 +712,7 @@ func settingsHasInclude(drupalConfig *DrupalSettings, siteSettingsPath string) (
// file, which contains ddev-specific configuration.
func appendIncludeToDrupalSettingsFile(siteSettingsPath string, appType string) error {
// Check if file is empty
contents, err := ioutil.ReadFile(siteSettingsPath)
contents, err := os.ReadFile(siteSettingsPath)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/ddevapp/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ddevapp
import (
"embed"
"github.com/drud/ddev/pkg/fileutil"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -35,7 +34,7 @@ func CopyEmbedAssets(fsys embed.FS, sourceDir string, targetDir string) error {
if err != nil {
return err
}
err = ioutil.WriteFile(localPath, content, 0755)
err = os.WriteFile(localPath, content, 0755)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/ddevapp/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ddevapp

import (
"github.com/drud/ddev/pkg/output"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -324,7 +323,7 @@ func (p *Provider) Write(configPath string) error {

// Read generic provider configuration from a specified location on disk.
func (p *Provider) Read(configPath string) error {
source, err := ioutil.ReadFile(configPath)
source, err := os.ReadFile(configPath)
if err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/ddevapp/providerAcquia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/drud/ddev/pkg/globalconfig"
"github.com/drud/ddev/pkg/nodeps"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -96,10 +95,10 @@ func TestAcquiaPull(t *testing.T) {
require.NoError(t, err)

// Build our acquia.yaml from the example file
s, err := ioutil.ReadFile(app.GetConfigPath("providers/acquia.yaml.example"))
s, err := os.ReadFile(app.GetConfigPath("providers/acquia.yaml.example"))
require.NoError(t, err)
x := strings.Replace(string(s), "project_id:", fmt.Sprintf("project_id: %s\n#project_id:", acquiaTestSite), -1)
err = ioutil.WriteFile(app.GetConfigPath("providers/acquia.yaml"), []byte(x), 0666)
err = os.WriteFile(app.GetConfigPath("providers/acquia.yaml"), []byte(x), 0666)
assert.NoError(err)
err = app.WriteConfig()
require.NoError(t, err)
Expand Down Expand Up @@ -194,10 +193,10 @@ func TestAcquiaPush(t *testing.T) {
require.NoError(t, err)

// Build our acquia.yaml from the example file
s, err := ioutil.ReadFile(app.GetConfigPath("providers/acquia.yaml.example"))
s, err := os.ReadFile(app.GetConfigPath("providers/acquia.yaml.example"))
require.NoError(t, err)
x := strings.Replace(string(s), "project_id:", fmt.Sprintf("project_id: %s\n#project_id:", acquiaTestSite), -1)
err = ioutil.WriteFile(app.GetConfigPath("providers/acquia.yaml"), []byte(x), 0666)
err = os.WriteFile(app.GetConfigPath("providers/acquia.yaml"), []byte(x), 0666)
assert.NoError(err)
err = app.WriteConfig()
require.NoError(t, err)
Expand All @@ -223,7 +222,7 @@ func TestAcquiaPush(t *testing.T) {
require.NoError(t, err)
fName := tval + ".txt"
fContent := []byte(tval)
err = ioutil.WriteFile(filepath.Join(siteDir, "sites/default/files", fName), fContent, 0644)
err = os.WriteFile(filepath.Join(siteDir, "sites/default/files", fName), fContent, 0644)
assert.NoError(err)

err = app.Push(provider, false, false)
Expand Down

0 comments on commit 827df16

Please sign in to comment.