Add a test for prefix suggestions

Pull request #7333 added reporting of the available prefixes
of an archive, if the requested prefix was not found. Add a
test to ensure this behavior remains functioning in the future.

Change-Id: Ia2ed23caeb49f5c0b176b7c6ec987c4ab9b27826
PiperOrigin-RevId: 234114064
diff --git a/src/test/shell/bazel/external_integration_test.sh b/src/test/shell/bazel/external_integration_test.sh
index bb2b222..9dc16ee 100755
--- a/src/test/shell/bazel/external_integration_test.sh
+++ b/src/test/shell/bazel/external_integration_test.sh
@@ -1741,4 +1741,74 @@
   expect_log 'SHA256 (.*/ext.tar) = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
 }
 
+function test_prefix_suggestions() {
+  # Verify that useful suggestions are made if an expected prefix
+  # is not found in an archive.
+  WRKDIR=$(mktemp -d "${TEST_TMPDIR}/testXXXXXX")
+  cd "${WRKDIR}"
+
+  mkdir -p ext-1.1/foo
+  touch ext-1.1/foo.txt
+
+  tar cvf ext.tar ext-1.1
+  rm -rf ext-1
+
+  mkdir main
+  cd main
+  cat > WORKSPACE <<EOF
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+http_archive(
+  name="ext",
+  strip_prefix="ext-1.0",
+  urls=["file://${WRKDIR}/ext.tar"],
+)
+EOF
+  cat > BUILD <<'EOF'
+genrule(
+  name = "it",
+  srcs = ["@ext//:foo.txt"],
+  outs = ["it.txt"],
+  cmd = "cp $< $@",
+)
+EOF
+  bazel build //:it > "${TEST_log}" 2>&1 && fail "expected failure" || :
+  expect_log "ext-1.0.*not found"
+  expect_log "prefixes.*ext-1.1"
+}
+
+function test_suggest_nostripprefix() {
+  # Verify that a suggestion is made about dropping an unnecessary
+  # `strip_prefix` argument.
+  WRKDIR=$(mktemp -d "${TEST_TMPDIR}/testXXXXXX")
+  cd "${WRKDIR}"
+
+  mkdir ext
+  touch ext/foo.txt
+  (cd ext && tar cvf "${WRKDIR}/ext.tar" foo.txt)
+
+  mkdir main
+  cd main
+  cat > WORKSPACE <<EOF
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+http_archive(
+  name="ext",
+  strip_prefix="ext-1.0",
+  urls=["file://${WRKDIR}/ext.tar"],
+)
+EOF
+  cat > BUILD <<'EOF'
+genrule(
+  name = "it",
+  srcs = ["@ext//:foo.txt"],
+  outs = ["it.txt"],
+  cmd = "cp $< $@",
+)
+EOF
+  bazel build //:it > "${TEST_log}" 2>&1 && fail "expected failure" || :
+  expect_log "ext-1.0.*not found"
+  expect_log "not.*any directory"
+  expect_log 'no need for `strip_prefix`'
+}
+
+
 run_suite "external tests"