Skip to content

Instantly share code, notes, and snippets.

@anonymouse64
Created July 16, 2020 18:42
Show Gist options
  • Select an option

  • Save anonymouse64/89ceb21d80772658c82d2766f8b25c10 to your computer and use it in GitHub Desktop.

Select an option

Save anonymouse64/89ceb21d80772658c82d2766f8b25c10 to your computer and use it in GitHub Desktop.
go-check MockCommand mocking of a fully verifying argument script
func mockRealSystemdMountInBash(c *C, expArgs [][]string) *testutil.MockCmd {
stateCallFile := filepath.Join(c.MkDir(), "systemd-mount-state-call")
template := fmt.Sprintf(`
STATE_FILE=%s
if [ ! -f "$STATE_FILE" ]; then
CALL_NUM=0
else
CALL_NUM=$(cat "$STATE_FILE")
fi
CALL_NUM=$(( CALL_NUM + 1 ))
echo "${CALL_NUM}" > "$STATE_FILE"
ALL_ARGS="$*"
#set -x
#exec 2>&1
fail() {
echo "unexpected systemd-mount call: ${ALL_ARGS}"
exit 1
}
case "${CALL_NUM}" in
`, stateCallFile)
for i, args := range expArgs {
argsAsString := strings.Join(args, " ")
caseStr := fmt.Sprintf(`
%d)
test "${ALL_ARGS}" = %q || fail ;;
`,
i+1,
argsAsString,
)
template += caseStr
}
// add the closing bit to the case
template += `
*)
fail
esac
`
return testutil.MockCommand(c, "systemd-mount", template)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment