Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(python): a test's nested helper keeps TESTS for its calls (#1548)

Martin Vogeleec3418fef18
diff --git a/src/pipeline/pass_tests.c b/src/pipeline/pass_tests.c
index 9cade02a0..a763016fb 100644
--- a/src/pipeline/pass_tests.c
+++ b/src/pipeline/pass_tests.c
@@ -28,6 +28,7 @@ enum {
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
#include "graph_buffer/graph_buffer.h"
+#include "discover/discover.h"
#include "foundation/log.h"
#include <stdio.h>
@@ -208,6 +209,48 @@ static char *test_to_prod_path(const char *test_path) {
return NULL;
}
+/* #1548: a def declared inside a Python test function (a closure, a local
+ * helper, a method of a test-local class) is that test's own code, extracted
+ * as a node of its own. Its calls exercise the target on the test's behalf, so
+ * TESTS is attributed to the nearest enclosing test function of the same file
+ * (QN prefix walk). Before #1548 those calls hung on the test FILE node, whose
+ * name passes the test-name check. NULL when no enclosing test function exists
+ * (a helper nested in a non-test function stays without TESTS, like a
+ * module-level helper). */
+static const cbm_gbuf_node_t *enclosing_python_test_func(const cbm_gbuf_t *gb,
+ const cbm_gbuf_node_t *src) {
+ if (!src->qualified_name || !src->file_path || !src->label ||
+ (strcmp(src->label, "Function") != 0 && strcmp(src->label, "Method") != 0)) {
+ return NULL;
+ }
+ const char *slash = strrchr(src->file_path, '/');
+ if (cbm_language_for_filename(slash ? slash + SKIP_ONE : src->file_path) != CBM_LANG_PYTHON) {
+ return NULL;
+ }
+ char scope[CBM_SZ_512];
+ size_t len = strlen(src->qualified_name);
+ if (len >= sizeof(scope)) {
+ return NULL;
+ }
+ memcpy(scope, src->qualified_name, len + SKIP_ONE);
+ for (char *dot = strrchr(scope, '.'); dot; dot = strrchr(scope, '.')) {
+ *dot = '\0';
+ const cbm_gbuf_node_t *outer = cbm_gbuf_find_by_qn(gb, scope);
+ if (!outer) {
+ continue;
+ }
+ if (!outer->file_path || strcmp(outer->file_path, src->file_path) != 0) {
+ return NULL; /* left the file: no enclosing test function */
+ }
+ if (outer->label &&
+ (strcmp(outer->label, "Function") == 0 || strcmp(outer->label, "Method") == 0) &&
+ cbm_is_test_func_name(outer->name)) {
+ return outer;
+ }
+ }
+ return NULL;
+}
+
/* Create TESTS edges from test functions to production functions via CALLS edges. */
static int create_tests_edges(cbm_pipeline_ctx_t *ctx) {
int count = 0;
@@ -238,11 +281,15 @@ static int create_tests_edges(cbm_pipeline_ctx_t *ctx) {
continue;
}
+ const cbm_gbuf_node_t *test_src = src;
if (!cbm_is_test_func_name(src->name)) {
- continue;
+ test_src = enclosing_python_test_func(ctx->gbuf, src);
+ if (!test_src) {
+ continue;
+ }
}
- cbm_gbuf_insert_edge(ctx->gbuf, src->id, tgt->id, "TESTS", "{}");
+ cbm_gbuf_insert_edge(ctx->gbuf, test_src->id, tgt->id, "TESTS", "{}");
count++;
}
return count;
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 25e467b99..8403a3aed 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -14818,6 +14818,101 @@ TEST(pipeline_python_init_function_shares_submodule_qn_parallel_issue1548) {
return pyn_run_init_collision(52);
}
+/* #1548 keeps TESTS for calls made through a test's own nested helper: the
+ * call now starts at the extracted helper def (not at the test FILE node, as
+ * before #1548), so TESTS is attributed to the enclosing test function. A
+ * helper nested in a NON-test function stays without TESTS, like a module-level
+ * helper. Both resolvers. */
+static const char pyn_tests_prod_py[] = "def target(value):\n"
+ " return value\n"
+ "\n"
+ "def other_target(value):\n"
+ " return value\n";
+static const char pyn_tests_test_py[] = "from pkg.prod import target, other_target\n"
+ "\n"
+ "def test_through_nested_helper():\n"
+ " def check(v):\n"
+ " return target(v)\n"
+ " assert check(1) == 1\n"
+ "\n"
+ "class TestGroup:\n"
+ " def test_method_helper(self):\n"
+ " def probe():\n"
+ " return target(2)\n"
+ " assert probe() == 2\n"
+ "\n"
+ "def make_fixture():\n"
+ " def build():\n"
+ " return other_target(3)\n"
+ " return build\n";
+
+static int pyn_run_nested_helper_tests(int fillers) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_ntests_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+ write_temp_file(tmp, "pkg/__init__.py", "");
+ write_temp_file(tmp, "pkg/prod.py", pyn_tests_prod_py);
+ write_temp_file(tmp, "tests/test_rows.py", pyn_tests_test_py);
+ for (int i = 0; i < fillers; i++) {
+ char name[64];
+ char body[128];
+ snprintf(name, sizeof(name), "filler%d.py", i);
+ snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
+ write_temp_file(tmp, name, body);
+ }
+ char *old_workers = getenv("CBM_WORKERS");
+ char *saved = old_workers ? strdup(old_workers) : NULL;
+ if (fillers > 0) {
+ cbm_setenv("CBM_WORKERS", "4", 1);
+ }
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/ntests.db", tmp);
+ cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ int rc = p ? cbm_pipeline_run(p) : -1;
+ cbm_store_t *s = rc == 0 ? cbm_store_open_path(db_path) : NULL;
+ bool fn_tests = false, method_tests = false, helper_calls = false, fixture_tests = true;
+ if (s) {
+ const char *project = cbm_pipeline_project_name(p);
+ helper_calls = pyn_edge(s, project, "tests.test_rows.test_through_nested_helper.check",
+ "pkg.prod.target", "CALLS");
+ fn_tests = pyn_edge(s, project, "tests.test_rows.test_through_nested_helper",
+ "pkg.prod.target", "TESTS");
+ method_tests = pyn_edge(s, project, "tests.test_rows.TestGroup.test_method_helper",
+ "pkg.prod.target", "TESTS");
+ fixture_tests = pyn_edge(s, project, "tests.test_rows.make_fixture",
+ "pkg.prod.other_target", "TESTS") ||
+ pyn_edge(s, project, "tests.test_rows.make_fixture.build",
+ "pkg.prod.other_target", "TESTS");
+ cbm_store_close(s);
+ }
+ if (p) {
+ cbm_pipeline_free(p);
+ }
+ if (saved) {
+ cbm_setenv("CBM_WORKERS", saved, 1);
+ free(saved);
+ } else {
+ cbm_unsetenv("CBM_WORKERS");
+ }
+ th_rmtree(tmp);
+ ASSERT_EQ(rc, 0);
+ ASSERT_TRUE(helper_calls); /* the call starts at the nested helper (#1548) */
+ ASSERT_TRUE(fn_tests); /* ... and the enclosing test function still TESTS the target */
+ ASSERT_TRUE(method_tests); /* the same inside a test method */
+ ASSERT_FALSE(fixture_tests); /* a helper nested in a non-test function: no TESTS */
+ PASS();
+}
+
+TEST(pipeline_python_nested_helper_keeps_tests_issue1548) {
+ return pyn_run_nested_helper_tests(0);
+}
+
+TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548) {
+ return pyn_run_nested_helper_tests(52);
+}
+
TEST(pipeline_python_project) {
/* Port of TestPipelinePythonProject */
const char *files[] = {"main.py", "utils.py"};
@@ -24321,6 +24416,8 @@ SUITE(pipeline) {
RUN_TEST(pipeline_python_local_def_shadows_module_def_parallel_issue1548);
RUN_TEST(pipeline_python_init_function_shares_submodule_qn_issue1548);
RUN_TEST(pipeline_python_init_function_shares_submodule_qn_parallel_issue1548);
+ RUN_TEST(pipeline_python_nested_helper_keeps_tests_issue1548);
+ RUN_TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548);
RUN_TEST(pipeline_header_include_target_is_independent_of_registration_order);
RUN_TEST(pipeline_imports_multi_symbol_edges);
RUN_TEST(pipeline_go_cross_package_call);