Using make and git diff for a simple and powerful test harness

https://chrismorgan.info/blog/make-and-git-diff-test-harness/

This is a barebones testing harness that uses a Makefile to set up tests on arbitrary scripts. This is relatively rudimentary, but I can can see a use for it to test simple CLI tools.

The Makefile itself is magical/incomprehensible. I didn’t realize Make was capable of being dynamic at this level. It looks for scriptname.test files, executes them, and compares stdout and stdin to scriptname.stdout and scriptname.stdin (respectively).

I’m not entirely sure why this uses git diff over plain diff though.

rwildcard = $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2) $(filter $2,$d))

.PHONY: test
test: $(patsubst %.test,%.stdout,$(call rwildcard,,%.test))

%.stdout: %.test
	./$< > $@ 2> $(patsubst %.stdout,%.stderr,$@) \
		|| (touch --date=@0 $@; false)
	git diff --exit-code --src-prefix=expected/ --dst-prefix=actual/ \
		$@ $(patsubst %.stdout,%.stderr,$@) \
		|| (touch --date=@0 $@; false)
Edit