Skip to content

Commit eec3418

Browse files
committed
fix(python): a test's nested helper keeps TESTS for its calls (DeusData#1548)
Since DeusData#1548 a def declared inside a Python test function is extracted, so its calls start at the helper instead of the test FILE node, whose name passed the test-name check. 4,641 of 6,315 TESTS edges lost on the corpus were this shape. TESTS for a call from such a helper is now attributed to the nearest enclosing test function of the same file; a helper nested in a non-test function stays without TESTS, like a module-level helper. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent d4beec3 commit eec3418

2 files changed

Lines changed: 146 additions & 2 deletions

File tree

‎src/pipeline/pass_tests.c‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum {
2828
#include "pipeline/pipeline.h"
2929
#include "pipeline/pipeline_internal.h"
3030
#include "graph_buffer/graph_buffer.h"
31+
#include "discover/discover.h"
3132
#include "foundation/log.h"
3233

3334
#include <stdio.h>
@@ -208,6 +209,48 @@ static char *test_to_prod_path(const char *test_path) {
208209
return NULL;
209210
}
210211

212+
/* #1548: a def declared inside a Python test function (a closure, a local
213+
* helper, a method of a test-local class) is that test's own code, extracted
214+
* as a node of its own. Its calls exercise the target on the test's behalf, so
215+
* TESTS is attributed to the nearest enclosing test function of the same file
216+
* (QN prefix walk). Before #1548 those calls hung on the test FILE node, whose
217+
* name passes the test-name check. NULL when no enclosing test function exists
218+
* (a helper nested in a non-test function stays without TESTS, like a
219+
* module-level helper). */
220+
static const cbm_gbuf_node_t *enclosing_python_test_func(const cbm_gbuf_t *gb,
221+
const cbm_gbuf_node_t *src) {
222+
if (!src->qualified_name || !src->file_path || !src->label ||
223+
(strcmp(src->label, "Function") != 0 && strcmp(src->label, "Method") != 0)) {
224+
return NULL;
225+
}
226+
const char *slash = strrchr(src->file_path, '/');
227+
if (cbm_language_for_filename(slash ? slash + SKIP_ONE : src->file_path) != CBM_LANG_PYTHON) {
228+
return NULL;
229+
}
230+
char scope[CBM_SZ_512];
231+
size_t len = strlen(src->qualified_name);
232+
if (len >= sizeof(scope)) {
233+
return NULL;
234+
}
235+
memcpy(scope, src->qualified_name, len + SKIP_ONE);
236+
for (char *dot = strrchr(scope, '.'); dot; dot = strrchr(scope, '.')) {
237+
*dot = '\0';
238+
const cbm_gbuf_node_t *outer = cbm_gbuf_find_by_qn(gb, scope);
239+
if (!outer) {
240+
continue;
241+
}
242+
if (!outer->file_path || strcmp(outer->file_path, src->file_path) != 0) {
243+
return NULL; /* left the file: no enclosing test function */
244+
}
245+
if (outer->label &&
246+
(strcmp(outer->label, "Function") == 0 || strcmp(outer->label, "Method") == 0) &&
247+
cbm_is_test_func_name(outer->name)) {
248+
return outer;
249+
}
250+
}
251+
return NULL;
252+
}
253+
211254
/* Create TESTS edges from test functions to production functions via CALLS edges. */
212255
static int create_tests_edges(cbm_pipeline_ctx_t *ctx) {
213256
int count = 0;
@@ -238,11 +281,15 @@ static int create_tests_edges(cbm_pipeline_ctx_t *ctx) {
238281
continue;
239282
}
240283

284+
const cbm_gbuf_node_t *test_src = src;
241285
if (!cbm_is_test_func_name(src->name)) {
242-
continue;
286+
test_src = enclosing_python_test_func(ctx->gbuf, src);
287+
if (!test_src) {
288+
continue;
289+
}
243290
}
244291

245-
cbm_gbuf_insert_edge(ctx->gbuf, src->id, tgt->id, "TESTS", "{}");
292+
cbm_gbuf_insert_edge(ctx->gbuf, test_src->id, tgt->id, "TESTS", "{}");
246293
count++;
247294
}
248295
return count;

‎tests/test_pipeline.c‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1482114916
TEST(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

Comments
 (0)