Skip to content

Commit

Permalink
refactor deprecated io/ioutil and k8s.io/utils/ptr packages
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Valdron <[email protected]>
  • Loading branch information
michael-valdron committed Jun 13, 2024
1 parent c9b9207 commit 39abcb9
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 53 deletions.
13 changes: 7 additions & 6 deletions pkg/devfile/generator/generators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ package generator

import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
"reflect"
"strings"
"testing"
"k8s.io/utils/ptr"

v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
Expand Down Expand Up @@ -1066,7 +1067,7 @@ func TestGetDeployment(t *testing.T) {
},
},
Containers: containers,
Replicas: pointer.Int32Ptr(1),
Replicas: ptr.To(int32(1)),
},
expected: appsv1.Deployment{
ObjectMeta: objectMetaDedicatedPod,
Expand All @@ -1083,7 +1084,7 @@ func TestGetDeployment(t *testing.T) {
Containers: containers,
},
},
Replicas: pointer.Int32Ptr(1),
Replicas: ptr.To(int32(1)),
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions pkg/devfile/generator/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
package generator

import (
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"

"github.com/devfile/api/v2/pkg/attributes"
"github.com/devfile/library/pkg/devfile/parser"
"github.com/devfile/library/pkg/devfile/parser/data"
Expand Down
20 changes: 10 additions & 10 deletions pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ package parser
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
"path"
"reflect"
"strings"
"testing"

v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/api/v2/pkg/attributes"
devfilepkg "github.com/devfile/api/v2/pkg/devfile"
Expand All @@ -28,17 +37,8 @@ import (
"github.com/devfile/library/pkg/testingutil"
"github.com/kylelemons/godebug/pretty"
"github.com/stretchr/testify/assert"
"io/ioutil"
kubev1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net"
"net/http"
"net/http/httptest"
"os"
"path"
"reflect"
"sigs.k8s.io/yaml"
"strings"
"testing"
)

const schemaVersion = string(data.APISchemaVersion220)
Expand Down Expand Up @@ -3861,7 +3861,7 @@ func Test_parseFromURI(t *testing.T) {
if err != nil {
fmt.Errorf("Test_parseFromURI() error: failed to marshall devfile data: %v", err)
}
err = ioutil.WriteFile(localRelativeURI, yamlData, 0644)
err = os.WriteFile(localRelativeURI, yamlData, 0644)
if err != nil {
fmt.Errorf("Test_parseFromURI() error: fail to write to file: %v", err)
}
Expand Down
57 changes: 43 additions & 14 deletions pkg/testingutil/filesystem/default_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ limitations under the License.
package filesystem

import (
"io/ioutil"
"os"
"path/filepath"
"time"
)

// DefaultFs implements Filesystem using same-named functions from "os" and "io/ioutil"
// DefaultFs implements Filesystem using same-named functions from "os"
type DefaultFs struct{}

var _ Filesystem = DefaultFs{}
Expand Down Expand Up @@ -97,33 +96,63 @@ func (DefaultFs) Getwd() (dir string, err error) {
return os.Getwd()
}

// ReadFile via ioutil.ReadFile
// ReadFile via os.ReadFile
func (DefaultFs) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
return os.ReadFile(filename)
}

// WriteFile via ioutil.WriteFile
// WriteFile via os.WriteFile
func (DefaultFs) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
return os.WriteFile(filename, data, perm)
}

