Tests: print a warning if zip failed

If test-setup.sh fails to create the undeclared
outputs zip, then print a warning but carry on.
Failing to create this zip is a nuisance but not a
fatal error.

The warning is printed to the test log. If the
test fails, this is printed to the console too. If
the test passes, then the warning remains only in
the test log (Bazel prints no output for passing
tests).

Fixes https://github.com/bazelbuild/bazel/issues/8336

Change-Id: Iee8121d76e96445252d97142cef68c50afbb25b1

Closes #8720.

Change-Id: Iee8121d76e96445252d97142cef68c50afbb25b1
PiperOrigin-RevId: 255401321
diff --git a/src/test/py/bazel/test_wrapper_test.py b/src/test/py/bazel/test_wrapper_test.py
index 6c122c2..77fdb01 100644
--- a/src/test/py/bazel/test_wrapper_test.py
+++ b/src/test/py/bazel/test_wrapper_test.py
@@ -164,7 +164,7 @@
             'shutil.copyfile(r.Rlocation("__main__/foo/dummy.ico"),',
             '                os.path.join(root, "out1", "data1.ico"))',
             'shutil.copyfile(r.Rlocation("__main__/foo/dummy.dat"),',
-            '                os.path.join(root, "out2", "data2.dat"))',
+            '                os.path.join(root, "out2", "my data 2.dat"))',
         ],
         executable=True)
 
@@ -413,7 +413,7 @@
             'empty/': 0,
             'empty/sub/': 0,
             'out1/data1.ico': 70,
-            'out2/data2.dat': 16
+            'out2/my data 2.dat': 16
         })
 
     undecl_mf = os.path.join(bazel_testlogs, 'foo', 'undecl_test',
@@ -439,7 +439,7 @@
             'image/icon', 'text/ico', 'application/ico'
         ]):
       self._FailWithOutput(mf_content)
-    if mf_content[1] != 'out2/data2.dat\t16\tapplication/octet-stream':
+    if mf_content[1] != 'out2/my data 2.dat\t16\tapplication/octet-stream':
       self._FailWithOutput(mf_content)
 
   def _AssertUndeclaredOutputsAnnotations(self, flags):
diff --git a/tools/test/test-setup.sh b/tools/test/test-setup.sh
index 92e9a38..522f7ba 100755
--- a/tools/test/test-setup.sh
+++ b/tools/test/test-setup.sh
@@ -354,10 +354,14 @@
 
 # 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
+  shopt -s dotglob
+  if [[ "$(echo *)" != "*" ]]; then
+    # If * found nothing, echo printed the literal *.
+    # Otherwise echo printed the top-level files and directories.
+    # Pass files to zip with *, so paths with spaces aren't broken up.
+    zip -qr "$TEST_UNDECLARED_OUTPUTS_ZIP" -- * 2>/dev/null || \
+        echo >&2 "Could not create \"$TEST_UNDECLARED_OUTPUTS_ZIP\": zip not found or failed"
+  fi
 fi
 
 exit $exitCode