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

🌱 Move from io/ioutil to io and os packages #1250

Merged
merged 2 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions checks/cii_best_practices.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"math"
"net/http"
"strings"
Expand Down Expand Up @@ -81,9 +81,9 @@ func CIIBestPractices(c *checker.CheckRequest) checker.CheckResult {
}
defer resp.Body.Close()

b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
if err != nil {
e := sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("ioutil.ReadAll: %v", err))
e := sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("io.ReadAll: %v", err))
return checker.CreateRuntimeErrorResult(CheckCIIBestPractices, e)
}

Expand Down
4 changes: 2 additions & 2 deletions checks/fileparser/github_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package fileparser

import (
"io/ioutil"
stdos "os"
"testing"

"github.com/rhysd/actionlint"
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestGitHubWorkflowShell(t *testing.T) {
tt := tt // Re-initializing variable so it is not changed while executing the closure below
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
content, err := ioutil.ReadFile(tt.filename)
content, err := stdos.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions checks/permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package checks

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

"github.com/ossf/scorecard/v3/checker"
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestGithubTokenPermissions(t *testing.T) {
if tt.filename == "" {
content = make([]byte, 0)
} else {
content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
panic(fmt.Errorf("cannot read file: %w", err))
}
Expand Down
24 changes: 12 additions & 12 deletions checks/pinned_dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package checks

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -105,7 +105,7 @@ func TestGithubWorkflowPinning(t *testing.T) {
var content []byte
var err error

content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestNonGithubWorkflowPinning(t *testing.T) {
if tt.filename == "" {
content = make([]byte, 0)
} else {
content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestGithubWorkflowPkgManagerPinning(t *testing.T) {
var content []byte
var err error

content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestDockerfilePinning(t *testing.T) {
if tt.filename == "" {
content = make([]byte, 0)
} else {
content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestDockerfilePinningWihoutHash(t *testing.T) {
var content []byte
var err error

content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -594,7 +594,7 @@ func TestDockerfileScriptDownload(t *testing.T) {
if tt.filename == "" {
content = make([]byte, 0)
} else {
content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -638,7 +638,7 @@ func TestDockerfileScriptDownloadInfo(t *testing.T) {
var content []byte
var err error

content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -747,7 +747,7 @@ func TestShellScriptDownload(t *testing.T) {
if tt.filename == "" {
content = make([]byte, 0)
} else {
content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -802,7 +802,7 @@ func TestShellScriptDownloadPinned(t *testing.T) {
var content []byte
var err error

content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -879,7 +879,7 @@ func TestGitHubWorflowRunDownload(t *testing.T) {
if tt.filename == "" {
content = make([]byte, 0)
} else {
content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down Expand Up @@ -942,7 +942,7 @@ func TestGitHubWorkflowUsesLineNumber(t *testing.T) {
tt := tt // Re-initializing variable so it is not changed while executing the closure below
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
content, err := ioutil.ReadFile(tt.filename)
content, err := os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions checks/shell_download_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package checks

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

Expand Down Expand Up @@ -75,7 +75,7 @@ func TestIsSupportedShellScriptFile(t *testing.T) {
t.Parallel()
var content []byte
var err error
content, err = ioutil.ReadFile(tt.filename)
content, err = os.ReadFile(tt.filename)
if err != nil {
t.Errorf("cannot read file: %v", err)
}
Expand Down
13 changes: 6 additions & 7 deletions clients/githubrepo/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -115,13 +114,13 @@ func (handler *tarballHandler) getTarball(ctx context.Context, repo *github.Repo
}

// Create a temp file. This automatically appends a random number to the name.
tempDir, err := ioutil.TempDir("", repoDir)
tempDir, err := os.MkdirTemp("", repoDir)
if err != nil {
return fmt.Errorf("ioutil.TempDir: %w", err)
return fmt.Errorf("os.MkdirTemp: %w", err)
}
repoFile, err := ioutil.TempFile(tempDir, repoFilename)
repoFile, err := os.CreateTemp(tempDir, repoFilename)
if err != nil {
return fmt.Errorf("ioutil.TempFile: %w", err)
return fmt.Errorf("os.CreateTemp: %w", err)
}
defer repoFile.Close()
if _, err := io.Copy(repoFile, resp.Body); err != nil {
Expand Down Expand Up @@ -220,9 +219,9 @@ func (handler *tarballHandler) listFiles(predicate func(string) (bool, error)) (
}

func (handler *tarballHandler) getFileContent(filename string) ([]byte, error) {
content, err := ioutil.ReadFile(filepath.Join(handler.tempDir, filename))
content, err := os.ReadFile(filepath.Join(handler.tempDir, filename))
if err != nil {
return content, fmt.Errorf("ioutil.ReadFile: %w", err)
return content, fmt.Errorf("os.ReadFile: %w", err)
}
return content, nil
}
Expand Down
5 changes: 2 additions & 3 deletions clients/githubrepo/tarball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
Expand All @@ -44,11 +43,11 @@ func isSortedString(x, y string) bool {
}

func setup(inputFile string) (tarballHandler, error) {
tempDir, err := ioutil.TempDir("", repoDir)
tempDir, err := os.MkdirTemp("", repoDir)
if err != nil {
return tarballHandler{}, fmt.Errorf("test failed to create TempDir: %w", err)
}
tempFile, err := ioutil.TempFile(tempDir, repoFilename)
tempFile, err := os.CreateTemp(tempDir, repoFilename)
if err != nil {
return tarballHandler{}, fmt.Errorf("test failed to create TempFile: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cron/bq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"strings"
Expand Down Expand Up @@ -122,7 +122,7 @@ func transferDataToBq(ctx context.Context,
return fmt.Errorf("error during http.Post to %s: %w", webhookURL, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading resp.Body: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions cron/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package config

import (
"errors"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -45,7 +44,7 @@ func getByteValueFromFile(filename string) ([]byte, error) {
return nil, nil
}
//nolint
return ioutil.ReadFile(filename)
return os.ReadFile(filename)
}

func TestYAMLParsing(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions cron/data/update/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -123,7 +122,7 @@ func getGoDeps(repo repositoryDepsURL) []data.RepoFormat {
//nolint
defer os.Chdir(pwd)
// creating temp dir for git clone
gitDir, err := ioutil.TempDir(pwd, "")
gitDir, err := os.MkdirTemp(pwd, "")
if err != nil {
log.Default().Println("Cannot create temporary dir", err)
return nil
Expand Down
3 changes: 1 addition & 2 deletions cron/format/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"testing"
Expand Down Expand Up @@ -459,7 +458,7 @@ func TestJSONOutput(t *testing.T) {
t.Parallel()
var content []byte
var err error
content, err = ioutil.ReadFile(tt.expected)
content, err = os.ReadFile(tt.expected)
if err != nil {
t.Fatalf("cannot read file: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cron/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"

Expand All @@ -42,7 +42,7 @@ var images = []string{
func scriptHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
jsonBytes, err := ioutil.ReadAll(r.Body)
jsonBytes, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, fmt.Sprintf("unable to read request body: %v", err),
http.StatusInternalServerError)
Expand Down
18 changes: 9 additions & 9 deletions docs/checks/internal/validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path"
"regexp"
"strings"
Expand Down Expand Up @@ -59,9 +59,9 @@ func listCheckFiles() (map[string]string, error) {
// Use regex to determine the file that contains the entry point.
// We're looking for `const someVarName = "CheckName"`.
regex := regexp.MustCompile(`const\s+[^"]*=\s+"(.*)"`)
files, err := ioutil.ReadDir("checks/")
files, err := os.ReadDir("checks/")
if err != nil {
return nil, fmt.Errorf("ioutil.ReadDir: %w", err)
return nil, fmt.Errorf("os.ReadDir: %w", err)
}

for _, file := range files {
Expand All @@ -70,9 +70,9 @@ func listCheckFiles() (map[string]string, error) {
}

fullpath := path.Join("checks/", file.Name())
content, err := ioutil.ReadFile(fullpath)
content, err := os.ReadFile(fullpath)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadFile: %s: %w", fullpath, err)
return nil, fmt.Errorf("os.ReadFile: %s: %w", fullpath, err)
}

res := regex.FindStringSubmatch(string(content))
Expand All @@ -94,9 +94,9 @@ func listCheckFiles() (map[string]string, error) {
func extractAPINames() ([]string, error) {
fns := []string{}
interfaceRe := regexp.MustCompile(`type\s+RepoClient\s+interface\s+{\s*`)
content, err := ioutil.ReadFile("clients/repo_client.go")
content, err := os.ReadFile("clients/repo_client.go")
if err != nil {
return nil, fmt.Errorf("ioutil.ReadFile: %s: %w", "clients/repo_client.go", err)
return nil, fmt.Errorf("os.ReadFile: %s: %w", "clients/repo_client.go", err)
}

locs := interfaceRe.FindIndex(content)
Expand Down Expand Up @@ -152,9 +152,9 @@ func supportedInterfacesFromImplementation(checkName string, checkFiles map[stri
return nil, fmt.Errorf("check %s does not exists", checkName)
}

content, err := ioutil.ReadFile(pathfn)
content, err := os.ReadFile(pathfn)
if err != nil {
return nil, fmt.Errorf("ioutil.ReadFile: %s: %w", pathfn, err)
return nil, fmt.Errorf("os.ReadFile: %s: %w", pathfn, err)
}

// For each API, check if it's used or not.
Expand Down
Loading