A single command to test all my changed Go packages
go test $(git ls-files --modified | xargs dirname | uniq | xargs -I '{}' echo "./{}")
I work in a Go monolith with many packages, and my patches often span multiple patches. Before I push to CI, I like to test all the packages with changed files, but enumerating all those packages by hand is tedious.
I’ve automated this with a short shell script:
go test $(git ls-files --modified \
| xargs dirname \
| uniq \
| xargs -I '{}' echo "./{}")Here’s how it works:
git ls-files --modified– print the path of every file with an unstaged changexargs dirname– print the list of folders/packagesuniq– de-duplicate the listxargs -I '{}' echo "./{}"– prepend./to each path, so the folder can be passed togo test