Skip to content

Commit

Permalink
Stop using deprecated ioutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
anishathalye committed May 16, 2024
1 parent 6b983e4 commit 9804a15
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 71 deletions.
4 changes: 2 additions & 2 deletions cmd/psc/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"io/ioutil"
"io"
"log"

"github.com/spf13/cobra"
Expand All @@ -26,7 +26,7 @@ func init() {
func preRoot(cmd *cobra.Command, args []string) {
if !rootFlags.debug {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
} else {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/db/db_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package db

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -32,7 +31,7 @@ func addAll(db *Session, infos []FileInfo) error {
}

func TestVersionOk(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -50,7 +49,7 @@ func TestVersionOk(t *testing.T) {
}

func TestVersionMismatch(t *testing.T) {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
Expand Down
33 changes: 16 additions & 17 deletions internal/periscope/ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package periscope
import (
"github.com/anishathalye/periscope/internal/testfs"

"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -237,8 +236,8 @@ func TestLsSpecialModes(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.Symlink(filepath.Join(dir, "x"), filepath.Join(dir, "z"))
ps, out, _ := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
Expand Down Expand Up @@ -499,8 +498,8 @@ func TestLsRejectSymlink(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.Symlink(dir, filepath.Join(dir, "rec"))
ps, out, stderr := newTest(fs)
err := ps.Ls([]string{filepath.Join(dir, "rec")}, &LsOptions{})
Expand Down Expand Up @@ -630,11 +629,11 @@ func TestLsRelative(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.Mkdir(filepath.Join(dir, "d"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
ps, out, _ := newTest(fs)
oldWd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -665,11 +664,11 @@ func TestLsRelativeRecursive(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.Mkdir(filepath.Join(dir, "d"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
ps, out, _ := newTest(fs)
oldWd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -699,11 +698,11 @@ func TestLsRelativeDir(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.Mkdir(filepath.Join(dir, "d"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
ps, out, _ := newTest(fs)
oldWd, err := os.Getwd()
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/periscope/periscope.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -83,7 +82,7 @@ func (ps *Periscope) progressBar(total int, template string) *pb.ProgressBar {
if w, ok := ps.errStream.(*os.File); ok && term.IsTerminal(int(w.Fd())) {
bar.SetWriter(ps.errStream)
} else {
bar.SetWriter(ioutil.Discard)
bar.SetWriter(io.Discard)
}
return bar
}
7 changes: 4 additions & 3 deletions internal/periscope/periscope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"github.com/anishathalye/periscope/internal/db"

"bytes"
"io/ioutil"
"io"
"log"
"os"
"path/filepath"
"testing"

Expand All @@ -17,7 +18,7 @@ func newTest(fs afero.Fs) (*Periscope, *bytes.Buffer, *bytes.Buffer) {
log.SetFlags(log.LstdFlags | log.Lshortfile)
} else {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
log.SetOutput(io.Discard)
}
db, err := db.NewInMemory()
if err != nil {
Expand Down Expand Up @@ -50,7 +51,7 @@ func checkErr(t *testing.T, err error) {
}

func tempDir() string {
dir, err := ioutil.TempDir("", "")
dir, err := os.MkdirTemp("", "")
if err != nil {
panic(err)
}
Expand Down
21 changes: 10 additions & 11 deletions internal/periscope/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/anishathalye/periscope/internal/testfs"

"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -172,10 +171,10 @@ func TestRefreshPermissionError(t *testing.T) {
defer os.RemoveAll(dir)
os.Mkdir(filepath.Join(dir, "d1"), 0o755)
os.Mkdir(filepath.Join(dir, "d2"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d1", "w"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d1", "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d2", "y"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d2", "z"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d1", "w"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d1", "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d2", "y"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d2", "z"), []byte{'b'}, 0o644)
ps, _, _ := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
os.Chmod(filepath.Join(dir, "d1"), 0o000)
Expand All @@ -192,10 +191,10 @@ func TestRefreshNonRegularFile(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "w"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "z"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "w"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "z"), []byte{'b'}, 0o644)
ps, _, _ := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
os.Remove(filepath.Join(dir, "w"))
Expand All @@ -215,8 +214,8 @@ func TestRefreshSymlinkDir(t *testing.T) {
defer os.RemoveAll(dir)
os.Mkdir(filepath.Join(dir, "d"), 0o755)
os.Mkdir(filepath.Join(dir, "d2"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d", "x"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d2", "y"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "x"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d2", "y"), []byte{'b'}, 0o644)
ps, _, _ := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
os.RemoveAll(filepath.Join(dir, "d2"))
Expand Down
17 changes: 8 additions & 9 deletions internal/periscope/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package periscope
import (
"github.com/anishathalye/periscope/internal/testfs"

"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -91,11 +90,11 @@ func TestReportRelative(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a', 'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a', 'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a', 'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a', 'a'}, 0o644)
os.Mkdir(filepath.Join(dir, "d"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
ps, out, _ := newTest(fs)
oldWd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -125,11 +124,11 @@ func TestReportRelativeArgument(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a', 'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a', 'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a', 'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a', 'a'}, 0o644)
os.Mkdir(filepath.Join(dir, "d"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "a"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "b"), []byte{'b'}, 0o644)
ps, out, _ := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
err := ps.Report(filepath.Join(dir, "d"), &ReportOptions{Relative: true})
Expand Down
21 changes: 10 additions & 11 deletions internal/periscope/rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/anishathalye/periscope/internal/testfs"

"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -470,8 +469,8 @@ func TestRmNoSymlinks(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.Symlink(dir, filepath.Join(dir, "rec"))
ps, out, stderr := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
Expand All @@ -492,8 +491,8 @@ func TestRmReplacedBySymlink(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
ps, out, stderr := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
os.Remove(filepath.Join(dir, "y"))
Expand Down Expand Up @@ -556,8 +555,8 @@ func TestRmLostPermission(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
ps, out, stderr := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
os.Chmod(filepath.Join(dir, "y"), 0o000)
Expand Down Expand Up @@ -592,9 +591,9 @@ func TestRmPartialPermission(t *testing.T) {
dir := tempDir()
defer os.RemoveAll(dir)
os.Mkdir(filepath.Join(dir, "d"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d", "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d", "y"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "z"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d", "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "z"), []byte{'a'}, 0o644)
ps, _, _ := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
os.Chmod(filepath.Join(dir, "d", "y"), 0o000)
Expand Down Expand Up @@ -707,7 +706,7 @@ func TestRmHardLink(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.Link(filepath.Join(dir, "x"), filepath.Join(dir, "y"))
ps, out, stderr := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
Expand Down
25 changes: 12 additions & 13 deletions internal/periscope/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/anishathalye/periscope/internal/testfs"

"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -164,10 +163,10 @@ func TestScanNoAccess(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "w"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o000)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'b'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "z"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "w"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o000)
os.WriteFile(filepath.Join(dir, "y"), []byte{'b'}, 0o644)
os.WriteFile(filepath.Join(dir, "z"), []byte{'b'}, 0o644)
fs.Mkdir(filepath.Join(dir, "d"), 0o644)
fs.Mkdir(filepath.Join(dir, "e"), 0o000)
ps, _, _ := newTest(fs)
Expand Down Expand Up @@ -345,8 +344,8 @@ func TestScanNoReadSymlinks(t *testing.T) {
fs := afero.NewOsFs()
dir := tempDir()
defer os.RemoveAll(dir)
ioutil.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "y"), []byte{'a'}, 0o644)
os.Symlink(filepath.Join(dir, "x"), filepath.Join(dir, "z"))
ps, _, _ := newTest(fs)
err := ps.Scan([]string{dir}, &ScanOptions{})
Expand All @@ -364,8 +363,8 @@ func TestScanNoDescendSymlinks(t *testing.T) {
dir := tempDir()
defer os.RemoveAll(dir)
os.Mkdir(filepath.Join(dir, "d1"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d1", "w"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d1", "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d1", "w"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d1", "x"), []byte{'a'}, 0o644)
os.Symlink(filepath.Join(dir, "d1"), filepath.Join(dir, "d2"))
ps, _, _ := newTest(fs)
ps.Scan([]string{dir}, &ScanOptions{})
Expand All @@ -382,8 +381,8 @@ func TestScanSymlink(t *testing.T) {
dir := tempDir()
defer os.RemoveAll(dir)
os.Mkdir(filepath.Join(dir, "d1"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d1", "w"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d1", "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d1", "w"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d1", "x"), []byte{'a'}, 0o644)
os.Symlink(filepath.Join(dir, "d1"), filepath.Join(dir, "d2"))
ps, _, _ := newTest(fs)
err := ps.Scan([]string{filepath.Join(dir, "d2")}, &ScanOptions{})
Expand All @@ -396,8 +395,8 @@ func TestScanInsideSymlink(t *testing.T) {
defer os.RemoveAll(dir)
os.Mkdir(filepath.Join(dir, "d1"), 0o755)
os.Mkdir(filepath.Join(dir, "d1", "d2"), 0o755)
ioutil.WriteFile(filepath.Join(dir, "d1", "d2", "w"), []byte{'a'}, 0o644)
ioutil.WriteFile(filepath.Join(dir, "d1", "d2", "x"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d1", "d2", "w"), []byte{'a'}, 0o644)
os.WriteFile(filepath.Join(dir, "d1", "d2", "x"), []byte{'a'}, 0o644)
os.Symlink(filepath.Join(dir, "d1"), filepath.Join(dir, "d3"))
ps, _, _ := newTest(fs)
err := ps.Scan([]string{filepath.Join(dir, "d2")}, &ScanOptions{})
Expand Down

0 comments on commit 9804a15

Please sign in to comment.