diff options
-rwxr-xr-x | src/test/shell/bazel/bazel_test_test.sh | 87 | ||||
-rwxr-xr-x | tools/test/test-setup.sh | 38 |
2 files changed, 125 insertions, 0 deletions
diff --git a/src/test/shell/bazel/bazel_test_test.sh b/src/test/shell/bazel/bazel_test_test.sh index 31dfa87da5..0cf9154594 100755 --- a/src/test/shell/bazel/bazel_test_test.sh +++ b/src/test/shell/bazel/bazel_test_test.sh @@ -513,4 +513,91 @@ EOF assert_equals "fail" "$(sed -n '3p' < bazel-testlogs/fail/test.log)" } +function test_undeclared_outputs_are_zipped_and_manifest_exists() { + mkdir -p dir + + cat <<'EOF' > dir/test.sh +#!/bin/sh +echo "some text" > "$TEST_UNDECLARED_OUTPUTS_DIR/text.txt" +echo "<!DOCTYPE html>" > "$TEST_UNDECLARED_OUTPUTS_DIR/fake.html" +echo "pass" +exit 0 +EOF + + chmod +x dir/test.sh + + cat <<'EOF' > dir/BUILD + sh_test( + name = "test", + srcs = [ "test.sh" ], + ) +EOF + + bazel test -s //dir:test &> $TEST_log || fail "expected success" + + # Newlines are useful around diffs. This helps us get them in bash strings. + N=$'\n' + + # Check that the undeclared outputs zip file exists. + outputs_zip=bazel-testlogs/dir/test/test.outputs/outputs.zip + [ -s $outputs_zip ] || fail "$outputs_zip was not present after test" + + # Check the contents of the zip file. + unzip -q "$outputs_zip" -d unzipped_outputs || fail "failed to unzip $outputs_zip" + cat > expected_text <<EOF +some text +EOF +diff "unzipped_outputs/text.txt" expected_text > d || fail "unzipped_outputs/text.txt differs from expected:$N$(cat d)$N" + cat > expected_html <<EOF +<!DOCTYPE html> +EOF +diff expected_html "unzipped_outputs/fake.html" > d || fail "unzipped_outputs/fake.html differs from expected:$N$(cat d)$N" + + # Check that the undeclared outputs manifest exists and that it has the + # correct contents. + outputs_manifest=bazel-testlogs/dir/test/test.outputs_manifest/MANIFEST + [ -s $outputs_manifest ] || fail "$outputs_manifest was not present after test" + cat > expected_manifest <<EOF +fake.html 16 text/html +text.txt 10 text/plain +EOF +diff expected_manifest "$outputs_manifest" > d || fail "$outputs_manifest differs from expected:$N$(cat d)$N" +} + +function test_undeclared_outputs_annotations_are_added() { + mkdir -p dir + + cat <<'EOF' > dir/test.sh +#!/bin/sh +echo "an annotation" > "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR/1.part" +echo "another annotation" > "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR/2.part" +echo "pass" +exit 0 +EOF + + chmod +x dir/test.sh + + cat <<'EOF' > dir/BUILD + sh_test( + name = "test", + srcs = [ "test.sh" ], + ) +EOF + + bazel test -s //dir:test &> $TEST_log || fail "expected success" + + # Newlines are useful around diffs. This helps us get them in bash strings. + N=$'\n' + + # Check that the undeclared outputs manifest exists and that it has the + # correct contents. + annotations=bazel-testlogs/dir/test/test.outputs_manifest/ANNOTATIONS + [ -s $annotations ] || fail "$annotations was not present after test" + cat > expected_annotations <<EOF +an annotation +another annotation +EOF +diff expected_annotations "$annotations" > d || fail "$annotations differs from expected:$N$(cat d)$N" +} + run_suite "bazel test tests" diff --git a/tools/test/test-setup.sh b/tools/test/test-setup.sh index 18796c3704..77b02083ba 100755 --- a/tools/test/test-setup.sh +++ b/tools/test/test-setup.sh @@ -223,4 +223,42 @@ for signal in $signals; do done write_xml_output_file +# Add all of the files from the undeclared outputs directory to the manifest. +if [[ -n "$TEST_UNDECLARED_OUTPUTS_DIR" && -n "$TEST_UNDECLARED_OUTPUTS_MANIFEST" ]]; then + # For each file, write a tab-separated line with name (relative to + # TEST_UNDECLARED_OUTPUTS_DIR), size, and mime type to the manifest. e.g. + # foo.txt 9 text/plain + while read -r undeclared_output; do + rel_path="${undeclared_output#$TEST_UNDECLARED_OUTPUTS_DIR/}" + # stat has different flags for different systems. -c is supported by GNU, + # and -f by BSD (and thus OSX). Try both. + file_size="$(stat -f%z "$undeclared_output" 2>/dev/null || stat -c%s "$undeclared_output" 2>/dev/null || echo "Could not stat $undeclared_output")" + file_type="$(file -L -b --mime-type "$undeclared_output")" + + printf "$rel_path\t$file_size\t$file_type\n" + done <<< "$(find -L "$TEST_UNDECLARED_OUTPUTS_DIR" -type f | sort)" \ + > "$TEST_UNDECLARED_OUTPUTS_MANIFEST" + if [[ ! -s "$TEST_UNDECLARED_OUTPUTS_MANIFEST" ]]; then + rm "$TEST_UNDECLARED_OUTPUTS_MANIFEST" + fi +fi + +# Add all of the custom manifest entries to the annotation file. +if [[ -n "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS" && \ + -n "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR" && \ + -d "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR" ]]; then + ( + shopt -s failglob + cat "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR"/*.part > "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS" + ) 2> /dev/null +fi + +# Zip up undeclared outputs. +if [[ -n "$TEST_UNDECLARED_OUTPUTS_ZIP" ]] && cd "$TEST_UNDECLARED_OUTPUTS_DIR"; then + ( + shopt -s dotglob failglob + zip -qr "$TEST_UNDECLARED_OUTPUTS_ZIP" -- * + ) 2> /dev/null +fi + exit $exitCode |