@@ -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
0 commit comments