@@ -14818,6 +14818,101 @@ TEST(pipeline_python_init_function_shares_submodule_qn_parallel_issue1548) {
1481814818 return pyn_run_init_collision(52);
1481914819}
1482014820
14821+ /* #1548 keeps TESTS for calls made through a test's own nested helper: the
14822+ * call now starts at the extracted helper def (not at the test FILE node, as
14823+ * before #1548), so TESTS is attributed to the enclosing test function. A
14824+ * helper nested in a NON-test function stays without TESTS, like a module-level
14825+ * helper. Both resolvers. */
14826+ static const char pyn_tests_prod_py[] = "def target(value):\n"
14827+ " return value\n"
14828+ "\n"
14829+ "def other_target(value):\n"
14830+ " return value\n";
14831+ static const char pyn_tests_test_py[] = "from pkg.prod import target, other_target\n"
14832+ "\n"
14833+ "def test_through_nested_helper():\n"
14834+ " def check(v):\n"
14835+ " return target(v)\n"
14836+ " assert check(1) == 1\n"
14837+ "\n"
14838+ "class TestGroup:\n"
14839+ " def test_method_helper(self):\n"
14840+ " def probe():\n"
14841+ " return target(2)\n"
14842+ " assert probe() == 2\n"
14843+ "\n"
14844+ "def make_fixture():\n"
14845+ " def build():\n"
14846+ " return other_target(3)\n"
14847+ " return build\n";
14848+
14849+ static int pyn_run_nested_helper_tests(int fillers) {
14850+ char tmp[256];
14851+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_ntests_XXXXXX");
14852+ if (!cbm_mkdtemp(tmp)) {
14853+ FAIL("tmpdir");
14854+ }
14855+ write_temp_file(tmp, "pkg/__init__.py", "");
14856+ write_temp_file(tmp, "pkg/prod.py", pyn_tests_prod_py);
14857+ write_temp_file(tmp, "tests/test_rows.py", pyn_tests_test_py);
14858+ for (int i = 0; i < fillers; i++) {
14859+ char name[64];
14860+ char body[128];
14861+ snprintf(name, sizeof(name), "filler%d.py", i);
14862+ snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
14863+ write_temp_file(tmp, name, body);
14864+ }
14865+ char *old_workers = getenv("CBM_WORKERS");
14866+ char *saved = old_workers ? strdup(old_workers) : NULL;
14867+ if (fillers > 0) {
14868+ cbm_setenv("CBM_WORKERS", "4", 1);
14869+ }
14870+ char db_path[512];
14871+ snprintf(db_path, sizeof(db_path), "%s/ntests.db", tmp);
14872+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
14873+ int rc = p ? cbm_pipeline_run(p) : -1;
14874+ cbm_store_t *s = rc == 0 ? cbm_store_open_path(db_path) : NULL;
14875+ bool fn_tests = false, method_tests = false, helper_calls = false, fixture_tests = true;
14876+ if (s) {
14877+ const char *project = cbm_pipeline_project_name(p);
14878+ helper_calls = pyn_edge(s, project, "tests.test_rows.test_through_nested_helper.check",
14879+ "pkg.prod.target", "CALLS");
14880+ fn_tests = pyn_edge(s, project, "tests.test_rows.test_through_nested_helper",
14881+ "pkg.prod.target", "TESTS");
14882+ method_tests = pyn_edge(s, project, "tests.test_rows.TestGroup.test_method_helper",
14883+ "pkg.prod.target", "TESTS");
14884+ fixture_tests = pyn_edge(s, project, "tests.test_rows.make_fixture",
14885+ "pkg.prod.other_target", "TESTS") ||
14886+ pyn_edge(s, project, "tests.test_rows.make_fixture.build",
14887+ "pkg.prod.other_target", "TESTS");
14888+ cbm_store_close(s);
14889+ }
14890+ if (p) {
14891+ cbm_pipeline_free(p);
14892+ }
14893+ if (saved) {
14894+ cbm_setenv("CBM_WORKERS", saved, 1);
14895+ free(saved);
14896+ } else {
14897+ cbm_unsetenv("CBM_WORKERS");
14898+ }
14899+ th_rmtree(tmp);
14900+ ASSERT_EQ(rc, 0);
14901+ ASSERT_TRUE(helper_calls); /* the call starts at the nested helper (#1548) */
14902+ ASSERT_TRUE(fn_tests); /* ... and the enclosing test function still TESTS the target */
14903+ ASSERT_TRUE(method_tests); /* the same inside a test method */
14904+ ASSERT_FALSE(fixture_tests); /* a helper nested in a non-test function: no TESTS */
14905+ PASS();
14906+ }
14907+
14908+ TEST(pipeline_python_nested_helper_keeps_tests_issue1548) {
14909+ return pyn_run_nested_helper_tests(0);
14910+ }
14911+
14912+ TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548) {
14913+ return pyn_run_nested_helper_tests(52);
14914+ }
14915+
1482114916TEST(pipeline_python_project) {
1482214917 /* Port of TestPipelinePythonProject */
1482314918 const char *files[] = {"main.py", "utils.py"};
@@ -24321,6 +24416,8 @@ SUITE(pipeline) {
2432124416 RUN_TEST(pipeline_python_local_def_shadows_module_def_parallel_issue1548);
2432224417 RUN_TEST(pipeline_python_init_function_shares_submodule_qn_issue1548);
2432324418 RUN_TEST(pipeline_python_init_function_shares_submodule_qn_parallel_issue1548);
24419+ RUN_TEST(pipeline_python_nested_helper_keeps_tests_issue1548);
24420+ RUN_TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548);
2432424421 RUN_TEST(pipeline_header_include_target_is_independent_of_registration_order);
2432524422 RUN_TEST(pipeline_imports_multi_symbol_edges);
2432624423 RUN_TEST(pipeline_go_cross_package_call);
0 commit comments