Skip to content

Commit

Permalink
test(cli): use JSON output to simplify tests (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner authored Jan 2, 2023
1 parent b9ce18f commit 1f0b0cf
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 79 deletions.
123 changes: 97 additions & 26 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import os
import shlex
import shutil
import subprocess
Expand All @@ -7,7 +7,7 @@
import pytest
from _pytest.tmpdir import TempPathFactory

from tests.utils import run_within_dir
from tests.utils import get_issues_report, run_within_dir


@pytest.fixture(scope="session")
Expand All @@ -21,18 +21,30 @@ def dir_with_venv_installed(tmp_path_factory: TempPathFactory) -> Path:

def test_cli_returns_error(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry ."), capture_output=True, text=True)
result = subprocess.run(shlex.split("poetry run deptry . -o report.json"), capture_output=True, text=True)

assert result.returncode == 1
assert "The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\n" in result.stderr
assert "There are dependencies missing from the project's list of dependencies:\n\n\twhite\n\n" in result.stderr
assert "There are imported modules from development dependencies detected:\n\n\tblack\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["isort", "requests"],
"transitive": [],
}


def test_cli_ignore_notebooks(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry . --ignore-notebooks"), capture_output=True, text=True)
result = subprocess.run(
shlex.split("poetry run deptry . --ignore-notebooks -o report.json"), capture_output=True, text=True
)

assert result.returncode == 1
assert "The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\ttoml\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["toml", "isort", "requests"],
"transitive": [],
}


def test_cli_ignore_flags(dir_with_venv_installed: Path) -> None:
Expand All @@ -42,6 +54,7 @@ def test_cli_ignore_flags(dir_with_venv_installed: Path) -> None:
capture_output=True,
text=True,
)

assert result.returncode == 0


Expand All @@ -52,53 +65,111 @@ def test_cli_skip_flags(dir_with_venv_installed: Path) -> None:
capture_output=True,
text=True,
)

assert result.returncode == 0


def test_cli_exclude(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(
shlex.split("poetry run deptry . --exclude src/notebook.ipynb "), capture_output=True, text=True
shlex.split("poetry run deptry . --exclude src/notebook.ipynb -o report.json"),
capture_output=True,
text=True,
)

assert result.returncode == 1
assert "The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\ttoml\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["toml", "isort", "requests"],
"transitive": [],
}


def test_cli_extend_exclude(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(
shlex.split("poetry run deptry . -ee src/notebook.ipynb "), capture_output=True, text=True
shlex.split("poetry run deptry . -ee src/notebook.ipynb -o report.json"), capture_output=True, text=True
)

assert result.returncode == 1
assert "The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\ttoml\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["toml", "isort", "requests"],
"transitive": [],
}


def test_cli_verbose(dir_with_venv_installed: Path) -> None:
def test_cli_not_verbose(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry . "), capture_output=True, text=True)
result = subprocess.run(shlex.split("poetry run deptry . -o report.json"), capture_output=True, text=True)

assert result.returncode == 1
assert "The project contains the following dependencies:" not in result.stderr
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["isort", "requests"],
"transitive": [],
}


def test_cli_verbose(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry . -v "), capture_output=True, text=True)
result = subprocess.run(
shlex.split("poetry run deptry . --verbose -o report.json"), capture_output=True, text=True
)

assert result.returncode == 1
assert "The project contains the following dependencies:" in result.stderr
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["isort", "requests"],
"transitive": [],
}


