Skip to content

Commit cc7b4a8

Browse files
committed
fix(extract): converge Function/Method is_test with the tests/ path filter
Function/Method nodes only got is_test=true via a Rust #[test] attribute check; the per-file test-file flag (cbm_is_test_file) was set on the Module node but never propagated to the definitions inside it, so e.g. a C function in tests/helpers/fixtures.c stayed is_test=false — invisible to store.c's is_test!=1 filters — even though trace_path's own independent path check already treated the file as a test. Propagate is_test_file into extract_func_def and push_method_def, and teach cbm_is_test_file the same tests/ (and test/, spec/, __tests__/) directory convention cbm_is_test_path already uses, so a non-test_-named file anywhere under tests/ is caught too. Separately, trace_call_path's own include_tests filter (mcp.c is_test_file) matched a nested ".../tests/..." path but not a project-root-relative one, so tests/repro/foo.c leaked into results with the default include_tests=false; add the missing tests/ (and test/, spec/, __tests__/) prefix check. Closes DeusData#1294. Signed-off-by: Yyunozor <yyunozor@icloud.com>
1 parent d698db8 commit cc7b4a8

5 files changed

Lines changed: 215 additions & 2 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3461,6 +3461,14 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
34613461
def.is_test = true;
34623462
}
34633463

3464+
// A function/method defined in a file cbm treats as a test file is itself
3465+
// a test, regardless of its own name (tests/helpers/fixtures.c has none
3466+
// of the test_/_test naming conventions, but every function in it is
3467+
// still test code) (#1294). OR'd so the Rust attribute check above, which
3468+
// additionally catches #[test] functions embedded in an otherwise regular
3469+
// .rs file, is never downgraded by this.
3470+
def.is_test = def.is_test || ctx->result->is_test_file;
3471+
34643472
// Docstring
34653473
def.docstring = extract_docstring(a, node, ctx->source, ctx->language);
34663474

@@ -4425,6 +4433,10 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,
44254433
// MinHash fingerprint
44264434
compute_fingerprint(ctx, &def, child);
44274435

4436+
// A method on a class defined in a test file (e.g. a JUnit/pytest
4437+
// TestFoo.test_bar) is itself a test, same as free functions (#1294).
4438+
def.is_test = def.is_test || ctx->result->is_test_file;
4439+
44284440
cbm_defs_push(&ctx->result->defs, a, def);
44294441
}
44304442

‎internal/cbm/helpers.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,19 @@ bool cbm_is_test_file(const char *rel_path, CBMLanguage lang) {
295295
}
296296
const char *base = path_basename(rel_path);
297297

