I’ve been writing a lot of Go code for the past few years and I have really come to appreciate the code generation patterns that Go developers use. In this case, the excellent Moq library to generate test doubles for unit tests. No third-party code is necessary to use these test doubles, which is an unusual joy nowadays with masses of transitive dependencies frequently required for anything interesting.
One thing that has been bugging me for a while with moq is that in its documentation and generated code it calls these test doubles mocks when they are clearly stubs. I know I’m getting on in years but mocks aren’t stubs is still one of my favourite hills to camp out on and yell at the kids. I seriously considered forking the tool and calling it stub (or stubborn) but while I’m irritated I don’t want to manage another open source tool.
To stop myself from being irritated at the generated code whenever I use this nifty test double generator I created a little bash script that now runs after go generate
in all my Makefiles.
#!/usr/bin/env bash
set -eo pipefail
grep -r -l 'Code generated by moq' --include='*.go' | grep -v 'vendor/' | while read -r src; do
echo "Converting ${src} ..."
sed 's/mocked/stubbed/g;s/mock/stub/g' "${src}" > "${src}.stub"
mv "${src}.stub" "${src}"
done
Hope you find it useful.