Skip to content

Commit 33a3f03

Browse files
authored
Merge pull request DeusData#1308 from Yyunozor/fix/issue-1294-is-test-tests-dir
fix(extract): converge Function/Method is_test with the tests/ path filter
2 parents f9e95e3 + cc7b4a8 commit 33a3f03

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
@@ -3747,6 +3747,14 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
37473747
def.is_test = true;
37483748
}
37493749

3750+
// A function/method defined in a file cbm treats as a test file is itself
3751+
// a test, regardless of its own name (tests/helpers/fixtures.c has none
3752+
// of the test_/_test naming conventions, but every function in it is
3753+
// still test code) (#1294). OR'd so the Rust attribute check above, which
3754+
// additionally catches #[test] functions embedded in an otherwise regular
3755+
// .rs file, is never downgraded by this.
3756+
def.is_test = def.is_test || ctx->result->is_test_file;
3757+
37503758
// Docstring
37513759
def.docstring = extract_docstring(a, node, ctx->source, ctx->language);
37523760

@@ -4728,6 +4736,10 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,
47284736
// MinHash fingerprint
47294737
compute_fingerprint(ctx, &def, child);
47304738

4739+
// A method on a class defined in a test file (e.g. a JUnit/pytest
4740+
// TestFoo.test_bar) is itself a test, same as free functions (#1294).
4741+
def.is_test = def.is_test || ctx->result->is_test_file;
4742+
47314743
cbm_defs_push(&ctx->result->defs, a, def);
47324744
}
47334745

‎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
@@ -5733,14 +5733,23 @@ static yyjson_doc *resolve_trace_edge_types(const char *args, const char *mode,
57335733
return NULL;
57345734
}
57355735

5736-
/* Check if a file path looks like a test file. */
5736+
/* Check if a file path looks like a test file. The substring checks below
5737+
* only catch a tests/ directory nested under another path component
5738+
* (".../tests/foo"); a project-root-relative path like "tests/repro/foo.c"
5739+
* has no leading slash before "tests" and fell through undetected, leaking
5740+
* whole test subtrees into query_graph/trace_path results with the default
5741+
* include_tests=false (#1294). */
57375742
static bool is_test_file(const char *path) {
57385743
if (!path) {
57395744
return false;
57405745
}
57415746
return strstr(path, "/test") != NULL || strstr(path, "test_") != NULL ||
57425747
strstr(path, "_test.") != NULL || strstr(path, "/tests/") != NULL ||
5743-
strstr(path, "/spec/") != NULL || strstr(path, ".test.") != NULL;
5748+
strstr(path, "/spec/") != NULL || strstr(path, ".test.") != NULL ||
5749+
strncmp(path, "tests/", SLEN("tests/")) == 0 ||
5750+
strncmp(path, "test/", SLEN("test/")) == 0 ||
5751+
strncmp(path, "spec/", SLEN("spec/")) == 0 ||
5752+
strncmp(path, "__tests__/", SLEN("__tests__/")) == 0;
57445753
}
57455754

