Skip to content

Commit e635309

Browse files
committed
test(cli): make single-word-arg quoting test hermetic
TestQuoteExecArgs_SingleWordArgsUnaffected exec'd a real `redis-cli PING` and keyed off "not found" in the error to decide pass/fail. That made it environment-dependent: on a machine where redis-cli is installed but no Redis is running, the binary runs and exits non-zero with "Connection refused" (no "not found"), failing the test for a reason unrelated to argument quoting. Rewrite it against the controlled argv-dumper — the same fixture the sibling multi-word test uses — and assert the arg survives verbatim (ARG<PING>, no split/merge/quote leakage). Now deterministic regardless of what's installed or running.
1 parent 8cfc05c commit e635309

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

‎internal/cli/accessory_test.go‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,34 @@ func TestQuoteExecArgs_PreservesMultiWordArgBoundary(t *testing.T) {
4242
}
4343
}
4444

45+
// TestQuoteExecArgs_SingleWordArgsUnaffected verifies the common case — a
46+
// command whose args have no whitespace — passes through quoteExecArgs
47+
// unaltered: two single-word args stay two args, neither split nor merged.
48+
//
49+
// It runs against the controlled argv-dumper, not a real binary. The prior
50+
// version exec'd `redis-cli PING` and keyed off "not found" in the error,
51+
// which made it environment-dependent and flaky: on a machine where
52+
// redis-cli IS installed but no Redis is running, the binary runs and exits
53+
// non-zero with "Connection refused" — no "not found" — and the test
54+
// failed for a reason unrelated to argument quoting.
4555
func TestQuoteExecArgs_SingleWordArgsUnaffected(t *testing.T) {
46-
command := quoteExecArgs([]string{"redis-cli", "PING"})
56+
dumper := writeArgvDumper(t)
57+
command := quoteExecArgs([]string{dumper, "PING"})
58+
4759
wrapped := "sh -c " + shQuoteForTest(command)
48-
// redis-cli isn't necessarily installed in the test sandbox — just
49-
// confirm the reconstructed command line is syntactically sane (the
50-
// shell can parse and attempt to exec it) rather than requiring the
51-
// binary to exist.
5260
out, err := exec.Command("/bin/sh", "-c", wrapped).CombinedOutput()
53-
if err == nil {
54-
return
61+
if err != nil {
62+
t.Fatalf("running wrapped command: %v (output: %s)", err, out)
63+
}
64+
65+
got := string(out)
66+
// The single-word arg survives as exactly one argument, verbatim.
67+
if !strings.Contains(got, "ARG<PING>") {
68+
t.Errorf("expected PING to survive as one argument, got:\n%s", got)
5569
}
56-
if !strings.Contains(string(out), "not found") && !strings.Contains(string(out), "No such file") {
57-
t.Errorf("unexpected shell error (command line may be malformed): %v (output: %s)", err, out)
70+
// No spurious splitting, empty args, or quote characters leaking through.
71+
if strings.Contains(got, "ARG<>") || strings.Contains(got, "ARG<'PING'>") {
72+
t.Errorf("single-word arg was altered by quoting:\n%s", got)
5873
}
5974
}
6075

0 commit comments

Comments
 (0)