def test_cli_with_json_output(dir_with_venv_installed: Path) -> None:
def test_cli_with_not_json_output(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
# assert that there is no json output
subprocess.run(shlex.split("poetry run deptry ."), capture_output=True, text=True)
# Remove previously generated `report.json`.
os.remove("report.json")

result = subprocess.run(shlex.split("poetry run deptry ."), capture_output=True, text=True)

assert len(list(Path(".").glob("*.json"))) == 0
assert (
"The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\nConsider removing them from your"
" project's dependencies. If a package is used for development purposes, you should add it to your"
" development dependencies instead.\n\n-----------------------------------------------------\n\nThere are"
" dependencies missing from the project's list of dependencies:\n\n\twhite\n\nConsider adding them to your"
" project's dependencies. \n\n-----------------------------------------------------\n\nThere are imported"
" modules from development dependencies detected:\n\n\tblack\n\n"
in result.stderr
)


# assert that there is json output
subprocess.run(shlex.split("poetry run deptry . -o deptry.json"), capture_output=True, text=True)
with open("deptry.json") as f:
data = json.load(f)
assert set(data["obsolete"]) == {"isort", "requests"}
assert set(data["missing"]) == {"white"}
assert set(data["misplaced_dev"]) == {"black"}
assert set(data["transitive"]) == set()
def test_cli_with_json_output(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry . -o deptry.json"), capture_output=True, text=True)

# Assert that we still write to console when generating a JSON report.
assert (
"The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\nConsider removing them from your"
" project's dependencies. If a package is used for development purposes, you should add it to your"
" development dependencies instead.\n\n-----------------------------------------------------\n\nThere are"
" dependencies missing from the project's list of dependencies:\n\n\twhite\n\nConsider adding them to your"
" project's dependencies. \n\n-----------------------------------------------------\n\nThere are imported"
" modules from development dependencies detected:\n\n\tblack\n\n"
in result.stderr
)
assert get_issues_report("deptry.json") == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["isort", "requests"],
"transitive": [],
}


def test_cli_help() -> None:
Expand Down
24 changes: 19 additions & 5 deletions tests/cli/test_cli_gitignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from _pytest.tmpdir import TempPathFactory

from tests.utils import run_within_dir
from tests.utils import get_issues_report, run_within_dir


@pytest.fixture(scope="session")
Expand All @@ -20,13 +20,27 @@ def dir_with_gitignore(tmp_path_factory: TempPathFactory) -> Path:

def test_cli_gitignore_is_used(dir_with_gitignore: Path) -> None:
with run_within_dir(dir_with_gitignore):
result = subprocess.run(shlex.split("deptry ."), capture_output=True, text=True)
result = subprocess.run(shlex.split("deptry . -o report.json"), capture_output=True, text=True)

assert result.returncode == 1
assert "The project contains obsolete dependencies:\n\n\tmypy\n\tpytest\n\trequests\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": [],
"missing": [],
"obsolete": ["requests", "mypy", "pytest"],
"transitive": [],
}


def test_cli_gitignore_is_not_used(dir_with_gitignore: Path) -> None:
with run_within_dir(dir_with_gitignore):
result = subprocess.run(shlex.split("deptry . --exclude build/|src/bar.py"), capture_output=True, text=True)
result = subprocess.run(
shlex.split("deptry . --exclude build/|src/bar.py -o report.json"), capture_output=True, text=True
)

assert result.returncode == 1
assert "The project contains obsolete dependencies:\n\n\tisort\n\tpytest\n\trequests\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": [],
"missing": [],
"obsolete": ["isort", "requests", "pytest"],
"transitive": [],
}
14 changes: 9 additions & 5 deletions tests/cli/test_cli_pdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from _pytest.tmpdir import TempPathFactory

from tests.utils import run_within_dir
from tests.utils import get_issues_report, run_within_dir


@pytest.fixture(scope="session")
Expand All @@ -20,8 +20,12 @@ def pdm_dir_with_venv_installed(tmp_path_factory: TempPathFactory) -> Path:

def test_cli_with_pdm(pdm_dir_with_venv_installed: Path) -> None:
with run_within_dir(pdm_dir_with_venv_installed):
result = subprocess.run(shlex.split("deptry ."), capture_output=True, text=True)
result = subprocess.run(shlex.split("deptry . -o report.json"), capture_output=True, text=True)

assert result.returncode == 1
assert "The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\n" in result.stderr
assert "There are dependencies missing from the project's list of dependencies:\n\n\twhite\n\n" in result.stderr
assert "There are imported modules from development dependencies detected:\n\n\tblack\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["isort", "requests"],
"transitive": [],
}
15 changes: 9 additions & 6 deletions tests/cli/test_cli_pep_621.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from _pytest.tmpdir import TempPathFactory

from tests.utils import run_within_dir
from tests.utils import get_issues_report, run_within_dir


@pytest.fixture(scope="session")
Expand All @@ -20,9 +20,12 @@ def pep_621_dir_with_venv_installed(tmp_path_factory: TempPathFactory) -> Path:

def test_cli_with_pep_621(pep_621_dir_with_venv_installed: Path) -> None:
with run_within_dir(pep_621_dir_with_venv_installed):
result = subprocess.run(shlex.split("deptry ."), capture_output=True, text=True)
result = subprocess.run(shlex.split("deptry . -o report.json"), capture_output=True, text=True)

