Skip to content

Commit

Permalink
More exhaustive assertions for native rule tag propagation tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Silic0nS0ldier authored and copybara-github committed Jan 31, 2025
1 parent b17a37b commit 73317d0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 62 deletions.
121 changes: 64 additions & 57 deletions src/test/shell/bazel/tags_propagation_native_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
package(default_visibility = ["//visibility:public"])
Expand All @@ -83,21 +82,21 @@ 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("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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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 <<EOF
Expand All @@ -250,17 +248,17 @@ 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 "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 <<EOF
Expand All @@ -276,9 +274,16 @@ 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"

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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/test/shell/unittest.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 73317d0

Please sign in to comment.