1
0
mirror of https://github.com/golang/go synced 2024-11-22 19:34:59 -07:00

os,path/filepath,testing: use slices to clean up tests

Replace reflect.DeepEqual with slices.Equal which is much faster.
This commit is contained in:
apocelipes 2024-05-24 00:35:07 +08:00
parent c5430dc1d8
commit 44ca9015d6
10 changed files with 19 additions and 23 deletions

View File

@ -6,7 +6,7 @@ package os_test
import ( import (
. "os" . "os"
"reflect" "slices"
"strings" "strings"
"testing" "testing"
) )
@ -91,7 +91,7 @@ func TestConsistentEnviron(t *testing.T) {
e0 := Environ() e0 := Environ()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
e1 := Environ() e1 := Environ()
if !reflect.DeepEqual(e0, e1) { if !slices.Equal(e0, e1) {
t.Fatalf("environment changed") t.Fatalf("environment changed")
} }
} }

View File

@ -5,7 +5,7 @@
package exec package exec
import ( import (
"reflect" "slices"
"testing" "testing"
) )
@ -60,7 +60,7 @@ func TestDedupEnv(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
got, err := dedupEnvCase(tt.noCase, tt.nulOK, tt.in) got, err := dedupEnvCase(tt.noCase, tt.nulOK, tt.in)
if !reflect.DeepEqual(got, tt.want) || (err != nil) != tt.wantErr { if !slices.Equal(got, tt.want) || (err != nil) != tt.wantErr {
t.Errorf("Dedup(%v, %q) = %q, %v; want %q, error:%v", tt.noCase, tt.in, got, err, tt.want, tt.wantErr) t.Errorf("Dedup(%v, %q) = %q, %v; want %q, error:%v", tt.noCase, tt.in, got, err, tt.want, tt.wantErr)
} }
} }

View File

@ -13,8 +13,8 @@ import (
"os" "os"
"os/user" "os/user"
"path/filepath" "path/filepath"
"reflect"
"runtime" "runtime"
"slices"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
@ -184,7 +184,7 @@ func TestImplicitPWD(t *testing.T) {
wantPWDs = nil wantPWDs = nil
} }
} }
if !reflect.DeepEqual(pwds, wantPWDs) { if !slices.Equal(pwds, wantPWDs) {
t.Errorf("PWD entries in cmd.Environ():\n\t%s\nwant:\n\t%s", strings.Join(pwds, "\n\t"), strings.Join(wantPWDs, "\n\t")) t.Errorf("PWD entries in cmd.Environ():\n\t%s\nwant:\n\t%s", strings.Join(pwds, "\n\t"), strings.Join(wantPWDs, "\n\t"))
} }
@ -257,7 +257,7 @@ func TestExplicitPWD(t *testing.T) {
} }
wantPWDs := []string{tc.pwd} wantPWDs := []string{tc.pwd}
if !reflect.DeepEqual(pwds, wantPWDs) { if !slices.Equal(pwds, wantPWDs) {
t.Errorf("PWD entries in cmd.Environ():\n\t%s\nwant:\n\t%s", strings.Join(pwds, "\n\t"), strings.Join(wantPWDs, "\n\t")) t.Errorf("PWD entries in cmd.Environ():\n\t%s\nwant:\n\t%s", strings.Join(pwds, "\n\t"), strings.Join(wantPWDs, "\n\t"))
} }

View File

@ -16,7 +16,6 @@ import (
. "os" . "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"slices" "slices"
@ -805,13 +804,13 @@ func TestReaddirStatFailures(t *testing.T) {
} }
if got, want := names(mustReadDir("initial readdir")), if got, want := names(mustReadDir("initial readdir")),
[]string{"good1", "good2", "x"}; !reflect.DeepEqual(got, want) { []string{"good1", "good2", "x"}; !slices.Equal(got, want) {
t.Errorf("initial readdir got %q; want %q", got, want) t.Errorf("initial readdir got %q; want %q", got, want)
} }
xerr = ErrNotExist xerr = ErrNotExist
if got, want := names(mustReadDir("with x disappearing")), if got, want := names(mustReadDir("with x disappearing")),
[]string{"good1", "good2"}; !reflect.DeepEqual(got, want) { []string{"good1", "good2"}; !slices.Equal(got, want) {
t.Errorf("with x disappearing, got %q; want %q", got, want) t.Errorf("with x disappearing, got %q; want %q", got, want)
} }

View File

@ -17,7 +17,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect"
"runtime" "runtime"
"slices" "slices"
"strings" "strings"
@ -778,7 +777,7 @@ func TestReadStdin(t *testing.T) {
for len(want) < 5 { for len(want) < 5 {
want = append(want, "") want = append(want, "")
} }
if !reflect.DeepEqual(all, want) { if !slices.Equal(all, want) {
t.Errorf("reading %q:\nhave %x\nwant %x", s, all, want) t.Errorf("reading %q:\nhave %x\nwant %x", s, all, want)
} }
}) })

