Skip to content

Commit

Permalink
Merge pull request #379 from neutrinoceros/bld/include_conftest_sdist
Browse files Browse the repository at this point in the history
BLD: ensure conftest.py is included in source distributions
  • Loading branch information
neutrinoceros authored Feb 5, 2025
2 parents 6ee5f27 + 78ce06d commit 5266625
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- uses: astral-sh/setup-uv@4db96194c378173c656ce18a155ffc14a9fc4355 # v5.2.2
- name: Build distributions
shell: bash -l {0}
run: uv build lib/inifix
run: uv build --package inifix

- name: Publish package distributions to PyPI
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
Expand Down
14 changes: 1 addition & 13 deletions conftest.py → lib/inifix/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sys
from pathlib import Path

import pytest

DATA_DIR = Path(__file__).parent / "lib" / "inifix" / "tests" / "data"
DATA_DIR = Path(__file__).parent / "data"
INIFILES_PATHS = list(DATA_DIR.glob("*.ini")) + list(DATA_DIR.glob("*.cfg"))
INIFILES_IDS = [inifile.name[:-4] for inifile in INIFILES_PATHS]

Expand Down Expand Up @@ -37,14 +36,3 @@ def inifile_with_sections(request):
@pytest.fixture(params=INIFILES_WO_SECTIONS.keys(), ids=INIFILES_WO_SECTIONS.values())
def inifile_without_sections(request):
return request.param


def pytest_report_header(config, start_path):
if sys.version_info >= (3, 13):
is_gil_enabled = sys._is_gil_enabled()
else:
is_gil_enabled = True

return [
f"{is_gil_enabled = }",
]
31 changes: 31 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
from pathlib import Path

import pytest

DATA_DIR = Path(__file__).parents[1] / "lib" / "inifix" / "tests" / "data"
INIFILES_PATHS = list(DATA_DIR.glob("*.ini")) + list(DATA_DIR.glob("*.cfg"))
INIFILES_IDS = [inifile.name[:-4] for inifile in INIFILES_PATHS]

INIFILES = dict(zip(INIFILES_PATHS, INIFILES_IDS, strict=True))


@pytest.fixture()
def datadir_root():
return DATA_DIR


@pytest.fixture(params=INIFILES.keys(), ids=INIFILES.values())
def inifile_root(request):
return request.param


def pytest_report_header(config, start_path):
if sys.version_info >= (3, 13):
is_gil_enabled = sys._is_gil_enabled()
else:
is_gil_enabled = True

return [
f"{is_gil_enabled = }",
]
66 changes: 33 additions & 33 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@


@pytest.fixture
def unformatted_files(datadir, tmp_path):
in_file = datadir / "format-in.ini"
def unformatted_files(datadir_root, tmp_path):
in_file = datadir_root / "format-in.ini"
files = []
for file_no in range(N_FILES):
new_file = tmp_path / f"{file_no}.ini"
Expand Down Expand Up @@ -83,10 +83,10 @@ def test_invalid_files(self, invalid_file):
assert result.stdout.startswith("Failed to validate")
assert result.exit_code != 0

def test_valid_files(self, inifile):
result = runner.invoke(app, ["validate", str(inifile)])
def test_valid_files(self, inifile_root):
result = runner.invoke(app, ["validate", str(inifile_root)])

assert result.stdout == f"Validated {inifile}\n"
assert result.stdout == f"Validated {inifile_root}\n"
assert result.stderr == ""
assert result.exit_code == 0

Expand All @@ -102,11 +102,11 @@ def test_concurrency(self, unformatted_files, capsys):

class TestFormat:
@pytest.mark.parametrize("args", [(), ("--skip-validation",)])
def test_format_keep_data(self, args, datadir, inifile, tmp_path):
target = tmp_path / inifile.name
def test_format_keep_data(self, args, datadir_root, inifile_root, tmp_path):
target = tmp_path / inifile_root.name

ref_data = inifix.load(inifile)
shutil.copyfile(inifile, target)
ref_data = inifix.load(inifile_root)
shutil.copyfile(inifile_root, target)