57465755
/* 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
@@ -4193,6 +4193,111 @@ TEST(extract_rust_test_attr_marks_is_test_issue855) {
41934193
PASS();
41944194
}
41954195

4196+
/* Function.is_test must follow the file's test-directory membership, not just
4197+
* a test_/_test basename convention: a helper under tests/ with none of those
4198+
* naming conventions (tests/helpers/fixtures.c) was previously indexed as an
4199+
* ordinary, non-test Function — invisible to store.c's `is_test != 1`
4200+
* dead-code/search filters even though trace_path's own independent path
4201+
* check already treated the same file as a test (#1294). */
4202+
TEST(extract_c_test_dir_marks_is_test_issue1294) {
4203+
const char *src = "void helper(void) {}\n";
4204+
4205+
/* Regression: a test_*-named file directly under tests/ must remain a
4206+
* test (this already worked before the fix). */
4207+
CBMFileResult *r1 = extract(src, CBM_LANG_C, "t", "tests/test_pipeline.c");
4208+
ASSERT_NOT_NULL(r1);
4209+
ASSERT_FALSE(r1->has_error);
4210+
ASSERT(has_def(r1, "Function", "helper"));
4211+
int reg = -1;
4212+
for (int i = 0; i < r1->defs.count; i++) {
4213+
if (strcmp(r1->defs.items[i].label, "Function") == 0 && r1->defs.items[i].name &&
4214+
strcmp(r1->defs.items[i].name, "helper") == 0) {
4215+
reg = r1->defs.items[i].is_test ? 1 : 0;
4216+
}
4217+
}
4218+
ASSERT(reg == 1 && "test_*.c directly under tests/ is a test (regression)");
4219+
cbm_free_result(r1);
4220+
4221+
/* Positive: a file under tests/ that matches none of the test_/_test
4222+
* naming conventions must now ALSO be a test. */
4223+
CBMFileResult *r2 = extract(src, CBM_LANG_C, "t", "tests/helpers/fixtures.c");
4224+
ASSERT_NOT_NULL(r2);
4225+
ASSERT_FALSE(r2->has_error);
4226+
ASSERT(has_def(r2, "Function", "helper"));
4227+
int nonconv = -1;
4228+
for (int i = 0; i < r2->defs.count; i++) {
4229+
if (strcmp(r2->defs.items[i].label, "Function") == 0 && r2->defs.items[i].name &&
4230+
strcmp(r2->defs.items[i].name, "helper") == 0) {
4231+
nonconv = r2->defs.items[i].is_test ? 1 : 0;
4232+
}
4233+
}
4234+
ASSERT(nonconv == 1 && "non-test_-named file under tests/ is now a test");
4235+
cbm_free_result(r2);
4236+
4237+
/* Negative: a file with no test/ directory or test_/_test naming in its
4238+
* path must never be flagged — this fix must not widen detection beyond
4239+
* the tests/ (and sibling) directory tree. */
4240+
CBMFileResult *r3 = extract(src, CBM_LANG_C, "t", "src/pipeline/helper.c");
4241+
ASSERT_NOT_NULL(r3);
4242+
ASSERT_FALSE(r3->has_error);
4243+
ASSERT(has_def(r3, "Function", "helper"));
4244+
int outside = -1;
4245+
for (int i = 0; i < r3->defs.count; i++) {
4246+
if (strcmp(r3->defs.items[i].label, "Function") == 0 && r3->defs.items[i].name &&
4247+
strcmp(r3->defs.items[i].name, "helper") == 0) {
4248+
outside = r3->defs.items[i].is_test ? 1 : 0;
4249+
}
4250+
}
4251+
ASSERT(outside == 0 && "file outside tests/ is never a test");
4252+
cbm_free_result(r3);
4253+
4254+
PASS();
4255+
}
4256+
4257+
/* Same convergence for Method definitions (push_method_def), which take a
4258+
* separate code path from free functions: a class method on a class defined
4259+
* under tests/ with no test_/_test naming (e.g. tests/helpers/base.py) must
4260+
* be marked is_test too, and a method on the same class outside tests/ must
4261+
* not be (#1294). */
4262+
TEST(extract_python_method_test_dir_marks_is_test_issue1294) {
4263+
const char *src = "class Foo:\n"
4264+
" def helper(self):\n"
4265+
" pass\n";
4266+
4267+
/* Python's LSP layer injects synthetic builtin stub Methods (str.upper,
4268+
* dict.get, ...) into defs.items alongside real ones (py_builtins.c), so
4269+
* matching must key on name, not just label="Method". */
4270+
CBMFileResult *r1 = extract(src, CBM_LANG_PYTHON, "t", "tests/helpers/base.py");
4271+
ASSERT_NOT_NULL(r1);
4272+
ASSERT_FALSE(r1->has_error);
4273+
ASSERT(has_def(r1, "Method", "helper"));
4274+
int in_tests = -1;
4275+
for (int i = 0; i < r1->defs.count; i++) {
4276+
if (strcmp(r1->defs.items[i].label, "Method") == 0 && r1->defs.items[i].name &&
4277+
strcmp(r1->defs.items[i].name, "helper") == 0) {
4278+
in_tests = r1->defs.items[i].is_test ? 1 : 0;
4279+
}
4280+
}
4281+
ASSERT(in_tests == 1 && "method on a class under tests/helpers/ is a test");
4282+
cbm_free_result(r1);
4283+
4284+
CBMFileResult *r2 = extract(src, CBM_LANG_PYTHON, "t", "app/models/base.py");
4285+
ASSERT_NOT_NULL(r2);
4286+
ASSERT_FALSE(r2->has_error);
4287+
ASSERT(has_def(r2, "Method", "helper"));
4288+
int outside_tests = -1;
4289+
for (int i = 0; i < r2->defs.count; i++) {
4290+
if (strcmp(r2->defs.items[i].label, "Method") == 0 && r2->defs.items[i].name &&
4291+
strcmp(r2->defs.items[i].name, "helper") == 0) {
4292+
outside_tests = r2->defs.items[i].is_test ? 1 : 0;
4293+
}
4294+
}
4295+
ASSERT(outside_tests == 0 && "method on a class outside tests/ is never a test");
4296+
cbm_free_result(r2);
4297+
4298+
PASS();
4299+
}
4300+
41964301
/* #1017: docstring truncation at MAX_COMMENT_LEN (500 bytes) can split a
41974302
* multi-byte UTF-8 character, leaving an incomplete byte sequence.
41984303
* Craft a Go comment whose 498th-500th bytes are a 3-byte CJK character
@@ -5528,6 +5633,8 @@ SUITE(extraction) {
55285633
RUN_TEST(extract_c_clean_file_no_recovery_duplicates_issue961);
55295634
RUN_TEST(walk_defs_no_truncation_over_4096_issue668);
55305635
RUN_TEST(extract_rust_test_attr_marks_is_test_issue855);
5636+
RUN_TEST(extract_c_test_dir_marks_is_test_issue1294);
5637+
RUN_TEST(extract_python_method_test_dir_marks_is_test_issue1294);
55315638
RUN_TEST(docstring_utf8_truncation_boundary_issue1017);
55325639
RUN_TEST(extract_ts_decorators_survive_interleaved_comment);
55335640

‎tests/test_mcp.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,77 @@ TEST(tool_trace_totals_respect_test_filter) {
17071707
PASS();
17081708
}
17091709

1710+
/* Same as above, but the test-side caller lives directly under a
1711+
* project-root tests/ directory with no test_/_test naming convention
1712+
* (tests/repro/helper.c) rather than tests/test_x.c. is_test_file() in
1713+
* mcp.c matched a NESTED ".../tests/..." path but not a project-root-
1714+
* relative one, so this row leaked into results with the default
1715+
* include_tests=false (#1294, secondary bug). */
1716+
TEST(tool_trace_totals_respect_test_filter_tests_root_subtree_issue1294) {
1717+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
1718+
ASSERT_NOT_NULL(srv);
1719+
cbm_store_t *st = cbm_mcp_server_store(srv);
1720+
const char *proj = "totproj2";
1721+
cbm_mcp_server_set_project(srv, proj);
1722+
cbm_store_upsert_project(st, proj, "/tmp/tot2");
1723+
1724+
cbm_node_t tgt = {.project = proj,
1725+
.label = "Function",
1726+
.name = "tgt2",
1727+
.qualified_name = "totproj2.a.tgt2",
1728+
.file_path = "a.c",
1729+
.start_line = 1,
1730+
.end_line = 5};
1731+
int64_t tid = cbm_store_upsert_node(st, &tgt);
1732+
ASSERT_GT(tid, 0);
1733+
cbm_node_t prod = {.project = proj,
1734+
.label = "Function",
1735+
.name = "prod_caller2",
1736+
.qualified_name = "totproj2.a.prod_caller2",
1737+
.file_path = "a.c",
1738+
.start_line = 10,
1739+
.end_line = 15};
1740+
int64_t pid = cbm_store_upsert_node(st, &prod);
1741+
ASSERT_GT(pid, 0);
1742+
cbm_node_t tst = {.project = proj,
1743+
.label = "Function",
1744+
.name = "repro_caller",
1745+
.qualified_name = "totproj2.t.repro_caller",
1746+
.file_path = "tests/repro/helper.c",
1747+
.start_line = 1,
1748+
.end_line = 5};
1749+
int64_t xid = cbm_store_upsert_node(st, &tst);
1750+
ASSERT_GT(xid, 0);
1751+
cbm_edge_t e1 = {.project = proj, .source_id = pid, .target_id = tid, .type = "CALLS"};
1752+
ASSERT_GT(cbm_store_insert_edge(st, &e1), 0);
1753+
cbm_edge_t e2 = {.project = proj, .source_id = xid, .target_id = tid, .type = "CALLS"};
1754+
ASSERT_GT(cbm_store_insert_edge(st, &e2), 0);
1755+
1756+
char *resp = cbm_mcp_server_handle(
1757+
srv, "{\"jsonrpc\":\"2.0\",\"id\":92,\"method\":\"tools/call\",\"params\":{"
1758+
"\"name\":\"trace_call_path\",\"arguments\":{\"project\":\"totproj2\","
1759+
"\"function_name\":\"tgt2\",\"direction\":\"inbound\"}}}");
1760+
ASSERT_NOT_NULL(resp);
1761+
char *inner = extract_text_content(resp);
1762+
free(resp);
1763+
ASSERT_NOT_NULL(inner);
1764+
ASSERT_NOT_NULL(strstr(inner, "callers_total: 1")); /* tests/repro/ row filtered by default */
1765+
free(inner);
1766+
1767+
resp = cbm_mcp_server_handle(
1768+
srv, "{\"jsonrpc\":\"2.0\",\"id\":93,\"method\":\"tools/call\",\"params\":{"
1769+
"\"name\":\"trace_call_path\",\"arguments\":{\"project\":\"totproj2\","
1770+
"\"function_name\":\"tgt2\",\"direction\":\"inbound\",\"include_tests\":true}}}");
1771+
ASSERT_NOT_NULL(resp);
1772+
inner = extract_text_content(resp);
1773+
free(resp);
1774+
ASSERT_NOT_NULL(inner);
1775+
ASSERT_NOT_NULL(strstr(inner, "callers_total: 2")); /* both visible now */
1776+
free(inner);
1777+
cbm_mcp_server_free(srv);
1778+
PASS();
1779+
}
1780+
17101781
/* SCC condensation (get_architecture aspect "cycles"): a 3-function CALLS
17111782
* cycle A->B->C->A must be reported as one circular dependency of size 3 with
17121783
* all three members; a separate acyclic chain (D->E) must NOT appear. The
@@ -10745,6 +10816,7 @@ SUITE(mcp) {
1074510816
RUN_TEST(tool_unknown_tool);
1074610817
RUN_TEST(tool_search_graph_basic);
1074710818
RUN_TEST(tool_trace_totals_respect_test_filter);
10819+
RUN_TEST(tool_trace_totals_respect_test_filter_tests_root_subtree_issue1294);
1074810820
RUN_TEST(tool_get_architecture_cycles_detects_scc);
1074910821
RUN_TEST(tool_get_code_snippet_clips_whole_file_node);
1075010822
RUN_TEST(tool_search_graph_includes_node_properties);

0 commit comments

Comments
 (0)