Files
deepset-ai--haystack/test/test_release_note_backticks.py
Stefano Fiorucci f4c02eb523 chore: Merge main into v3 (#11573)
Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: NIK-TIGER-BILL <59732804+NIK-TIGER-BILL@users.noreply.github.com>
Co-authored-by: Sebastian Husch Lee <10526848+sjrl@users.noreply.github.com>
Co-authored-by: Pragnyan Ramtha <pragnyanramtha@gmail.com>
Co-authored-by: Julian Risch <julian.risch@deepset.ai>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Aarkin Karnik <56022539+Aarkin7@users.noreply.github.com>
Co-authored-by: Haystack Bot <73523382+HaystackBot@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: davidsbatista <7937824+davidsbatista@users.noreply.github.com>
Co-authored-by: bogdankostic <48713846+bogdankostic@users.noreply.github.com>
Co-authored-by: bogdankostic <bogdankostic@web.de>
Co-authored-by: Sachin Yadav <118251564+sachinn854@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Abdulhamid Onawole <93547612+HamidOna@users.noreply.github.com>
Co-authored-by: David S. Batista <dsbatista@gmail.com>
Co-authored-by: Aditya Raut <rautaditya2606@gmail.com>
Co-authored-by: Nhat Huy Vu <150295143+MechaCritter@users.noreply.github.com>
Co-authored-by: julian-risch <4181769+julian-risch@users.noreply.github.com>
Co-authored-by: kota <lakotawilsonproton@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: devteamaegis <devteam.aegis@gmail.com>
Co-authored-by: Ritwij Aryan Parmar <88580521+RitwijParmar@users.noreply.github.com>
Co-authored-by: Srivatsa Kamballa <skamb10@uic.edu>
Co-authored-by: vidigoat <vidit.patankar16@gmail.com>
Co-authored-by: Syed Shahmeer Ali <syedshahmeerali196@gmail.com>
Co-authored-by: Mohamed Arbi <mohammedarbinsibi@gmail.com>
Co-authored-by: Zamuldinov Nikita <59732804+NIK-TIGER-BILL@users.noreply.github.com>
Co-authored-by: Bilge Yücel <bilgeyucel96@gmail.com>
Co-authored-by: jdoughty04 <jonathandoughty04@gmail.com>
Co-authored-by: Govindh Kishore <govindhkishore7@gmail.com>
2026-06-10 14:30:34 +02:00

91 lines
3.5 KiB
Python

# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import importlib.util
import subprocess
import sys
from pathlib import Path
import pytest
_SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "release_note_backticks.py"
_spec = importlib.util.spec_from_file_location("release_note_backticks", _SCRIPT)
_module = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_module)
fix_text = _module.fix_text
class TestFixText:
@pytest.mark.parametrize(
"text, expected",
[
("Use `OpenAIChatGenerator` now.", "Use ``OpenAIChatGenerator`` now."),
("Set `api_key` and `azure_endpoint`.", "Set ``api_key`` and ``azure_endpoint``."),
('Call `Secret.from_env_var("X")`.', 'Call ``Secret.from_env_var("X")``.'),
("Leading `code` token.", "Leading ``code`` token."),
],
)
def test_converts_single_to_double(self, text, expected):
assert fix_text(text) == expected
@pytest.mark.parametrize(
"text",
[
"Already correct: ``OpenAIChatGenerator``.",
"Two literals ``Secret`` and ``api_key``.",
"No inline code at all here.",
"RST role :func:`do_thing` must stay single.",
"Link `Haystack <https://haystack.deepset.ai>`_ must stay single.",
],
)
def test_leaves_valid_rst_untouched(self, text):
assert fix_text(text) == text
def test_only_single_backticks_in_mixed_text_are_converted(self):
text = "Use ``Secret`` for `api_key` and `azure_endpoint`."
expected = "Use ``Secret`` for ``api_key`` and ``azure_endpoint``."
assert fix_text(text) == expected
def test_is_idempotent(self):
once = fix_text("Set `x` and `y`.")
assert fix_text(once) == once
def test_unbalanced_single_backtick_is_left_untouched(self):
# A stray, unpaired backtick is ambiguous, so we never rewrite it.
text = "An unbalanced `backtick stays as is.\n"
assert fix_text(text) == text
class TestCli:
def test_fix_rewrites_file_and_is_idempotent(self, tmp_path):
note = tmp_path / "note.yaml"
note.write_text("enhancements:\n - |\n Use `Foo` and `Bar` now.\n", encoding="utf-8")
first = subprocess.run([sys.executable, str(_SCRIPT), str(note)], capture_output=True, text=True, check=False)
assert first.returncode == 1
assert "``Foo``" in note.read_text(encoding="utf-8")
assert "``Bar``" in note.read_text(encoding="utf-8")
# Running again on the now-fixed file is a no-op and succeeds.
second = subprocess.run([sys.executable, str(_SCRIPT), str(note)], capture_output=True, text=True, check=False)
assert second.returncode == 0
def test_check_mode_reports_without_modifying(self, tmp_path):
note = tmp_path / "note.yaml"
content = "enhancements:\n - |\n Use `Foo` now.\n"
note.write_text(content, encoding="utf-8")
result = subprocess.run(
[sys.executable, str(_SCRIPT), "--check", str(note)], capture_output=True, text=True, check=False
)
assert result.returncode == 1
assert note.read_text(encoding="utf-8") == content
def test_clean_file_passes(self, tmp_path):
note = tmp_path / "note.yaml"
note.write_text("enhancements:\n - |\n Use ``Foo`` now.\n", encoding="utf-8")
result = subprocess.run([sys.executable, str(_SCRIPT), str(note)], capture_output=True, text=True, check=False)
assert result.returncode == 0