Skip to content

Commit 9d22f43

Browse files
github-actions[bot]mohammed ahmed
andcommitted
style: fix linting issues in vitest setupFiles PR
- Move `import re` to module-level (was inside function body) - Add encoding="utf-8" to read_text() call - Fix tests: use tmp_path fixture, add -> None return types, add encoding args Co-authored-by: mohammed ahmed <undefined@users.noreply.github.com>
1 parent 0116a1f commit 9d22f43

2 files changed

Lines changed: 34 additions & 70 deletions

File tree

codeflash/languages/javascript/vitest_runner.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import annotations
88

99
import os
10+
import re
1011
import subprocess
1112
import time
1213
from pathlib import Path
@@ -169,18 +170,17 @@ def _is_vitest_workspace(project_root: Path) -> bool:
169170
return False
170171

171172
try:
172-
content = vitest_config.read_text()
173+
content = vitest_config.read_text(encoding="utf-8")
173174
# Check for actual workspace configuration patterns (not just the word "workspace" in comments)
174175
# Valid indicators:
175176
# - defineWorkspace() function call
176177
# - workspace: [ array config
177178
# - separate vitest.workspace.ts/js file
178-
import re
179179
# Match defineWorkspace calls or workspace: property assignments
180180
workspace_pattern = re.compile(
181-
r'(?:^|[^a-zA-Z_])defineWorkspace\s*\(|' # defineWorkspace( function call
182-
r'(?:^|[^a-zA-Z_])workspace\s*:\s*\[', # workspace: [ array
183-
re.MULTILINE
181+
r"(?:^|[^a-zA-Z_])defineWorkspace\s*\(|" # defineWorkspace( function call
182+
r"(?:^|[^a-zA-Z_])workspace\s*:\s*\[", # workspace: [ array
183+
re.MULTILINE,
184184
)
185185
if workspace_pattern.search(content):
186186
return True
Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
1-
"""Test that Codeflash Vitest config properly handles setupFiles from project config.
2-
3-
This test verifies that when creating a custom Vitest config, setupFiles paths
4-
are converted to absolute paths or cleared to prevent resolution issues in nested directories.
5-
"""
6-
71
from pathlib import Path
8-
import tempfile
9-
import pytest
10-
112

12-
def test_codeflash_vitest_config_overrides_setupfiles():
13-
"""Test that generated config overrides setupFiles to prevent path resolution issues.
3+
import pytest
144

15-
When a project has setupFiles with relative paths, and Codeflash generates tests
16-
for functions in nested directories, those relative paths will resolve incorrectly.
5+
from codeflash.languages.javascript.vitest_runner import _ensure_codeflash_vitest_config
176

18-
The fix: Convert setupFiles paths to absolute, or disable them for generated tests.
19-
"""
20-
from codeflash.languages.javascript.vitest_runner import _ensure_codeflash_vitest_config
217

22-
with tempfile.TemporaryDirectory() as tmpdir:
23-
project_root = Path(tmpdir)
8+
def test_codeflash_vitest_config_overrides_setupfiles(tmp_path: Path) -> None:
9+
project_root = tmp_path.resolve()
2410

25-
# Create a project with setup file
26-
(project_root / "test").mkdir()
27-
setup_file = project_root / "test" / "setup.ts"
28-
setup_file.write_text("// Setup file\n")
11+
# Create a project with setup file
12+
(project_root / "test").mkdir()
13+
(project_root / "test" / "setup.ts").write_text("// Setup file\n", encoding="utf-8")
2914

30-
# Create vitest config with relative setupFiles path
31-
vitest_config = """import { defineConfig } from 'vitest/config';
15+
vitest_config = """import { defineConfig } from 'vitest/config';
3216
3317
export default defineConfig({
3418
test: {
@@ -37,61 +21,41 @@ def test_codeflash_vitest_config_overrides_setupfiles():
3721
},
3822
});
3923
"""
40-
(project_root / "vitest.config.ts").write_text(vitest_config)
41-
42-
# Call the function to create Codeflash config
43-
codeflash_config_path = _ensure_codeflash_vitest_config(project_root)
44-
45-
# Verify the config was created
46-
assert codeflash_config_path is not None
47-
assert codeflash_config_path.exists()
48-
49-
# Read the generated config
50-
config_content = codeflash_config_path.read_text()
24+
(project_root / "vitest.config.ts").write_text(vitest_config, encoding="utf-8")
5125

52-
# The config should either:
53-
# 1. Set setupFiles to an empty array (disable setup files for generated tests)
54-
# 2. OR convert the path to absolute using project root resolution
26+
codeflash_config_path = _ensure_codeflash_vitest_config(project_root)
5527

56-
# Check that setupFiles is mentioned and handled in the merge
57-
assert "setupFiles" in config_content, (
58-
"Generated config must explicitly handle setupFiles to prevent "
59-
"relative path resolution issues. Current config:\n" + config_content
60-
)
28+
assert codeflash_config_path is not None
29+
assert codeflash_config_path.exists()
6130

62-
# The config should set setupFiles to [] or to absolute paths
63-
# This prevents the relative path from being resolved incorrectly
64-
assert ("setupFiles: []" in config_content or
65-
"setupFiles:" in config_content), (
66-
"setupFiles must be explicitly set in the merged config"
67-
)
31+
config_content = codeflash_config_path.read_text(encoding="utf-8")
6832

33+
assert "setupFiles" in config_content, (
34+
"Generated config must explicitly handle setupFiles to prevent "
35+
"relative path resolution issues. Current config:\n" + config_content
36+
)
37+
assert "setupFiles: []" in config_content or "setupFiles:" in config_content, (
38+
"setupFiles must be explicitly set in the merged config"
39+
)
6940

70-
def test_codeflash_vitest_config_without_setupfiles():
71-
"""Test that configs without setupFiles still work correctly."""
72-
from codeflash.languages.javascript.vitest_runner import _ensure_codeflash_vitest_config
7341

74-
with tempfile.TemporaryDirectory() as tmpdir:
75-
project_root = Path(tmpdir)
42+
def test_codeflash_vitest_config_without_setupfiles(tmp_path: Path) -> None:
43+
project_root = tmp_path.resolve()
7644

77-
# Create vitest config WITHOUT setupFiles
78-
vitest_config = """import { defineConfig } from 'vitest/config';
45+
vitest_config = """import { defineConfig } from 'vitest/config';
7946
8047
export default defineConfig({
8148
test: {
8249
include: ["src/**/*.test.ts"],
8350
},
8451
});
8552
"""
86-
(project_root / "vitest.config.ts").write_text(vitest_config)
53+
(project_root / "vitest.config.ts").write_text(vitest_config, encoding="utf-8")
8754

88-
# Call the function to create Codeflash config
89-
codeflash_config_path = _ensure_codeflash_vitest_config(project_root)
55+
codeflash_config_path = _ensure_codeflash_vitest_config(project_root)
9056

91-
# Verify the config was created
92-
assert codeflash_config_path is not None
93-
assert codeflash_config_path.exists()
57+
assert codeflash_config_path is not None
58+
assert codeflash_config_path.exists()
9459

95-
# Config should be created successfully
96-
config_content = codeflash_config_path.read_text()
97-
assert "mergeConfig" in config_content or "defineConfig" in config_content
60+
config_content = codeflash_config_path.read_text(encoding="utf-8")
61+
assert "mergeConfig" in config_content or "defineConfig" in config_content

0 commit comments

Comments
 (0)