Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add nox task to verify dependency declarations #236

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
13e9f39
add dependancy_check.py + test
Jannis-Mittenzwei Aug 15, 2024
d96fb65
fix dependancy_check.py
Jannis-Mittenzwei Aug 20, 2024
da4854e
add output for problematic dependencies
Jannis-Mittenzwei Aug 20, 2024
767e38c
output changed
Jannis-Mittenzwei Aug 21, 2024
1ffe47d
add nox task + test
Jannis-Mittenzwei Sep 3, 2024
4e13573
fix f-string new line problem
Jannis-Mittenzwei Sep 3, 2024
9c33b61
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Sep 3, 2024
6d14d58
new version
Jannis-Mittenzwei Sep 3, 2024
21a7aff
new version
Jannis-Mittenzwei Sep 3, 2024
b7c04a8
Merge remote-tracking branch 'origin/feature/#233-add-nox-task-to-ver…
Jannis-Mittenzwei Sep 3, 2024
7b88101
new version
Jannis-Mittenzwei Sep 3, 2024
5f41253
resolves the conversations
Jannis-Mittenzwei Sep 11, 2024
06d6594
fix type error
Jannis-Mittenzwei Sep 17, 2024
1757ed0
add capsys to dependencies check test
Jannis-Mittenzwei Sep 17, 2024
f48801b
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Sep 24, 2024
2a11ca0
resolves the conversations
Jannis-Mittenzwei Sep 24, 2024
836ab35
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Sep 24, 2024
5414dd7
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Sep 25, 2024
cbd41da
update changelog
Jannis-Mittenzwei Sep 25, 2024
44d6cd5
resolves the conversations
Jannis-Mittenzwei Sep 25, 2024
ca17f92
Merge branch 'main' into feature/#233-add-nox-task-to-verify-dependen…
Jannis-Mittenzwei Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## ✨ Added

* Added cookiecutter-template for creating new project
* #233: Added nox task to verify dependency-declarations

## 🔩 Internal

Expand Down
81 changes: 81 additions & 0 deletions exasol/toolbox/nox/_dependencies_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import tomlkit
import sys
from pathlib import Path
import nox
from nox import Session
from noxconfig import PROJECT_CONFIG
from typing import (
List,
Dict
)
import rich.console


@nox.session(name="dependencies-check", python=False)
def dependency_check(session: Session) -> None:
content = Path(PROJECT_CONFIG.root, "pyproject.toml").read_text()
dependencies = Dependencies(content).parse()
console = rich.console.Console()
if illegal := dependencies.illegal:
report_illegal(illegal, console)
sys.exit(1)


class Dependencies:
ILLEGAL_DEPENDENCIES = ['url', 'git', 'path']

def __init__(self, pyproject_toml: str):
self.illegal_dict: Dict[str, List[str]] = {}
self.content = pyproject_toml
Comment on lines +27 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after construction the class should be initialized properly


def parse(self) -> "Dependencies":
def source_filter(version) -> bool:
for f in self.ILLEGAL_DEPENDENCIES:
if f in version:
return True
return False

def extract_dependencies(section) -> List[str]:
dependencies = []
for name, version in section.items():
if source_filter(version):
dependencies.append(f"{name} = {version}")
return dependencies

illegal: Dict[str, List[str]] = {}
toml = tomlkit.loads(self.content)
poetry = toml.get("tool", {}).get("poetry", {})

part = poetry.get("dependencies", {})
dependencies_list = extract_dependencies(part)
if dependencies_list:
illegal["tool.poetry.dependencies"] = dependencies_list

part = poetry.get("dev", {}).get("dependencies", {})
dependencies_list = extract_dependencies(part)
if dependencies_list:
illegal["tool.poetry.dev.dependencies"] = dependencies_list

part = poetry.get("group", {})
for group, content in part.items():
dependencies_list = extract_dependencies(content.get("dependencies", {}))
if dependencies_list:
illegal[f"tool.poetry.group.{group}.dependencies"] = dependencies_list

self.illegal_dict = illegal
return self

