Skip to content

Commit

Permalink
testutil: support empty string args in fakecommand (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
flotter authored Aug 1, 2023
1 parent 523aa06 commit f3d036c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 4 additions & 5 deletions internals/testutil/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type FakeCmd struct {
// faking commands that need "\n" in their args (like zenity)
// we use the following convention:
// - generate \0 to separate args
// - generate \0\0 to separate commands
// - generate \0\f\n\r magic sequence to separate commands
var scriptTpl = `#!/bin/bash
printf "%%s" "$(basename "$0")" >> %[1]q
printf '\0' >> %[1]q
Expand All @@ -102,7 +102,7 @@ for arg in "$@"; do
printf '\0' >> %[1]q
done
printf '\0' >> %[1]q
printf '\f\n\r' >> %[1]q
%s
`

Expand Down Expand Up @@ -176,12 +176,11 @@ func (cmd *FakeCmd) Calls() [][]string {
if err != nil {
panic(err)
}
logContent := strings.TrimSuffix(string(raw), "\000")
logContent := strings.TrimSuffix(string(raw), "\000\f\n\r")

allCalls := [][]string{}
calls := strings.Split(logContent, "\000\000")
calls := strings.Split(logContent, "\000\f\n\r")
for _, call := range calls {
call = strings.TrimSuffix(call, "\000")
allCalls = append(allCalls, strings.Split(call, "\000"))
}
return allCalls
Expand Down
6 changes: 6 additions & 0 deletions internals/testutil/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ func (s *fakeCommandSuite) TestFakeCommand(c *check.C) {
c.Assert(err, check.IsNil)
err = exec.Command("cmd", "second-run", "--arg1", "arg2", "a %s").Run()
c.Assert(err, check.IsNil)
err = exec.Command("cmd", "third-run", "--arg1", "arg2", "").Run()
c.Assert(err, check.IsNil)
err = exec.Command("cmd", "forth-run", "--arg1", "arg2", "", "a %s").Run()
c.Assert(err, check.IsNil)
c.Assert(fake.Calls(), check.DeepEquals, [][]string{
{"cmd", "first-run", "--arg1", "arg2", "a space"},
{"cmd", "second-run", "--arg1", "arg2", "a %s"},
{"cmd", "third-run", "--arg1", "arg2", ""},
{"cmd", "forth-run", "--arg1", "arg2", "", "a %s"},
})
}

Expand Down

0 comments on commit f3d036c

Please sign in to comment.