Skip to content

Commit

Permalink
testscript,goproxytest: use filepath.WalkDir
Browse files Browse the repository at this point in the history
This can easily save hundreds of stat calls per test script,
particularly when testing Go tools.

While here, remove a few uses of the deprecated io/ioutil in cmd,
and check some missed errors in txtar-c.
  • Loading branch information
mvdan committed Oct 26, 2023
1 parent 32ae337 commit 0bcf77f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 31 deletions.
18 changes: 10 additions & 8 deletions cmd/txtar-addmod/addmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -82,7 +81,7 @@ func main1() int {
log.SetFlags(0)

var err error
tmpdir, err = ioutil.TempDir("", "txtar-addmod-")
tmpdir, err = os.MkdirTemp("", "txtar-addmod-")
if err != nil {
log.Fatal(err)
}
Expand All @@ -106,7 +105,7 @@ func main1() int {

exitCode := 0
for _, arg := range modules {
if err := ioutil.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module m\n"), 0o666); err != nil {
if err := os.WriteFile(filepath.Join(tmpdir, "go.mod"), []byte("module m\n"), 0o666); err != nil {
fatalf("%v", err)
}
run(goCmd, "get", "-d", arg)
Expand All @@ -130,13 +129,13 @@ func main1() int {
}
path = encpath

mod, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".mod"))
mod, err := os.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".mod"))
if err != nil {
log.Printf("%s: %v", arg, err)
exitCode = 1
continue
}
info, err := ioutil.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".info"))
info, err := os.ReadFile(filepath.Join(gopath, "pkg/mod/cache/download", path, "@v", vers+".info"))
if err != nil {
log.Printf("%s: %v", arg, err)
exitCode = 1
Expand All @@ -149,7 +148,7 @@ func main1() int {
title += "@" + vers
}
dir = filepath.Clean(dir)
modDir := strings.Replace(path, "/", "_", -1) + "_" + vers
modDir := strings.ReplaceAll(path, "/", "_") + "_" + vers
filePrefix := ""
if targetDir == "-" {
filePrefix = ".gomodproxy/" + modDir + "/"
Expand All @@ -162,6 +161,9 @@ func main1() int {
{Name: filePrefix + ".info", Data: info},
}
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
Expand All @@ -177,7 +179,7 @@ func main1() int {
// not including all files via -all
return nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
Expand All @@ -201,7 +203,7 @@ func main1() int {
break
}
} else {
if err := ioutil.WriteFile(filepath.Join(targetDir, modDir+".txtar"), data, 0o666); err != nil {
if err := os.WriteFile(filepath.Join(targetDir, modDir+".txtar"), data, 0o666); err != nil {
log.Printf("%s: %v", arg, err)
exitCode = 1
continue
Expand Down
14 changes: 9 additions & 5 deletions cmd/txtar-c/savedir.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"bytes"
stdflag "flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -59,7 +58,10 @@ func main1() int {

a := new(txtar.Archive)
dir = filepath.Clean(dir)
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == dir {
return nil
}
Expand All @@ -73,9 +75,9 @@ func main1() int {
if !info.Mode().IsRegular() {
return nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
log.Fatal(err)
return err
}
if !utf8.Valid(data) {
log.Printf("%s: ignoring file with invalid UTF-8 data", path)
Expand Down Expand Up @@ -103,7 +105,9 @@ func main1() int {
Data: data,
})
return nil
})
}); err != nil {
log.Fatal(err)
}

data := txtar.Format(a)
os.Stdout.Write(data)
Expand Down
4 changes: 2 additions & 2 deletions cmd/txtar-x/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"os"

Expand Down Expand Up @@ -47,7 +47,7 @@ func main1() int {

var a *txtar.Archive
if flag.NArg() == 0 {
data, err := ioutil.ReadAll(os.Stdin)
data, err := io.ReadAll(os.Stdin)
if err != nil {
log.Printf("cannot read stdin: %v", err)
return 1
Expand Down
5 changes: 2 additions & 3 deletions cmd/txtar-x/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"bytes"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -34,11 +33,11 @@ func unquote(ts *testscript.TestScript, neg bool, args []string) {
}
for _, arg := range args {
file := ts.MkAbs(arg)
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
ts.Check(err)
data = bytes.Replace(data, []byte("\n>"), []byte("\n"), -1)
data = bytes.TrimPrefix(data, []byte(">"))
err = ioutil.WriteFile(file, data, 0o666)
err = os.WriteFile(file, data, 0o666)
ts.Check(err)
}
}
22 changes: 11 additions & 11 deletions goproxytest/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io/fs"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -91,26 +91,26 @@ func (srv *Server) Close() {
}

func (srv *Server) readModList() error {
infos, err := ioutil.ReadDir(srv.dir)
entries, err := os.ReadDir(srv.dir)
if err != nil {
return err
}
for _, info := range infos {
name := info.Name()
for _, entry := range entries {
name := entry.Name()
switch {
case strings.HasSuffix(name, ".txt"):
name = strings.TrimSuffix(name, ".txt")
case strings.HasSuffix(name, ".txtar"):
name = strings.TrimSuffix(name, ".txtar")
case info.IsDir():
case entry.IsDir():
default:
continue
}
i := strings.LastIndex(name, "_v")
if i < 0 {
continue
}
encPath := strings.Replace(name[:i], "_", "/", -1)
encPath := strings.ReplaceAll(name[:i], "_", "/")
path, err := module.UnescapePath(encPath)
if err != nil {
return fmt.Errorf("cannot decode module path in %q: %v", name, err)
Expand Down Expand Up @@ -286,7 +286,7 @@ func (srv *Server) readArchive(path, vers string) *txtar.Archive {
return nil
}

prefix := strings.Replace(enc, "/", "_", -1)
prefix := strings.ReplaceAll(enc, "/", "_")
name := filepath.Join(srv.dir, prefix+"_"+encVers)
txtName := name + ".txt"
txtarName := name + ".txtar"
Expand All @@ -300,18 +300,18 @@ func (srv *Server) readArchive(path, vers string) *txtar.Archive {
// fall back to trying a directory
a = new(txtar.Archive)

err = filepath.Walk(name, func(path string, info os.FileInfo, err error) error {
err = filepath.WalkDir(name, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == name && !info.IsDir() {
if path == name && !entry.IsDir() {
return fmt.Errorf("expected a directory root")
}
if info.IsDir() {
if entry.IsDir() {
return nil
}
arpath := filepath.ToSlash(strings.TrimPrefix(path, name+string(os.PathSeparator)))
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -1265,11 +1265,11 @@ func (ts *TestScript) parse(line string) []string {
func removeAll(dir string) error {
// module cache has 0o444 directories;
// make them writable in order to remove content.
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
filepath.WalkDir(dir, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return nil // ignore errors walking in file system
}
if info.IsDir() {
if entry.IsDir() {
os.Chmod(path, 0o777)
}
return nil
Expand Down

0 comments on commit 0bcf77f

Please sign in to comment.