]> Cypherpunks.ru repositories - gostls13.git/commitdiff
internal/zstd: use dynamic path resolution for zstd in tests
authoraimuz <mr.imuz@gmail.com>
Wed, 8 Nov 2023 12:10:13 +0000 (12:10 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 8 Nov 2023 17:52:52 +0000 (17:52 +0000)
Abstract the hardcoded '/usr/bin/zstd' paths in fuzz and unit tests
to support systems where zstd may be installed at different locations.
The `findZstd` function uses `exec.LookPath` to locate the binary,
enhancing test portability.

Fixes #64000

Change-Id: I0ebe5bbcf3ddc6fccf176c13639ca9d855bcab87
GitHub-Last-Rev: c4dfe1139bdc2f4f3200f80b314a02b5df5cd995
GitHub-Pull-Request: golang/go#64002
Reviewed-on: https://go-review.googlesource.com/c/go/+/540522
Reviewed-by: Klaus Post <klauspost@gmail.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/internal/zstd/fuzz_test.go
src/internal/zstd/zstd_test.go

index bb6f0a9721f803190cdff3f3ac1318ef6841bf8b..12738519f89607167525c7c7c05de170472fa9ac 100644 (file)
@@ -43,9 +43,7 @@ func FuzzReader(f *testing.F) {
 // explore the space of decompressor behavior, since it can't see
 // what the compressor is doing. But it's better than nothing.
 func FuzzDecompressor(f *testing.F) {
-       if _, err := os.Stat("/usr/bin/zstd"); err != nil {
-               f.Skip("skipping because /usr/bin/zstd does not exist")
-       }
+       zstd := findZstd(f)
 
        for _, test := range tests {
                f.Add([]byte(test.uncompressed))
@@ -61,7 +59,7 @@ func FuzzDecompressor(f *testing.F) {
        f.Add(bigData(f))
 
        f.Fuzz(func(t *testing.T, b []byte) {
-               cmd := exec.Command("/usr/bin/zstd", "-z")
+               cmd := exec.Command(zstd, "-z")
                cmd.Stdin = bytes.NewReader(b)
                var compressed bytes.Buffer
                cmd.Stdout = &compressed
@@ -84,9 +82,7 @@ func FuzzDecompressor(f *testing.F) {
 // Fuzz test to check that if we can decompress some data,
 // so can zstd, and that we get the same result.
 func FuzzReverse(f *testing.F) {
-       if _, err := os.Stat("/usr/bin/zstd"); err != nil {
-               f.Skip("skipping because /usr/bin/zstd does not exist")
-       }
+       zstd := findZstd(f)
 
        for _, test := range tests {
                f.Add([]byte(test.compressed))
@@ -100,7 +96,7 @@ func FuzzReverse(f *testing.F) {
                r := NewReader(bytes.NewReader(b))
                goExp, goErr := io.ReadAll(r)
 
-               cmd := exec.Command("/usr/bin/zstd", "-d")
+               cmd := exec.Command(zstd, "-d")
                cmd.Stdin = bytes.NewReader(b)
                var uncompressed bytes.Buffer
                cmd.Stdout = &uncompressed
index 70141b02d055ace8ca53e6152bd40eb5f9bbdd43..4ae6f2b3982c1cc3ed88d393255b9b5b1c762393 100644 (file)
@@ -167,10 +167,17 @@ func bigData(t testing.TB) []byte {
        return bigDataBytes
 }
 
+func findZstd(t testing.TB) string {
+       zstd, err := exec.LookPath("zstd")
+       if err != nil {
+               t.Skip("skipping because zstd not found")
+       }
+       return zstd
+}
+
 var (
        zstdBigOnce  sync.Once
        zstdBigBytes []byte
-       zstdBigSkip  bool
        zstdBigErr   error
 )
 
@@ -180,13 +187,10 @@ var (
 func zstdBigData(t testing.TB) []byte {
        input := bigData(t)
 
-       zstdBigOnce.Do(func() {
-               if _, err := os.Stat("/usr/bin/zstd"); err != nil {
-                       zstdBigSkip = true
-                       return
-               }
+       zstd := findZstd(t)
 
-               cmd := exec.Command("/usr/bin/zstd", "-z")
+       zstdBigOnce.Do(func() {
+               cmd := exec.Command(zstd, "-z")
                cmd.Stdin = bytes.NewReader(input)
                var compressed bytes.Buffer
                cmd.Stdout = &compressed
@@ -198,9 +202,6 @@ func zstdBigData(t testing.TB) []byte {
 
                zstdBigBytes = compressed.Bytes()
        })
-       if zstdBigSkip {
-               t.Skip("skipping because /usr/bin/zstd does not exist")
-       }
        if zstdBigErr != nil {
                t.Fatal(zstdBigErr)
        }
@@ -217,7 +218,7 @@ func TestLarge(t *testing.T) {
        data := bigData(t)
        compressed := zstdBigData(t)
 
-       t.Logf("/usr/bin/zstd compressed %d bytes to %d", len(data), len(compressed))
+       t.Logf("zstd compressed %d bytes to %d", len(data), len(compressed))
 
        r := NewReader(bytes.NewReader(compressed))
        got, err := io.ReadAll(r)