Skip to content

Commit

Permalink
Merge pull request #120 from rinchsan/replace-deprecated-function-usages
Browse files Browse the repository at this point in the history
  • Loading branch information
rinchsan authored Sep 16, 2022
2 parents 8389fc4 + 4b77714 commit f261744
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 52 deletions.
37 changes: 21 additions & 16 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,40 @@ on:

jobs:

go-versions:
name: Fetch Go versions
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.versions.outputs.value }}
steps:
- name: Fetch versions
id: versions
run: |
versions=$(curl -s 'https://go.dev/dl/?mode=json' | jq -c 'map(.version[2:])')
echo "::set-output name=value::${versions}"
lint:
name: Lint
needs:
- go-versions
strategy:
matrix:
go-version: ${{ fromJson(needs.go-versions.outputs.versions) }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.49
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19.x
- name: Run govulncheck
run: go run golang.org/x/vuln/cmd/govulncheck@latest ./...

go-versions:
name: Fetch Go versions
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.versions.outputs.value }}
steps:
- name: Fetch versions
id: versions
run: |
versions=$(curl -s 'https://go.dev/dl/?mode=json' | jq -c 'map(.version[2:])')
echo "::set-output name=value::${versions}"
test:
name: Test
needs:
Expand Down
5 changes: 2 additions & 3 deletions cmd/gosimports/gosimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fmt"
"go/scanner"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -163,7 +162,7 @@ func processFile(filename string, in io.Reader, out io.Writer, argType argumentT
if fi, err := os.Stat(filename); err == nil {
perms = fi.Mode() & os.ModePerm
}
err = ioutil.WriteFile(filename, res, perms)
err = os.WriteFile(filename, res, perms)
if err != nil {
return err
}
Expand Down Expand Up @@ -316,7 +315,7 @@ func gofmtMain() {
}

func writeTempFile(dir, prefix string, data []byte) (string, error) {
file, err := ioutil.TempFile(dir, prefix)
file, err := os.CreateTemp(dir, prefix)
if err != nil {
return "", err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/fastwalk/fastwalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -35,7 +34,7 @@ func formatFileModes(m map[string]os.FileMode) string {
}

func testFastWalk(t *testing.T, files map[string]string, callback func(path string, typ os.FileMode) error, want map[string]os.FileMode) {
tempdir, err := ioutil.TempDir("", "test-fast-walk")
tempdir, err := os.MkdirTemp("", "test-fast-walk")
if err != nil {
t.Fatal(err)
}
Expand All @@ -51,7 +50,7 @@ func testFastWalk(t *testing.T, files map[string]string, callback func(path stri
if strings.HasPrefix(contents, "LINK:") {
symlinks[file] = filepath.FromSlash(strings.TrimPrefix(contents, "LINK:"))
} else {
err = ioutil.WriteFile(file, []byte(contents), 0644)
err = os.WriteFile(file, []byte(contents), 0644)
}
if err != nil {
t.Fatal(err)
Expand All @@ -63,7 +62,7 @@ func testFastWalk(t *testing.T, files map[string]string, callback func(path stri
for file, dst := range symlinks {
err = os.Symlink(dst, file)
if err != nil {
if writeErr := ioutil.WriteFile(file, []byte(dst), 0644); writeErr == nil {
if writeErr := os.WriteFile(file, []byte(dst), 0644); writeErr == nil {
// Couldn't create symlink, but could write the file.
// Probably this filesystem doesn't support symlinks.
// (Perhaps we are on an older Windows and not running as administrator.)
Expand Down
3 changes: 1 addition & 2 deletions internal/gopathwalk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -135,7 +134,7 @@ func (w *walker) init() {
// The provided path is one of the $GOPATH entries with "src" appended.
func (w *walker) getIgnoredDirs(path string) []string {
file := filepath.Join(path, ".goimportsignore")
slurp, err := ioutil.ReadFile(file)
slurp, err := os.ReadFile(file)
if w.opts.Logf != nil {
if err != nil {
w.opts.Logf("%v", err)
Expand Down
9 changes: 4 additions & 5 deletions internal/gopathwalk/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package gopathwalk

import (
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -22,7 +21,7 @@ func TestShouldTraverse(t *testing.T) {
t.Skipf("skipping symlink-requiring test on %s", runtime.GOOS)
}

dir, err := ioutil.TempDir("", "gosimports-")
dir, err := os.MkdirTemp("", "gosimports-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -90,7 +89,7 @@ func TestShouldTraverse(t *testing.T) {

// TestSkip tests that various gosimports rules are followed in non-modules mode.
func TestSkip(t *testing.T) {
dir, err := ioutil.TempDir("", "gosimports-")
dir, err := os.MkdirTemp("", "gosimports-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -125,7 +124,7 @@ func TestSkip(t *testing.T) {

// TestSkipFunction tests that scan successfully skips directories from user callback.
func TestSkipFunction(t *testing.T) {
dir, err := ioutil.TempDir("", "gosimports-")
dir, err := os.MkdirTemp("", "gosimports-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -165,7 +164,7 @@ func mapToDir(destDir string, files map[string]string) error {
if strings.HasPrefix(contents, "LINK:") {
err = os.Symlink(strings.TrimPrefix(contents, "LINK:"), file)
} else {
err = ioutil.WriteFile(file, []byte(contents), 0644)
err = os.WriteFile(file, []byte(contents), 0644)
}
if err != nil {
return err
Expand Down
30 changes: 26 additions & 4 deletions internal/imports/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"go/build"
"go/parser"
"go/token"
"io/ioutil"
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -106,7 +106,7 @@ func parseOtherFiles(fset *token.FileSet, srcDir, filename string) []*ast.File {
considerTests := strings.HasSuffix(filename, "_test.go")

fileBase := filepath.Base(filename)
packageFileInfos, err := ioutil.ReadDir(srcDir)
packageFileInfos, err := os.ReadDir(srcDir)
if err != nil {
return nil
}
Expand Down Expand Up @@ -858,7 +858,21 @@ func (e *ProcessEnv) buildContext() (*build.Context, error) {
// HACK: setting any of the Context I/O hooks prevents Import from invoking
// 'go list', regardless of GO111MODULE. This is undocumented, but it's
// unlikely to change before GOPATH support is removed.
ctx.ReadDir = ioutil.ReadDir
ctx.ReadDir = func(dirname string) ([]fs.FileInfo, error) {
entries, err := os.ReadDir(dirname)
if err != nil {
return nil, err
}
infos := make([]fs.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
continue
}
infos = append(infos, info)
}
return infos, nil
}

return &ctx, nil
}
Expand Down Expand Up @@ -1328,10 +1342,18 @@ func VendorlessPath(ipath string) string {

func loadExportsFromFiles(ctx context.Context, env *ProcessEnv, dir string, includeTest bool) (string, []string, error) {
// Look for non-test, buildable .go files which could provide exports.
all, err := ioutil.ReadDir(dir)
entries, err := os.ReadDir(dir)
if err != nil {
return "", nil, err
}
all := make([]os.FileInfo, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
continue
}
all = append(all, info)
}
var files []os.FileInfo
for _, fi := range all {
name := fi.Name()
Expand Down
4 changes: 2 additions & 2 deletions internal/imports/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -1787,7 +1787,7 @@ func (t *goimportTest) process(module, file string, contents []byte, opts *Optio
func (t *goimportTest) processNonModule(file string, contents []byte, opts *Options) ([]byte, error) {
if contents == nil {
var err error
contents, err = ioutil.ReadFile(file)
contents, err = os.ReadFile(file)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/imports/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -264,7 +263,7 @@ func (r *ModuleResolver) findPackage(importPath string) (*gocommand.ModuleJSON,
}

// Not cached. Read the filesystem.
pkgFiles, err := ioutil.ReadDir(pkgDir)
pkgFiles, err := os.ReadDir(pkgDir)
if err != nil {
continue
}
Expand Down Expand Up @@ -361,7 +360,7 @@ func (r *ModuleResolver) dirIsNestedModule(dir string, mod *gocommand.ModuleJSON

func (r *ModuleResolver) modInfo(dir string) (modDir string, modName string) {
readModName := func(modFile string) string {
modBytes, err := ioutil.ReadFile(modFile)
modBytes, err := os.ReadFile(modFile)
if err != nil {
return ""
}
Expand Down
21 changes: 10 additions & 11 deletions internal/imports/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"archive/zip"
"context"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -197,18 +196,18 @@ import _ "rsc.io/quote"
if err := os.Chmod(filepath.Join(found.dir, "go.mod"), 0644); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(found.dir, "go.mod"), []byte("module bad.com\n"), 0644); err != nil {
if err := os.WriteFile(filepath.Join(found.dir, "go.mod"), []byte("module bad.com\n"), 0644); err != nil {
t.Fatal(err)
}

// Test that with its cache of module packages it still finds the package.
mt.assertScanFinds("rsc.io/quote", "quote")

// Rewrite the main package so that rsc.io/quote is not in scope.
if err := ioutil.WriteFile(filepath.Join(mt.env.WorkingDir, "go.mod"), []byte("module x\n"), 0644); err != nil {
if err := os.WriteFile(filepath.Join(mt.env.WorkingDir, "go.mod"), []byte("module x\n"), 0644); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(mt.env.WorkingDir, "x.go"), []byte("package x\n"), 0644); err != nil {
if err := os.WriteFile(filepath.Join(mt.env.WorkingDir, "x.go"), []byte("package x\n"), 0644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -1002,7 +1001,7 @@ func setup(t *testing.T, extraEnv map[string]string, main, wd string) *modTest {

proxyOnce.Do(func() {
var err error
proxyDir, err = ioutil.TempDir("", "proxy-")
proxyDir, err = os.MkdirTemp("", "proxy-")
if err != nil {
t.Fatal(err)
}
Expand All @@ -1011,7 +1010,7 @@ func setup(t *testing.T, extraEnv map[string]string, main, wd string) *modTest {
}
})

dir, err := ioutil.TempDir("", t.Name())
dir, err := os.MkdirTemp("", t.Name())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1072,7 +1071,7 @@ func writeModule(dir, ar string) error {
return err
}

if err := ioutil.WriteFile(fpath, f.Data, 0644); err != nil {
if err := os.WriteFile(fpath, f.Data, 0644); err != nil {
return err
}
}
Expand All @@ -1082,7 +1081,7 @@ func writeModule(dir, ar string) error {
// writeProxy writes all the txtar-formatted modules in arDir to a proxy
// directory in dir.
func writeProxy(dir, arDir string) error {
files, err := ioutil.ReadDir(arDir)
files, err := os.ReadDir(arDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -1125,7 +1124,7 @@ func writeProxyModule(base, arPath string) error {
z := zip.NewWriter(f)
for _, f := range a.Files {
if f.Name[0] == '.' {
if err := ioutil.WriteFile(filepath.Join(dir, ver+f.Name), f.Data, 0644); err != nil {
if err := os.WriteFile(filepath.Join(dir, ver+f.Name), f.Data, 0644); err != nil {
return err
}
} else {
Expand Down Expand Up @@ -1195,7 +1194,7 @@ import _ "rsc.io/quote"
// Tests that crud in the module cache is ignored.
func TestInvalidModCache(t *testing.T) {
testenv.NeedsGo1Point(t, 11)
dir, err := ioutil.TempDir("", t.Name())
dir, err := os.MkdirTemp("", t.Name())
if err != nil {
t.Fatal(err)
}
Expand All @@ -1205,7 +1204,7 @@ func TestInvalidModCache(t *testing.T) {
if err := os.MkdirAll(filepath.Join(dir, "gopath/pkg/mod/sabotage"), 0777); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(dir, "gopath/pkg/mod/sabotage/x.go"), []byte("package foo\n"), 0777); err != nil {
if err := os.WriteFile(filepath.Join(dir, "gopath/pkg/mod/sabotage/x.go"), []byte("package foo\n"), 0777); err != nil {
t.Fatal(err)
}
env := &ProcessEnv{
Expand Down
3 changes: 1 addition & 2 deletions internal/testenv/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"go/build"
exec "golang.org/x/sys/execabs"
"io/ioutil"
"os"
"runtime"
"runtime/debug"
Expand Down Expand Up @@ -70,7 +69,7 @@ func hasTool(tool string) error {
switch tool {
case "patch":
// check that the patch tools supports the -o argument
temp, err := ioutil.TempFile("", "patch-test")
temp, err := os.CreateTemp("", "patch-test")
if err != nil {
return err
}
Expand Down

0 comments on commit f261744

Please sign in to comment.