Skip to content

Commit 52de770

Browse files
authored
match exclude key exactly when detecting injection (#2444)
match extend key exactly when injecting
1 parent 7bbe763 commit 52de770

4 files changed

Lines changed: 249 additions & 29 deletions

File tree

‎src/hatch/cli/fmt/core.py‎

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,24 @@ def write_config_file(self, *, preview: bool) -> None:
9595

9696
old_contents = self.user_config_file.read_text()
9797
config_path = str(self.internal_config_file).replace("\\", "\\\\")
98-
if self.user_config_file.name == "pyproject.toml":
99-
lines = old_contents.splitlines()
100-
try:
101-
index = lines.index("[tool.ruff]")
102-
except ValueError:
103-
lines.extend((
104-
"",
105-
"[tool.ruff]",
106-
f'extend = "{config_path}"',
107-
))
108-
else:
109-
# Only insert if extend is not already present in the [tool.ruff] section
110-
next_section = next(
111-
(i for i in range(index + 1, len(lines)) if lines[i].startswith("[")),
112-
len(lines),
113-
)
114-
section_lines = lines[index + 1 : next_section]
115-
if not any(line.startswith("extend") for line in section_lines):
98+
99+
if not self.user_config_extends:
100+
if self.user_config_file.name == "pyproject.toml":
101+
lines = old_contents.splitlines()
102+
try:
103+
index = lines.index("[tool.ruff]")
104+
except ValueError:
105+
lines.extend((
106+
"",
107+
"[tool.ruff]",
108+
f'extend = "{config_path}"',
109+
))
110+
else:
116111
lines.insert(index + 1, f'extend = "{config_path}"')
117112

118-
contents = "\n".join(lines)
119-
elif not any(line.startswith("extend") for line in old_contents.splitlines()):
120-
contents = f'extend = "{config_path}"\n{old_contents}'
113+
contents = "\n".join(lines)
114+
else:
115+
contents = f'extend = "{config_path}"\n{old_contents}'
121116
else:
122117
contents = old_contents
123118

@@ -148,6 +143,15 @@ def user_config(self) -> dict[str, Any]:
148143

149144
return load_toml_data(self.user_config_file.read_text())
150145

146+
@cached_property
147+
def user_config_extends(self) -> bool:
148+
"""Returns True if the user configuration extends another file."""
149+
if self.user_config_file is None:
150+
return False
151+
if self.user_config_file.name == "pyproject.toml":
152+
return "extend" in self.user_config.get("tool", {}).get("ruff", {})
153+
return "extend" in self.user_config
154+
151155
@cached_property
152156
def linter_preview(self) -> bool:
153157
return self.get_config("lint").get("preview", False)

‎tests/cli/check/test_check_code.py‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,45 @@ def test_existing_config_with_extend_in_pyproject(self, hatch, temp_dir, config_
420420

421421
# extend must not be duplicated
422422
internal_content = user_config.read_text()
423-
assert internal_content.count("extend") == 1
423+
assert internal_content.count("extend =") == 1
424424
assert existing_extend in internal_content
425425

426+
@pytest.mark.usefixtures("env_run")
427+
def test_existing_config_with_extend_exclude_in_pyproject(self, hatch, temp_dir, config_file, defaults_file_stable):
428+
config_file.model.template.plugins["default"]["tests"] = False
429+
config_file.save()
430+
431+
project_name = "My.App"
432+
433+
with temp_dir.as_cwd():
434+
result = hatch("new", project_name)
435+
436+
assert result.exit_code == 0, result.output
437+
438+
project_path = temp_dir / "my-app"
439+
data_path = temp_dir / "data"
440+
data_path.mkdir()
441+
442+
config_dir = data_path / "env" / ".internal" / "hatch-check-code" / ".config" / project_path.id
443+
default_config = config_dir / "ruff_defaults.toml"
444+
user_config = config_dir / "pyproject.toml"
445+
446+
project_file = project_path / "pyproject.toml"
447+
old_contents = project_file.read_text()
448+
extend_exclude = 'extend-exclude = ["src/bad.py"]'
449+
project_file.write_text(f"[tool.ruff]\n{extend_exclude}\n{old_contents}")
450+
451+
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
452+
result = hatch("check", "code")
453+
454+
assert result.exit_code == 0, result.output
455+
456+
assert default_config.read_text() == defaults_file_stable
457+
458+
internal_content = user_config.read_text()
459+
assert internal_content.count("extend =") == 1
460+
assert extend_exclude in internal_content
461+
426462
@pytest.mark.usefixtures("env_run")
427463
def test_existing_ruff_toml_with_extend(self, hatch, temp_dir, config_file, defaults_file_stable):
428464
config_file.model.template.plugins["default"]["tests"] = False
@@ -456,5 +492,41 @@ def test_existing_ruff_toml_with_extend(self, hatch, temp_dir, config_file, defa
456492

457493
# extend must not be duplicated
458494
internal_content = user_config.read_text()
459-
assert internal_content.count("extend") == 1
495+
assert internal_content.count("extend =") == 1
460496
assert existing_extend in internal_content
497+
498+
@pytest.mark.usefixtures("env_run")
499+
def test_existing_ruff_toml_with_extend_exclude(self, hatch, temp_dir, config_file, defaults_file_stable):
500+
config_file.model.template.plugins["default"]["tests"] = False
501+
config_file.save()
502+
503+
project_name = "My.App"
504+
505+
with temp_dir.as_cwd():
506+
result = hatch("new", project_name)
507+
508+
assert result.exit_code == 0, result.output
509+
510+
project_path = temp_dir / "my-app"
511+
data_path = temp_dir / "data"
512+
data_path.mkdir()
513+
514+
config_dir = data_path / "env" / ".internal" / "hatch-check-code" / ".config" / project_path.id
515+
default_config = config_dir / "ruff_defaults.toml"
516+
user_config = config_dir / "ruff.toml"
517+
518+
extend_exclude = 'extend-exclude = ["src/bad.py"]'
519+
ruff_toml = project_path / "ruff.toml"
520+
ruff_toml.write_text(f"{extend_exclude}\nline-length = 88\n")
521+
522+
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
523+
result = hatch("check", "code")
524+
525+
assert result.exit_code == 0, result.output
526+
527+
assert default_config.read_text() == defaults_file_stable
528+
529+
# extend must not be duplicated
530+
internal_content = user_config.read_text()
531+
assert internal_content.count("extend =") == 1
532+
assert extend_exclude in internal_content

‎tests/cli/check/test_check_fmt.py‎

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,46 @@ def test_existing_config_with_extend_in_pyproject(self, hatch, temp_dir, config_
334334

335335
# extend must not be duplicated
336336
internal_content = user_config.read_text()
337-
assert internal_content.count("extend") == 1
337+
assert internal_content.count("extend =") == 1
338338
assert existing_extend in internal_content
339339

340+
@pytest.mark.usefixtures("env_run")
341+
def test_existing_config_with_extend_exclude_in_pyproject(self, hatch, temp_dir, config_file, defaults_file_stable):
342+
config_file.model.template.plugins["default"]["tests"] = False
343+
config_file.save()
344+
345+
project_name = "My.App"
346+
347+
with temp_dir.as_cwd():
348+
result = hatch("new", project_name)
349+
350+
assert result.exit_code == 0, result.output
351+
352+
project_path = temp_dir / "my-app"
353+
data_path = temp_dir / "data"
354+
data_path.mkdir()
355+
356+
config_dir = data_path / "env" / ".internal" / "hatch-check-fmt" / ".config" / project_path.id
357+
default_config = config_dir / "ruff_defaults.toml"
358+
user_config = config_dir / "pyproject.toml"
359+
360+
project_file = project_path / "pyproject.toml"
361+
old_contents = project_file.read_text()
362+
extend_exclude = 'extend-exclude = ["src/bad.py"]'
363+
project_file.write_text(f"[tool.ruff]\n{extend_exclude}\n{old_contents}")
364+
365+
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
366+
result = hatch("check", "fmt")
367+
368+
assert result.exit_code == 0, result.output
369+
370+
assert default_config.read_text() == defaults_file_stable
371+
372+
# extend must not be duplicated
373+
internal_content = user_config.read_text()
374+
assert internal_content.count("extend =") == 1
375+
assert extend_exclude in internal_content
376+
340377
@pytest.mark.usefixtures("env_run")
341378
def test_existing_ruff_toml_with_extend(self, hatch, temp_dir, config_file, defaults_file_stable):
342379
config_file.model.template.plugins["default"]["tests"] = False
@@ -370,5 +407,41 @@ def test_existing_ruff_toml_with_extend(self, hatch, temp_dir, config_file, defa
370407

371408
# extend must not be duplicated
372409
internal_content = user_config.read_text()
373-
assert internal_content.count("extend") == 1
410+
assert internal_content.count("extend =") == 1
374411
assert existing_extend in internal_content
412+
413+
@pytest.mark.usefixtures("env_run")
414+
def test_existing_ruff_toml_with_extend_exclude(self, hatch, temp_dir, config_file, defaults_file_stable):
415+
config_file.model.template.plugins["default"]["tests"] = False
416+
config_file.save()
417+
418+
project_name = "My.App"
419+
420+
with temp_dir.as_cwd():
421+
result = hatch("new", project_name)
422+
423+
assert result.exit_code == 0, result.output
424+
425+
project_path = temp_dir / "my-app"
426+
data_path = temp_dir / "data"
427+
data_path.mkdir()
428+
429+
config_dir = data_path / "env" / ".internal" / "hatch-check-fmt" / ".config" / project_path.id
430+
default_config = config_dir / "ruff_defaults.toml"
431+
user_config = config_dir / "ruff.toml"
432+
433+
extend_exclude = 'extend-exclude = ["src/bad.py"]'
434+
ruff_toml = project_path / "ruff.toml"
435+
ruff_toml.write_text(f"{extend_exclude}\nline-length = 88\n")
436+
437+
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
438+
result = hatch("check", "fmt")
439+
440+
assert result.exit_code == 0, result.output
441+
442+
assert default_config.read_text() == defaults_file_stable
443+
444+
# extend must not be duplicated
445+
internal_content = user_config.read_text()
446+
assert internal_content.count("extend =") == 1
447+
assert extend_exclude in internal_content

‎tests/cli/check/test_check_group.py‎

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,44 @@ def test_existing_extend_in_pyproject_not_duplicated(self, hatch, temp_dir, conf
101101
internal_pyproject = config_dir / "pyproject.toml"
102102
assert internal_pyproject.is_file(), f"missing {internal_pyproject}"
103103
contents = internal_pyproject.read_text()
104-
assert contents.count("extend") == 1, (
105-
f"{env_name}: 'extend' appears {contents.count('extend')} times, expected 1"
104+
assert contents.count("extend =") == 1, (
105+
f"{env_name}: 'extend =' appears {contents.count('extend =')} times, expected 1"
106+
)
107+
108+
@pytest.mark.usefixtures("env_run")
109+
def test_existing_extend_exclude_in_pyproject(self, hatch, temp_dir, config_file):
110+
config_file.model.template.plugins["default"]["tests"] = False
111+
config_file.save()
112+
113+
project_name = "My.App"
114+
115+
with temp_dir.as_cwd():
116+
result = hatch("new", project_name)
117+
118+
assert result.exit_code == 0, result.output
119+
120+
project_path = temp_dir / "my-app"
121+
data_path = temp_dir / "data"
122+
data_path.mkdir()
123+
124+
pyproject = project_path / "pyproject.toml"
125+
pyproject.write_text(pyproject.read_text() + '\n[tool.ruff]\nextend-exclude = ["src/bad.py"]\n')
126+
127+
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
128+
result = hatch("check")
129+
130+
assert result.exit_code == 0, result.output
131+
132+
for env_name in ("hatch-check-code", "hatch-check-fmt"):
133+
config_dir = data_path / "env" / ".internal" / env_name / ".config" / project_path.id
134+
internal_pyproject = config_dir / "pyproject.toml"
135+
assert internal_pyproject.is_file(), f"missing {internal_pyproject}"
136+
contents = internal_pyproject.read_text()
137+
assert contents.count("extend =") == 1, (
138+
f"{env_name}: 'extend =' appears {contents.count('extend =')} times, expected 1"
139+
)
140+
assert contents.count("extend-exclude") == 1, (
141+
f"{env_name}: 'extend-exclude' appears {contents.count('extend-exclude')} times, expected 1"
106142
)
107143

108144
@pytest.mark.usefixtures("env_run")
@@ -133,6 +169,41 @@ def test_existing_ruff_toml_with_extend_not_duplicated(self, hatch, temp_dir, co
133169
internal_ruff_toml = config_dir / "ruff.toml"
134170
assert internal_ruff_toml.is_file(), f"missing {internal_ruff_toml}"
135171
contents = internal_ruff_toml.read_text()
136-
assert contents.count("extend") == 1, (
137-
f"{env_name}: 'extend' appears {contents.count('extend')} times, expected 1"
172+
assert contents.count("extend =") == 1, (
173+
f"{env_name}: 'extend =' appears {contents.count('extend =')} times, expected 1"
174+
)
175+
176+
@pytest.mark.usefixtures("env_run")
177+
def test_existing_ruff_toml_with_extend_exclude_preserved(self, hatch, temp_dir, config_file):
178+
config_file.model.template.plugins["default"]["tests"] = False
179+
config_file.save()
180+
181+
project_name = "My.App"
182+
183+
with temp_dir.as_cwd():
184+
result = hatch("new", project_name)
185+
186+
assert result.exit_code == 0, result.output
187+
188+
project_path = temp_dir / "my-app"
189+
data_path = temp_dir / "data"
190+
data_path.mkdir()
191+
192+
(project_path / "ruff.toml").write_text('line-length = 88\nextend-exclude = ["src/bad.py"]')
193+
194+
with project_path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
195+
result = hatch("check")
196+
197+
assert result.exit_code == 0, result.output
198+
199+
for env_name in ("hatch-check-code", "hatch-check-fmt"):
200+
config_dir = data_path / "env" / ".internal" / env_name / ".config" / project_path.id
201+
internal_ruff_toml = config_dir / "ruff.toml"
202+
assert internal_ruff_toml.is_file(), f"missing {internal_ruff_toml}"
203+
contents = internal_ruff_toml.read_text()
204+
assert contents.count("extend =") == 1, (
205+
f"{env_name}: 'extend =' appears {contents.count('extend =')} times, expected 1"
206+
)
207+
assert contents.count("extend-exclude =") == 1, (
208+
f"{env_name}: 'extend-exclude =' appears {contents.count('extend =')} times, expected 1"
138209
)

0 commit comments

Comments
 (0)