// TempDir via ioutil.TempDir
func (DefaultFs) TempDir(dir, prefix string) (string, error) {
return ioutil.TempDir(dir, prefix)
// MkdirTemp via os.MkdirTemp
func (DefaultFs) MkdirTemp(dir, prefix string) (string, error) {
return os.MkdirTemp(dir, prefix)
}

// TempFile via ioutil.TempFile
func (DefaultFs) TempFile(dir, prefix string) (File, error) {
file, err := ioutil.TempFile(dir, prefix)
// Deprecated: as ioutil.TempDir is deprecated TempDir is replaced by MkdirTemp which uses os.MkdirTemp.
// TempDir now uses MkdirTemp.
func (fs DefaultFs) TempDir(dir, prefix string) (string, error) {
return fs.MkdirTemp(dir, prefix)
}

// CreateTemp via os.CreateTemp
func (DefaultFs) CreateTemp(dir, prefix string) (File, error) {
file, err := os.CreateTemp(dir, prefix)
if err != nil {
return nil, err
}
return &defaultFile{file}, nil
}

// ReadDir via ioutil.ReadDir
// TempFile via ioutil.TempFile
// Deprecated: as ioutil.TempFile is deprecated TempFile is replaced by CreateTemp which uses os.CreateTemp.
// TempFile now uses CreateTemp.
func (fs DefaultFs) TempFile(dir, prefix string) (File, error) {
return fs.CreateTemp(dir, prefix)
}

// ReadDir via os.ReadDir
func (DefaultFs) ReadDir(dirname string) ([]os.FileInfo, error) {
return ioutil.ReadDir(dirname)
dirEntries, err := os.ReadDir(dirname)

if err != nil {
return []os.FileInfo{}, err
}

dirsInfo := make([]os.FileInfo, 0, len(dirEntries))
for _, dirEntry := range dirEntries {
info, err := dirEntry.Info()

if err != nil {
return dirsInfo, err
}

dirsInfo = append(dirsInfo, info)
}

return dirsInfo, nil
}

// Walk via filepath.Walk
Expand Down
4 changes: 2 additions & 2 deletions pkg/testingutil/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ type Filesystem interface {
Remove(name string) error
Chmod(name string, mode os.FileMode) error
Getwd() (dir string, err error)

// from "io/ioutil"
ReadFile(filename string) ([]byte, error)
WriteFile(filename string, data []byte, perm os.FileMode) error
TempDir(dir, prefix string) (string, error)
TempFile(dir, prefix string) (File, error)
ReadDir(dirname string) ([]os.FileInfo, error)

// from "filepath"
Walk(root string, walkFn filepath.WalkFunc) error
}

Expand Down
15 changes: 13 additions & 2 deletions pkg/util/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package util

import (
"io/ioutil"
"os"
"path/filepath"
"time"
Expand All @@ -26,11 +25,23 @@ import (

// cleanHttpCache checks cacheDir and deletes all files that were modified more than cacheTime back
func cleanHttpCache(cacheDir string, cacheTime time.Duration) error {
cacheFiles, err := ioutil.ReadDir(cacheDir)
dirEntries, err := os.ReadDir(cacheDir)

if err != nil {
return err
}

cacheFiles := make([]os.FileInfo, 0, len(dirEntries))
for _, dirEntry := range dirEntries {
info, err := dirEntry.Info()

if err != nil {
return err
}

cacheFiles = append(cacheFiles, info)
}

for _, f := range cacheFiles {
if f.ModTime().Add(cacheTime).Before(time.Now()) {
klog.V(4).Infof("Removing cache file %s, because it is older than %s", f.Name(), cacheTime.String())
Expand Down
19 changes: 15 additions & 4 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"math/big"
"net"
"net/http"
Expand Down Expand Up @@ -789,7 +788,7 @@ func HTTPGetRequest(request HTTPRequestParams, cacheFor int) ([]byte, error) {
}

// Process http response
bytes, err := ioutil.ReadAll(resp.Body)
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -825,11 +824,23 @@ func FilterIgnores(filesChanged, filesDeleted, absIgnoreRules []string) (filesCh
// IsValidProjectDir checks that the folder to download the project from devfile is
// either empty or only contains the devfile used.
func IsValidProjectDir(path string, devfilePath string) error {
files, err := ioutil.ReadDir(path)
dirEntries, err := os.ReadDir(path)

if err != nil {
return err
}

files := make([]os.FileInfo, 0, len(dirEntries))
for _, dirEntry := range dirEntries {
info, err := dirEntry.Info()

if err != nil {
return err
}

files = append(files, info)
}

if len(files) > 1 {
return errors.Errorf("Folder %s is not empty. It can only contain the devfile used.", path)
} else if len(files) == 1 {
Expand Down Expand Up @@ -1050,7 +1061,7 @@ func DownloadFileInMemory(url string) ([]byte, error) {
}
defer resp.Body.Close()

return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

// ValidateK8sResourceName sanitizes kubernetes resource name with the following requirements:
Expand Down
15 changes: 13 additions & 2 deletions tests/v2/integrationTest/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package api
import (
"context"
"fmt"
"io/ioutil"
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -44,12 +44,23 @@ func getTestDevfileList(subdir string, testValid bool) ([]string, error) {

fileList := make([]string, 0)
subdirPath := "../../examples/source/devfiles/" + subdir + "/"
files, err := ioutil.ReadDir(subdirPath)
dirEntries, err := os.ReadDir(subdirPath)

if err != nil {
commonUtils.LogErrorMessage(fmt.Sprintf("Error in getting file list from the directory: %s : %v", subdirPath, err))
}

files := make([]os.FileInfo, 0, len(dirEntries))
for _, dirEntry := range dirEntries {
info, err := dirEntry.Info()

if err != nil {
commonUtils.LogErrorMessage(fmt.Sprintf("Error in getting file information from file list in: %s : %v", subdirPath, err))
}

files = append(files, info)
}

for _, file := range files {
if !file.IsDir() {
r, err := regexp.MatchString("^devfile.+yaml$", file.Name())
Expand Down
6 changes: 3 additions & 3 deletions tests/v2/utils/library/command_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package utils
import (
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/google/go-cmp/cmp"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -81,7 +81,7 @@ func VerifyCommands(devfile *commonUtils.TestDevfile, parserCommands []schema.Co
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename)))
} else {
err = ioutil.WriteFile(parserFilename, c, 0644)
err = os.WriteFile(parserFilename, c, 0644)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename)))
}
Expand All @@ -91,7 +91,7 @@ func VerifyCommands(devfile *commonUtils.TestDevfile, parserCommands []schema.Co
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename)))
} else {
err = ioutil.WriteFile(testFilename, c, 0644)
err = os.WriteFile(testFilename, c, 0644)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename)))
}
Expand Down
6 changes: 3 additions & 3 deletions tests/v2/utils/library/component_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package utils
import (
"errors"
"fmt"
"io/ioutil"
"os"

schema "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
commonUtils "github.com/devfile/api/v2/test/v200/utils/common"
Expand Down Expand Up @@ -87,7 +87,7 @@ func VerifyComponents(devfile *commonUtils.TestDevfile, parserComponents []schem
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", parserFilename)))
} else {
err = ioutil.WriteFile(parserFilename, c, 0644)
err = os.WriteFile(parserFilename, c, 0644)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", parserFilename)))
}
Expand All @@ -97,7 +97,7 @@ func VerifyComponents(devfile *commonUtils.TestDevfile, parserComponents []schem
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......marshall devfile %s", testFilename)))
} else {
err = ioutil.WriteFile(testFilename, c, 0644)
err = os.WriteFile(testFilename, c, 0644)
if err != nil {
errorString = append(errorString, commonUtils.LogErrorMessage(fmt.Sprintf(".......write devfile %s", testFilename)))
}
Expand Down
Loading

0 comments on commit 39abcb9

Please sign in to comment.