Skip to content

Commit

Permalink
Move away from deprecated ioutils (#825)
Browse files Browse the repository at this point in the history
Fix #806
  • Loading branch information
mgechev authored May 16, 2023
1 parent b508fa8 commit 6d5bc51
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/mgechev/revive/formatter"

Expand Down Expand Up @@ -140,7 +140,7 @@ func GetLintingRules(config *lint.Config, extraRules []lint.Rule) ([]lint.Rule,
}

func parseConfig(path string, config *lint.Config) error {
file, err := ioutil.ReadFile(path)
file, err := os.ReadFile(path)
if err != nil {
return errors.New("cannot read the config file")
}
Expand Down
3 changes: 1 addition & 2 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package logging

import (
"io"
"io/ioutil"
"log"
"os"
)
Expand All @@ -27,7 +26,7 @@ func GetLogger() (*log.Logger, error) {
}
} else {
// Suppress all logging output if debug mode is disabled
writer = ioutil.Discard
writer = io.Discard
}

logger = log.New(writer, "", log.LstdFlags)
Expand Down
4 changes: 2 additions & 2 deletions revivelib/core.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package revivelib

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

"github.com/mgechev/dots"
Expand Down Expand Up @@ -88,7 +88,7 @@ func (r *Revive) Lint(patterns ...*LintPattern) (<-chan lint.Failure, error) {
}

revive := lint.New(func(file string) ([]byte, error) {
contents, err := ioutil.ReadFile(file)
contents, err := os.ReadFile(file)

if err != nil {
return nil, errors.Wrap(err, "reading file "+file)
Expand Down
16 changes: 11 additions & 5 deletions test/golint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package test

import (
"flag"
"io/ioutil"
"os"
"path"
"regexp"
"testing"
Expand Down Expand Up @@ -41,9 +41,9 @@ func TestAll(t *testing.T) {
t.Fatalf("Bad -lint.match value %q: %v", *lintMatch, err)
}

fis, err := ioutil.ReadDir(baseDir)
fis, err := os.ReadDir(baseDir)
if err != nil {
t.Fatalf("ioutil.ReadDir: %v", err)
t.Fatalf("os.ReadDir: %v", err)
}
if len(fis) == 0 {
t.Fatalf("no files in %v", baseDir)
Expand All @@ -53,12 +53,18 @@ func TestAll(t *testing.T) {
continue
}
t.Run(fi.Name(), func(t *testing.T) {
src, err := ioutil.ReadFile(path.Join(baseDir, fi.Name()))
filePath := path.Join(baseDir, fi.Name())
src, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("Failed reading %s: %v", fi.Name(), err)
}

if err := assertFailures(t, baseDir, fi, src, rules, map[string]lint.RuleConfig{}); err != nil {
fileInfo, err := os.Stat(filePath)
if err != nil {
t.Fatalf("Failed reading %s: %v", fi.Name(), err)
}

if err := assertFailures(t, baseDir, fileInfo, src, rules, map[string]lint.RuleConfig{}); err != nil {
t.Errorf("Linting %s: %v", fi.Name(), err)
}
})
Expand Down
7 changes: 3 additions & 4 deletions test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"go/parser"
"go/token"
"go/types"
"io/ioutil"
"os"
"strconv"
"strings"
Expand All @@ -21,7 +20,7 @@ import (
func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.RuleConfig) {
baseDir := "../testdata/"
filename = filename + ".go"
src, err := ioutil.ReadFile(baseDir + filename)
src, err := os.ReadFile(baseDir + filename)
if err != nil {
t.Fatalf("Bad filename path in test for %s: %v", rule.Name(), err)
}
Expand All @@ -42,7 +41,7 @@ func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.Rul

func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Rule, config map[string]lint.RuleConfig) error {
l := lint.New(func(file string) ([]byte, error) {
return ioutil.ReadFile(baseDir + file)
return os.ReadFile(baseDir + file)
}, 0)

ps, err := l.Lint([][]string{{fi.Name()}}, rules, lint.Config{
Expand All @@ -64,7 +63,7 @@ func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Ru

func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rules []lint.Rule, config map[string]lint.RuleConfig) error {
l := lint.New(func(file string) ([]byte, error) {
return ioutil.ReadFile(baseDir + file)
return os.ReadFile(baseDir + file)
}, 0)

ins := parseInstructions(t, fi.Name(), src)
Expand Down
3 changes: 1 addition & 2 deletions testdata/unused-param.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fixtures
import (
"fmt"
"go/ast"
"io/ioutil"
"os"
"runtime"
"testing"
Expand Down Expand Up @@ -103,7 +102,7 @@ func getCompareFailCause(n *node, which int, prevValue string, prevIndex uint64)

func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rules []lint.Rule, config map[string]lint.RuleConfig) error { // MATCH /parameter 'src' seems to be unused, consider removing or renaming it as _/
l := lint.New(func(file string) ([]byte, error) {
return ioutil.ReadFile(baseDir + file)
return os.ReadFile(baseDir + file)
})

ps, err := l.Lint([][]string{[]string{fi.Name()}}, rules, lint.Config{
Expand Down

0 comments on commit 6d5bc51

Please sign in to comment.