diff --git a/README.md b/README.md index 035eeda..a06f914 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ if err != nil { } // Write a file and track it with `git add`. -err = ioutil.WriteFile("foo.txt", []byte("Hello, World!\n"), 0666) +err = os.WriteFile("foo.txt", []byte("Hello, World!\n"), 0o666) if err != nil { return err } diff --git a/cat_test.go b/cat_test.go index de15abe..e25250d 100644 --- a/cat_test.go +++ b/cat_test.go @@ -16,7 +16,7 @@ package git import ( "context" - "io/ioutil" + "io" "strings" "testing" @@ -76,7 +76,7 @@ func TestCat(t *testing.T) { if err != nil { t.Fatal("Cat error:", err) } - got, err := ioutil.ReadAll(r) + got, err := io.ReadAll(r) if string(got) != test.want { t.Errorf("read content = %q; want %q", got, test.want) } diff --git a/example_test.go b/example_test.go index 7b8144a..30cc6f4 100644 --- a/example_test.go +++ b/example_test.go @@ -19,7 +19,8 @@ package git_test import ( "context" "fmt" - "io/ioutil" + + "os" "gg-scm.io/pkg/git" ) @@ -90,7 +91,7 @@ func ExampleGit_Commit() { } // Write a file and track it with `git add`. - err = ioutil.WriteFile("foo.txt", []byte("Hello, World!\n"), 0666) + err = os.WriteFile("foo.txt", []byte("Hello, World!\n"), 0o666) if err != nil { // handle error } diff --git a/git_test.go b/git_test.go index 656e3c6..8ef576f 100644 --- a/git_test.go +++ b/git_test.go @@ -16,7 +16,6 @@ package git import ( "context" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -58,15 +57,7 @@ func TestLocalCommand(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { ctx := context.Background() - dir, err := ioutil.TempDir("", "gg_gittool_test") - if err != nil { - t.Fatal(err) - } - defer func() { - if err := os.Remove(dir); err != nil { - t.Error("cleaning up directory:", err) - } - }() + dir := t.TempDir() var hookArgs []string var env []string if test.env != nil { @@ -221,7 +212,7 @@ type testEnv struct { } func newTestEnv(ctx context.Context, gitExe string) (*testEnv, error) { - topPath, err := ioutil.TempDir("", "gg_git_test") + topPath, err := os.MkdirTemp("", "gg_git_test") if err != nil { return nil, err } diff --git a/internal/filesystem/filesystem.go b/internal/filesystem/filesystem.go index 0fc980c..8833266 100644 --- a/internal/filesystem/filesystem.go +++ b/internal/filesystem/filesystem.go @@ -19,7 +19,6 @@ package filesystem import ( "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -125,17 +124,17 @@ func (dir Dir) Apply(ops ...Operation) error { case nop: // Do nothing. case opWrite: - if err := os.MkdirAll(filepath.Dir(p), 0777); err != nil { + if err := os.MkdirAll(filepath.Dir(p), 0o777); err != nil { return err } - if err := ioutil.WriteFile(p, []byte(o.arg), 0666); err != nil { + if err := os.WriteFile(p, []byte(o.arg), 0o666); err != nil { return err } case opMkdir: - if err := os.MkdirAll(filepath.Dir(p), 0777); err != nil { + if err := os.MkdirAll(filepath.Dir(p), 0o777); err != nil { return err } - if err := os.Mkdir(p, 0777); err != nil { + if err := os.Mkdir(p, 0o777); err != nil { return err } case opRemove: diff --git a/internal/filesystem/filesystem_test.go b/internal/filesystem/filesystem_test.go index 559eeaf..aa8d5e5 100644 --- a/internal/filesystem/filesystem_test.go +++ b/internal/filesystem/filesystem_test.go @@ -15,7 +15,6 @@ package filesystem import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -24,21 +23,13 @@ import ( func TestApply(t *testing.T) { t.Run("Write/Top", func(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "gg_filesystem") - if err != nil { - t.Fatal(err) - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - t.Error("clean up temp dir:", err) - } - }() + dir := t.TempDir() const wantContent = "Hello, World!\n" - err = Dir(dir).Apply(Write("foo.txt", wantContent)) + err := Dir(dir).Apply(Write("foo.txt", wantContent)) if err != nil { t.Error("Apply(...) =", err) } - got, err := ioutil.ReadFile(filepath.Join(dir, "foo.txt")) + got, err := os.ReadFile(filepath.Join(dir, "foo.txt")) if err != nil { t.Error(err) } else if string(got) != wantContent { @@ -47,21 +38,13 @@ func TestApply(t *testing.T) { }) t.Run("Write/SubDir", func(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "gg_filesystem") - if err != nil { - t.Fatal(err) - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - t.Error("clean up temp dir:", err) - } - }() + dir := t.TempDir() const wantContent = "Hello, World!\n" - err = Dir(dir).Apply(Write("foo/bar/baz.txt", wantContent)) + err := Dir(dir).Apply(Write("foo/bar/baz.txt", wantContent)) if err != nil { t.Error("Apply(...) =", err) } - got, err := ioutil.ReadFile(filepath.Join(dir, "foo", "bar", "baz.txt")) + got, err := os.ReadFile(filepath.Join(dir, "foo", "bar", "baz.txt")) if err != nil { t.Error(err) } else if string(got) != wantContent { @@ -70,21 +53,13 @@ func TestApply(t *testing.T) { }) t.Run("Mkdir/New", func(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "gg_filesystem") - if err != nil { - t.Fatal(err) - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - t.Error("clean up temp dir:", err) - } - }() - err = Dir(dir).Apply(Mkdir("foo/bar/baz")) + dir := t.TempDir() + err := Dir(dir).Apply(Mkdir("foo/bar/baz")) if err != nil { t.Error("Apply(...) =", err) } madeDir := filepath.Join(dir, "foo", "bar", "baz") - got, err := ioutil.ReadDir(madeDir) + got, err := os.ReadDir(madeDir) if err != nil { t.Error(err) } else if len(got) > 0 { @@ -93,46 +68,30 @@ func TestApply(t *testing.T) { }) t.Run("Mkdir/Exists", func(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "gg_filesystem") - if err != nil { - t.Fatal(err) - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - t.Error("clean up temp dir:", err) - } - }() + dir := t.TempDir() if err := os.MkdirAll(filepath.Join(dir, "foo", "bar"), 0777); err != nil { t.Fatal(err) } - err = Dir(dir).Apply(Mkdir("foo/bar")) + err := Dir(dir).Apply(Mkdir("foo/bar")) if err == nil { t.Error("Apply(...) = nil; want error") } }) t.Run("Remove/Exists", func(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "gg_filesystem") - if err != nil { - t.Fatal(err) - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - t.Error("clean up temp dir:", err) - } - }() + dir := t.TempDir() parent := filepath.Join(dir, "foo") if err := os.Mkdir(parent, 0777); err != nil { t.Fatal(err) } - if err := ioutil.WriteFile(filepath.Join(parent, "bar.txt"), []byte("sup"), 0666); err != nil { + if err := os.WriteFile(filepath.Join(parent, "bar.txt"), []byte("sup"), 0o666); err != nil { t.Fatal(err) } - err = Dir(dir).Apply(Remove("foo/bar.txt")) + err := Dir(dir).Apply(Remove("foo/bar.txt")) if err != nil { t.Error("Apply(...) =", err) } - got, err := ioutil.ReadDir(parent) + got, err := os.ReadDir(parent) if err != nil { t.Error(err) } else if len(got) > 0 { @@ -141,24 +100,16 @@ func TestApply(t *testing.T) { }) t.Run("Remove/DoesNotExist", func(t *testing.T) { t.Parallel() - dir, err := ioutil.TempDir("", "gg_filesystem") - if err != nil { - t.Fatal(err) - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - t.Error("clean up temp dir:", err) - } - }() + dir := t.TempDir() parent := filepath.Join(dir, "foo") if err := os.Mkdir(parent, 0777); err != nil { t.Fatal(err) } - err = Dir(dir).Apply(Remove("foo/bar.txt")) + err := Dir(dir).Apply(Remove("foo/bar.txt")) if err == nil { t.Error("Apply(...) = nil; want error") } - got, err := ioutil.ReadDir(parent) + got, err := os.ReadDir(parent) if err != nil { t.Error(err) } else if len(got) > 0 { @@ -168,21 +119,13 @@ func TestApply(t *testing.T) { } func TestReadFile(t *testing.T) { - dir, err := ioutil.TempDir("", "gg_filesystem") - if err != nil { - t.Fatal(err) - } - defer func() { - if err := os.RemoveAll(dir); err != nil { - t.Error("clean up temp dir:", err) - } - }() + dir := t.TempDir() parent := filepath.Join(dir, "foo") if err := os.Mkdir(parent, 0777); err != nil { t.Fatal(err) } const want = "Hello, World!\n" - if err := ioutil.WriteFile(filepath.Join(parent, "bar.txt"), []byte(want), 0666); err != nil { + if err := os.WriteFile(filepath.Join(parent, "bar.txt"), []byte(want), 0666); err != nil { t.Fatal(err) } got, err := Dir(dir).ReadFile("foo/bar.txt") diff --git a/object/tree_test.go b/object/tree_test.go index 309aa61..4dbf51c 100644 --- a/object/tree_test.go +++ b/object/tree_test.go @@ -19,7 +19,6 @@ package object import ( "encoding" "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -142,7 +141,7 @@ var treeTests = []struct { func TestParseTree(t *testing.T) { for _, test := range treeTests { t.Run(test.name, func(t *testing.T) { - src, err := ioutil.ReadFile(filepath.Join("testdata", fmt.Sprintf("tree-%x", test.id))) + src, err := os.ReadFile(filepath.Join("testdata", fmt.Sprintf("tree-%x", test.id))) if err != nil { t.Fatal(err) } @@ -161,7 +160,7 @@ func TestParseTree(t *testing.T) { func TestTreeMarshalBinary(t *testing.T) { for _, test := range treeTests { t.Run(test.name, func(t *testing.T) { - want, err := ioutil.ReadFile(filepath.Join("testdata", fmt.Sprintf("tree-%x", test.id))) + want, err := os.ReadFile(filepath.Join("testdata", fmt.Sprintf("tree-%x", test.id))) if err != nil { t.Fatal(err) } diff --git a/packfile/buildindex.go b/packfile/buildindex.go index e75b944..71298b6 100644 --- a/packfile/buildindex.go +++ b/packfile/buildindex.go @@ -24,7 +24,6 @@ import ( "hash" "hash/crc32" "io" - "io/ioutil" "sort" "sync" @@ -203,7 +202,7 @@ func baseIndexPass(r *byteReaderCounter, nobjs uint32) (*baseIndex, error) { objType := hdr.Type.NonDelta() if objType == "" { // Deltified object. - size, err := io.Copy(ioutil.Discard, z) + size, err := io.Copy(io.Discard, z) if err != nil { return nil, err } diff --git a/packfile/client/http.go b/packfile/client/http.go index 2e2df84..f4af8e8 100644 --- a/packfile/client/http.go +++ b/packfile/client/http.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/url" "strings" @@ -141,7 +140,7 @@ func (r *httpRemote) uploadPack(ctx context.Context, extraParams string, request contentTypeHeader: {"application/x-git-upload-pack-request"}, gitProtocolHeader: {extraParams}, }, - Body: ioutil.NopCloser(request), + Body: io.NopCloser(request), }) if err != nil { return nil, err diff --git a/packfile/client/pull_test.go b/packfile/client/pull_test.go index 39359c2..54ffb01 100644 --- a/packfile/client/pull_test.go +++ b/packfile/client/pull_test.go @@ -25,7 +25,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/http/cgi" "net/http/httptest" @@ -281,7 +280,7 @@ func initPullTestRepository(ctx context.Context, g *git.Git, dir string) (*pullT } const filename1 = "1.txt" const fileContent = "Hello, World!\n" - err = ioutil.WriteFile(filepath.Join(dir, filename1), []byte(fileContent), 0o666) + err = os.WriteFile(filepath.Join(dir, filename1), []byte(fileContent), 0o666) if err != nil { return nil, err } @@ -307,7 +306,7 @@ func initPullTestRepository(ctx context.Context, g *git.Git, dir string) (*pullT } const filename2 = "2.txt" - err = ioutil.WriteFile(filepath.Join(dir, filename2), []byte(fileContent), 0o666) + err = os.WriteFile(filepath.Join(dir, filename2), []byte(fileContent), 0o666) if err != nil { return nil, err } @@ -482,7 +481,7 @@ func serveHTTPRepository(gitExe string, dir string) *httptest.Server { // Go's net/http/cgi server doesn't support chunked encoding, which is // a non-standard CGI feature that Apache supports. https://golang.org/issue/5613 // We're okay with a really inefficient implementation for tests. - bodyFile, err := ioutil.TempFile("", "git-server-body") + bodyFile, err := os.CreateTemp("", "git-server-body") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/packfile/delta.go b/packfile/delta.go index 9c6c706..912f619 100644 --- a/packfile/delta.go +++ b/packfile/delta.go @@ -22,7 +22,6 @@ import ( "encoding/binary" "fmt" "io" - "io/ioutil" "gg-scm.io/pkg/git/object" ) @@ -172,7 +171,7 @@ func DeltaObjectSize(delta ByteReader) (int64, error) { case instruction != 0: // Add new data n += int64(instruction) - if err := copyN(ioutil.Discard, delta, int64(instruction)); err != nil { + if err := copyN(io.Discard, delta, int64(instruction)); err != nil { return 0, fmt.Errorf("calculate delta object size: %w", err) } default: diff --git a/packfile/index.go b/packfile/index.go index 7130951..7a94d75 100644 --- a/packfile/index.go +++ b/packfile/index.go @@ -21,7 +21,6 @@ import ( "crypto/sha1" "fmt" "io" - "io/ioutil" "sort" "gg-scm.io/pkg/git/githash" @@ -213,7 +212,7 @@ func readIndexV1(r io.Reader) (*Index, error) { const fanOutEntryCount = 256 func readIndexObjectCount(r io.Reader) (uint32, error) { - if _, err := io.CopyN(ioutil.Discard, r, (fanOutEntryCount-1)*4); err != nil { + if _, err := io.CopyN(io.Discard, r, (fanOutEntryCount-1)*4); err != nil { return 0, fmt.Errorf("fanout table: %w", err) } var raw [4]byte diff --git a/packfile/index_test.go b/packfile/index_test.go index d896f5f..e51d115 100644 --- a/packfile/index_test.go +++ b/packfile/index_test.go @@ -19,7 +19,6 @@ package packfile import ( "bytes" "encoding" - "io/ioutil" "os" "path/filepath" "testing" @@ -118,7 +117,7 @@ func TestIndexEncodeV1(t *testing.T) { continue } t.Run(test.name, func(t *testing.T) { - want, err := ioutil.ReadFile(filepath.Join("testdata", test.name+".idx1")) + want, err := os.ReadFile(filepath.Join("testdata", test.name+".idx1")) if err != nil { t.Fatal(err) } @@ -133,7 +132,7 @@ func TestIndexEncodeV1(t *testing.T) { } t.Run("Nil", func(t *testing.T) { - want, err := ioutil.ReadFile(filepath.Join("testdata", "Empty.idx1")) + want, err := os.ReadFile(filepath.Join("testdata", "Empty.idx1")) if err != nil { t.Fatal(err) } @@ -153,7 +152,7 @@ func TestIndexEncodeV2(t *testing.T) { continue } t.Run(test.name, func(t *testing.T) { - want, err := ioutil.ReadFile(filepath.Join("testdata", test.name+".idx2")) + want, err := os.ReadFile(filepath.Join("testdata", test.name+".idx2")) if err != nil { t.Fatal(err) } @@ -168,7 +167,7 @@ func TestIndexEncodeV2(t *testing.T) { } t.Run("BigOffset", func(t *testing.T) { - want, err := ioutil.ReadFile(filepath.Join("testdata", "BigOffset.idx2")) + want, err := os.ReadFile(filepath.Join("testdata", "BigOffset.idx2")) if err != nil { t.Fatal(err) } @@ -182,7 +181,7 @@ func TestIndexEncodeV2(t *testing.T) { }) t.Run("Nil", func(t *testing.T) { - want, err := ioutil.ReadFile(filepath.Join("testdata", "Empty.idx2")) + want, err := os.ReadFile(filepath.Join("testdata", "Empty.idx2")) if err != nil { t.Fatal(err) } diff --git a/packfile/reader.go b/packfile/reader.go index df64ed6..194f820 100644 --- a/packfile/reader.go +++ b/packfile/reader.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "gg-scm.io/pkg/git/githash" "gg-scm.io/pkg/git/object" @@ -89,7 +88,7 @@ func (r *Reader) Next() (*Header, error) { return nil, err } if r.dataReader != nil { - n, err := io.Copy(ioutil.Discard, r.dataReader) + n, err := io.Copy(io.Discard, r.dataReader) if err != nil { return nil, fmt.Errorf("packfile: advance to next object: %w", err) } @@ -104,7 +103,7 @@ func (r *Reader) Next() (*Header, error) { if r.nobjs == 0 { // Consume trailing checksum. // TODO(someday): Verify integrity. This is a SHA-1 hash. - if _, err := io.CopyN(ioutil.Discard, &r.r, githash.SHA1Size); err != nil { + if _, err := io.CopyN(io.Discard, &r.r, githash.SHA1Size); err != nil { return nil, fmt.Errorf("packfile: read trailing checksum: %w", err) } return nil, io.EOF diff --git a/packfile/reader_test.go b/packfile/reader_test.go index c051ab7..f8101d3 100644 --- a/packfile/reader_test.go +++ b/packfile/reader_test.go @@ -21,7 +21,6 @@ import ( "bytes" "errors" "io" - "io/ioutil" "os" "path/filepath" "testing" @@ -272,7 +271,7 @@ func readAll(br ByteReader) ([]unpackedObject, error) { } return got, err } - data, err := ioutil.ReadAll(r) + data, err := io.ReadAll(r) got = append(got, unpackedObject{ Header: hdr, Data: data, @@ -314,7 +313,7 @@ func hashLiteral(s string) githash.SHA1 { } func BenchmarkReader(b *testing.B) { - data, err := ioutil.ReadFile(filepath.Join("testdata", "FirstCommit.pack")) + data, err := os.ReadFile(filepath.Join("testdata", "FirstCommit.pack")) if err != nil { b.Fatal(err) } @@ -332,7 +331,7 @@ func BenchmarkReader(b *testing.B) { if err != nil { b.Fatal(err) } - if _, err := io.Copy(ioutil.Discard, r); err != nil { + if _, err := io.Copy(io.Discard, r); err != nil { b.Fatal(err) } } diff --git a/packfile/writer_test.go b/packfile/writer_test.go index 7d1d669..ef0ef4e 100644 --- a/packfile/writer_test.go +++ b/packfile/writer_test.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/binary" "io" - "io/ioutil" "testing" "github.com/google/go-cmp/cmp" @@ -76,7 +75,7 @@ func TestWriter(t *testing.T) { } t.Run("TooLong", func(t *testing.T) { - w := NewWriter(ioutil.Discard, 1) + w := NewWriter(io.Discard, 1) const blobContent = "Hi" _, err := w.WriteHeader(&Header{ Type: Blob, @@ -93,7 +92,7 @@ func TestWriter(t *testing.T) { }) t.Run("TooShort", func(t *testing.T) { - w := NewWriter(ioutil.Discard, 1) + w := NewWriter(io.Discard, 1) const blobContent = "Hello" _, err := w.WriteHeader(&Header{ Type: Blob,