@property
def illegal(self) -> Dict[str, List[str]]:
return self.illegal_dict
Comment on lines +31 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def parse(self) -> "Dependencies":
def source_filter(version) -> bool:
for f in self.ILLEGAL_DEPENDENCIES:
if f in version:
return True
return False
def extract_dependencies(section) -> List[str]:
dependencies = []
for name, version in section.items():
if source_filter(version):
dependencies.append(f"{name} = {version}")
return dependencies
illegal: Dict[str, List[str]] = {}
toml = tomlkit.loads(self.content)
poetry = toml.get("tool", {}).get("poetry", {})
part = poetry.get("dependencies", {})
dependencies_list = extract_dependencies(part)
if dependencies_list:
illegal["tool.poetry.dependencies"] = dependencies_list
part = poetry.get("dev", {}).get("dependencies", {})
dependencies_list = extract_dependencies(part)
if dependencies_list:
illegal["tool.poetry.dev.dependencies"] = dependencies_list
part = poetry.get("group", {})
for group, content in part.items():
dependencies_list = extract_dependencies(content.get("dependencies", {}))
if dependencies_list:
illegal[f"tool.poetry.group.{group}.dependencies"] = dependencies_list
self.illegal_dict = illegal
return self
@property
def illegal(self) -> Dict[str, List[str]]:
return self.illegal_dict
class Dependencies:
def __init__(self, illegal: Dict[str, List[str]] | None):
self._illegal = illegal or {}
@staticmethod
def parse(pyproject_toml: str) -> "Dependencies":
def _source_filter(version) -> bool:
ILLEGAL_SPECIFIERS = ['url', 'git', 'path']
return any(
specifier in version
for specifier in ILLEGAL_SPECIFIERS
)
def _extract_dependencies(section) -> List[str]:
dependencies = []
for name, version in section.items():
if _source_filter(version):
dependencies.append(f"{name} = {version}")
return dependencies
illegal: Dict[str, List[str]] = {}
toml = tomlkit.loads(pyproject_toml)
poetry = toml.get("tool", {}).get("poetry", {})
part = poetry.get("dependencies", {})
dependencies_list = _extract_dependencies(part)
if dependencies_list:
illegal["tool.poetry.dependencies"] = dependencies_list
part = poetry.get("dev", {}).get("dependencies", {})
dependencies_list = _extract_dependencies(part)
if dependencies_list:
illegal["tool.poetry.dev.dependencies"] = dependencies_list
part = poetry.get("group", {})
for group, content in part.items():
dependencies_list = _extract_dependencies(content.get("dependencies", {}))
if dependencies_list:
illegal[f"tool.poetry.group.{group}.dependencies"] = dependencies_list
return Dependencies(illegal)
@property
def illegal(self) -> Dict[str, List[str]]:
return self._illegal



def report_illegal(illegal: Dict[str, List[str]], console: rich.console.Console):
count = sum(len(deps) for deps in illegal.values())
suffix = "y" if count == 1 else "ies"
console.print(f"{count} illegal dependenc{suffix}\n", style="red")
for section, dependencies in illegal.items():
console.print(f"\\[{section}]", style="red")
for dependency in dependencies:
console.print(dependency, style="red")
console.print("")
3 changes: 3 additions & 0 deletions exasol/toolbox/nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
integration_tests,
unit_tests,
)
from exasol.toolbox.nox._dependencies_check import (
dependency_check
)
from noxconfig import PROJECT_CONFIG


Expand Down
147 changes: 147 additions & 0 deletions test/unit/dependencies_check_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import pytest
import rich.console

from exasol.toolbox.nox._dependencies_check import Dependencies, report_illegal


