Skip to content

Commit bf3657f

Browse files
authored
Merge pull request #857 from moofone/fix/855-rust-test-attr-is_test
fix(extract-rust): mark #[test]/#[tokio::test] fns is_test so they are filtered
2 parents a4e1f21 + 7b7e056 commit bf3657f

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,35 @@ static const char **extract_decorators(CBMArena *a, TSNode node, const char *sou
18691869
* first and one branch is lost (#495). Fold the cfg predicate into the QN so
18701870
* each cfg-gated twin gets a DISTINCT, predicate-encoding QN. Returns the
18711871
* (possibly suffixed) QN; the original QN when no cfg attribute is present. */
1872+
/* Rust: mark a function as a test when it carries a test attribute (#855).
1873+
* cbm's test detection is otherwise file-path-based (cbm_is_test_file:
1874+
* *_test.rs / test_*), so inline #[test]/#[tokio::test] functions inside a
1875+
* regular .rs file are indexed as ordinary Functions (is_test=false) and leak
1876+
* past the store.c `is_test != 1` filter into graph/agent context. The
1877+
* attribute_item text extract_decorators stores is the bracketed form
1878+
* ("#[test]", "#[tokio::test]", "#[tokio::test(...)]", ...). */
1879+
static bool rust_def_is_test(const char *const *decorators) {
1880+
if (!decorators) {
1881+
return false;
1882+
}
1883+
for (int i = 0; decorators[i]; i++) {
1884+
const char *d = decorators[i];
1885+
/* Path-qualified async/param test macros (substring match, robust to the
1886+
* optional argument list and the surrounding #[ ]). */
1887+
if (strstr(d, "tokio::test") || strstr(d, "async_std::test") ||
1888+
strstr(d, "actix_rt::test") || strstr(d, "test_case::case")) {
1889+
return true;
1890+
}
1891+
/* Bare #[test] / #[test(...)]: match the bracketed path exactly so we do
1892+
* NOT match the unrelated #[test_case::case] (handled above) or a
1893+
* hypothetical #[test_crate]. */
1894+
if (strstr(d, "#[test]") || strstr(d, "#[test(")) {
1895+
return true;
1896+
}
1897+
}
1898+
return false;
1899+
}
1900+
18721901
static const char *rust_cfg_qualified_name(CBMArena *a, const char *base_qn,
18731902
const char *const *decorators) {
18741903
if (!decorators) {
@@ -3192,6 +3221,7 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
31923221
// predicate into the QN so both branches survive the graph upsert (#495).
31933222
if (ctx->language == CBM_LANG_RUST) {
31943223
def.qualified_name = rust_cfg_qualified_name(a, def.qualified_name, def.decorators);
3224+
def.is_test = rust_def_is_test(def.decorators);
31953225
}
31963226

31973227
// Docstring

‎tests/test_extraction.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3350,6 +3350,46 @@ TEST(walk_defs_no_truncation_over_4096_issue668) {
33503350
* Suite
33513351
* ═══════════════════════════════════════════════════════════════════ */
33523352

3353+
/* Rust: inline #[test]/#[tokio::test] functions must be marked is_test so the
3354+
* store.c `is_test != 1` filter excludes them from graph context. Detection is
3355+
* otherwise file-path-based (cbm_is_test_file), so test fns in a regular .rs
3356+
* file leak. (#855) */
3357+
TEST(extract_rust_test_attr_marks_is_test_issue855) {
3358+
CBMFileResult *r = extract(
3359+
"pub fn real_fn() {}\n"
3360+
"\n"
3361+
"#[test]\n"
3362+
"fn sync_test() {}\n"
3363+
"\n"
3364+
"#[tokio::test]\n"
3365+
"async fn async_test() {}\n",
3366+
CBM_LANG_RUST, "t", "src/lib.rs");
3367+
ASSERT_NOT_NULL(r);
3368+
ASSERT_FALSE(r->has_error);
3369+
3370+
int real = -1, sync = -1, asyn = -1;
3371+
for (int i = 0; i < r->defs.count; i++) {
3372+
const char *n = r->defs.items[i].name;
3373+
if (!n) {
3374+
continue;
3375+
}
3376+
if (strcmp(n, "real_fn") == 0) {
3377+
real = r->defs.items[i].is_test ? 1 : 0;
3378+
} else if (strcmp(n, "sync_test") == 0) {
3379+
sync = r->defs.items[i].is_test ? 1 : 0;
3380+
} else if (strcmp(n, "async_test") == 0) {
3381+
asyn = r->defs.items[i].is_test ? 1 : 0;
3382+
}
3383+
}
3384+
ASSERT(real >= 0 && sync >= 0 && asyn >= 0 && "all three fns extracted");
3385+
ASSERT(real == 0 && "real_fn is NOT a test");
3386+
ASSERT(sync == 1 && "#[test] fn is_test");
3387+
ASSERT(asyn == 1 && "#[tokio::test] fn is_test");
3388+
3389+
cbm_free_result(r);
3390+
PASS();
3391+
}
3392+
33533393
SUITE(extraction) {
33543394
/* Initialize extraction library */
33553395
cbm_init();
@@ -3597,6 +3637,7 @@ SUITE(extraction) {
35973637
RUN_TEST(complexity_go_method_receiver_self_recursion);
35983638
RUN_TEST(complexity_access_depth_and_params);
35993639
RUN_TEST(walk_defs_no_truncation_over_4096_issue668);
3640+
RUN_TEST(extract_rust_test_attr_marks_is_test_issue855);
36003641

36013642
cbm_shutdown();
36023643
}

0 commit comments

Comments
 (0)