Skip to content

Instantly share code, notes, and snippets.

@StevenACoffman
Last active August 18, 2022 21:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenACoffman/a4067151bd10627d6cdcbddb96cdc40f to your computer and use it in GitHub Desktop.
Save StevenACoffman/a4067151bd10627d6cdcbddb96cdc40f to your computer and use it in GitHub Desktop.
Testing Go module updates

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment