Improve error message of gcs_file & add test (#254)

* Improve error message of gcs_file & add test

Make sure we clearly print the name of the GCS file the file was
downloaded from, the expected and actual digests. Also adds a simple
test that ensures the downloaded file actually exists.

* Run buildifier
diff --git a/WORKSPACE b/WORKSPACE
index 9837304..39360b9 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -76,3 +76,14 @@
         "https://github.com/bazelbuild/bazel-toolchains/archive/44200e0c026d86c53470d107b3697a3e46469c43.tar.gz",
     ],
 )
+
+# Download test file to test gcs_file rule
+load("@bazel_toolchains//rules:gcs.bzl", "gcs_file")
+
+gcs_file(
+    name = "download_test_gcs_file",
+    bucket = "gs://bazel-toolchains-test",
+    downloaded_file_path = "test.txt",
+    file = "test.txt",
+    sha256 = "5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9",
+)
diff --git a/rules/gcs.bzl b/rules/gcs.bzl
index 2d82a7e..2d97ea2 100644
--- a/rules/gcs.bzl
+++ b/rules/gcs.bzl
@@ -56,7 +56,12 @@
 
     gsutil_cp_and_validate_result = ctx.execute(["bash", "gsutil_cp_and_validate.sh"])
     if gsutil_cp_and_validate_result.return_code == 255:
-        fail("SHA256 of downloaded file does not match given SHA256: %s" % gsutil_cp_and_validate_result.stderr)
+        fail("SHA256 of file {} from bucket {} does not match given SHA256: {} {}".format(
+            ctx.attr.file,
+            ctx.attr.bucket,
+            gsutil_cp_and_validate_result.stdout,
+            gsutil_cp_and_validate_result.stderr,
+        ))
     elif gsutil_cp_and_validate_result.return_code != 0:
         fail("gsutil cp command failed: %s" % (gsutil_cp_and_validate_result.stderr))
 
diff --git a/rules/gsutil_cp_and_validate.sh.tpl b/rules/gsutil_cp_and_validate.sh.tpl
index 16d27c8..df022e6 100644
--- a/rules/gsutil_cp_and_validate.sh.tpl
+++ b/rules/gsutil_cp_and_validate.sh.tpl
@@ -14,11 +14,13 @@
 
 #!/bin/bash
 # Template script to download GCS file and validate its digest
-set -ex
+set -e
 
+echo "Downloading %{BUCKET}/%{FILE} to %{DOWNLOAD_PATH}"
 gsutil cp %{BUCKET}/%{FILE} %{DOWNLOAD_PATH}
-
-if [ $(sha256sum %{DOWNLOAD_PATH} | head -c 64) != %{SHA256} ]; then
+digest=$(sha256sum %{DOWNLOAD_PATH} | head -c 64)
+if [ $digest != %{SHA256} ]; then
+  echo "actual digest: $digest, expected: %{SHA256}"
   exit -1
 else
   exit 0
diff --git a/tests/rules/BUILD b/tests/rules/BUILD
new file mode 100644
index 0000000..b6d0df1
--- /dev/null
+++ b/tests/rules/BUILD
@@ -0,0 +1,8 @@
+# Simply ensures the file supposed to be downloaded by the gce_file
+# test rule was actually downloaded
+sh_test(
+    name = "gcs_file_test",
+    srcs = [":file_existence_test.sh"],
+    args = ["$(location @download_test_gcs_file//file:test.txt)"],
+    data = ["@download_test_gcs_file//file:test.txt"],
+)
diff --git a/tests/rules/file_existence_test.sh b/tests/rules/file_existence_test.sh
new file mode 100755
index 0000000..5b113a7
--- /dev/null
+++ b/tests/rules/file_existence_test.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+#
+# Copyright 2017 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Script to run the executable generated from a docker_toolchain_autoconfig rule
+# and then check the toolchain configs for the c++ auto generated config exist.
+#
+# This script should be passed in 'srcs' of a sh_test test rule. The sh_test
+# rule is expected to have the name {docker_toolchain_autoconfig_name}_test,
+# where {docker_toolchain_autoconfig_name} is the docker_toolchain_autoconfig
+# rule you would like to build and run.
+
+set -e
+
+if [ -e $1 ]; then
+  echo "Verified $1 exists"
+else
+  echo "$1 did not exist" && false
+fi