Skip to content

Commit

Permalink
chore: Deprecate usage of ioutil package (sourcegraph#21796)
Browse files Browse the repository at this point in the history
The package was deprecated in Go 1.16:
https://golang.org/doc/go1.16#ioutil

A CI check was also added so that we don't import it again in the future.
  • Loading branch information
ryanslade committed Jun 7, 2021
1 parent 25fc33e commit ef76c4a
Show file tree
Hide file tree
Showing 178 changed files with 560 additions and 566 deletions.
4 changes: 2 additions & 2 deletions cmd/frontend/backend/go_importers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"path"
"path/filepath"
Expand Down Expand Up @@ -93,7 +93,7 @@ func CountGoImporters(ctx context.Context, repo api.RepoName) (count int, err er
Path string
}
}
bytes, err := ioutil.ReadAll(response.Body)
bytes, err := io.ReadAll(response.Body)
if err != nil {
return 0, err
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/frontend/backend/go_importers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package backend
import (
"bytes"
"context"
"io/ioutil"
"io"
"io/fs"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -48,8 +49,8 @@ func TestCountGoImporters(t *testing.T) {
git.Mocks.ResolveRevision = func(spec string, opt git.ResolveRevisionOptions) (api.CommitID, error) {
return "c", nil
}
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]os.FileInfo, error) {
return []os.FileInfo{
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]fs.FileInfo, error) {
return []fs.FileInfo{
&util.FileInfo{Name_: "d/a.go", Mode_: os.ModeDir},
&util.FileInfo{Name_: "d/b.go", Mode_: os.ModeDir},
&util.FileInfo{Name_: "c.go", Mode_: 0},
Expand Down Expand Up @@ -80,7 +81,7 @@ type mockRoundTripper struct {
func (t mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(t.response)),
Body: io.NopCloser(bytes.NewBufferString(t.response)),
Header: make(http.Header),
}, nil
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/frontend/backend/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"io/fs"
"strconv"

"github.com/inconshreveable/log15"
Expand All @@ -31,23 +31,23 @@ func InventoryContext(repo api.RepoName, commitID api.CommitID, forceEnhancedLan
return inventory.Context{}, errors.Errorf("refusing to compute inventory for non-absolute commit ID %q", commitID)
}

cacheKey := func(e os.FileInfo) string {
cacheKey := func(e fs.FileInfo) string {
info, ok := e.Sys().(git.ObjectInfo)
if !ok {
return "" // not cacheable
}
return info.OID().String()
}
invCtx := inventory.Context{
ReadTree: func(ctx context.Context, path string) ([]os.FileInfo, error) {
ReadTree: func(ctx context.Context, path string) ([]fs.FileInfo, error) {
// TODO: As a perf optimization, we could read multiple levels of the Git tree at once
// to avoid sequential tree traversal calls.
return git.ReadDir(ctx, repo, commitID, path, false)
},
NewFileReader: func(ctx context.Context, path string) (io.ReadCloser, error) {
return git.NewFileReader(ctx, repo, commitID, path)
},
CacheGet: func(e os.FileInfo) (inventory.Inventory, bool) {
CacheGet: func(e fs.FileInfo) (inventory.Inventory, bool) {
cacheKey := cacheKey(e)
if cacheKey == "" {
return inventory.Inventory{}, false // not cacheable
Expand All @@ -62,7 +62,7 @@ func InventoryContext(repo api.RepoName, commitID api.CommitID, forceEnhancedLan
}
return inventory.Inventory{}, false
},
CacheSet: func(e os.FileInfo, inv inventory.Inventory) {
CacheSet: func(e fs.FileInfo, inv inventory.Inventory) {
cacheKey := cacheKey(e)
if cacheKey == "" {
return // not cacheable
Expand Down
12 changes: 6 additions & 6 deletions cmd/frontend/backend/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"io/fs"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -124,24 +124,24 @@ func TestReposGetInventory(t *testing.T) {
return &protocol.RepoLookupResult{Repo: &protocol.RepoInfo{Name: wantRepo}}, nil
}
defer func() { repoupdater.MockRepoLookup = nil }()
git.Mocks.Stat = func(commit api.CommitID, path string) (os.FileInfo, error) {
git.Mocks.Stat = func(commit api.CommitID, path string) (fs.FileInfo, error) {
if commit != wantCommitID {
t.Errorf("got commit %q, want %q", commit, wantCommitID)
}
return &util.FileInfo{Name_: path, Mode_: os.ModeDir, Sys_: gitObjectInfo(wantRootOID)}, nil
}
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]os.FileInfo, error) {
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]fs.FileInfo, error) {
if commit != wantCommitID {
t.Errorf("got commit %q, want %q", commit, wantCommitID)
}
switch name {
case "":
return []os.FileInfo{
return []fs.FileInfo{
&util.FileInfo{Name_: "a", Mode_: os.ModeDir, Sys_: gitObjectInfo("oid-a")},
&util.FileInfo{Name_: "b.go", Size_: 12},
}, nil
case "a":
return []os.FileInfo{&util.FileInfo{Name_: "a/c.m", Size_: 24}}, nil
return []fs.FileInfo{&util.FileInfo{Name_: "a/c.m", Size_: 24}}, nil
default:
panic("unhandled mock ReadDir " + name)
}
Expand All @@ -159,7 +159,7 @@ func TestReposGetInventory(t *testing.T) {
default:
panic("unhandled mock ReadFile " + name)
}
return ioutil.NopCloser(bytes.NewReader(data)), nil
return io.NopCloser(bytes.NewReader(data)), nil
}
defer git.ResetMocks()

Expand Down
10 changes: 5 additions & 5 deletions cmd/frontend/graphqlbackend/git_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package graphqlbackend

import (
"context"
"os"
"io/fs"
"path"
"sort"
"strings"
Expand Down Expand Up @@ -30,14 +30,14 @@ func (r *GitTreeEntryResolver) Entries(ctx context.Context, args *gitTreeEntryCo
}

func (r *GitTreeEntryResolver) Directories(ctx context.Context, args *gitTreeEntryConnectionArgs) ([]*GitTreeEntryResolver, error) {
return r.entries(ctx, args, func(fi os.FileInfo) bool { return fi.Mode().IsDir() })
return r.entries(ctx, args, func(fi fs.FileInfo) bool { return fi.Mode().IsDir() })
}

func (r *GitTreeEntryResolver) Files(ctx context.Context, args *gitTreeEntryConnectionArgs) ([]*GitTreeEntryResolver, error) {
return r.entries(ctx, args, func(fi os.FileInfo) bool { return !fi.Mode().IsDir() })
return r.entries(ctx, args, func(fi fs.FileInfo) bool { return !fi.Mode().IsDir() })
}

func (r *GitTreeEntryResolver) entries(ctx context.Context, args *gitTreeEntryConnectionArgs, filter func(fi os.FileInfo) bool) ([]*GitTreeEntryResolver, error) {
func (r *GitTreeEntryResolver) entries(ctx context.Context, args *gitTreeEntryConnectionArgs, filter func(fi fs.FileInfo) bool) ([]*GitTreeEntryResolver, error) {
entries, err := git.ReadDir(
ctx,
r.commit.repoResolver.RepoName(),
Expand Down Expand Up @@ -83,7 +83,7 @@ func (r *GitTreeEntryResolver) entries(ctx context.Context, args *gitTreeEntryCo
return l, nil
}

type byDirectory []os.FileInfo
type byDirectory []fs.FileInfo

func (s byDirectory) Len() int {
return len(s)
Expand Down
7 changes: 4 additions & 3 deletions cmd/frontend/graphqlbackend/git_tree_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphqlbackend

import (
"context"
"io/fs"
"net/url"
neturl "net/url"
"os"
Expand Down Expand Up @@ -43,13 +44,13 @@ type GitTreeEntryResolver struct {

// stat is this tree entry's file info. Its Name method must return the full path relative to
// the root, not the basename.
stat os.FileInfo
stat fs.FileInfo

isRecursive bool // whether entries is populated recursively (otherwise just current level of hierarchy)
isSingleChild *bool // whether this is the single entry in its parent. Only set by the (&GitTreeEntryResolver) entries.
}

func NewGitTreeEntryResolver(commit *GitCommitResolver, db dbutil.DB, stat os.FileInfo) *GitTreeEntryResolver {
func NewGitTreeEntryResolver(commit *GitCommitResolver, db dbutil.DB, stat fs.FileInfo) *GitTreeEntryResolver {
return &GitTreeEntryResolver{db: db, commit: commit, stat: stat}
}

Expand Down Expand Up @@ -192,7 +193,7 @@ func cloneURLToRepoName(ctx context.Context, cloneURL string) (string, error) {
return string(repoName), nil
}

func CreateFileInfo(path string, isDir bool) os.FileInfo {
func CreateFileInfo(path string, isDir bool) fs.FileInfo {
return fileInfo{path: path, isDir: isDir}
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/frontend/graphqlbackend/git_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphqlbackend

import (
"context"
"io/fs"
"os"
"testing"

Expand Down Expand Up @@ -29,7 +30,7 @@ func TestGitTree(t *testing.T) {
}
backend.Mocks.Repos.MockGetCommit_Return_NoCheck(t, &git.Commit{ID: exampleCommitSHA1})

git.Mocks.Stat = func(commit api.CommitID, path string) (os.FileInfo, error) {
git.Mocks.Stat = func(commit api.CommitID, path string) (fs.FileInfo, error) {
if string(commit) != exampleCommitSHA1 {
t.Errorf("got commit %q, want %q", commit, exampleCommitSHA1)
}
Expand All @@ -38,7 +39,7 @@ func TestGitTree(t *testing.T) {
}
return &util.FileInfo{Name_: path, Mode_: os.ModeDir}, nil
}
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]os.FileInfo, error) {
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]fs.FileInfo, error) {
if string(commit) != exampleCommitSHA1 {
t.Errorf("got commit %q, want %q", commit, exampleCommitSHA1)
}
Expand All @@ -48,7 +49,7 @@ func TestGitTree(t *testing.T) {
if recurse {
t.Error("got recurse == false, want true")
}
return []os.FileInfo{
return []fs.FileInfo{
&util.FileInfo{Name_: name + "/testDirectory", Mode_: os.ModeDir},
&util.FileInfo{Name_: name + "/Geoffrey's random queries.32r242442bf", Mode_: os.ModeDir},
&util.FileInfo{Name_: name + "/testFile", Mode_: 0},
Expand Down
8 changes: 4 additions & 4 deletions cmd/frontend/graphqlbackend/graphqlbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestMain(m *testing.M) {
flag.Parse()
if !testing.Verbose() {
log15.Root().SetHandler(log15.LvlFilterHandler(log15.LvlError, log15.Root().GetHandler()))
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
}
os.Exit(m.Run())
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestAffiliatedRepositories(t *testing.T) {
}
}
return &http.Response{
Body: ioutil.NopCloser(buf),
Body: io.NopCloser(buf),
StatusCode: http.StatusOK,
}, nil
},
Expand All @@ -206,7 +206,7 @@ func TestAffiliatedRepositories(t *testing.T) {
t.Fatal(err)
}
return &http.Response{
Body: ioutil.NopCloser(buf),
Body: io.NopCloser(buf),
StatusCode: http.StatusOK,
}, nil
},
Expand Down
3 changes: 1 addition & 2 deletions cmd/frontend/graphqlbackend/repository_comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"html/template"
"io"
"io/ioutil"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -49,7 +48,7 @@ func TestRepositoryComparison(t *testing.T) {
if len(args) < 1 && args[0] != "diff" {
t.Fatalf("gitserver.ExecReader received wrong args: %v", args)
}
return ioutil.NopCloser(strings.NewReader(testDiff + testCopyDiff)), nil
return io.NopCloser(strings.NewReader(testDiff + testCopyDiff)), nil
}
t.Cleanup(func() { git.Mocks.ExecReader = nil })

Expand Down
4 changes: 2 additions & 2 deletions cmd/frontend/graphqlbackend/search_results_stats_languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package graphqlbackend
import (
"context"
"errors"
"os"
"io/fs"
"sync"

"github.com/neelance/parallel"
Expand Down Expand Up @@ -51,7 +51,7 @@ func searchResultsStatsLanguages(ctx context.Context, matches []result.Match) ([

// Records the work necessary for a batch (repoCommit).
type fileStatsWork struct {
fullEntries []os.FileInfo // matched these full files
fullEntries []fs.FileInfo // matched these full files
partialFiles map[string]uint64 // file with line matches (path) -> count of lines matching
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"os"
"io/fs"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -35,7 +34,7 @@ func TestSearchResultsStatsLanguages(t *testing.T) {
default:
panic("unhandled mock NewFileReader " + name)
}
return ioutil.NopCloser(bytes.NewReader(data)), nil
return io.NopCloser(bytes.NewReader(data)), nil
}
const wantDefaultBranchRef = "refs/heads/foo"
git.Mocks.ExecSafe = func(params []string) (stdout, stderr []byte, exitCode int, err error) {
Expand Down Expand Up @@ -66,7 +65,7 @@ func TestSearchResultsStatsLanguages(t *testing.T) {

tests := map[string]struct {
results []result.Match
getFiles []os.FileInfo
getFiles []fs.FileInfo
want []inventory.Lang // TotalBytes values are incorrect (known issue doc'd in GraphQL schema)
}{
"empty": {
Expand Down Expand Up @@ -96,7 +95,7 @@ func TestSearchResultsStatsLanguages(t *testing.T) {
results: []result.Match{
&result.RepoMatch{Name: "r"},
},
getFiles: []os.FileInfo{
getFiles: []fs.FileInfo{
fileInfo{path: "two.go"},
fileInfo{path: "three.go"},
},
Expand All @@ -105,7 +104,7 @@ func TestSearchResultsStatsLanguages(t *testing.T) {
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]os.FileInfo, error) {
git.Mocks.ReadDir = func(commit api.CommitID, name string, recurse bool) ([]fs.FileInfo, error) {
return test.getFiles, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestSetExternalServiceRepos(t *testing.T) {
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte{})),
Body: io.NopCloser(bytes.NewReader([]byte{})),
}, nil
}),
}
Expand Down
Loading

0 comments on commit ef76c4a

Please sign in to comment.