|
| 1 | +"""Tests for config_parser.py — monorepo language detection priority.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +from pathlib import Path |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from codeflash.code_utils.config_parser import parse_config_file |
| 13 | + |
| 14 | + |
| 15 | +class TestMonorepoConfigPriority: |
| 16 | + """Verify that closer config files win over parent Java build files in monorepos.""" |
| 17 | + |
| 18 | + def test_closer_package_json_wins_over_parent_pom_xml(self, tmp_path: Path) -> None: |
| 19 | + """In monorepo/frontend/, a local package.json should win over a parent pom.xml.""" |
| 20 | + # Parent Java project |
| 21 | + (tmp_path / "pom.xml").write_text("<project></project>", encoding="utf-8") |
| 22 | + (tmp_path / "src" / "main" / "java").mkdir(parents=True) |
| 23 | + |
| 24 | + # Child JS project |
| 25 | + frontend = tmp_path / "frontend" |
| 26 | + frontend.mkdir() |
| 27 | + (frontend / "package.json").write_text( |
| 28 | + json.dumps({"name": "frontend", "codeflash": {"moduleRoot": "src"}}), |
| 29 | + encoding="utf-8", |
| 30 | + ) |
| 31 | + (frontend / "src").mkdir() |
| 32 | + |
| 33 | + with patch("codeflash.code_utils.config_parser.Path") as mock_path_cls: |
| 34 | + mock_path_cls.cwd.return_value = frontend |
| 35 | + # find_package_json also uses Path.cwd; mock it at the source |
| 36 | + with patch("codeflash.code_utils.config_js.Path") as mock_js_path_cls: |
| 37 | + mock_js_path_cls.cwd.return_value = frontend |
| 38 | + # Also need to let normal Path operations work |
| 39 | + mock_path_cls.side_effect = Path |
| 40 | + mock_path_cls.cwd.return_value = frontend |
| 41 | + mock_js_path_cls.side_effect = Path |
| 42 | + mock_js_path_cls.cwd.return_value = frontend |
| 43 | + |
| 44 | + config, root = parse_config_file() |
| 45 | + |
| 46 | + # Should detect JS, not Java |
| 47 | + assert config.get("language") != "java", ( |
| 48 | + "Closer package.json should take priority over parent pom.xml" |
| 49 | + ) |
| 50 | + |
| 51 | + def test_java_wins_when_no_closer_js_config(self, tmp_path: Path) -> None: |
| 52 | + """When only a pom.xml exists (no package.json/pyproject.toml closer), Java config wins.""" |
| 53 | + (tmp_path / "pom.xml").write_text("<project></project>", encoding="utf-8") |
| 54 | + (tmp_path / "src" / "main" / "java").mkdir(parents=True) |
| 55 | + |
| 56 | + with patch("codeflash.code_utils.config_parser.Path") as mock_path_cls: |
| 57 | + mock_path_cls.side_effect = Path |
| 58 | + mock_path_cls.cwd.return_value = tmp_path |
| 59 | + with patch("codeflash.code_utils.config_js.Path") as mock_js_path_cls: |
| 60 | + mock_js_path_cls.side_effect = Path |
| 61 | + mock_js_path_cls.cwd.return_value = tmp_path |
| 62 | + |
| 63 | + config, root = parse_config_file() |
| 64 | + |
| 65 | + assert config.get("language") == "java" |
| 66 | + |
| 67 | + def test_same_level_package_json_wins_over_pom_xml(self, tmp_path: Path) -> None: |
| 68 | + """When pom.xml and package.json are at the same level, package.json wins (more specific).""" |
| 69 | + (tmp_path / "pom.xml").write_text("<project></project>", encoding="utf-8") |
| 70 | + (tmp_path / "src" / "main" / "java").mkdir(parents=True) |
| 71 | + (tmp_path / "package.json").write_text( |
| 72 | + json.dumps({"name": "mixed-project", "codeflash": {"moduleRoot": "src"}}), |
| 73 | + encoding="utf-8", |
| 74 | + ) |
| 75 | + |
| 76 | + with patch("codeflash.code_utils.config_parser.Path") as mock_path_cls: |
| 77 | + mock_path_cls.side_effect = Path |
| 78 | + mock_path_cls.cwd.return_value = tmp_path |
| 79 | + with patch("codeflash.code_utils.config_js.Path") as mock_js_path_cls: |
| 80 | + mock_js_path_cls.side_effect = Path |
| 81 | + mock_js_path_cls.cwd.return_value = tmp_path |
| 82 | + |
| 83 | + config, root = parse_config_file() |
| 84 | + |
| 85 | + assert config.get("language") != "java", ( |
| 86 | + "Same-level package.json should take priority over pom.xml" |
| 87 | + ) |
0 commit comments