Skip to content

Commit

Permalink
precommit: add check for modified files under packages with `main-onl…
Browse files Browse the repository at this point in the history
…y` targets. (digital-asset#17993)

Since digital-asset#17989

A simple heuristic to remind you to consider adding `run-all-tests: true` to your commit message if making changes in corners of the codebase most likely affected.
  • Loading branch information
raphael-speyer-da authored Dec 8, 2023
1 parent 4794454 commit 51bf3da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ repos:
language: system
pass_filenames: false
entry: "pre-commit/platform-independence-check.sh"
- id: mainonly
name: main only
description: Check for changes under packages with targets tagged with main-only
language: system
pass_filenames: true
entry: "dade-check-main-only-files"
types: [text]
30 changes: 30 additions & 0 deletions dev-env/bin/dade-check-main-only-files
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
#
# Checks whether any of the modified files sit under packages with targets marked as 'main-only'.
#
# This is as intended a simple heuristic to avoid accidentally omitting 'run-all-tests: true'
# on a PR branch which touches those files.
#

DADE_SKIP_MAIN_ONLY_CHECK="${DADE_SKIP_MAIN_ONLY_CHECK:-$(git log -1 --format=%b | tr -d ' ' | grep '^run-all-tests:true$')}"
if [[ -n "$DADE_SKIP_MAIN_ONLY_CHECK" ]]; then
# The user has been explicit about whether they want all tests run,
# either by setting the env var, or adding a line to their last commit.
exit 0;
fi

MODIFIED_FILES=$(echo "$@" | xargs -n1 | sort)
MAIN_ONLY_DIRS=$(bazel query "attr(tags, 'main-only', //...)" 2>/dev/null | awk -F: '{print $1}' | sort -u | sed -e 's|^//||')

# Does a cartesian product of the main-only dirs and modified files, and filters to files that are within dir.
SUSPECT_FILES=$(join -j99 <(echo "$MAIN_ONLY_DIRS") <(echo "$MODIFIED_FILES") | awk '$1 == substr($2, 1, length($1)) {print $2}')

if [[ -n "$SUSPECT_FILES" ]]; then
echo "
The following modified files sit under a directory with 'main-only' targets:
$SUSPECT_FILES
Consider putting a line with 'run-all-tests: true' in your PR description, or \`export DADE_SKIP_MAIN_ONLY_CHECK=1\` and try again.
" >&2
exit 1
fi

0 comments on commit 51bf3da

Please sign in to comment.