298+
/* Directory-based, language-agnostic: a file under a conventional test
299+
* directory is a test file regardless of its own basename (e.g.
300+
* tests/helpers/fixtures.c, which no per-language suffix/prefix rule
301+
* below would otherwise catch). Mirrors the directory set cbm_is_test_path
302+
* (src/pipeline/pass_tests.c) already uses for TESTS-edge detection, so
303+
* the extraction-time is_test_file flag agrees with it (#1294). */
304+
if (strstr(rel_path, "__tests__/") || strstr(rel_path, "/tests/") ||
305+
strstr(rel_path, "/test/") || strstr(rel_path, "/spec/") ||
306+
has_prefix(rel_path, "tests/") || has_prefix(rel_path, "test/") ||
307+
has_prefix(rel_path, "spec/") || has_prefix(rel_path, "__tests__/")) {
308+
return true;
309+
}
310+
298311
switch (lang) {
299312
case CBM_LANG_GO:
300313
return has_suffix(base, "_test.go");

‎src/mcp/mcp.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5628,14 +5628,23 @@ static yyjson_doc *resolve_trace_edge_types(const char *args, const char *mode,
56285628
return NULL;
56295629
}
56305630

5631-
/* Check if a file path looks like a test file. */
5631+
/* Check if a file path looks like a test file. The substring checks below
5632+
* only catch a tests/ directory nested under another path component
5633+
* (".../tests/foo"); a project-root-relative path like "tests/repro/foo.c"
5634+
* has no leading slash before "tests" and fell through undetected, leaking
5635+
* whole test subtrees into query_graph/trace_path results with the default
5636+
* include_tests=false (#1294). */
56325637
static bool is_test_file(const char *path) {
56335638
if (!path) {
56345639
return false;
56355640
}
56365641
return strstr(path, "/test") != NULL || strstr(path, "test_") != NULL ||
56375642
strstr(path, "_test.") != NULL || strstr(path, "/tests/") != NULL ||
5638-
strstr(path, "/spec/") != NULL || strstr(path, ".test.") != NULL;
5643+
strstr(path, "/spec/") != NULL || strstr(path, ".test.") != NULL ||
5644+
strncmp(path, "tests/", SLEN("tests/")) == 0 ||
5645+
strncmp(path, "test/", SLEN("test/")) == 0 ||
5646+
strncmp(path, "spec/", SLEN("spec/")) == 0 ||
5647+
strncmp(path, "__tests__/", SLEN("__tests__/")) == 0;
56395648
}
56405649

56415650
/* Convert BFS traversal results into a yyjson_mut array. */

‎tests/test_extraction.c‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,111 @@ TEST(extract_rust_test_attr_marks_is_test_issue855) {
40934093
PASS();
40944094
}
40954095

4096+
/* Function.is_test must follow the file's test-directory membership, not just
4097+
* a test_/_test basename convention: a helper under tests/ with none of those
4098+
* naming conventions (tests/helpers/fixtures.c) was previously indexed as an
4099+
* ordinary, non-test Function — invisible to store.c's `is_test != 1`
4100+
* dead-code/search filters even though trace_path's own independent path
4101+
* check already treated the same file as a test (#1294). */
4102+
TEST(extract_c_test_dir_marks_is_test_issue1294) {
4103+
const char *src = "void helper(void) {}\n";
4104+
4105+
/* Regression: a test_*-named file directly under tests/ must remain a
4106+
* test (this already worked before the fix). */
4107+
CBMFileResult *r1 = extract(src, CBM_LANG_C, "t", "tests/test_pipeline.c");
4108+
ASSERT_NOT_NULL(r1);
4109+
ASSERT_FALSE(r1->has_error);
4110+
ASSERT(has_def(r1, "Function", "helper"));
4111+
int reg = -1;
4112+
for (int i = 0; i < r1->defs.count; i++) {
4113+
if (strcmp(r1->defs.items[i].label, "Function") == 0 && r1->defs.items[i].name &&
4114+
strcmp(r1->defs.items[i].name, "helper") == 0) {
4115+
reg = r1->defs.items[i].is_test ? 1 : 0;
4116+
}
4117+
}
4118+
ASSERT(reg == 1 && "test_*.c directly under tests/ is a test (regression)");
4119+
cbm_free_result(r1);
4120+
4121+
/* Positive: a file under tests/ that matches none of the test_/_test
4122+
* naming conventions must now ALSO be a test. */
4123+
CBMFileResult *r2 = extract(src, CBM_LANG_C, "t", "tests/helpers/fixtures.c");
4124+
ASSERT_NOT_NULL(r2);
4125+
ASSERT_FALSE(r2->has_error);
4126+
ASSERT(has_def(r2, "Function", "helper"));
4127+
int nonconv = -1;
4128+
for (int i = 0; i < r2->defs.count; i++) {
4129+
if (strcmp(r2->defs.items[i].label, "Function") == 0 && r2->defs.items[i].name &&
4130+
strcmp(r2->defs.items[i].name, "helper") == 0) {
4131+
nonconv = r2->defs.items[i].is_test ? 1 : 0;
4132+
}
4133+
}
4134+
ASSERT(nonconv == 1 && "non-test_-named file under tests/ is now a test");
4135+
cbm_free_result(r2);
4136+
4137+
/* Negative: a file with no test/ directory or test_/_test naming in its
4138+
* path must never be flagged — this fix must not widen detection beyond
4139+
* the tests/ (and sibling) directory tree. */
4140+
CBMFileResult *r3 = extract(src, CBM_LANG_C, "t", "src/pipeline/helper.c");
4141+
ASSERT_NOT_NULL(r3);
4142+
ASSERT_FALSE(r3->has_error);
4143+
ASSERT(has_def(r3, "Function", "helper"));
4144+
int outside = -1;
4145+
for (int i = 0; i < r3->defs.count; i++) {
4146+
if (strcmp(r3->defs.items[i].label, "Function") == 0 && r3->defs.items[i].name &&
4147+
strcmp(r3->defs.items[i].name, "helper") == 0) {
4148+
outside = r3->defs.items[i].is_test ? 1 : 0;
4149+
}
4150+
}
4151+
ASSERT(outside == 0 && "file outside tests/ is never a test");
4152+
cbm_free_result(r3);
4153+
4154+
PASS();
4155+
}
4156+
4157+
/* Same convergence for Method definitions (push_method_def), which take a
4158+
* separate code path from free functions: a class method on a class defined
4159+
* under tests/ with no test_/_test naming (e.g. tests/helpers/base.py) must
4160+
* be marked is_test too, and a method on the same class outside tests/ must
4161+
* not be (#1294). */
4162+
TEST(extract_python_method_test_dir_marks_is_test_issue1294) {
4163+
const char *src = "class Foo:\n"
4164+
" def helper(self):\n"
4165+
" pass\n";
4166+
4167+
/* Python's LSP layer injects synthetic builtin stub Methods (str.upper,
4168+
* dict.get, ...) into defs.items alongside real ones (py_builtins.c), so
4169+
* matching must key on name, not just label="Method". */
4170+
CBMFileResult *r1 = extract(src, CBM_LANG_PYTHON, "t", "tests/helpers/base.py");
4171+
ASSERT_NOT_NULL(r1);
4172+
ASSERT_FALSE(r1->has_error);
4173+
ASSERT(has_def(r1, "Method", "helper"));
4174+
int in_tests = -1;
4175+
for (int i = 0; i < r1->defs.count; i++) {
4176+
if (strcmp(r1->defs.items[i].label, "Method") == 0 && r1->defs.items[i].name &&
4177+
strcmp(r1->defs.items[i].name, "helper") == 0) {
4178+
in_tests = r1->defs.items[i].is_test ? 1 : 0;
4179+
}
4180+
}
4181+
ASSERT(in_tests == 1 && "method on a class under tests/helpers/ is a test");
4182+
cbm_free_result(r1);
4183+
4184+
CBMFileResult *r2 = extract(src, CBM_LANG_PYTHON, "t", "app/models/base.py");
4185+
ASSERT_NOT_NULL(r2);
4186+
ASSERT_FALSE(r2->has_error);
4187+
ASSERT(has_def(r2, "Method", "helper"));
4188+
int outside_tests = -1;
4189+
for (int i = 0; i < r2->defs.count; i++) {
4190+
if (strcmp(r2->defs.items[i].label, "Method") == 0 && r2->defs.items[i].name &&
4191+
strcmp(r2->defs.items[i].name, "helper") == 0) {
4192+
outside_tests = r2->defs.items[i].is_test ? 1 : 0;
4193+
}
4194+
}
4195+
ASSERT(outside_tests == 0 && "method on a class outside tests/ is never a test");
4196+
cbm_free_result(r2);
4197+
4198+
PASS();
4199+
}
4200+
40964201
/* #1017: docstring truncation at MAX_COMMENT_LEN (500 bytes) can split a
40974202
* multi-byte UTF-8 character, leaving an incomplete byte sequence.
40984203
* Craft a Go comment whose 498th-500th bytes are a 3-byte CJK character
@@ -5340,6 +5445,8 @@ SUITE(extraction) {
53405445
RUN_TEST(extract_c_clean_file_no_recovery_duplicates_issue961);
53415446
RUN_TEST(walk_defs_no_truncation_over_4096_issue668);
53425447
RUN_TEST(extract_rust_test_attr_marks_is_test_issue855);
5448+
RUN_TEST(extract_c_test_dir_marks_is_test_issue1294);
5449+
RUN_TEST(extract_python_method_test_dir_marks_is_test_issue1294);
53435450
RUN_TEST(docstring_utf8_truncation_boundary_issue1017);
53445451
RUN_TEST(extract_ts_decorators_survive_interleaved_comment);
53455452

‎tests/test_mcp.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,77 @@ TEST(tool_trace_totals_respect_test_filter) {
16791679
PASS();
16801680
}
16811681

1682+
/* Same as above, but the test-side caller lives directly under a
1683+
* project-root tests/ directory with no test_/_test naming convention
1684+
* (tests/repro/helper.c) rather than tests/test_x.c. is_test_file() in
1685+
* mcp.c matched a NESTED ".../tests/..." path but not a project-root-
1686+
* relative one, so this row leaked into results with the default
1687+
* include_tests=false (#1294, secondary bug). */
1688+
TEST(tool_trace_totals_respect_test_filter_tests_root_subtree_issue1294) {
1689+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
1690+
ASSERT_NOT_NULL(srv);
1691+
cbm_store_t *st = cbm_mcp_server_store(srv);
1692+
const char *proj = "totproj2";
1693+
cbm_mcp_server_set_project(srv, proj);
1694+
cbm_store_upsert_project(st, proj, "/tmp/tot2");
1695+
1696+
cbm_node_t tgt = {.project = proj,
1697+
.label = "Function",
1698+
.name = "tgt2",
1699+
.qualified_name = "totproj2.a.tgt2",
1700+
.file_path = "a.c",
1701+
.start_line = 1,
1702+
.end_line = 5};
1703+
int64_t tid = cbm_store_upsert_node(st, &tgt);
1704+
ASSERT_GT(tid, 0);
1705+
cbm_node_t prod = {.project = proj,
1706+
.label = "Function",
1707+
.name = "prod_caller2",
1708+
.qualified_name = "totproj2.a.prod_caller2",
1709+
.file_path = "a.c",
1710+
.start_line = 10,
1711+
.end_line = 15};
1712+
int64_t pid = cbm_store_upsert_node(st, &prod);
1713+
ASSERT_GT(pid, 0);
1714+
cbm_node_t tst = {.project = proj,
1715+
.label = "Function",
1716+
.name = "repro_caller",
1717+
.qualified_name = "totproj2.t.repro_caller",
1718+
.file_path = "tests/repro/helper.c",
1719+
.start_line = 1,
1720+
.end_line = 5};
1721+
int64_t xid = cbm_store_upsert_node(st, &tst);
1722+
ASSERT_GT(xid, 0);
1723+
cbm_edge_t e1 = {.project = proj, .source_id = pid, .target_id = tid, .type = "CALLS"};
1724+
ASSERT_GT(cbm_store_insert_edge(st, &e1), 0);
1725+
cbm_edge_t e2 = {.project = proj, .source_id = xid, .target_id = tid, .type = "CALLS"};
1726+
ASSERT_GT(cbm_store_insert_edge(st, &e2), 0);
1727+
1728+
char *resp = cbm_mcp_server_handle(
1729+
srv, "{\"jsonrpc\":\"2.0\",\"id\":92,\"method\":\"tools/call\",\"params\":{"
1730+
"\"name\":\"trace_call_path\",\"arguments\":{\"project\":\"totproj2\","
1731+
"\"function_name\":\"tgt2\",\"direction\":\"inbound\"}}}");
1732+
ASSERT_NOT_NULL(resp);
1733+
char *inner = extract_text_content(resp);
1734+
free(resp);
1735+
ASSERT_NOT_NULL(inner);
1736+
ASSERT_NOT_NULL(strstr(inner, "callers_total: 1")); /* tests/repro/ row filtered by default */
1737+
free(inner);
1738+
1739+
resp = cbm_mcp_server_handle(
1740+
srv, "{\"jsonrpc\":\"2.0\",\"id\":93,\"method\":\"tools/call\",\"params\":{"
1741+
"\"name\":\"trace_call_path\",\"arguments\":{\"project\":\"totproj2\","
1742+
"\"function_name\":\"tgt2\",\"direction\":\"inbound\",\"include_tests\":true}}}");
1743+
ASSERT_NOT_NULL(resp);
1744+
inner = extract_text_content(resp);
1745+
free(resp);
1746+
ASSERT_NOT_NULL(inner);
1747+
ASSERT_NOT_NULL(strstr(inner, "callers_total: 2")); /* both visible now */
1748+
free(inner);
1749+
cbm_mcp_server_free(srv);
1750+
PASS();
1751+
}
1752+
16821753
/* SCC condensation (get_architecture aspect "cycles"): a 3-function CALLS
16831754
* cycle A->B->C->A must be reported as one circular dependency of size 3 with
16841755
* all three members; a separate acyclic chain (D->E) must NOT appear. The
@@ -10139,6 +10210,7 @@ SUITE(mcp) {
1013910210
RUN_TEST(tool_unknown_tool);
1014010211
RUN_TEST(tool_search_graph_basic);
1014110212
RUN_TEST(tool_trace_totals_respect_test_filter);
10213+
RUN_TEST(tool_trace_totals_respect_test_filter_tests_root_subtree_issue1294);
1014210214
RUN_TEST(tool_get_architecture_cycles_detects_scc);
1014310215
RUN_TEST(tool_get_code_snippet_clips_whole_file_node);
1014410216
RUN_TEST(tool_search_graph_includes_node_properties);

0 commit comments

Comments
 (0)