Skip to content

Commit 4b59b9a

Browse files
committed
test(python): a decorated class nested in a class body is extracted
RED on its own: push_nested_class_nodes does not look through decorated_definition. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 090580b commit 4b59b9a

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

‎tests/test_pipeline.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15024,6 +15024,94 @@ TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548) {
1502415024
return pyn_run_nested_helper_tests(52);
1502515025
}
1502615026

15027+
/* A decorated class nested in a class body (`@dataclass class Meta:` inside a
15028+
* class) is a nested class like an undecorated one: it gets its Class node and
15029+
* its methods their Method nodes. push_nested_class_nodes looked through
15030+
* field/template/declaration and conditional wrappers but not through Python's
15031+
* decorated_definition, so the decorated class and everything in it was never
15032+
* extracted. Both resolvers. */
15033+
static const char pyn_decorated_nested_py[] = "def deco(cls):\n"
15034+
" return cls\n"
15035+
"\n"
15036+
"class Outer:\n"
15037+
" @deco\n"
15038+
" class Decorated:\n"
15039+
" def run(self):\n"
15040+
" return 1\n"
15041+
"\n"
15042+
" class Plain:\n"
15043+
" def go(self):\n"
15044+
" return 2\n";
15045+
15046+
static bool pyn_node_exists(cbm_store_t *s, const char *project, const char *rel_qn,
15047+
const char *label) {
15048+
char qn[512];
15049+
pyn_qn(qn, sizeof(qn), project, rel_qn);
15050+
cbm_node_t n = {0};
15051+
bool found = cbm_store_find_node_by_qn(s, project, qn, &n) == CBM_STORE_OK && n.label &&
15052+
strcmp(n.label, label) == 0;
15053+
cbm_node_free_fields(&n);
15054+
return found;
15055+
}
15056+
15057+
static int pyn_run_decorated_nested(int fillers) {
15058+
char tmp[256];
15059+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_decnest_XXXXXX");
15060+
if (!cbm_mkdtemp(tmp)) {
15061+
FAIL("tmpdir");
15062+
}
15063+
write_temp_file(tmp, "sd.py", pyn_decorated_nested_py);
15064+
for (int i = 0; i < fillers; i++) {
15065+
char name[64];
15066+
char body[128];
15067+
snprintf(name, sizeof(name), "filler%d.py", i);
15068+
snprintf(body, sizeof(body), "def filler%d():\n return %d\n", i, i);
15069+
write_temp_file(tmp, name, body);
15070+
}
15071+
char *old_workers = getenv("CBM_WORKERS");
15072+
char *saved = old_workers ? strdup(old_workers) : NULL;
15073+
if (fillers > 0) {
15074+
cbm_setenv("CBM_WORKERS", "4", 1);
15075+
}
15076+
char db_path[512];
15077+
snprintf(db_path, sizeof(db_path), "%s/decnest.db", tmp);
15078+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
15079+
int rc = p ? cbm_pipeline_run(p) : -1;
15080+
cbm_store_t *s = rc == 0 ? cbm_store_open_path(db_path) : NULL;
15081+
const char *project = p ? cbm_pipeline_project_name(p) : NULL;
15082+
bool dec_class = s && pyn_node_exists(s, project, "sd.Outer.Decorated", "Class");
15083+
bool dec_method = s && pyn_node_exists(s, project, "sd.Outer.Decorated.run", "Method");
15084+
bool plain_class = s && pyn_node_exists(s, project, "sd.Outer.Plain", "Class");
15085+
bool plain_method = s && pyn_node_exists(s, project, "sd.Outer.Plain.go", "Method");
15086+
if (s) {
15087+
cbm_store_close(s);
15088+
}
15089+
if (p) {
15090+
cbm_pipeline_free(p);
15091+
}
15092+
if (saved) {
15093+
cbm_setenv("CBM_WORKERS", saved, 1);
15094+
free(saved);
15095+
} else {
15096+
cbm_unsetenv("CBM_WORKERS");
15097+
}
15098+
th_rmtree(tmp);
15099+
ASSERT_EQ(rc, 0);
15100+
ASSERT_TRUE(dec_class); /* the decorated nested class is extracted */
15101+
ASSERT_TRUE(dec_method); /* ... and its method */
15102+
ASSERT_TRUE(plain_class); /* control: undecorated nested class */
15103+
ASSERT_TRUE(plain_method); /* control */
15104+
PASS();
15105+
}
15106+
15107+
TEST(pipeline_python_decorated_nested_class_extracted) {
15108+
return pyn_run_decorated_nested(0);
15109+
}
15110+
15111+
TEST(pipeline_python_decorated_nested_class_extracted_parallel) {
15112+
return pyn_run_decorated_nested(52);
15113+
}
15114+
1502715115
TEST(pipeline_python_project) {
1502815116
/* Port of TestPipelinePythonProject */
1502915117
const char *files[] = {"main.py", "utils.py"};
@@ -24531,6 +24619,8 @@ SUITE(pipeline) {
2453124619
RUN_TEST(pipeline_python_init_function_shares_submodule_qn_parallel_issue1548);
2453224620
RUN_TEST(pipeline_python_nested_helper_keeps_tests_issue1548);
2453324621
RUN_TEST(pipeline_python_nested_helper_keeps_tests_parallel_issue1548);
24622+
RUN_TEST(pipeline_python_decorated_nested_class_extracted);
24623+
RUN_TEST(pipeline_python_decorated_nested_class_extracted_parallel);
2453424624
RUN_TEST(pipeline_header_include_target_is_independent_of_registration_order);
2453524625
RUN_TEST(pipeline_imports_multi_symbol_edges);
2453624626
RUN_TEST(pipeline_go_cross_package_call);

0 commit comments

Comments
 (0)