Skip to content

Commit 3d85b88

Browse files
ckolli5tjgq
andauthored
Add a flag to expose undeclared test outputs in unzipped form. (bazelbuild#15431)
Closes bazelbuild#14568. Closes bazelbuild#15199. PiperOrigin-RevId: 440932893 Co-authored-by: Tiago Quelhas <[email protected]>
1 parent 374400d commit 3d85b88

File tree

5 files changed

+140
-13
lines changed

5 files changed

+140
-13
lines changed

src/main/java/com/google/devtools/build/lib/analysis/test/TestConfiguration.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ public static class TestOptions extends FragmentOptions {
298298
help = "If true, then Bazel will run coverage postprocessing for test in a new spawn.")
299299
public boolean splitCoveragePostProcessing;
300300

301+
@Option(
302+
name = "zip_undeclared_test_outputs",
303+
defaultValue = "true",
304+
documentationCategory = OptionDocumentationCategory.TESTING,
305+
effectTags = {OptionEffectTag.TEST_RUNNER},
306+
help = "If true, undeclared test outputs will be archived in a zip file.")
307+
public boolean zipUndeclaredTestOutputs;
308+
301309
@Override
302310
public FragmentOptions getHost() {
303311
TestOptions hostOptions = (TestOptions) getDefault();
@@ -373,7 +381,7 @@ public Label getCoverageSupport(){
373381
return options.coverageSupport;
374382
}
375383

376-
public Label getCoverageReportGenerator(){
384+
public Label getCoverageReportGenerator() {
377385
return options.coverageReportGenerator;
378386
}
379387

@@ -410,6 +418,10 @@ public boolean splitCoveragePostProcessing() {
410418
return options.splitCoveragePostProcessing;
411419
}
412420

421+
public boolean getZipUndeclaredTestOutputs() {
422+
return options.zipUndeclaredTestOutputs;
423+
}
424+
413425
/**
414426
* Option converter that han handle two styles of value for "--runs_per_test":
415427
*

src/main/java/com/google/devtools/build/lib/analysis/test/TestRunnerAction.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ public List<ActionInput> getSpawnOutputs() {
339339
outputs.add(ActionInputHelper.fromPath(getSplitLogsPath()));
340340
outputs.add(ActionInputHelper.fromPath(getUnusedRunfilesLogPath()));
341341
outputs.add(ActionInputHelper.fromPath(getInfrastructureFailureFile()));
342-
outputs.add(ActionInputHelper.fromPath(getUndeclaredOutputsZipPath()));
342+
if (testConfiguration.getZipUndeclaredTestOutputs()) {
343+
outputs.add(ActionInputHelper.fromPath(getUndeclaredOutputsZipPath()));
344+
} else {
345+
outputs.add(ActionInputHelper.fromPath(getUndeclaredOutputsDir()));
346+
}
343347
outputs.add(ActionInputHelper.fromPath(getUndeclaredOutputsManifestPath()));
344348
outputs.add(ActionInputHelper.fromPath(getUndeclaredOutputsAnnotationsPath()));
345349
outputs.add(ActionInputHelper.fromPath(getUndeclaredOutputsAnnotationsPbPath()));
@@ -383,12 +387,20 @@ public ImmutableList<Pair<String, Path>> getTestOutputsMapping(
383387
builder.add(
384388
Pair.of(TestFileNameConstants.TEST_WARNINGS, resolvedPaths.getTestWarningsPath()));
385389
}
386-
if (resolvedPaths.getUndeclaredOutputsZipPath().exists()) {
390+
if (testConfiguration.getZipUndeclaredTestOutputs()
391+
&& resolvedPaths.getUndeclaredOutputsZipPath().exists()) {
387392
builder.add(
388393
Pair.of(
389394
TestFileNameConstants.UNDECLARED_OUTPUTS_ZIP,
390395
resolvedPaths.getUndeclaredOutputsZipPath()));
391396
}
397+
if (!testConfiguration.getZipUndeclaredTestOutputs()
398+
&& resolvedPaths.getUndeclaredOutputsDir().exists()) {
399+
builder.add(
400+
Pair.of(
401+
TestFileNameConstants.UNDECLARED_OUTPUTS_DIR,
402+
resolvedPaths.getUndeclaredOutputsDir()));
403+
}
392404
if (resolvedPaths.getUndeclaredOutputsManifestPath().exists()) {
393405
builder.add(
394406
Pair.of(
@@ -451,6 +463,7 @@ protected void computeKey(
451463
fp.addInt(runNumber);
452464
fp.addInt(executionSettings.getTotalRuns());
453465
fp.addBoolean(configuration.isCodeCoverageEnabled());
466+
fp.addBoolean(testConfiguration.getZipUndeclaredTestOutputs());
454467
fp.addStringMap(getExecutionInfo());
455468
}
456469

@@ -615,7 +628,10 @@ public void setupEnvVariables(Map<String, String> env, Duration timeout) {
615628

616629
env.put("TEST_LOGSPLITTER_OUTPUT_FILE", getSplitLogsPath().getPathString());
617630

618-
env.put("TEST_UNDECLARED_OUTPUTS_ZIP", getUndeclaredOutputsZipPath().getPathString());
631+
if (testConfiguration.getZipUndeclaredTestOutputs()) {
632+
env.put("TEST_UNDECLARED_OUTPUTS_ZIP", getUndeclaredOutputsZipPath().getPathString());
633+
}
634+
619635
env.put("TEST_UNDECLARED_OUTPUTS_DIR", getUndeclaredOutputsDir().getPathString());
620636
env.put("TEST_UNDECLARED_OUTPUTS_MANIFEST", getUndeclaredOutputsManifestPath().getPathString());
621637
env.put(

src/main/java/com/google/devtools/build/lib/buildeventstream/TestFileNameConstants.java

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class TestFileNameConstants {
3333
"test.outputs_manifest__ANNOTATIONS.pb";
3434
public static final String UNDECLARED_OUTPUTS_MANIFEST = "test.outputs_manifest__MANIFEST";
3535
public static final String UNDECLARED_OUTPUTS_ZIP = "test.outputs__outputs.zip";
36+
public static final String UNDECLARED_OUTPUTS_DIR = "test.outputs";
3637
public static final String UNUSED_RUNFILES_LOG = "test.unused_runfiles_log";
3738
public static final String TEST_COVERAGE = "test.lcov";
3839
public static final String BASELINE_COVERAGE = "baseline.lcov";

src/test/shell/bazel/bazel_test_test.sh

+101-6
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ EOF
695695
assert_equals "fail" "$(awk "NR == $(wc -l < $TEST_log)" $TEST_log)"
696696
}
697697

698-
function test_undeclared_outputs_are_zipped_and_manifest_exists() {
698+
function setup_undeclared_outputs_test() {
699699
mkdir -p dir
700700

701701
cat <<'EOF' > dir/test.sh
@@ -714,36 +714,131 @@ sh_test(
714714
srcs = [ "test.sh" ],
715715
)
716716
EOF
717+
}
718+
719+
function test_undeclared_outputs_are_zipped() {
720+
setup_undeclared_outputs_test
721+
722+
local -r outputs_dir=bazel-testlogs/dir/test/test.outputs
723+
local -r outputs_zip=$outputs_dir/outputs.zip
724+
local -r output_text=$outputs_dir/text.txt
725+
local -r output_html=$outputs_dir/fake.html
717726

718727
bazel test -s //dir:test &> $TEST_log || fail "expected success"
719728

720729
# Newlines are useful around diffs. This helps us get them in bash strings.
721730
N=$'\n'
722731

723732
# Check that the undeclared outputs zip file exists.
724-
outputs_zip=bazel-testlogs/dir/test/test.outputs/outputs.zip
725733
[ -s $outputs_zip ] || fail "$outputs_zip was not present after test"
726734

735+
# Check that the original undeclared outputs no longer exist.
736+
[ -e $output_text ] && fail "$output_text was present after test"
737+
[ -e $output_text ] && fail "$output_text was present after test"
738+
739+
727740
# Check the contents of the zip file.
728741
unzip -q "$outputs_zip" -d unzipped_outputs || fail "failed to unzip $outputs_zip"
729742
cat > expected_text <<EOF
730743
some text
731744
EOF
732-
diff "unzipped_outputs/text.txt" expected_text > d || fail "unzipped_outputs/text.txt differs from expected:$N$(cat d)$N"
745+
diff "unzipped_outputs/text.txt" expected_text > d || fail "unzipped_outputs/text.txt differs from expected:$N$(cat d)$N"
746+
cat > expected_html <<EOF
747+
<!DOCTYPE html>
748+
EOF
749+
diff expected_html "unzipped_outputs/fake.html" > d || fail "unzipped_outputs/fake.html differs from expected:$N$(cat d)$N"
750+
}
751+
752+
function test_undeclared_outputs_are_not_zipped() {
753+
setup_undeclared_outputs_test
754+
755+
local -r outputs_dir=bazel-testlogs/dir/test/test.outputs
756+
local -r outputs_zip=$outputs_dir/outputs.zip
757+
local -r output_text=$outputs_dir/text.txt
758+
local -r output_html=$outputs_dir/fake.html
759+
760+
bazel test -s --nozip_undeclared_test_outputs //dir:test &> $TEST_log || fail "expected success"
761+
762+
# Newlines are useful around diffs. This helps us get them in bash strings.
763+
N=$'\n'
764+
765+
# Check that the undeclared outputs zip file does not exist.
766+
[ -e $outputs_zip ] && fail "$outputs_zip was present after test"
767+
768+
# Check that the undeclared outputs exist.
769+
[ -e $output_text ] || fail "$output_text was not present after test"
770+
[ -e $output_text ] || fail "$output_text was not present after test"
771+
772+
# Check the contents of the undeclared outputs.
773+
cat > expected_text <<EOF
774+
some text
775+
EOF
776+
diff "$outputs_dir/text.txt" expected_text > d || fail "$outputs_dir/text.txt differs from expected:$N$(cat d)$N"
733777
cat > expected_html <<EOF
734778
<!DOCTYPE html>
735779
EOF
736-
diff expected_html "unzipped_outputs/fake.html" > d || fail "unzipped_outputs/fake.html differs from expected:$N$(cat d)$N"
780+
diff expected_html "$outputs_dir/fake.html" > d || fail "$outputs_dir/fake.html differs from expected:$N$(cat d)$N"
781+
}
782+
783+
function test_undeclared_outputs_zipped_then_unzipped() {
784+
setup_undeclared_outputs_test
785+
786+
local -r outputs_dir=bazel-testlogs/dir/test/test.outputs
787+
local -r outputs_zip=$outputs_dir/outputs.zip
788+
local -r output_text=$outputs_dir/text.txt
789+
local -r output_html=$outputs_dir/fake.html
790+
791+
bazel test -s //dir:test &> $TEST_log || fail "expected success"
792+
793+
[ -s $output_text ] && fail "$output_text was present after test"
794+
[ -s $output_html ] && fail "$output_html was present after test"
795+
[ -s $outputs_zip ] || fail "$outputs_zip was not present after test"
796+
797+
bazel test -s --nozip_undeclared_test_outputs //dir:test &> $TEST_log || fail "expected success"
798+
799+
[ -s $outputs_zip ] && fail "$outputs_zip was present after test"
800+
[ -s $output_text ] || fail "$output_text was not present after test"
801+
[ -s $output_html ] || fail "$output_html was not present after test"
802+
}
803+
804+
function test_undeclared_outputs_unzipped_then_zipped() {
805+
setup_undeclared_outputs_test
806+
807+
local -r outputs_dir=bazel-testlogs/dir/test/test.outputs
808+
local -r outputs_zip=$outputs_dir/outputs.zip
809+
local -r output_text=$outputs_dir/text.txt
810+
local -r output_html=$outputs_dir/fake.html
811+
812+
bazel test -s --nozip_undeclared_test_outputs //dir:test &> $TEST_log || fail "expected success"
813+
814+
[ -s $outputs_zip ] && fail "$outputs_zip was present after test"
815+
[ -s $output_text ] || fail "$output_text was not present after test"
816+
[ -s $output_html ] || fail "$output_html was not present after test"
817+
818+
bazel test -s //dir:test &> $TEST_log || fail "expected success"
819+
820+
[ -s $output_text ] && fail "$output_text was present after test"
821+
[ -s $output_html ] && fail "$output_html was present after test"
822+
[ -s $outputs_zip ] || fail "$outputs_zip was not present after test"
823+
}
824+
825+
function test_undeclared_outputs_manifest_is_created() {
826+
setup_undeclared_outputs_test
827+
828+
bazel test -s //dir:test &> $TEST_log || fail "expected success"
829+
830+
# Newlines are useful around diffs. This helps us get them in bash strings.
831+
N=$'\n'
737832

738833
# Check that the undeclared outputs manifest exists and that it has the
739834
# correct contents.
740-
outputs_manifest=bazel-testlogs/dir/test/test.outputs_manifest/MANIFEST
835+
local -r outputs_manifest=bazel-testlogs/dir/test/test.outputs_manifest/MANIFEST
741836
[ -s $outputs_manifest ] || fail "$outputs_manifest was not present after test"
742837
cat > expected_manifest <<EOF
743838
fake.html 16 text/html
744839
text.txt 10 text/plain
745840
EOF
746-
diff expected_manifest "$outputs_manifest" > d || fail "$outputs_manifest differs from expected:$N$(cat d)$N"
841+
diff expected_manifest "$outputs_manifest" > d || fail "$outputs_manifest differs from expected:$N$(cat d)$N"
747842
}
748843

749844
function test_undeclared_outputs_annotations_are_added() {

tools/test/test-setup.sh

+6-3
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ is_absolute "$TEST_UNDECLARED_OUTPUTS_DIR" ||
5454
TEST_UNDECLARED_OUTPUTS_DIR="$PWD/$TEST_UNDECLARED_OUTPUTS_DIR"
5555
is_absolute "$TEST_UNDECLARED_OUTPUTS_MANIFEST" ||
5656
TEST_UNDECLARED_OUTPUTS_MANIFEST="$PWD/$TEST_UNDECLARED_OUTPUTS_MANIFEST"
57-
is_absolute "$TEST_UNDECLARED_OUTPUTS_ZIP" ||
58-
TEST_UNDECLARED_OUTPUTS_ZIP="$PWD/$TEST_UNDECLARED_OUTPUTS_ZIP"
57+
if [[ -n "$TEST_UNDECLARED_OUTPUTS_ZIP" ]]; then
58+
is_absolute "$TEST_UNDECLARED_OUTPUTS_ZIP" ||
59+
TEST_UNDECLARED_OUTPUTS_ZIP="$PWD/$TEST_UNDECLARED_OUTPUTS_ZIP"
60+
fi
5961
is_absolute "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS" ||
6062
TEST_UNDECLARED_OUTPUTS_ANNOTATIONS="$PWD/$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS"
6163
is_absolute "$TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR" ||
@@ -420,7 +422,8 @@ if [[ -n "$TEST_UNDECLARED_OUTPUTS_ZIP" ]] && cd "$TEST_UNDECLARED_OUTPUTS_DIR";
420422
# If * found nothing, echo printed the literal *.
421423
# Otherwise echo printed the top-level files and directories.
422424
# Pass files to zip with *, so paths with spaces aren't broken up.
423-
zip -qr "$TEST_UNDECLARED_OUTPUTS_ZIP" -- * 2>/dev/null || \
425+
# Remove original files after zipping them.
426+
zip -qrm "$TEST_UNDECLARED_OUTPUTS_ZIP" -- * 2>/dev/null || \
424427
echo >&2 "Could not create \"$TEST_UNDECLARED_OUTPUTS_ZIP\": zip not found or failed"
425428
fi
426429
fi

0 commit comments

Comments
 (0)