Skip to content

Commit 515145a

Browse files
committed
ci: update files according to other projects
1 parent 9e4ea1a commit 515145a

File tree

10 files changed

+45
-27
lines changed

10 files changed

+45
-27
lines changed

conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
# resolve_package_path is called from _pytest.pathlib.import_path
2121
# takes a full abs path to the test file and needs to return the path to the 'root' package on the filesystem
2222
resolve_pkg_path_orig = _pytest.pathlib.resolve_package_path
23+
24+
2325
def resolve_package_path(path: pathlib.Path) -> Optional[pathlib.Path]:
2426
result = path # search from the test file upwards
2527
for parent in result.parents:
@@ -30,6 +32,8 @@ def resolve_package_path(path: pathlib.Path) -> Optional[pathlib.Path]:
3032
if path.name == 'conftest.py':
3133
return resolve_pkg_path_orig(path)
3234
raise RuntimeError("Couldn't determine path for ", path)
35+
36+
3337
_pytest.pathlib.resolve_package_path = resolve_package_path
3438

3539

@@ -38,10 +42,14 @@ def resolve_package_path(path: pathlib.Path) -> Optional[pathlib.Path]:
3842
# so we need to point it at the absolute path properly
3943
# not sure what are the consequences.. maybe it wouldn't be able to run against installed packages? not sure..
4044
search_pypath_orig = _pytest.main.search_pypath
45+
46+
4147
def search_pypath(module_name: str) -> str:
4248
mpath = root_dir / module_name.replace('.', os.sep)
4349
if not mpath.is_dir():
4450
mpath = mpath.with_suffix('.py')
4551
assert mpath.exists(), mpath # just in case
4652
return str(mpath)
53+
54+
4755
_pytest.main.search_pypath = search_pypath

old/setup.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
11
# see https://github.com/karlicoss/pymplate for up-to-date reference
22

33

4-
from setuptools import setup, find_namespace_packages # type: ignore
4+
from setuptools import setup, find_namespace_packages # type: ignore
55

66

77
def main() -> None:
88
# works with both ordinary and namespace packages
99
pkgs = find_namespace_packages('src')
10-
pkg = min(pkgs) # lexicographically smallest is the correct one usually?
10+
pkg = min(pkgs) # lexicographically smallest is the correct one usually?
1111
setup(
1212
name=pkg,
1313
use_scm_version={
1414
'version_scheme': 'python-simplified-semver',
1515
'local_scheme': 'dirty-tag',
1616
},
1717
setup_requires=['setuptools_scm'],
18-
1918
# otherwise mypy won't work
2019
# https://mypy.readthedocs.io/en/stable/installed_packages.html#making-pep-561-compatible-packages
2120
zip_safe=False,
22-
2321
packages=pkgs,
2422
package_dir={'': 'src'},
2523
# necessary so that package works with mypy
2624
package_data={pkg: ['py.typed']},
27-
2825
## ^^^ this should be mostly automatic and not requiring any changes
29-
3026
install_requires=[
3127
# vvv example of git repo dependency
3228
# 'repo @ git+https://github.com/karlicoss/repo.git',
33-
3429
# vvv example of local file dependency. yes, DUMMY is necessary for some reason
3530
# 'repo @ git+file://DUMMY/path/to/repo',
3631
],
3732
extras_require={
3833
'testing': ['pytest'],
39-
'linting': ['pytest', 'ruff', 'mypy', 'lxml'], # lxml for mypy coverage report
34+
'linting': ['pytest', 'ruff', 'mypy', 'lxml'], # lxml for mypy coverage report
4035
},
41-
42-
4336
# this needs to be set if you're planning to upload to pypi
4437
# url='',
4538
# author='',
4639
# author_email='',
4740
# description='',
48-
4941
# Rest of the stuff -- classifiers, license, etc, I don't think it matters for pypi
5042
# it's just unnecessary duplication
5143
)
5244

5345

5446
if __name__ == '__main__':
5547
main()
56-

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# see https://github.com/karlicoss/pymplate for up-to-date reference
22
[project]
3-
dynamic = ["version"] # version is managed by setuptools_scm
3+
dynamic = ["version"] # version is managed by build backend
44
name = "karlicoss_pymplate"
55
dependencies = [
66
]

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
[pytest]
22
# discover files that don't follow test_ naming. Useful to keep tests along with the source code
33
python_files = *.py
4+
5+
# this setting only impacts package/module naming under pytest, not the discovery
6+
consider_namespace_packages = true
7+
48
addopts =
59
# prevent pytest cache from being created... it craps into project dir and I never use it anyway
610
-p no:cacheprovider

ruff.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ lint.ignore = [
137137
"SIM", # some if statements crap
138138
"RSE102", # complains about missing parens in exceptions
139139
##
140+
]
141+
140142

141-
"RUF001", # complains about unicode.. but we're legit using it in tests
143+
lint.exclude = [
144+
"old/**",
145+
"tests/testdata/**",
142146
]

