From 73317d00065ba31d74fb7eaa647f5cc03e9df8cc Mon Sep 17 00:00:00 2001 From: Jordan Mele Date: Fri, 31 Jan 2025 04:56:12 -0800 Subject: [PATCH] More exhaustive assertions for native rule tag propagation tests In `src/test/shell/bazel/tags_propagation_native_test.sh` * **`assert_contains_n "Command Line:" n output1`** `ExecutionInfo: {` is inappropriate as `TemplateExpand` also have execution info. * **`assert_contains_n "(local|no-cache|no-remote):" n output1`** Counting `n` increases confidence that all expected actions have the execution info they should have. e.g. tags propagated for tests, but not other actions when `--incompatible_allow_tags_propagation=false` * **`--experimental_allow_tags_propagation` -> `--incompatible_allow_tags_propagation`** Addresses warnings in test logs. Changes in `src/test/shell/unittest.bash` address a bug in `assert_contains_n` which on failure was using the wrong argument to refer to the input file. For consistency other places were also updated to use `$file` instead of the argument number. Closes #25050. PiperOrigin-RevId: 721731868 Change-Id: I3684b29df06539764be99f7db731cbe0efa0f913 --- .../bazel/tags_propagation_native_test.sh | 121 +++++++++--------- src/test/shell/unittest.bash | 10 +- 2 files changed, 69 insertions(+), 62 deletions(-) diff --git a/src/test/shell/bazel/tags_propagation_native_test.sh b/src/test/shell/bazel/tags_propagation_native_test.sh index b92c561c86d3c3..63964ef2963f94 100755 --- a/src/test/shell/bazel/tags_propagation_native_test.sh +++ b/src/test/shell/bazel/tags_propagation_native_test.sh @@ -40,35 +40,34 @@ EOF int main() { std::cout << "Hello test!" << std::endl; return 0; } EOF - bazel aquery --experimental_allow_tags_propagation 'mnemonic("CppCompile", //test:test)' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation 'mnemonic("CppCompile", //test:test)' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 1 output1 + assert_contains_n "local:" 1 output1 + assert_contains_n "no-cache:" 1 output1 + assert_contains_n "no-remote:" 1 output1 - bazel aquery --experimental_allow_tags_propagation 'mnemonic("CppArchive", outputs(".*/libtest.a", //test:test))' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation 'mnemonic("CppArchive", outputs(".*/libtest.a", //test:test))' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 1 output1 + assert_contains_n "local:" 1 output1 + assert_contains_n "no-cache:" 1 output1 + assert_contains_n "no-remote:" 1 output1 if [ "${PLATFORM}" != "darwin" ]; then # Darwin does not support implicit "nodeps" shared libraries. - bazel aquery --experimental_allow_tags_propagation 'mnemonic("CppLink", outputs(".*/libtest.so", //test:test))' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation 'mnemonic("CppLink", outputs(".*/libtest.so", //test:test))' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 1 output1 + assert_contains_n "local:" 1 output1 + assert_contains_n "no-cache:" 1 output1 + assert_contains_n "no-remote:" 1 output1 fi } function test_cc_binary_tags_propagated() { - mkdir -p test cat > test/BUILD < output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation 'mnemonic("CppCompile", //test:test)' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 1 output1 + assert_contains_n "local:" 1 output1 + assert_contains_n "no-cache:" 1 output1 + assert_contains_n "no-remote:" 1 output1 - bazel aquery --experimental_allow_tags_propagation 'mnemonic("CppLink", //test:test)' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation 'mnemonic("CppLink", //test:test)' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 1 output1 + assert_contains_n "local:" 1 output1 + assert_contains_n "no-cache:" 1 output1 + assert_contains_n "no-remote:" 1 output1 } function test_java_library_tags_propagated() { @@ -119,13 +118,13 @@ public class Main { } EOF - bazel aquery --experimental_allow_tags_propagation '//test:test' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation '//test:test' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 3 output1 + assert_contains_n "local:" 3 output1 + assert_contains_n "no-cache:" 3 output1 + assert_contains_n "no-remote:" 3 output1 } function test_java_binary_tags_propagated() { @@ -148,13 +147,13 @@ public class Main { } EOF - bazel aquery --experimental_allow_tags_propagation '//test:test' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation '//test:test' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 4 output1 + assert_contains_n "local:" 4 output1 + assert_contains_n "no-cache:" 4 output1 + assert_contains_n "no-remote:" 4 output1 } function write_hello_library_files() { @@ -203,14 +202,13 @@ function test_java_header_tags_propagated() { mkdir "$pkg" || fail "mkdir $pkg" write_hello_library_files "$pkg" - bazel aquery --experimental_allow_tags_propagation --java_header_compilation=true //$pkg/java/main:main > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation --java_header_compilation=true //$pkg/java/main:main > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 - + assert_contains_n "Command Line:" 4 output1 + assert_contains_n "local:" 4 output1 + assert_contains_n "no-cache:" 4 output1 + assert_contains_n "no-remote:" 4 output1 } function test_genrule_tags_propagated() { @@ -225,16 +223,16 @@ genrule( ) EOF - bazel aquery --experimental_allow_tags_propagation '//test:test' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation '//test:test' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 1 output1 + assert_contains_n "local:" 1 output1 + assert_contains_n "no-cache:" 1 output1 + assert_contains_n "no-remote:" 1 output1 } -# Test a native test rule rule which has tags, that should be propagated (independent of flags) +# Test a native test rule which has tags, that should be propagated (independent of flags) function test_test_rules_tags_propagated() { mkdir -p test cat > test/BUILD < output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation=false '//test:test' > output1 2> $TEST_log \ || fail "should have generated output successfully" - assert_contains "ExecutionInfo: {" output1 - assert_contains "local:" output1 - assert_contains "no-cache:" output1 - assert_contains "no-remote:" output1 + assert_contains_n "Command Line:" 4 output1 + assert_contains_n "local:" 1 output1 + assert_contains_n "no-cache:" 1 output1 + assert_contains_n "no-remote:" 1 output1 } # Test a basic native rule which has tags, that should not be propagated -# as --experimental_allow_tags_propagation flag set to false +# as --incompatible_allow_tags_propagation flag set to false function test_cc_library_tags_not_propagated_when_incompatible_flag_off() { mkdir -p test cat > test/BUILD < output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation=false '//test:test' > output1 2> $TEST_log \ || fail "should have generated output successfully" + if is_darwin; then + # CppCompile, CppArchive + assert_contains_n "Command Line:" 2 output1 + else + # CppCompile, CppArchive, CppLink + assert_contains_n "Command Line:" 3 output1 + fi assert_not_contains "local:" output1 assert_not_contains "no-cache:" output1 assert_not_contains "no-remote:" output1 @@ -300,9 +305,10 @@ EOF int main() { std::cout << "Hello test!" << std::endl; return 0; } EOF - bazel aquery --experimental_allow_tags_propagation=false '//test:test' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation=false '//test:test' > output1 2> $TEST_log \ || fail "should have generated output successfully" + assert_contains_n "Command Line:" 3 output1 assert_not_contains "local:" output1 assert_not_contains "no-cache:" output1 assert_not_contains "no-remote:" output1 @@ -327,9 +333,10 @@ public class Main { } EOF - bazel aquery --experimental_allow_tags_propagation=false '//test:test' > output1 2> $TEST_log \ + bazel aquery --incompatible_allow_tags_propagation=false '//test:test' > output1 2> $TEST_log \ || fail "should have generated output successfully" + assert_contains_n "Command Line:" 3 output1 assert_not_contains "local:" output1 assert_not_contains "no-cache:" output1 assert_not_contains "no-remote:" output1 diff --git a/src/test/shell/unittest.bash b/src/test/shell/unittest.bash index 41192a71d2b9d7..7878605a06632a 100644 --- a/src/test/shell/unittest.bash +++ b/src/test/shell/unittest.bash @@ -477,7 +477,7 @@ function assert_contains() { local message=${3:-"Expected regexp '$pattern' not found in '$file'"} grep -sq -- "$pattern" "$file" && return 0 - fail "$message" $(__copy_to_undeclared_outputs "$2") + fail "$message" $(__copy_to_undeclared_outputs "$file") return 1 } @@ -497,7 +497,7 @@ function assert_not_contains() { return 1 fi - fail "$message" $(__copy_to_undeclared_outputs "$2") + fail "$message" $(__copy_to_undeclared_outputs "$file") return 1 } @@ -515,7 +515,7 @@ function assert_contains_n() { fi (( count == expectednum )) && return 0 - fail "$message" $(__copy_to_undeclared_outputs "$2") + fail "$message" $(__copy_to_undeclared_outputs "$file") return 1 } @@ -556,7 +556,7 @@ function assert_empty_file() { return 0 fi - fail "$message" $(__copy_to_undeclared_outputs "$1") + fail "$message" $(__copy_to_undeclared_outputs "$file") return 1 } @@ -571,7 +571,7 @@ function assert_nonempty_file() { return 0 fi - fail "$message" $(__copy_to_undeclared_outputs "$1") + fail "$message" $(__copy_to_undeclared_outputs "$file") return 1 }