diff --git a/scripts/presubmit-tests.sh b/scripts/presubmit-tests.sh index b10a8515cfc..3f15cf3abaf 100755 --- a/scripts/presubmit-tests.sh +++ b/scripts/presubmit-tests.sh @@ -43,7 +43,7 @@ IS_DOCUMENTATION_PR=0 # Returns true if PR only contains the given file regexes. # Parameters: $1 - file regexes, space separated. function pr_only_contains() { - [[ -z "$(echo "${CHANGED_FILES}" | grep -v \(${1// /\\|}\)$))" ]] + [[ -z "$(echo "${CHANGED_FILES}" | grep -v "\(${1// /\\|}\)$")" ]] } # List changed files in the current PR. diff --git a/test/unit/presubmit-tests.sh b/test/unit/presubmit-tests.sh index 1b2bef809c8..134acb340e3 100755 --- a/test/unit/presubmit-tests.sh +++ b/test/unit/presubmit-tests.sh @@ -175,6 +175,55 @@ function test_custom_runners_fail_fast() { } } +function test_exempt_md() { + source $(dirname $0)/../../scripts/presubmit-tests.sh + + function list_changed_files() { + echo "README.md" + echo "OWNERS" + echo "foo.png" + } + initialize_environment + (( IS_DOCUMENTATION_PR )) || test_failed "README.md is documentation" + (( ! IS_PRESUBMIT_EXEMPT_PR )) || test_failed "README.md is not exempt" +} + +function test_exempt_no_md() { + source $(dirname $0)/../../scripts/presubmit-tests.sh + + function list_changed_files() { + echo "OWNERS" + echo "AUTHORS" + } + initialize_environment + (( IS_DOCUMENTATION_PR )) || test_failed "OWNERS is considered documentation" + (( IS_PRESUBMIT_EXEMPT_PR )) || test_failed "OWNERS is exempt" +} + +function test_exempt_md_code() { + source $(dirname $0)/../../scripts/presubmit-tests.sh + + function list_changed_files() { + echo "OWNERS" + echo "README.md" + echo "foo.go" + } + initialize_environment + (( ! IS_DOCUMENTATION_PR )) || test_failed "README.md is documentation" + (( ! IS_PRESUBMIT_EXEMPT_PR )) || test_failed "foo.go is not exempt" +} + +function test_exempt_code() { + source $(dirname $0)/../../scripts/presubmit-tests.sh + + function list_changed_files() { + echo "foo.go" + } + initialize_environment + (( ! IS_DOCUMENTATION_PR )) || test_failed "foo.go is not documentation" + (( ! IS_PRESUBMIT_EXEMPT_PR )) || test_failed "foo.go is not exempt" +} + function run_markdown_build_tests() { function list_changed_files() { echo "README.md" @@ -190,6 +239,13 @@ function run_main() { main } +echo ">> Testing presubmit exempt flow" + +test_function ${SUCCESS} "Changed files in commit" call_function_pre test_exempt_md +test_function ${SUCCESS} "Changed files in commit" call_function_pre test_exempt_no_md +test_function ${SUCCESS} "Changed files in commit" call_function_pre test_exempt_md_code +test_function ${SUCCESS} "Changed files in commit" call_function_pre test_exempt_code + echo ">> Testing custom test runners" test_function ${SUCCESS} "Test passed" call_function_pre test_custom_runners_all run_main