@@ -4715,6 +4715,77 @@ TEST(extract_java_method_annotations_issue382) {
47154715 PASS ();
47164716}
47174717
4718+ /* Distilled from PR #1245 (Andrew Hundt): unittest.mock's `@patch("x")` is a
4719+ * bare decorator whose name collides with the HTTP verb, and its only argument
4720+ * is a dotted target, not a path. The "/" fallback then minted a Route handler
4721+ * PATCH "/" for every mocked test function. Rule: a decorator call WITH
4722+ * arguments but no path-shaped one is not a route. */
4723+ TEST (extract_python_mock_patch_is_not_route ) {
4724+ CBMFileResult * r = extract ("from unittest.mock import patch\n\n"
4725+ "@patch(\"subprocess.run\")\n"
4726+ "def test_cmd(mock_run):\n"
4727+ " pass\n\n"
4728+ "@app.patch(\"/items/{id}\")\n"
4729+ "def update_item():\n"
4730+ " pass\n" ,
4731+ CBM_LANG_PYTHON , "t" , "test_routes.py" );
4732+ ASSERT_NOT_NULL (r );
4733+ ASSERT_FALSE (r -> has_error );
4734+
4735+ const CBMDefinition * mocked = find_def_by_name (r , "test_cmd" );
4736+ ASSERT_NOT_NULL (mocked );
4737+ ASSERT_NULL (mocked -> route_path );
4738+ ASSERT_NULL (mocked -> route_method );
4739+
4740+ const CBMDefinition * route = find_def_by_name (r , "update_item" );
4741+ ASSERT_NOT_NULL (route );
4742+ ASSERT_STR_EQ (route -> route_path , "/items/{id}" );
4743+ ASSERT_STR_EQ (route -> route_method , "PATCH" );
4744+
4745+ cbm_free_result (r );
4746+ PASS ();
4747+ }
4748+
4749+ /* Companion to the mock-patch case: the narrowing must not cost real routes.
4750+ * A receiver-less framework decorator with a path-shaped argument (Litestar /
4751+ * BlackSheep `@get("/x")`) stays a route, `@mock.patch("x")` (receiver, but
4752+ * no path) is not one, and a zero-argument `@app.route()` keeps the "/"
4753+ * default main emits today. */
4754+ TEST (extract_python_bare_decorator_route_rules ) {
4755+ CBMFileResult * r = extract ("from litestar import get\n"
4756+ "from unittest import mock\n\n"
4757+ "@get(\"/health\")\n"
4758+ "def health():\n"
4759+ " pass\n\n"
4760+ "@mock.patch(\"os.getcwd\")\n"
4761+ "def test_cwd(mock_cwd):\n"
4762+ " pass\n\n"
4763+ "@app.route()\n"
4764+ "def index():\n"
4765+ " pass\n" ,
4766+ CBM_LANG_PYTHON , "t" , "routes.py" );
4767+ ASSERT_NOT_NULL (r );
4768+ ASSERT_FALSE (r -> has_error );
4769+
4770+ const CBMDefinition * health = find_def_by_name (r , "health" );
4771+ ASSERT_NOT_NULL (health );
4772+ ASSERT_STR_EQ (health -> route_path , "/health" );
4773+ ASSERT_STR_EQ (health -> route_method , "GET" );
4774+
4775+ const CBMDefinition * mocked = find_def_by_name (r , "test_cwd" );
4776+ ASSERT_NOT_NULL (mocked );
4777+ ASSERT_NULL (mocked -> route_path );
4778+ ASSERT_NULL (mocked -> route_method );
4779+
4780+ const CBMDefinition * index = find_def_by_name (r , "index" );
4781+ ASSERT_NOT_NULL (index );
4782+ ASSERT_STR_EQ (index -> route_path , "/" );
4783+ ASSERT_STR_EQ (index -> route_method , "ANY" );
4784+
4785+ cbm_free_result (r );
4786+ PASS ();
4787+ }
4788+
47184789/* ── ArkTS (HarmonyOS .ets) ─────────────────────────────────────── */
47194790
47204791TEST (arkts_component_struct ) {
@@ -9829,6 +9900,8 @@ SUITE(extraction) {
98299900 RUN_TEST (js_index_module_qn_not_collide_with_folder );
98309901 RUN_TEST (python_regular_module_qn_unchanged );
98319902 RUN_TEST (extract_java_method_annotations_issue382 );
9903+ RUN_TEST (extract_python_mock_patch_is_not_route );
9904+ RUN_TEST (extract_python_bare_decorator_route_rules );
98329905 RUN_TEST (arkts_component_struct );
98339906 RUN_TEST (arkts_exported_struct_decorators );
98349907 RUN_TEST (arkts_member_decorators );
0 commit comments