|
19 | 19 | HelperFunction, |
20 | 20 | Language, |
21 | 21 | ParentInfo, |
22 | | - ReferenceInfo, |
23 | 22 | TestInfo, |
24 | 23 | TestResult, |
25 | 24 | ) |
|
29 | 28 | if TYPE_CHECKING: |
30 | 29 | from collections.abc import Sequence |
31 | 30 |
|
| 31 | + from codeflash.languages.base import ReferenceInfo |
32 | 32 | from codeflash.languages.treesitter_utils import TypeDefinition |
33 | 33 |
|
34 | 34 | logger = logging.getLogger(__name__) |
@@ -966,11 +966,7 @@ def find_helper_functions(self, function: FunctionInfo, project_root: Path) -> l |
966 | 966 | return [] |
967 | 967 |
|
968 | 968 | def find_references( |
969 | | - self, |
970 | | - function: FunctionInfo, |
971 | | - project_root: Path, |
972 | | - tests_root: Path | None = None, |
973 | | - max_files: int = 500, |
| 969 | + self, function: FunctionInfo, project_root: Path, tests_root: Path | None = None, max_files: int = 500 |
974 | 970 | ) -> list[ReferenceInfo]: |
975 | 971 | """Find all references (call sites) to a function across the codebase. |
976 | 972 |
|
@@ -1835,51 +1831,98 @@ def get_module_path(self, source_file: Path, project_root: Path, tests_root: Pat |
1835 | 1831 | rel_path = source_file.relative_to(project_root) |
1836 | 1832 | return "../" + rel_path.with_suffix("").as_posix() |
1837 | 1833 |
|
| 1834 | + def verify_requirements(self, project_root: Path, test_framework: str = "jest") -> tuple[bool, list[str]]: |
| 1835 | + """Verify that all JavaScript requirements are met. |
| 1836 | +
|
| 1837 | + Checks for: |
| 1838 | + 1. Node.js installation |
| 1839 | + 2. npm availability |
| 1840 | + 3. Test framework (jest/vitest) installation |
| 1841 | + 4. node_modules existence |
| 1842 | +
|
| 1843 | + Args: |
| 1844 | + project_root: The project root directory. |
| 1845 | + test_framework: The test framework to check for ("jest" or "vitest"). |
| 1846 | +
|
| 1847 | + Returns: |
| 1848 | + Tuple of (success, list of error messages). |
| 1849 | +
|
| 1850 | + """ |
| 1851 | + errors: list[str] = [] |
| 1852 | + |
| 1853 | + # Check Node.js |
| 1854 | + try: |
| 1855 | + result = subprocess.run(["node", "--version"], check=False, capture_output=True, text=True, timeout=10) |
| 1856 | + if result.returncode != 0: |
| 1857 | + errors.append("Node.js is not installed. Please install Node.js 18+ from https://nodejs.org/") |
| 1858 | + except FileNotFoundError: |
| 1859 | + errors.append("Node.js is not installed. Please install Node.js 18+ from https://nodejs.org/") |
| 1860 | + except Exception as e: |
| 1861 | + errors.append(f"Failed to check Node.js: {e}") |
| 1862 | + |
| 1863 | + # Check npm |
| 1864 | + try: |
| 1865 | + result = subprocess.run(["npm", "--version"], check=False, capture_output=True, text=True, timeout=10) |
| 1866 | + if result.returncode != 0: |
| 1867 | + errors.append("npm is not available. Please ensure npm is installed with Node.js.") |
| 1868 | + except FileNotFoundError: |
| 1869 | + errors.append("npm is not available. Please ensure npm is installed with Node.js.") |
| 1870 | + except Exception as e: |
| 1871 | + errors.append(f"Failed to check npm: {e}") |
| 1872 | + |
| 1873 | + # Check node_modules exists |
| 1874 | + node_modules = project_root / "node_modules" |
| 1875 | + if not node_modules.exists(): |
| 1876 | + errors.append( |
| 1877 | + f"node_modules not found in {project_root}. Please run 'npm install' to install dependencies." |
| 1878 | + ) |
| 1879 | + else: |
| 1880 | + # Check test framework is installed |
| 1881 | + framework_path = node_modules / test_framework |
| 1882 | + if not framework_path.exists(): |
| 1883 | + errors.append( |
| 1884 | + f"{test_framework} is not installed. " |
| 1885 | + f"Please run 'npm install --save-dev {test_framework}' to install it." |
| 1886 | + ) |
| 1887 | + |
| 1888 | + return len(errors) == 0, errors |
| 1889 | + |
1838 | 1890 | def ensure_runtime_environment(self, project_root: Path) -> bool: |
1839 | 1891 | """Ensure codeflash npm package is installed. |
1840 | 1892 |
|
1841 | 1893 | Attempts to install the npm package for test instrumentation. |
1842 | | - Falls back to copying files if npm install fails. |
1843 | 1894 |
|
1844 | 1895 | Args: |
1845 | 1896 | project_root: The project root directory. |
1846 | 1897 |
|
1847 | 1898 | Returns: |
1848 | | - True if npm package is installed, False if falling back to file copy. |
| 1899 | + True if npm package is installed, False otherwise. |
1849 | 1900 |
|
1850 | 1901 | """ |
1851 | | - import subprocess |
1852 | | - |
1853 | 1902 | from codeflash.cli_cmds.console import logger |
1854 | 1903 |
|
1855 | | - # Check if package is already installed |
1856 | 1904 | node_modules_pkg = project_root / "node_modules" / "codeflash" |
1857 | 1905 | if node_modules_pkg.exists(): |
1858 | 1906 | logger.debug("codeflash already installed") |
1859 | 1907 | return True |
1860 | 1908 |
|
1861 | | - # Try to install from local package first (for development) |
1862 | | - local_package_path = Path(__file__).parent.parent.parent.parent / "packages" / "cli" |
1863 | | - if local_package_path.exists(): |
1864 | | - try: |
1865 | | - result = subprocess.run( |
1866 | | - ["npm", "install", "--save-dev", str(local_package_path)], |
1867 | | - check=False, |
1868 | | - cwd=project_root, |
1869 | | - capture_output=True, |
1870 | | - text=True, |
1871 | | - timeout=120, |
1872 | | - ) |
1873 | | - if result.returncode == 0: |
1874 | | - logger.debug("Installed codeflash from local package") |
1875 | | - return True |
1876 | | - logger.warning(f"Failed to install local package: {result.stderr}") |
1877 | | - except Exception as e: |
1878 | | - logger.warning(f"Error installing local package: {e}") |
1879 | | - |
1880 | | - # Could try npm registry here in the future: |
1881 | | - # subprocess.run(["npm", "install", "--save-dev", "codeflash"], ...) |
| 1909 | + try: |
| 1910 | + result = subprocess.run( |
| 1911 | + ["npm", "install", "--save-dev", "codeflash"], |
| 1912 | + check=False, |
| 1913 | + cwd=project_root, |
| 1914 | + capture_output=True, |
| 1915 | + text=True, |
| 1916 | + timeout=120, |
| 1917 | + ) |
| 1918 | + if result.returncode == 0: |
| 1919 | + logger.debug("Installed codeflash from npm registry") |
| 1920 | + return True |
| 1921 | + logger.warning(f"Failed to install codeflash: {result.stderr}") |
| 1922 | + except Exception as e: |
| 1923 | + logger.warning(f"Error installing codeflash: {e}") |
1882 | 1924 |
|
| 1925 | + logger.error("Could not install codeflash. Please run: npm install --save-dev codeflash") |
1883 | 1926 | return False |
1884 | 1927 |
|
1885 | 1928 | def instrument_existing_test( |
|
0 commit comments