Coverage for tests / integration / tools / svelte_check / conftest.py: 68%

38 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Pytest configuration for svelte-check integration tests.""" 

2 

3from __future__ import annotations 

4 

5import shutil 

6import subprocess 

7from collections.abc import Sequence 

8from pathlib import Path 

9 

10import pytest 

11 

12 

13def _check_command_version(cmd: Sequence[str], timeout: int) -> bool: 

14 """Run a version command and return whether it succeeded. 

15 

16 Args: 

17 cmd: Command and arguments to execute. 

18 timeout: Timeout in seconds for the subprocess. 

19 

20 Returns: 

21 True if the command exits with returncode 0, False otherwise. 

22 """ 

23 try: 

24 result = subprocess.run( 

25 cmd, 

26 capture_output=True, 

27 timeout=timeout, 

28 check=False, 

29 ) 

30 return result.returncode == 0 

31 except (subprocess.TimeoutExpired, OSError): 

32 return False 

33 

34 

35def svelte_check_is_available() -> bool: 

36 """Check if svelte-check is installed and actually works. 

37 

38 This checks both that the command exists AND that it executes successfully, 

39 which handles cases where a wrapper script exists but the underlying 

40 tool isn't installed. Also checks bunx/npx fallbacks. 

41 

42 Returns: 

43 True if svelte-check is installed and working, False otherwise. 

44 """ 

45 if shutil.which("svelte-check") is not None and _check_command_version( 

46 ["svelte-check", "--version"], 

47 timeout=10, 

48 ): 

49 return True 

50 

51 if shutil.which("bunx") is not None and _check_command_version( 

52 ["bunx", "svelte-check", "--version"], 

53 timeout=30, 

54 ): 

55 return True 

56 

57 return shutil.which("npx") is not None and _check_command_version( 

58 ["npx", "svelte-check", "--version"], 

59 timeout=30, 

60 ) 

61 

62 

63def _find_project_root() -> Path: 

64 """Find project root by looking for pyproject.toml. 

65 

66 Returns: 

67 Path to the project root directory. 

68 

69 Raises: 

70 RuntimeError: If pyproject.toml is not found in any parent directory. 

71 """ 

72 path = Path(__file__).resolve() 

73 for parent in path.parents: 

74 if (parent / "pyproject.toml").exists(): 

75 return parent 

76 raise RuntimeError("pyproject.toml not found in parent directories") 

77 

78 

79# Paths to test samples 

80SAMPLE_DIR = _find_project_root() / "test_samples" 

81SVELTE_CHECK_SAMPLES = SAMPLE_DIR / "tools" / "web" / "svelte_check" 

82CLEAN_SAMPLE = SVELTE_CHECK_SAMPLES / "svelte_check_clean.svelte" 

83VIOLATION_SAMPLE = SVELTE_CHECK_SAMPLES / "svelte_check_violations.svelte" 

84 

85 

86@pytest.fixture 

87def svelte_check_violation_file(tmp_path: Path) -> str: 

88 """Copy the svelte-check violation sample to a temp directory. 

89 

90 Args: 

91 tmp_path: Pytest fixture providing a temporary directory. 

92 

93 Returns: 

94 Path to the copied file as a string. 

95 """ 

96 dst = tmp_path / "svelte_check_violations.svelte" 

97 shutil.copy(VIOLATION_SAMPLE, dst) 

98 return str(dst) 

99 

100 

101@pytest.fixture 

102def svelte_check_clean_file(tmp_path: Path) -> str: 

103 """Copy the svelte-check clean sample to a temp directory. 

104 

105 Args: 

106 tmp_path: Pytest fixture providing a temporary directory. 

107 

108 Returns: 

109 Path to the copied file as a string. 

110 """ 

111 dst = tmp_path / "svelte_check_clean.svelte" 

112 shutil.copy(CLEAN_SAMPLE, dst) 

113 return str(dst)