Skip to content

Commit 374a56c

Browse files
aimuzgopherbot
authored andcommitted
internal/zstd: use dynamic path resolution for zstd in tests
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: c4dfe11 GitHub-Pull-Request: #64002 Reviewed-on: https://go-review.googlesource.com/c/go/+/540522 Reviewed-by: Klaus Post <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Auto-Submit: Bryan Mills <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent eebeca8 commit 374a56c

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

src/internal/zstd/fuzz_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ func FuzzReader(f *testing.F) {
4343
// explore the space of decompressor behavior, since it can't see
4444
// what the compressor is doing. But it's better than nothing.
4545
func FuzzDecompressor(f *testing.F) {
46-
if _, err := os.Stat("/usr/bin/zstd"); err != nil {
47-
f.Skip("skipping because /usr/bin/zstd does not exist")
48-
}
46+
zstd := findZstd(f)
4947

5048
for _, test := range tests {
5149
f.Add([]byte(test.uncompressed))
@@ -61,7 +59,7 @@ func FuzzDecompressor(f *testing.F) {
6159
f.Add(bigData(f))
6260

6361
f.Fuzz(func(t *testing.T, b []byte) {
64-
cmd := exec.Command("/usr/bin/zstd", "-z")
62+
cmd := exec.Command(zstd, "-z")
6563
cmd.Stdin = bytes.NewReader(b)
6664
var compressed bytes.Buffer
6765
cmd.Stdout = &compressed
@@ -84,9 +82,7 @@ func FuzzDecompressor(f *testing.F) {
8482
// Fuzz test to check that if we can decompress some data,
8583
// so can zstd, and that we get the same result.
8684
func FuzzReverse(f *testing.F) {
87-
if _, err := os.Stat("/usr/bin/zstd"); err != nil {
88-
f.Skip("skipping because /usr/bin/zstd does not exist")
89-
}
85+
zstd := findZstd(f)
9086

9187
for _, test := range tests {
9288
f.Add([]byte(test.compressed))
@@ -100,7 +96,7 @@ func FuzzReverse(f *testing.F) {
10096
r := NewReader(bytes.NewReader(b))
10197
goExp, goErr := io.ReadAll(r)
10298

103-
cmd := exec.Command("/usr/bin/zstd", "-d")
99+
cmd := exec.Command(zstd, "-d")
104100
cmd.Stdin = bytes.NewReader(b)
105101
var uncompressed bytes.Buffer
106102
cmd.Stdout = &uncompressed

src/internal/zstd/zstd_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,17 @@ func bigData(t testing.TB) []byte {
167167
return bigDataBytes
168168
}
169169

170+
func findZstd(t testing.TB) string {
171+
zstd, err := exec.LookPath("zstd")
172+
if err != nil {
173+
t.Skip("skipping because zstd not found")
174+
}
175+
return zstd
176+
}
177+
170178
var (
171179
zstdBigOnce sync.Once
172180
zstdBigBytes []byte
173-
zstdBigSkip bool
174181
zstdBigErr error
175182
)
176183

@@ -180,13 +187,10 @@ var (
180187
func zstdBigData(t testing.TB) []byte {
181188
input := bigData(t)
182189

183-
zstdBigOnce.Do(func() {
184-
if _, err := os.Stat("/usr/bin/zstd"); err != nil {
185-
zstdBigSkip = true
186-
return
187-
}
190+
zstd := findZstd(t)
188191

189-
cmd := exec.Command("/usr/bin/zstd", "-z")
192+
zstdBigOnce.Do(func() {
193+
cmd := exec.Command(zstd, "-z")
190194
cmd.Stdin = bytes.NewReader(input)
191195
var compressed bytes.Buffer
192196
cmd.Stdout = &compressed
@@ -198,9 +202,6 @@ func zstdBigData(t testing.TB) []byte {
198202

199203
zstdBigBytes = compressed.Bytes()
200204
})
201-
if zstdBigSkip {
202-
t.Skip("skipping because /usr/bin/zstd does not exist")
203-
}
204205
if zstdBigErr != nil {
205206
t.Fatal(zstdBigErr)
206207
}
@@ -217,7 +218,7 @@ func TestLarge(t *testing.T) {
217218
data := bigData(t)
218219
compressed := zstdBigData(t)
219220

220-
t.Logf("/usr/bin/zstd compressed %d bytes to %d", len(data), len(compressed))
221+
t.Logf("zstd compressed %d bytes to %d", len(data), len(compressed))
221222

222223
r := NewReader(bytes.NewReader(compressed))
223224
got, err := io.ReadAll(r)

0 commit comments

Comments
 (0)