Skip to content

Commit

Permalink
mockgen: replace io/ioutil with io and os package
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Jul 14, 2023
1 parent bdf9df3 commit f18d287
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 42 deletions.
11 changes: 5 additions & 6 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"fmt"
"go/token"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand All @@ -37,10 +36,10 @@ import (
"strings"
"unicode"

"go.uber.org/mock/mockgen/model"

"golang.org/x/mod/modfile"
toolsimports "golang.org/x/tools/imports"

"go.uber.org/mock/mockgen/model"
)

const (
Expand Down Expand Up @@ -151,7 +150,7 @@ func main() {
g.mockNames = parseMockNames(*mockNames)
}
if *copyrightFile != "" {
header, err := ioutil.ReadFile(*copyrightFile)
header, err := os.ReadFile(*copyrightFile)
if err != nil {
log.Fatalf("Failed reading copyright file: %v", err)
}
Expand All @@ -167,7 +166,7 @@ func main() {
if err := os.MkdirAll(filepath.Dir(*destination), os.ModePerm); err != nil {
log.Fatalf("Unable to create directory: %v", err)
}
existing, err := ioutil.ReadFile(*destination)
existing, err := os.ReadFile(*destination)
if err != nil && !errors.Is(err, os.ErrNotExist) {
log.Fatalf("Failed reading pre-exiting destination file: %v", err)
}
Expand Down Expand Up @@ -798,7 +797,7 @@ func parsePackageImport(srcDir string) (string, error) {
if moduleMode != "off" {
currentDir := srcDir
for {
dat, err := ioutil.ReadFile(filepath.Join(currentDir, "go.mod"))
dat, err := os.ReadFile(filepath.Join(currentDir, "go.mod"))
if os.IsNotExist(err) {
if currentDir == filepath.Dir(currentDir) {
// at the root
Expand Down
35 changes: 5 additions & 30 deletions mockgen/mockgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -369,17 +368,9 @@ func Test_createPackageMap(t *testing.T) {
}

func TestParsePackageImport_FallbackGoPath(t *testing.T) {
goPath, err := ioutil.TempDir("", "gopath")
if err != nil {
t.Error(err)
}
defer func() {
if err = os.RemoveAll(goPath); err != nil {
t.Error(err)
}
}()
goPath := t.TempDir()
srcDir := filepath.Join(goPath, "src/example.com/foo")
err = os.MkdirAll(srcDir, 0755)
err := os.MkdirAll(srcDir, 0755)
if err != nil {
t.Error(err)
}
Expand All @@ -404,33 +395,17 @@ func TestParsePackageImport_FallbackMultiGoPath(t *testing.T) {
var goPathList []string

// first gopath
goPath, err := ioutil.TempDir("", "gopath1")
if err != nil {
t.Error(err)
}
goPath := t.TempDir()
goPathList = append(goPathList, goPath)
defer func() {
if err = os.RemoveAll(goPath); err != nil {
t.Error(err)
}
}()
srcDir := filepath.Join(goPath, "src/example.com/foo")
err = os.MkdirAll(srcDir, 0755)
err := os.MkdirAll(srcDir, 0755)
if err != nil {
t.Error(err)
}

// second gopath
goPath, err = ioutil.TempDir("", "gopath2")
if err != nil {
t.Error(err)
}
goPath = t.TempDir()
goPathList = append(goPathList, goPath)
defer func() {
if err = os.RemoveAll(goPath); err != nil {
t.Error(err)
}
}()

goPaths := strings.Join(goPathList, string(os.PathListSeparator))
key := "GOPATH"
Expand Down
4 changes: 2 additions & 2 deletions mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -774,7 +774,7 @@ func isVariadic(f *ast.FuncType) bool {

// packageNameOfDir get package import path via dir
func packageNameOfDir(srcDir string) (string, error) {
files, err := ioutil.ReadDir(srcDir)
files, err := os.ReadDir(srcDir)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 3 additions & 4 deletions mockgen/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"go/build"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -92,7 +91,7 @@ func writeProgram(importPath string, symbols []string) ([]byte, error) {

// run the given program and parse the output as a model.Package.
func run(program string) (*model.Package, error) {
f, err := ioutil.TempFile("", "")
f, err := os.CreateTemp("", "")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,7 +132,7 @@ func run(program string) (*model.Package, error) {
// parses the output as a model.Package.
func runInDir(program []byte, dir string) (*model.Package, error) {
// We use TempDir instead of TempFile so we can control the filename.
tmpDir, err := ioutil.TempDir(dir, "gomock_reflect_")
tmpDir, err := os.MkdirTemp(dir, "gomock_reflect_")
if err != nil {
return nil, err
}
Expand All @@ -149,7 +148,7 @@ func runInDir(program []byte, dir string) (*model.Package, error) {
progBinary += ".exe"
}

if err := ioutil.WriteFile(filepath.Join(tmpDir, progSource), program, 0600); err != nil {
if err := os.WriteFile(filepath.Join(tmpDir, progSource), program, 0600); err != nil {
return nil, err
}

Expand Down

0 comments on commit f18d287

Please sign in to comment.