Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Go 1.19 #203

Merged
merged 8 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: 1.19
go-version-file: 'go.mod'
- name: Run tests
run: make test
- name: Codecov
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Check out code into the Go module directory
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Setup Go environment
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
with:
go-version: 1.19
go-version-file: 'go.mod'
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

- name: Check go mod status
run: |
make gomod_tidy
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/devfile/library/v2

go 1.18
go 1.19

require (
github.com/devfile/api/v2 v2.2.1
Expand Down
3 changes: 1 addition & 2 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
Expand Down Expand Up @@ -573,7 +572,7 @@ func getDevfileFromRegistry(id, registryURL, version string, httpTimeout *int) (
}

func getResourcesFromRegistry(id, registryURL, destDir string) error {
stackDir, err := ioutil.TempDir(os.TempDir(), fmt.Sprintf("registry-resources-%s", id))
stackDir, err := os.MkdirTemp(os.TempDir(), fmt.Sprintf("registry-resources-%s", id))
if err != nil {
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -4095,7 +4094,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 Expand Up @@ -5180,7 +5179,7 @@ spec:
fmt.Errorf("Test_getKubernetesDefinitionFromUri() error: failed to create folder: %v, error: %v", path.Dir(localDeployFilePath), err)
}

err = ioutil.WriteFile(localDeployFilePath, []byte(deployContent), 0644)
err = os.WriteFile(localDeployFilePath, []byte(deployContent), 0644)
if err != nil {
fmt.Errorf("Test_getKubernetesDefinitionFromUri() error: fail to write to file: %v", err)
}
Expand Down
56 changes: 43 additions & 13 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,64 @@ 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)
}

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

// TempDir via ioutil.TempDir
func (DefaultFs) TempDir(dir, prefix string) (string, error) {
return ioutil.TempDir(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)
}

// TempFile via ioutil.TempFile
func (DefaultFs) TempFile(dir, prefix string) (File, error) {
file, err := ioutil.TempFile(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
13 changes: 11 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,21 @@

// 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)
cacheEntries, err := os.ReadDir(cacheDir)

Check warning on line 28 in pkg/util/httpcache.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/httpcache.go#L28

Added line #L28 was not covered by tests
if err != nil {
return err
}

cacheFiles := make([]os.FileInfo, 0, len(cacheEntries))
for _, cacheEntry := range cacheEntries {
info, err := cacheEntry.Info()
if err != nil {
return err
}

Check warning on line 38 in pkg/util/httpcache.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/httpcache.go#L33-L38

Added lines #L33 - L38 were not covered by tests

cacheFiles = append(cacheFiles, info)

Check warning on line 40 in pkg/util/httpcache.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/httpcache.go#L40

Added line #L40 was not covered by tests
}

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: 14 additions & 5 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"math/big"
"net"
"net/http"
Expand Down Expand Up @@ -820,7 +819,7 @@
}

// 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 @@ -856,11 +855,21 @@
// 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)
fileEntries, err := os.ReadDir(path)

Check warning on line 858 in pkg/util/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/util.go#L858

Added line #L858 was not covered by tests
if err != nil {
return err
}

files := make([]os.FileInfo, 0, len(fileEntries))
for _, fileEntry := range fileEntries {
info, err := fileEntry.Info()
if err != nil {
return err
}

Check warning on line 868 in pkg/util/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/util.go#L863-L868

Added lines #L863 - L868 were not covered by tests

files = append(files, info)

Check warning on line 870 in pkg/util/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/util.go#L870

Added line #L870 was not covered by tests
}

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 @@ -1091,7 +1100,7 @@
}
defer resp.Body.Close()

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

Check warning on line 1103 in pkg/util/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/util.go#L1103

Added line #L1103 was not covered by tests
}

// DownloadInMemory uses HTTPRequestParams to download the file and return bytes.
Expand Down Expand Up @@ -1147,7 +1156,7 @@
}
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
20 changes: 10 additions & 10 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ package util

import (
"fmt"
"github.com/devfile/library/v2/pkg/testingutil/filesystem"
"github.com/kylelemons/godebug/pretty"
"github.com/stretchr/testify/assert"
"io/ioutil"
corev1 "k8s.io/api/core/v1"
"net"
"net/http"
"net/http/httptest"
Expand All @@ -33,6 +28,11 @@ import (
"runtime"
"strconv"
"testing"

"github.com/devfile/library/v2/pkg/testingutil/filesystem"
"github.com/kylelemons/godebug/pretty"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
)

func TestNamespaceOpenShiftObject(t *testing.T) {
Expand Down Expand Up @@ -919,7 +919,7 @@ func TestDownloadFile(t *testing.T) {
t.Errorf("Failed to download file with error %s", err)
}

got, err := ioutil.ReadFile(tt.filepath)
got, err := os.ReadFile(tt.filepath)
if err != nil {
t.Errorf("Failed to read file with error %s", err)
}
Expand Down Expand Up @@ -1043,11 +1043,11 @@ func TestValidateK8sResourceName(t *testing.T) {

func TestValidateFile(t *testing.T) {
// Create temp dir and temp file
tempDir, err := ioutil.TempDir("", "")
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
}
tempFile, err := ioutil.TempFile(tempDir, "")
tempFile, err := os.CreateTemp(tempDir, "")
if err != nil {
t.Errorf("Failed to create temp file: %s, error: %v", tempFile.Name(), err)
}
Expand Down Expand Up @@ -1086,13 +1086,13 @@ func TestValidateFile(t *testing.T) {

func TestCopyFile(t *testing.T) {
// Create temp dir
tempDir, err := ioutil.TempDir("", "")
tempDir, err := os.MkdirTemp("", "")
if err != nil {
t.Errorf("Failed to create temp dir: %s, error: %v", tempDir, err)
}

// Create temp file under temp dir as source file
tempFile, err := ioutil.TempFile(tempDir, "")
tempFile, err := os.CreateTemp(tempDir, "")
if err != nil {
t.Errorf("Failed to create temp file: %s, error: %v", tempFile.Name(), err)
}
Expand Down
3 changes: 1 addition & 2 deletions replaceSchemaFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import (
"fmt"
"io/ioutil"
"os"
"strings"
)
Expand All @@ -38,7 +37,7 @@
fmt.Printf("Writing to file: %s\n", filePath)
fileContent := fmt.Sprintf("package %s\n\n// %s\nconst %s = `%s\n`\n", packageVersion, schemaURL, jsonSchemaVersion, newSchema)

if err := ioutil.WriteFile(filePath, []byte(fileContent), 0644); err != nil {
if err := os.WriteFile(filePath, []byte(fileContent), 0644); err != nil {
Fixed Show fixed Hide fixed
printErr(err)
os.Exit(1)
}
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
Loading
Loading