assert result.returncode == 1
assert (
"The project contains obsolete dependencies:\n\n\tisort\n\tmypy\n\tpytest\n\trequests\n\n" in result.stderr
)
assert "There are dependencies missing from the project's list of dependencies:\n\n\twhite\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": [],
"missing": ["white"],
"obsolete": ["isort", "requests", "mypy", "pytest"],
"transitive": [],
}
17 changes: 11 additions & 6 deletions tests/cli/test_cli_pyproject_different_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from _pytest.tmpdir import TempPathFactory

from tests.utils import run_within_dir
from tests.utils import get_issues_report, run_within_dir


@pytest.fixture(scope="session")
Expand All @@ -21,10 +21,15 @@ def pep_621_dir_with_pyproject_different_directory(tmp_path_factory: TempPathFac
def test_cli_with_pyproject_different_directory(pep_621_dir_with_pyproject_different_directory: Path) -> None:
with run_within_dir(pep_621_dir_with_pyproject_different_directory):
result = subprocess.run(
shlex.split("deptry --config a_sub_directory/pyproject.toml src"), capture_output=True, text=True
shlex.split("deptry --config a_sub_directory/pyproject.toml src -o report.json"),
capture_output=True,
text=True,
)

assert result.returncode == 1
assert (
"The project contains obsolete dependencies:\n\n\tisort\n\tmypy\n\tpytest\n\trequests\n\n" in result.stderr
)
assert "There are dependencies missing from the project's list of dependencies:\n\n\twhite\n\n" in result.stderr
assert get_issues_report() == {
"misplaced_dev": [],
"missing": ["white"],
"obsolete": ["isort", "requests", "mypy", "pytest"],
"transitive": [],
}
44 changes: 20 additions & 24 deletions tests/cli/test_cli_requirements_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from _pytest.tmpdir import TempPathFactory

from tests.utils import run_within_dir
from tests.utils import get_issues_report, run_within_dir


@pytest.fixture(scope="session")
Expand All @@ -29,42 +29,38 @@ def requirements_txt_dir_with_venv_installed(tmp_path_factory: TempPathFactory)
def test_cli_single_requirements_txt(requirements_txt_dir_with_venv_installed: Path) -> None:
with run_within_dir(requirements_txt_dir_with_venv_installed):
result = subprocess.run(
shlex.split("deptry . --requirements-txt requirements.txt --requirements-txt-dev requirements-dev.txt"),
shlex.split(
"deptry . --requirements-txt requirements.txt --requirements-txt-dev requirements-dev.txt -o"
" report.json"
),
capture_output=True,
text=True,
)

assert result.returncode == 1
assert (
"The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\nConsider removing them from your"
" project's dependencies. If a package is used for development purposes, you should add it to your"
" development dependencies instead.\n\n-----------------------------------------------------\n\nThere are"
" dependencies missing from the project's list of dependencies:\n\n\twhite\n\nConsider adding them to your"
" project's dependencies. \n\n-----------------------------------------------------\n\nThere are transitive"
" dependencies that should be explicitly defined as dependencies:\n\n\turllib3\n\nThey are currently"
" imported but not specified directly as your project's"
" dependencies.\n\n-----------------------------------------------------\n\nThere are imported modules from"
" development dependencies detected:\n\n\tblack\n\n"
in result.stderr
)
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["isort", "requests"],
"transitive": ["urllib3"],
}


def test_cli_multiple_requirements_txt(requirements_txt_dir_with_venv_installed: Path) -> None:
with run_within_dir(requirements_txt_dir_with_venv_installed):
result = subprocess.run(
shlex.split(
"deptry . --requirements-txt requirements.txt,requirements-2.txt --requirements-txt-dev"
" requirements-dev.txt,requirements-typing.txt"
" requirements-dev.txt,requirements-typing.txt -o report.json"
),
capture_output=True,
text=True,
)

assert result.returncode == 1
assert (
"The project contains obsolete dependencies:\n\n\tisort\n\trequests\n\nConsider removing them from your"
" project's dependencies. If a package is used for development purposes, you should add it to your"
" development dependencies instead.\n\n-----------------------------------------------------\n\nThere are"
" dependencies missing from the project's list of dependencies:\n\n\twhite\n\nConsider adding them to your"
" project's dependencies. \n\n-----------------------------------------------------\n\nThere are imported"
" modules from development dependencies detected:\n\n\tblack\n\n"
in result.stderr
)
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["isort", "requests"],
"transitive": [],
}
Loading

0 comments on commit 1f0b0cf

Please sign in to comment.