|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from codegen.sdk.codebase.config import ProjectConfig |
| 8 | +from codegen.shared.enums.programming_language import ProgrammingLanguage |
| 9 | + |
| 10 | + |
| 11 | +def test_explicit_language_respected(): |
| 12 | + """Test that explicitly provided language is respected and not overridden by detection.""" |
| 13 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 14 | + # Create a temporary directory with more TypeScript files than Python files |
| 15 | + ts_dir = Path(tmp_dir) / "ts" |
| 16 | + py_dir = Path(tmp_dir) / "py" |
| 17 | + ts_dir.mkdir() |
| 18 | + py_dir.mkdir() |
| 19 | + |
| 20 | + # Create TypeScript files |
| 21 | + for i in range(5): |
| 22 | + with open(ts_dir / f"file{i}.ts", "w") as f: |
| 23 | + f.write(f"// TypeScript file {i}") |
| 24 | + |
| 25 | + # Create fewer Python files |
| 26 | + for i in range(2): |
| 27 | + with open(py_dir / f"file{i}.py", "w") as f: |
| 28 | + f.write(f"# Python file {i}") |
| 29 | + |
| 30 | + # Initialize git repo |
| 31 | + os. system( f"cd {tmp_dir} && git init && git config user.email '[email protected]' && git config user.name 'Test User' && git add . && git commit -m 'Initial commit'") |
| 32 | + |
| 33 | + # Test with explicit Python language |
| 34 | + project_config = ProjectConfig.from_path( |
| 35 | + path=str(tmp_dir), |
| 36 | + programming_language=ProgrammingLanguage.PYTHON |
| 37 | + ) |
| 38 | + |
| 39 | + # Verify that the language is Python, not TypeScript (which would be detected based on file count) |
| 40 | + assert project_config.programming_language == ProgrammingLanguage.PYTHON |
| 41 | + |
| 42 | + # Test with explicit TypeScript language |
| 43 | + project_config = ProjectConfig.from_path( |
| 44 | + path=str(py_dir), # Use Python directory |
| 45 | + programming_language=ProgrammingLanguage.TYPESCRIPT |
| 46 | + ) |
| 47 | + |
| 48 | + # Verify that the language is TypeScript, not Python (which would be detected based on file count) |
| 49 | + assert project_config.programming_language == ProgrammingLanguage.TYPESCRIPT |
| 50 | + |
| 51 | + |
| 52 | +def test_subfolder_language_detection(): |
| 53 | + """Test that language detection respects the specified subfolder.""" |
| 54 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 55 | + # Create a temporary directory with TypeScript files in root and Python files in subfolder |
| 56 | + ts_dir = Path(tmp_dir) |
| 57 | + py_dir = Path(tmp_dir) / "python_only" |
| 58 | + py_dir.mkdir() |
| 59 | + |
| 60 | + # Create TypeScript files in root |
| 61 | + for i in range(5): |
| 62 | + with open(ts_dir / f"file{i}.ts", "w") as f: |
| 63 | + f.write(f"// TypeScript file {i}") |
| 64 | + |
| 65 | + # Create Python files in subfolder |
| 66 | + for i in range(3): |
| 67 | + with open(py_dir / f"file{i}.py", "w") as f: |
| 68 | + f.write(f"# Python file {i}") |
| 69 | + |
| 70 | + # Initialize git repo |
| 71 | + os. system( f"cd {tmp_dir} && git init && git config user.email '[email protected]' && git config user.name 'Test User' && git add . && git commit -m 'Initial commit'") |
| 72 | + |
| 73 | + # Test with root path - should detect TypeScript |
| 74 | + project_config = ProjectConfig.from_path( |
| 75 | + path=str(tmp_dir) |
| 76 | + ) |
| 77 | + assert project_config.programming_language == ProgrammingLanguage.TYPESCRIPT |
| 78 | + |
| 79 | + # Test with Python subfolder path - should detect Python |
| 80 | + project_config = ProjectConfig.from_path( |
| 81 | + path=str(py_dir) |
| 82 | + ) |
| 83 | + assert project_config.programming_language == ProgrammingLanguage.PYTHON |
0 commit comments