Add tests for cquery's `config` function.

Also clean up some existing tests.

PiperOrigin-RevId: 420038680
diff --git a/src/test/shell/integration/BUILD b/src/test/shell/integration/BUILD
index 777144a..52d1c46 100644
--- a/src/test/shell/integration/BUILD
+++ b/src/test/shell/integration/BUILD
@@ -285,6 +285,7 @@
         ":test-deps",
         "@bazel_tools//tools/bash/runfiles",
     ],
+    shard_count = 5,
 )
 
 sh_test(
diff --git a/src/test/shell/integration/configured_query_test.sh b/src/test/shell/integration/configured_query_test.sh
index fa66cc9..0f5e02f 100755
--- a/src/test/shell/integration/configured_query_test.sh
+++ b/src/test/shell/integration/configured_query_test.sh
@@ -70,10 +70,10 @@
 sh_library(name='japanese')
 EOF
 
- bazel cquery "deps(//$pkg:maple)" > output 2>"$TEST_log" || fail "Expected success"
+  bazel cquery "deps(//$pkg:maple)" > output 2>"$TEST_log" || fail "Expected success"
 
- assert_contains "//$pkg:maple" output
- assert_contains "//$pkg:japanese" output
+  assert_contains "//$pkg:maple" output
+  assert_contains "//$pkg:japanese" output
 }
 
 function test_basic_query_output_textproto() {
@@ -84,10 +84,10 @@
 sh_library(name='japanese')
 EOF
 
- bazel cquery --output=textproto "deps(//$pkg:maple)" > output 2>"$TEST_log" || fail "Expected success"
+  bazel cquery --output=textproto "deps(//$pkg:maple)" > output 2>"$TEST_log" || fail "Expected success"
 
- assert_contains "name: \"//$pkg:maple\"" output
- assert_contains "name: \"//$pkg:japanese\"" output
+  assert_contains "name: \"//$pkg:maple\"" output
+  assert_contains "name: \"//$pkg:japanese\"" output
 }
 
 function test_basic_query_output_labelkind() {
@@ -98,12 +98,11 @@
 cc_binary(name='japanese', srcs = ['japanese.cc'])
 EOF
 
- bazel cquery --output=label_kind "deps(//$pkg:maple)" > output 2>"$TEST_log" ||
- --noimplicit_deps --nohost_deps fail "Expected success"
+  bazel cquery --output=label_kind "deps(//$pkg:maple)" > output 2>"$TEST_log" || fail "Expected success"
 
- assert_contains "sh_library rule //$pkg:maple" output
- assert_contains "cc_binary rule //$pkg:japanese" output
- assert_contains "source file //$pkg:japanese.cc" output
+  assert_contains "sh_library rule //$pkg:maple" output
+  assert_contains "cc_binary rule //$pkg:japanese" output
+  assert_contains "source file //$pkg:japanese.cc" output
 }
 
 function test_config_checksum_determinism() {
@@ -1305,4 +1304,81 @@
   assert_not_equals "${output_before}" "${output_after}"
 }
 
+function set_up_config_test() {
+  mkdir -p $pkg
+
+  # Use a rule that has a configuration transition.
+  cat > $pkg/rule.bzl <<EOF
+def _impl(ctx):
+    pass
+
+demo_rule = rule(
+    implementation = _impl,
+    attrs = {
+        "binary": attr.label(cfg = "exec"),
+    },
+)
+EOF
+
+  cat > $pkg/tool.sh <<EOF
+echo "Hello"
+exit 0
+EOF
+  chmod +x $pkg/tool.sh
+
+  cat > $pkg/BUILD <<EOF
+load("//$pkg:rule.bzl", "demo_rule")
+
+sh_binary(name = "tool", srcs = ["tool.sh"])
+
+demo_rule(
+    name = 'demo',
+    binary = ":tool",
+)
+EOF
+
+  # Find out what the specific configurations are.
+  bazel cquery "deps(//$pkg:demo)" &>"$TEST_log" || fail "Unexpected failure"
+
+  # Find the target config.
+  target_config="$(grep "^//$pkg:demo ([0-9a-f]\+)" $TEST_log | sed -e 's,.*(\([^)]*\)).*,\1,')"
+  tool_config="$(grep "^//$pkg:tool ([0-9a-f]\+)" $TEST_log | sed -e 's,.*(\([^)]*\)).*,\1,')"
+
+  assert_not_equals "$target_config" "$tool_config"
+}
+
+function test_config_function() {
+  local -r pkg=$FUNCNAME
+
+  set_up_config_test $pkg
+
+  # Actually call config, verify output
+  bazel cquery "config(//$pkg:demo, $target_config)" &>"$TEST_log" || fail "Unexpected failure"
+  expect_log "^//$pkg:demo ($target_config)"
+  bazel cquery "config(//$pkg:tool, $tool_config)" &>"$TEST_log" || fail "Unexpected failure"
+  expect_log "^//$pkg:tool ($tool_config)"
+}
+
+function test_config_function_wrong_order() {
+  local -r pkg=$FUNCNAME
+
+  set_up_config_test $pkg
+
+  # Actually call config, verify output
+  bazel cquery "config($target_config, //$pkg:demo)" &>"$TEST_log" && fail "Expected to fail"
+  expect_not_log "^//$pkg:demo ($target_config)"
+  expect_log "couldn't determine target from filename '$target_config'"
+}
+
+function test_config_function_invalid_config() {
+  local -r pkg=$FUNCNAME
+
+  set_up_config_test $pkg
+
+  # Actually call config, verify output
+  bazel cquery "config(//$pkg:demo, notaconfighash)" &>"$TEST_log" && fail "Expected to fail"
+  expect_not_log "^//$pkg:demo ($target_config)"
+  expect_log "Unknown configuration ID 'notaconfighash'"
+}
+
 run_suite "${PRODUCT_NAME} configured query tests"