View File

@ -9,7 +9,6 @@ import (
"internal/testenv" "internal/testenv"
"os" "os"
. "path/filepath" . "path/filepath"
"reflect"
"runtime" "runtime"
"slices" "slices"
"strings" "strings"
@ -367,7 +366,7 @@ func TestNonWindowsGlobEscape(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Glob error for %q: %s", pattern, err) t.Fatalf("Glob error for %q: %s", pattern, err)
} }
if !reflect.DeepEqual(matches, want) { if !slices.Equal(matches, want) {
t.Fatalf("Glob(%#q) = %v want %v", pattern, matches, want) t.Fatalf("Glob(%#q) = %v want %v", pattern, matches, want)
} }
} }

View File

@ -364,7 +364,7 @@ func TestSplitList(t *testing.T) {
tests = append(tests, winsplitlisttests...) tests = append(tests, winsplitlisttests...)
} }
for _, test := range tests { for _, test := range tests {
if l := filepath.SplitList(test.list); !reflect.DeepEqual(l, test.result) { if l := filepath.SplitList(test.list); !slices.Equal(l, test.result) {
t.Errorf("SplitList(%#q) = %#q, want %#q", test.list, l, test.result) t.Errorf("SplitList(%#q) = %#q, want %#q", test.list, l, test.result)
} }
} }
@ -1004,7 +1004,7 @@ func TestWalkSymlinkRoot(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if !reflect.DeepEqual(walked, tt.want) { if !slices.Equal(walked, tt.want) {
t.Logf("Walk(%#q) visited %#q; want %#q", tt.root, walked, tt.want) t.Logf("Walk(%#q) visited %#q; want %#q", tt.root, walked, tt.want)
if slices.Contains(tt.buggyGOOS, runtime.GOOS) { if slices.Contains(tt.buggyGOOS, runtime.GOOS) {
t.Logf("(ignoring known bug on %v)", runtime.GOOS) t.Logf("(ignoring known bug on %v)", runtime.GOOS)
@ -1950,7 +1950,7 @@ func TestIssue51617(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
want := []string{".", "a", filepath.Join("a", "bad"), filepath.Join("a", "next")} want := []string{".", "a", filepath.Join("a", "bad"), filepath.Join("a", "next")}
if !reflect.DeepEqual(saw, want) { if !slices.Equal(saw, want) {
t.Errorf("got directories %v, want %v", saw, want) t.Errorf("got directories %v, want %v", saw, want)
} }
} }

View File

@ -13,8 +13,8 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect"
"runtime/debug" "runtime/debug"
"slices"
"strings" "strings"
"testing" "testing"
) )
@ -83,7 +83,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
case err != nil: case err != nil:
t.Errorf("%d,%d: execution error %v\n%q", ti, i, err, out) t.Errorf("%d,%d: execution error %v\n%q", ti, i, err, out)
return return
case !reflect.DeepEqual(out, exp): case !slices.Equal(out, exp):
t.Errorf("%d,%d: expected %#q, got %#q", ti, i, exp, out) t.Errorf("%d,%d: expected %#q, got %#q", ti, i, exp, out)
return return
default: default:

View File

@ -11,7 +11,6 @@ import (
"io" "io"
"io/fs" "io/fs"
"path" "path"
"reflect"
"slices" "slices"
"strings" "strings"
"testing/iotest" "testing/iotest"
@ -358,7 +357,7 @@ func (t *fsTester) checkGlob(dir string, list []fs.DirEntry) {
t.errorf("%s: Glob(%#q): %w", dir, glob, err) t.errorf("%s: Glob(%#q): %w", dir, glob, err)
return return
} }
if reflect.DeepEqual(want, names) { if slices.Equal(want, names) {
return return
} }

View File

@ -7,9 +7,9 @@ package testing
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"reflect"
"regexp" "regexp"
"runtime" "runtime"
"slices"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -886,7 +886,7 @@ func TestCleanup(t *T) {
t.Cleanup(func() { cleanups = append(cleanups, 1) }) t.Cleanup(func() { cleanups = append(cleanups, 1) })
t.Cleanup(func() { cleanups = append(cleanups, 2) }) t.Cleanup(func() { cleanups = append(cleanups, 2) })
}) })
if got, want := cleanups, []int{2, 1}; !reflect.DeepEqual(got, want) { if got, want := cleanups, []int{2, 1}; !slices.Equal(got, want) {
t.Errorf("unexpected cleanup record; got %v want %v", got, want) t.Errorf("unexpected cleanup record; got %v want %v", got, want)
} }
} }