Skip to content

Commit

Permalink
Move all files for main project and plugins into a single repo to mak…
Browse files Browse the repository at this point in the history
…e it easier for testing.
  • Loading branch information
tdenewiler committed Apr 7, 2024
1 parent 7c298d8 commit a82bbc5
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
**/*.egg-info/
**/.output-py*/
**/.tox/
**/build/
*.code-workspace
__pycache__
venv/
96 changes: 96 additions & 0 deletions tox-pyproject-plugins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Tox Pyproject Plugins

Example project with plugins.

Make sure to have a virtual environment where the `tox-pyproject` package has already been installed.
The directory structure used looks like:

- ~/src/tox-pyproject (git repo)
- tox-pyproject
- tox-pyproject-plugins
- venv

Create and activate a virtual environment, then install the `tox-pyproject` package.

```shell
cd ~/src/tox-pyproject
python3 -m venv venv
. venv/bin/activate
cd tox-pyproject
pip install .
```

Install the `tox-pyproject-plugins` package and make sure that both `a` and `b` plugins are discovered.

```shell
cd ~/src/tox-pyproject/tox-pyproject-plugins
pip install .
tox_project
```

Expected results are

```shell
$ tox_pyproject
discovered plugins: [EntryPoint(name='a', value='tox_pyproject.plugins.discovery.a:ADiscoveryPlugin', group='tox_pyproject.plugins.discovery'), EntryPoint(name='b', value='tox_pyproject.plugins.discovery.b:BDiscoveryPlugin', group='tox_pyproject.plugins.discovery')]
start
---Discovery---
Running A discovery plugin...
A discovery plugin done.
Running B discovery plugin...
B discovery plugin done.
All plugins run: ['A', 'B']
---Discovery---
success: True
```

Running `tox-pyproject-plugins` unit tests with `pytest` works.

```shell
cd ~/src/tox-pyproject/tox-pyproject-plugins
pip install .
pip install .[test]
pytest tests/
```

Running the unit tests with `tox` does not work.

```shell
tox
```

There are errors related to `ModuleNotFoundError` imported from the `tox-pyproject` package.

```shell
_______________________________________________________________ ERROR collecting tests/b/test_b.py _______________________________________________________________
ImportError while importing test module '/home/thomas/src/tox-pyproject/tox-pyproject-plugins/tests/b/test_b.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
../tests/b/test_b.py:5: in <module>
from tox_pyproject.plugins.discovery.b import BDiscoveryPlugin
../src/tox_pyproject/plugins/discovery/b.py:3: in <module>
from tox_pyproject.config import Config
E ModuleNotFoundError: No module named 'tox_pyproject.config'
```
The missing module is in the tox virtual environment.
```shell
$ ls ~/src/tox-pyproject/tox-pyproject-plugins/.tox/py310/lib/python3.10/site-packages/tox_pyproject/
app.py config.py __init__.py plugins __pycache__ tox_pyproject.py
```
After running tox once (so the tox virtual environment is created) it is possible to run the import command that failed
inside the tox virtual environment.
```shell
deactivate # from original virtual environment
. .tox/py310/bin/activate
python
from tox_pyproject.config import Config
```
It is not clear why the module is available in the tox virtual environment, but is not found when running the `tox`
command.
42 changes: 42 additions & 0 deletions tox-pyproject-plugins/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "tox-pyproject-plugins"
authors = [{name = "X"}]
description="Y"
version = "0.0.1"
readme = "README.md"
requires-python = ">=3.8"
license = {text = "CC0-1.0"}
classifiers = [
"License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Software Development :: Testing",
]

dependencies = [
"tox-pyproject @ file:https:///home/thomas/src/tox-pyproject/tox-pyproject",
]

[project.entry-points."tox_pyproject.plugins.discovery"]
b = "tox_pyproject.plugins.discovery.b:BDiscoveryPlugin"

[project.optional-dependencies]
test = [
"PyYAML",
"coverage",
"lark",
"pytest",
"pytest-cov",
"tox",
]

[tool.isort]
profile = "black"
1 change: 1 addition & 0 deletions tox-pyproject-plugins/src/tox_pyproject/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Statick tool."""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""TeX plugins for Statick."""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""TeX file discovery plugin."""
19 changes: 19 additions & 0 deletions tox-pyproject-plugins/src/tox_pyproject/plugins/discovery/b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Discover B files to analyze."""

from tox_pyproject.config import Config


class BDiscoveryPlugin():
"""Discover B files to analyze."""

def get_name(self) -> str:
"""Get name of discovery type."""
return "B"

def scan(self) -> list[str]:
"""Scan package looking for B files."""
config = Config()
b_files: list[str] = []
b_files.append(config.default_level)

return b_files
29 changes: 29 additions & 0 deletions tox-pyproject-plugins/tests/b/test_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Unit tests for the B discovery plugin."""

import sys

from tox_pyproject.plugins.discovery.b import BDiscoveryPlugin

if sys.version_info < (3, 10):
from importlib_metadata import entry_points
else:
from importlib.metadata import entry_points


def test_b_discovery_plugin_found():
"""Test that the plugin manager finds the B discovery plugin."""
discovery_plugins = {}
plugins = entry_points(group="tox_pyproject.plugins.discovery")
for plugin_type in plugins:
plugin = plugin_type.load()
discovery_plugins[plugin_type.name] = plugin()
assert any(
plugin.get_name() == "B" for _, plugin in list(discovery_plugins.items())
)


def test_b_discovery_plugin_scan_valid():
"""Test that the B discovery plugin runs."""
bdp = BDiscoveryPlugin()
found = bdp.scan()
assert found == ["threshold"]
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pyproject.toml → tox-pyproject/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test = [
"PyYAML",
"coverage",
"lark",
"pytest",
"pytest==8.0.1", # pin until ROS plugin is fixed at https://github.com/ros2/launch/pull/766
"pytest-cov",
"tox",
]
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions tox-pyproject/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[tox]
envlist = py38, py39, py310, py311, py312
skip_missing_interpreters = true

[pytest]
norecursedirs = .tox build

[gh-actions]
python =
3.8: py38
3.9: py39
3.10: py310
3.11: py311
3.12: py312

[testenv]
changedir = {toxinidir}/.output-{envname}
deps =
.[test]
commands =
pytest \
--cov={toxinidir}/src/tox_pyproject \
--cov-report term-missing \
--doctest-modules \
--junit-xml=tox-pyproject-{envname}-junit.xml \
--junit-prefix={envname} \
{toxinidir} {posargs}
coverage xml

0 comments on commit a82bbc5

Please sign in to comment.