Skip to content

Commit

Permalink
test: split unit/functional tests and reduce project bootstrap boiler…
Browse files Browse the repository at this point in the history
…plate (#350)

* test: split unit and functional tests

* test: reduce project bootstrap boilerplate
  • Loading branch information
mkniewallner authored May 6, 2023
1 parent d7ee7b2 commit 8b956e4
Show file tree
Hide file tree
Showing 39 changed files with 225 additions and 238 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ show_error_codes = true
module = [
"deptry.core",
"scripts.generate_stdlibs",
"tests.test_core",
"tests.unit.test_core",
]
warn_unused_ignores = false

Expand Down
39 changes: 0 additions & 39 deletions tests/cli/test_cli_pdm.py

This file was deleted.

39 changes: 0 additions & 39 deletions tests/cli/test_cli_pep_621.py

This file was deleted.

39 changes: 0 additions & 39 deletions tests/cli/test_cli_pyproject_different_directory.py

This file was deleted.

39 changes: 0 additions & 39 deletions tests/cli/test_cli_src_directory.py

This file was deleted.

File renamed without changes.
File renamed without changes.
57 changes: 23 additions & 34 deletions tests/cli/test_cli.py → tests/functional/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@

import os
import shlex
import shutil
import subprocess
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
from click.testing import CliRunner

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

if TYPE_CHECKING:
from _pytest.tmpdir import TempPathFactory
from tests.functional.types import ToolSpecificProjectBuilder


@pytest.fixture(scope="session")
def dir_with_venv_installed(tmp_path_factory: TempPathFactory) -> Path:
tmp_path_proj = tmp_path_factory.getbasetemp() / "example_project"
shutil.copytree("tests/data/example_project", tmp_path_proj)
with run_within_dir(tmp_path_proj):
assert subprocess.check_call(shlex.split("poetry install --no-interaction --no-root")) == 0
return tmp_path_proj


def test_cli_returns_error(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_returns_error(poetry_project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(poetry_project_builder("example_project")):
result = CliRunner().invoke(deptry, ". -o report.json")

assert result.exit_code == 1
Expand All @@ -39,8 +28,8 @@ def test_cli_returns_error(dir_with_venv_installed: Path) -> None:
}


def test_cli_ignore_notebooks(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_ignore_notebooks(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
result = CliRunner().invoke(deptry, ". --ignore-notebooks -o report.json")

assert result.exit_code == 1
Expand All @@ -52,22 +41,22 @@ def test_cli_ignore_notebooks(dir_with_venv_installed: Path) -> None:
}


def test_cli_ignore_flags(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_ignore_flags(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
result = CliRunner().invoke(deptry, ". --ignore-obsolete isort,pkginfo,requests -im white -id black")

assert result.exit_code == 0


def test_cli_skip_flags(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_skip_flags(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
result = CliRunner().invoke(deptry, ". --skip-obsolete --skip-missing --skip-misplaced-dev --skip-transitive")

assert result.exit_code == 0


def test_cli_exclude(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_exclude(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
result = CliRunner().invoke(deptry, ". --exclude src/notebook.ipynb -o report.json")

assert result.exit_code == 1
Expand All @@ -79,8 +68,8 @@ def test_cli_exclude(dir_with_venv_installed: Path) -> None:
}


def test_cli_extend_exclude(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_extend_exclude(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
result = CliRunner().invoke(deptry, ". -ee src/notebook.ipynb -o report.json")

assert result.exit_code == 1
Expand All @@ -92,8 +81,8 @@ def test_cli_extend_exclude(dir_with_venv_installed: Path) -> None:
}


def test_cli_known_first_party(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_known_first_party(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
result = CliRunner().invoke(deptry, ". --known-first-party white -o report.json")

assert result.exit_code == 1
Expand All @@ -105,8 +94,8 @@ def test_cli_known_first_party(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):
def test_cli_not_verbose(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
result = subprocess.run(shlex.split("poetry run deptry . -o report.json"), capture_output=True, text=True)

assert result.returncode == 1
Expand All @@ -119,8 +108,8 @@ def test_cli_not_verbose(dir_with_venv_installed: Path) -> None:
}


def test_cli_verbose(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_verbose(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
result = subprocess.run(
shlex.split("poetry run deptry . --verbose -o report.json"), capture_output=True, text=True
)
Expand All @@ -135,8 +124,8 @@ def test_cli_verbose(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):
def test_cli_with_not_json_output(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
# Remove previously generated `report.json`.
os.remove("report.json")

Expand All @@ -155,8 +144,8 @@ def test_cli_with_not_json_output(dir_with_venv_installed: Path) -> None:
)


def test_cli_with_json_output(dir_with_venv_installed: Path) -> None:
with run_within_dir(dir_with_venv_installed):
def test_cli_with_json_output(project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(project_builder("example_project", "poetry install --no-interaction --no-root")):
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.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
from __future__ import annotations

import shlex
import shutil
import subprocess
from typing import TYPE_CHECKING

import pytest
from click.testing import CliRunner

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

if TYPE_CHECKING:
from pathlib import Path
from tests.functional.types import ToolSpecificProjectBuilder

from _pytest.tmpdir import TempPathFactory


@pytest.fixture(scope="session")
def dir_with_gitignore(tmp_path_factory: TempPathFactory) -> Path:
tmp_path_proj = tmp_path_factory.getbasetemp() / "project_with_gitignore"
shutil.copytree("tests/data/project_with_gitignore", str(tmp_path_proj))
with run_within_dir(tmp_path_proj):
assert subprocess.check_call(shlex.split("pip install .")) == 0
return tmp_path_proj


def test_cli_gitignore_is_used(dir_with_gitignore: Path) -> None:
with run_within_dir(dir_with_gitignore):
def test_cli_gitignore_is_used(pip_project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(pip_project_builder("project_with_gitignore")):
result = CliRunner().invoke(deptry, shlex.split(". -o report.json"))

assert result.exit_code == 1
Expand All @@ -39,8 +25,8 @@ def test_cli_gitignore_is_used(dir_with_gitignore: Path) -> None:
}


def test_cli_gitignore_is_not_used(dir_with_gitignore: Path) -> None:
with run_within_dir(dir_with_gitignore):
def test_cli_gitignore_is_not_used(pip_project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(pip_project_builder("project_with_gitignore")):
result = CliRunner().invoke(deptry, ". --exclude build/|src/bar.py -o report.json")

assert result.exit_code == 1
Expand Down
24 changes: 24 additions & 0 deletions tests/functional/cli/test_cli_pdm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from click.testing import CliRunner

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

if TYPE_CHECKING:
from tests.functional.types import ToolSpecificProjectBuilder


def test_cli_with_pdm(pdm_project_builder: ToolSpecificProjectBuilder) -> None:
with run_within_dir(pdm_project_builder("project_with_pdm")):
result = CliRunner().invoke(deptry, ". -o report.json")

assert result.exit_code == 1
assert get_issues_report() == {
"misplaced_dev": ["black"],
"missing": ["white"],
"obsolete": ["isort", "requests"],
"transitive": [],
}
Loading

0 comments on commit 8b956e4

Please sign in to comment.