So when we updated a few Go libraries, we wanted to run all the relevant tests... but not ALL tests. I thought this shell script one of my co-workers (@csilvers) used was very clever and handy:
git grep -l -e opentelemetry -e otelsql '*.go' | grep -v testdata | xargs -n1 dirname | sed 's,^,./,' | sort -u | xargs gotestsum
Walking through what it does:
- git-grep
-l, --files-with-matches
,-e <pattern>
- grep
-v, --invert-match
Invert the sense of matching, to select non-matching lines. (exclude testdata dir matches) - xargs
-n max-args
Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit. - dirname - strip last component from file name
- sed
's,^,./,'
- prefix./
to beginning of all lines e.g.services/users/resolvers
becomes./services/users/resolvers
- sort
-u, --unique
- sort lines of text files, output only the first of an equal run xargs
- build and execute command lines from standard input