result = runner.invoke(app, ["format", str(target), *args])
assert isinstance(result.exit_code, int)
Expand All @@ -118,7 +118,7 @@ def test_format_keep_data(self, args, datadir, inifile, tmp_path):
"infile, expect_diff",
[("format-in.ini", True), ("format-out.ini", False)],
)
def test_exact_format_diff(self, datadir, infile, expect_diff):
def test_exact_format_diff(self, datadir_root, infile, expect_diff):
def diff_file(f1: Path, f2: Path) -> str:
return (
"\n".join(
Expand All @@ -132,30 +132,30 @@ def diff_file(f1: Path, f2: Path) -> str:
+ "\n"
)

body = (datadir / infile).read_text()
body = (datadir_root / infile).read_text()

result = runner.invoke(app, ["format", str(datadir / infile), "--diff"])
result = runner.invoke(app, ["format", str(datadir_root / infile), "--diff"])
if expect_diff:
assert result.exit_code != 0
expected = diff_file(datadir / infile, datadir / "format-out.ini")
expected = diff_file(datadir_root / infile, datadir_root / "format-out.ini")
assert result.stdout == expected
assert result.stderr == ""
else:
assert result.exit_code == 0
assert result.stdout == ""
assert result.stderr == ""

assert (datadir / infile).read_text() == body
assert (datadir_root / infile).read_text() == body

def test_exact_format_inplace(self, datadir, tmp_path):
def test_exact_format_inplace(self, datadir_root, tmp_path):
target = tmp_path / "result.stdout.ini"
shutil.copyfile(datadir / "format-in.ini", target)
shutil.copyfile(datadir_root / "format-in.ini", target)

result = runner.invoke(app, ["format", str(target)])
assert result.exit_code != 0
assert_text_equal(result.stdout, f"Fixing {target}")

expected = (datadir / "format-out.ini").read_text()
expected = (datadir_root / "format-out.ini").read_text()
res = target.read_text()
assert res == expected

Expand Down Expand Up @@ -203,9 +203,9 @@ def test_empty_file(self, tmp_path):
result.stdout, f"Error: {str(target)!r} appears to be empty.\n"
)

def test_error_read_only_file(self, inifile, tmp_path):
target = tmp_path / inifile.name
shutil.copy(inifile, target)
def test_error_read_only_file(self, inifile_root, tmp_path):
target = tmp_path / inifile_root.name
shutil.copy(inifile_root, target)

data = target.read_text()
if inifix.format_string(data) == data:
Expand All @@ -220,9 +220,9 @@ def test_error_read_only_file(self, inifile, tmp_path):
f"Error: could not write to {target} (permission denied)\n",
)

def test_diff_stdout(self, inifile, tmp_path):
target = tmp_path / inifile.name
shutil.copy(inifile, target)
def test_diff_stdout(self, inifile_root, tmp_path):
target = tmp_path / inifile_root.name
shutil.copy(inifile_root, target)

result = runner.invoke(app, ["format", str(target), "--diff"])
# we can't predict if formatting is needed
Expand All @@ -233,11 +233,11 @@ def test_diff_stdout(self, inifile, tmp_path):
else:
assert result.stdout != ""

def test_report_noop(self, datadir, tmp_path):
inifile = datadir / "format-out.ini"
target = tmp_path / inifile.name
def test_report_noop(self, datadir_root, tmp_path):
inifile_root = datadir_root / "format-out.ini"
target = tmp_path / inifile_root.name

shutil.copyfile(inifile, target)
shutil.copyfile(inifile_root, target)

result = runner.invoke(app, ["format", str(target), "--report-noop"])
assert result.exit_code == 0
Expand All @@ -254,11 +254,11 @@ def test_format_quoted_strings_with_whitespaces(self, tmp_path):

assert target.read_text() == expected

def test_data_preservation(self, inifile, tmp_path):
def test_data_preservation(self, inifile_root, tmp_path):
# check that perilous string manipulations do not destroy data
initial_mapping = inifix.load(inifile)
target = tmp_path / inifile.name
shutil.copyfile(inifile, target)
initial_mapping = inifix.load(inifile_root)
target = tmp_path / inifile_root.name
shutil.copyfile(inifile_root, target)
runner.invoke(app, ["format", str(target)])
round_mapping = inifix.load(target)
assert round_mapping == initial_mapping
Expand All @@ -273,15 +273,15 @@ def test_single_core(self, monkeypatch, tmp_path):
monkeypatch.setattr(inifix_cli, "get_cpu_count", lambda: 1)
runner.invoke(app, ["format", str(target)])

def test_concurrency(self, datadir, unformatted_files):
def test_concurrency(self, datadir_root, unformatted_files):
result = runner.invoke(app, ["format", *(str(f) for f in unformatted_files)])
assert result.exit_code != 0

# order of lines doesn't matter and is not guaranteed
err_lines = result.stdout.splitlines()
assert set(err_lines) == {f"Fixing {file}" for file in unformatted_files}

expected = (datadir / "format-out.ini").read_text()
expected = (datadir_root / "format-out.ini").read_text()
for file in unformatted_files:
body = file.read_text()
assert body == expected

0 comments on commit 5266625

Please sign in to comment.