mod common;
use common::sqry_bin;
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
fn indexed_mixed_platform_workspace() -> TempDir {
let temp = TempDir::new().expect("tempdir");
fs::write(temp.path().join("go.mod"), "module cfgtest\n\ngo 1.21\n").expect("write go.mod");
fs::write(
temp.path().join("linux_only.go"),
"//go:build linux\n\npackage cfgtest\n\nfunc LinuxOnly() {}\n",
)
.expect("write linux_only.go");
fs::write(
temp.path().join("compound.go"),
"//go:build linux && amd64\n\npackage cfgtest\n\nfunc LinuxAmd64() {}\n",
)
.expect("write compound.go");
fs::write(
temp.path().join("darwin.go"),
"//go:build darwin\n\npackage cfgtest\n\nfunc Darwin() {}\n",
)
.expect("write darwin.go");
fs::write(
temp.path().join("plain.go"),
"package cfgtest\n\nfunc Plain() {}\n",
)
.expect("write plain.go");
Command::new(sqry_bin())
.arg("index")
.arg("--force")
.arg(temp.path())
.env("NO_COLOR", "1")
.assert()
.success();
temp
}
#[test]
fn cli_query_cfg_bare_returns_linux_symbols() {
let temp = indexed_mixed_platform_workspace();
let assert = Command::new(sqry_bin())
.arg("plan-query")
.arg("kind:function cfg:linux")
.arg(temp.path())
.env("NO_COLOR", "1")
.assert()
.success();
let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
assert!(
stdout.contains("LinuxOnly"),
"bare cfg:linux must surface the single-flag Linux function; stdout={stdout:?}",
);
assert!(
stdout.contains("LinuxAmd64"),
"bare cfg:linux must surface compound `linux && amd64`; stdout={stdout:?}",
);
assert!(
!stdout.contains("Darwin"),
"bare cfg:linux must NOT surface Darwin-only function; stdout={stdout:?}",
);
assert!(
!stdout.contains("Plain"),
"bare cfg:linux must NOT surface unconstrained function; stdout={stdout:?}",
);
}
#[test]
fn cli_query_cfg_quoted_literal_match_is_exact() {
let temp = indexed_mixed_platform_workspace();
let assert = Command::new(sqry_bin())
.arg("plan-query")
.arg("kind:function cfg:\"linux && amd64\"")
.arg(temp.path())
.env("NO_COLOR", "1")
.assert()
.success();
let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
assert!(
stdout.contains("LinuxAmd64"),
"quoted byte-exact `linux && amd64` must surface LinuxAmd64; stdout={stdout:?}",
);
assert!(
!stdout.contains("Darwin"),
"quoted byte-exact must NOT surface Darwin (no overlap); stdout={stdout:?}",
);
}
#[test]
fn cli_query_cfg_invalid_predicate_exits_non_zero() {
let temp = indexed_mixed_platform_workspace();
Command::new(sqry_bin())
.arg("plan-query")
.arg("kind:function cfg:\"unterminated")
.arg(temp.path())
.env("NO_COLOR", "1")
.assert()
.failure()
.stderr(predicate::str::contains("parse").or(predicate::str::contains("Parse")));
}
#[test]
fn cli_query_cfg_unknown_flag_zero_results() {
let temp = indexed_mixed_platform_workspace();
let assert = Command::new(sqry_bin())
.arg("plan-query")
.arg("kind:function cfg:linnnux")
.arg(temp.path())
.env("NO_COLOR", "1")
.assert()
.success();
let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
assert!(
stdout.trim().is_empty(),
"unknown cfg flag must yield zero results (predicate-miss); stdout={stdout:?}",
);
}
#[test]
fn cli_query_cfg_json_returns_well_formed_array() {
let temp = indexed_mixed_platform_workspace();
let assert = Command::new(sqry_bin())
.arg("--json")
.arg("plan-query")
.arg("kind:function cfg:linux")
.arg(temp.path())
.env("NO_COLOR", "1")
.assert()
.success();
let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
let parsed: serde_json::Value =
serde_json::from_str(stdout.trim()).expect("plan-query --json must emit valid JSON");
let arr = parsed
.as_array()
.expect("plan-query --json must emit a JSON array");
assert!(
!arr.is_empty(),
"cfg:linux must surface LinuxOnly (single-flag match); stdout={stdout:?}",
);
for hit in arr {
for field in ["name", "qualified_name", "kind", "file", "line"] {
assert!(
hit.get(field).is_some(),
"each PlanQueryHit must carry `{field}`; hit={hit:?}",
);
}
}
}