src/karlicoss_pymplate/module.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
def test_module_name() -> None:
10+
assert __package__ == 'karlicoss_pymplate'
1011
# just in case because pytest might have some shenanigans with test module names depending on args..
1112
assert __name__ == 'karlicoss_pymplate.module'
1213

src/karlicoss_pymplate/subpackage/submodule.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ def test() -> None:
44

55
# check names because pytest might mess this up (also see conftest.py for some hacks)
66
assert __package__ == 'karlicoss_pymplate.subpackage'
7-
assert __name__ == 'karlicoss_pymplate.subpackage.submodule'
7+
assert __name__ == 'karlicoss_pymplate.subpackage.submodule'
88

99

1010
## just check that mypy works
1111
class A:
1212
pass
1313

14+
1415
import typing
1516

1617
if typing.TYPE_CHECKING:

tests/test_pytest.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
#!/usr/bin/env python3
22
from __future__ import annotations
3+
34
# not using pytest here to avoid interference from actual pytest
45
import contextlib
56
import os
6-
from pathlib import Path
77
import re
8+
import shlex
89
import shutil
9-
import sys
1010
import subprocess
11+
import sys
12+
from collections.abc import Iterator
13+
from pathlib import Path
1114
from tempfile import TemporaryDirectory
12-
from typing import Iterator
1315

1416
THISDIR = Path(__file__).absolute().parent
1517
GIT_ROOT = THISDIR.parent
1618

19+
1720
# only available since python 3.11
1821
class contextlib_chdir(contextlib.AbstractContextManager):
1922
def __init__(self, path):
2023
self.path = path
2124
self._old_cwd = []
2225

2326
def __enter__(self):
24-
self._old_cwd.append(os.getcwd())
27+
self._old_cwd.append(Path.cwd())
2528
os.chdir(self.path)
2629

2730
def __exit__(self, *excinfo):
@@ -32,21 +35,25 @@ def __exit__(self, *excinfo):
3235
def fixture() -> Iterator[Path]:
3336
with TemporaryDirectory() as td:
3437
root = Path(td)
35-
shutil.copy(GIT_ROOT / 'pytest.ini' , root / 'pytest.ini')
38+
shutil.copy(GIT_ROOT / 'pytest.ini', root / 'pytest.ini')
3639
shutil.copy(GIT_ROOT / 'conftest.py', root / 'conftest.py')
40+
# TODO overwrite consider namesspace pkgs here for now??
3741
shutil.copytree(THISDIR / 'testdata' / 'src', root / 'src')
3842
with contextlib_chdir(root):
3943
yield root
4044

45+
4146
def run(package: str) -> list[str]:
4247
# returns the tests that ran
43-
output = subprocess.check_output([sys.executable, '-m', 'pytest', '--pyargs', package, '-s'], text=True, stderr=subprocess.STDOUT)
48+
cmd = [sys.executable, '-m', 'pytest', '--pyargs', package, '-s']
49+
print('pytest command: ', shlex.join(cmd), file=sys.stderr)
50+
output = subprocess.check_output(cmd, text=True, stderr=subprocess.STDOUT, env={'PYTHONPATH': 'src', **os.environ})
4451
return sorted(re.findall(r'RUNNING ([\w.]+)', output))
4552

4653

4754
def test_basic() -> None:
4855
# should collect/run all tests
49-
with fixture() as root:
56+
with fixture() as _root:
5057
res = run('mypkg')
5158
assert res == [
5259
'doctest',
@@ -59,7 +66,7 @@ def test_basic() -> None:
5966

6067

6168
def test_subpackage_1() -> None:
62-
with fixture() as root:
69+
with fixture() as _root:
6370
res = run('mypkg.a')
6471
assert res == [
6572
'mypkg.a.aa.main',
@@ -68,15 +75,15 @@ def test_subpackage_1() -> None:
6875

6976

7077
def test_subpackage_2() -> None:
71-
with fixture() as root:
78+
with fixture() as _root:
7279
res = run('mypkg.a.aa')
7380
assert res == [
7481
'mypkg.a.aa.main',
7582
], res
7683

7784

7885
def test_module_1() -> None:
79-
with fixture() as root:
86+
with fixture() as _root:
8087
res = run('mypkg.main')
8188
assert res == [
8289
'doctest',
@@ -86,7 +93,7 @@ def test_module_1() -> None:
8693

8794
def test_module_2() -> None:
8895
# checks that it works with __init__.py
89-
with fixture() as root:
96+
with fixture() as _root:
9097
res = run('mypkg.c')
9198
assert res == [
9299
'mypkg.c',
@@ -105,5 +112,6 @@ def main() -> None:
105112
print("RUNNING", test)
106113
test()
107114

115+
108116
if __name__ == '__main__':
109117
main()

tests/testdata/src/mypkg/a/aa/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ...b import for_aa
22

3+
34
def test():
45
print("RUNNING", __name__)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ package = uv-editable
2626
skip_install = true
2727
dependency_groups = testing
2828
commands =
29-
{envpython} -m ruff check src/ \
29+
{envpython} -m ruff check \
3030
{posargs}
3131

3232

0 commit comments

Comments
 (0)