@pytest.mark.parametrize(
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
"toml,expected",
[
(
"""
""",
{}
),
(
"""
[tool.poetry.dependencies]
python = "^3.8"
example-url1 = {url = "https://example.com/my-package-0.1.0.tar.gz"}

[tool.poetry.dev.dependencies]
nox = ">=2022.8.7"
example-url2 = {url = "https://example.com/my-package-0.2.0.tar.gz"}

[tool.poetry.group.test.dependencies]
sphinx = ">=5.3,<8"
example-git = {git = "[email protected]:requests/requests.git"}

[tool.poetry.group.dev.dependencies]
pytest = ">=7.2.2,<9"
example-path1 = {path = "../my-package/dist/my-package-0.1.0.tar.gz"}
""",
{
"tool.poetry.dependencies": ["example-url1 = {'url': 'https://example.com/my-package-0.1.0.tar.gz'}"],
"tool.poetry.dev.dependencies": ["example-url2 = {'url': 'https://example.com/my-package-0.2.0.tar.gz'}"],
"tool.poetry.group.test.dependencies": ["example-git = {'git': '[email protected]:requests/requests.git'}"],
"tool.poetry.group.dev.dependencies": ["example-path1 = {'path': '../my-package/dist/my-package-0.1.0.tar.gz'}"],
}
),
(
"""
[tool.poetry.dev.dependencies]
nox = ">=2022.8.7"
example-url2 = {url = "https://example.com/my-package-0.2.0.tar.gz"}

[tool.poetry.group.test.dependencies]
sphinx = ">=5.3,<8"
example-git = {git = "[email protected]:requests/requests.git"}

[tool.poetry.group.dev.dependencies]
pytest = ">=7.2.2,<9"
example-path1 = {path = "../my-package/dist/my-package-0.1.0.tar.gz"}
""",
{
"tool.poetry.dev.dependencies": ["example-url2 = {'url': 'https://example.com/my-package-0.2.0.tar.gz'}"],
"tool.poetry.group.test.dependencies": ["example-git = {'git': '[email protected]:requests/requests.git'}"],
"tool.poetry.group.dev.dependencies": ["example-path1 = {'path': '../my-package/dist/my-package-0.1.0.tar.gz'}"],
}
),
(
"""
[tool.poetry.dependencies]
python = "^3.8"
example-url1 = {url = "https://example.com/my-package-0.1.0.tar.gz"}

[tool.poetry.group.test.dependencies]
sphinx = ">=5.3,<8"
example-git = {git = "[email protected]:requests/requests.git"}

[tool.poetry.group.dev.dependencies]
pytest = ">=7.2.2,<9"
example-path1 = {path = "../my-package/dist/my-package-0.1.0.tar.gz"}
""",
{
"tool.poetry.dependencies": ["example-url1 = {'url': 'https://example.com/my-package-0.1.0.tar.gz'}"],
"tool.poetry.group.test.dependencies": ["example-git = {'git': '[email protected]:requests/requests.git'}"],
"tool.poetry.group.dev.dependencies": ["example-path1 = {'path': '../my-package/dist/my-package-0.1.0.tar.gz'}"],
}
),
(
"""
[tool.poetry.dependencies]
python = "^3.8"
example-url1 = {url = "https://example.com/my-package-0.1.0.tar.gz"}

[tool.poetry.dev.dependencies]
nox = ">=2022.8.7"
example-url2 = {url = "https://example.com/my-package-0.2.0.tar.gz"}
""",
{
"tool.poetry.dependencies": ["example-url1 = {'url': 'https://example.com/my-package-0.1.0.tar.gz'}"],
"tool.poetry.dev.dependencies": ["example-url2 = {'url': 'https://example.com/my-package-0.2.0.tar.gz'}"],
}
)
]
)
def test_dependency_check_parse(toml, expected):
dependencies = Dependencies(toml).parse()
assert dependencies.illegal == expected


@pytest.mark.parametrize(
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved Hide resolved
"toml,expected",
[
(
"""
[tool.poetry.dependencies]
python = "^3.8"
example-url1 = {url = "https://example.com/my-package-0.1.0.tar.gz"}

[tool.poetry.dev.dependencies]
nox = ">=2022.8.7"
example-url2 = {url = "https://example.com/my-package-0.2.0.tar.gz"}

[tool.poetry.group.test.dependencies]
sphinx = ">=5.3,<8"
example-git = {git = "[email protected]:requests/requests.git"}

[tool.poetry.group.dev.dependencies]
pytest = ">=7.2.2,<9"
example-path1 = {path = "../my-package/dist/my-package-0.1.0.tar.gz"}
example-path2 = {path = "../my-package/dist/my-package-0.2.0.tar.gz"}
""",
"""5 illegal dependencies

[tool.poetry.dependencies]
example-url1 = {'url': 'https://example.com/my-package-0.1.0.tar.gz'}

[tool.poetry.dev.dependencies]
example-url2 = {'url': 'https://example.com/my-package-0.2.0.tar.gz'}

[tool.poetry.group.test.dependencies]
example-git = {'git': '[email protected]:requests/requests.git'}

[tool.poetry.group.dev.dependencies]
example-path1 = {'path': '../my-package/dist/my-package-0.1.0.tar.gz'}
example-path2 = {'path': '../my-package/dist/my-package-0.2.0.tar.gz'}

"""
),
]
)
def test_dependencies_check_report(toml, expected, capsys):
console = rich.console.Console()
dependencies = Dependencies(toml).parse()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dependencies = Dependencies(toml).parse()
dependencies = Dependencies.parse(toml)

report_illegal(dependencies.illegal, console)
assert capsys.readouterr().out == expected