Skip to content

Commit

Permalink
Run flake8 on tests (#147)
Browse files Browse the repository at this point in the history
* chore(flake8): run on `tests` directory
* chore: comply with `flake8` on `tests`
* chore(mypy): type check `scripts` module
  • Loading branch information
mkniewallner authored Oct 1, 2022
1 parent 243ff7a commit 55aa5fd
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 67 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ preview = true
profile = "black"

[tool.mypy]
files = ["deptry"]
files = ["deptry", "scripts"]
disallow_untyped_defs = true
disallow_any_unimported = true
no_implicit_optional = true
Expand Down
55 changes: 29 additions & 26 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,31 @@
@pytest.fixture(scope="session")
def dir_with_venv_installed(tmp_path_factory):
tmp_path_proj = tmp_path_factory.getbasetemp() / "example_project"
shutil.copytree("tests/data/example_project", str(tmp_path_proj))
with run_within_dir(str(tmp_path_proj)):
subprocess.check_call(shlex.split("poetry install --no-interaction --no-root")) == 0
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


@pytest.fixture(scope="session")
def requirements_txt_dir_with_venv_installed(tmp_path_factory):
tmp_path_proj = tmp_path_factory.getbasetemp() / "project_with_requirements_txt"
shutil.copytree("tests/data/project_with_requirements_txt", str(tmp_path_proj))
with run_within_dir(str(tmp_path_proj)):
subprocess.check_call(
shlex.split(
"pip install -r requirements.txt -r requirements-dev.txt -r requirements-2.txt -r"
" requirements-typing.txt"
with run_within_dir(tmp_path_proj):
assert (
subprocess.check_call(
shlex.split(
"pip install -r requirements.txt -r requirements-dev.txt -r requirements-2.txt -r"
" requirements-typing.txt"
)
)
) == 0
== 0
)
return tmp_path_proj


def test_cli_returns_error(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry ."), capture_output=True, text=True)
assert result.returncode == 1
print(result.stderr)
Expand All @@ -43,14 +46,14 @@ def test_cli_returns_error(dir_with_venv_installed):


def test_cli_ignore_notebooks(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry . --ignore-notebooks"), 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


def test_cli_ignore_flags(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(
shlex.split("poetry run deptry . --ignore-obsolete isort,pkginfo,requests -im white -id black"),
capture_output=True,
Expand All @@ -60,7 +63,7 @@ def test_cli_ignore_flags(dir_with_venv_installed):


def test_cli_skip_flags(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(
shlex.split("poetry run deptry . --skip-obsolete --skip-missing --skip-misplaced-dev --skip-transitive"),
capture_output=True,
Expand All @@ -70,7 +73,7 @@ def test_cli_skip_flags(dir_with_venv_installed):


def test_cli_exclude(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
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
)
Expand All @@ -79,7 +82,7 @@ def test_cli_exclude(dir_with_venv_installed):


def test_cli_extend_exclude(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
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
)
Expand All @@ -88,7 +91,7 @@ def test_cli_extend_exclude(dir_with_venv_installed):


def test_cli_single_requirements_txt(requirements_txt_dir_with_venv_installed):
with run_within_dir(str(requirements_txt_dir_with_venv_installed)):
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"),
capture_output=True,
Expand All @@ -110,7 +113,7 @@ def test_cli_single_requirements_txt(requirements_txt_dir_with_venv_installed):


def test_cli_multiple_requirements_txt(requirements_txt_dir_with_venv_installed):
with run_within_dir(str(requirements_txt_dir_with_venv_installed)):
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"
Expand All @@ -132,19 +135,19 @@ def test_cli_multiple_requirements_txt(requirements_txt_dir_with_venv_installed)


def test_cli_verbose(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry . "), capture_output=True, text=True)
assert result.returncode == 1
assert not "The project contains the following dependencies:" in result.stderr
assert "The project contains the following dependencies:" not in result.stderr

with run_within_dir(str(dir_with_venv_installed)):
with run_within_dir(dir_with_venv_installed):
result = subprocess.run(shlex.split("poetry run deptry . -v "), capture_output=True, text=True)
assert result.returncode == 1
assert "The project contains the following dependencies:" in result.stderr


def test_cli_with_json_output(dir_with_venv_installed):
with run_within_dir(str(dir_with_venv_installed)):
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)
assert len(list(pathlib.Path(".").glob("*.json"))) == 0
Expand All @@ -153,11 +156,11 @@ def test_cli_with_json_output(dir_with_venv_installed):
subprocess.run(shlex.split("poetry run deptry . -o deptry.json"), capture_output=True, text=True)
with open("deptry.json", "r") as f:
data = json.load(f)
assert set(data["obsolete"]) == set(["isort", "requests"])
assert set(data["missing"]) == set(["white"])
assert set(data["misplaced_dev"]) == set(["black"])
assert set(data["transitive"]) == set([])
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_help():
subprocess.check_call(shlex.split("deptry --help")) == 0
assert subprocess.check_call(shlex.split("deptry --help")) == 0
2 changes: 0 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

import pytest

from deptry.core import Core
Expand Down
8 changes: 4 additions & 4 deletions tests/test_dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
def test_simple_dependency():
dependency = Dependency("click")
assert dependency.name == "click"
assert dependency.top_levels == set(["click"])
assert dependency.top_levels == {"click"}


def test_create_default_top_level_if_metadata_not_found():
dependency = Dependency("Foo-bar")
assert dependency.name == "Foo-bar"
assert dependency.top_levels == set(["foo_bar"])
assert dependency.top_levels == {"foo_bar"}


def test_read_top_level_from_top_level_txt():
Expand All @@ -31,7 +31,7 @@ def read_text(self, file_name):
mock.return_value = MockDistribution()
dependency = Dependency("Foo-bar")
assert dependency.name == "Foo-bar"
assert dependency.top_levels == set(["foo_bar", "foo", "bar"])
assert dependency.top_levels == {"foo_bar", "foo", "bar"}


def test_read_top_level_from_record():
Expand All @@ -58,4 +58,4 @@ def read_text(self, file_name):
mock.return_value = MockDistribution()
dependency = Dependency("Foo-bar")
assert dependency.name == "Foo-bar"
assert dependency.top_levels == set(["foo_bar", "black", "blackd"])
assert dependency.top_levels == {"foo_bar", "black", "blackd"}
30 changes: 14 additions & 16 deletions tests/test_import_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import logging
import os
from ast import Import
from pathlib import Path
from unittest.mock import Mock

Expand All @@ -10,14 +8,14 @@

def test_import_parser_py():
imported_modules = ImportParser().get_imported_modules_from_file(Path("tests/data/some_imports.py"))
assert set(imported_modules) == set(["os", "pathlib", "typing", "pandas", "numpy"])
assert set(imported_modules) == {"os", "pathlib", "typing", "pandas", "numpy"}


def test_import_parser_ipynb():
imported_modules = ImportParser().get_imported_modules_from_file(
Path("tests/data/example_project/src/notebook.ipynb")
)
assert set(imported_modules) == set(["click", "urllib3", "toml"])
assert set(imported_modules) == {"click", "urllib3", "toml"}


def test_import_parser_ifelse():
Expand All @@ -33,7 +31,7 @@ def test_import_parser_ifelse():
import logging
"""
)
assert set(imported_modules) == set(["numpy", "pandas", "typing", "logging"])
assert set(imported_modules) == {"numpy", "pandas", "typing", "logging"}


def test_import_parser_tryexcept():
Expand All @@ -47,7 +45,7 @@ def test_import_parser_tryexcept():
import logging
"""
)
assert set(imported_modules) == set(["numpy", "pandas", "click", "logging"])
assert set(imported_modules) == {"numpy", "pandas", "click", "logging"}


def test_import_parser_func():
Expand All @@ -59,7 +57,7 @@ def func():
import click
"""
)
assert set(imported_modules) == set(["numpy", "pandas", "click"])
assert set(imported_modules) == {"numpy", "pandas", "click"}


def test_import_parser_class():
Expand All @@ -72,20 +70,20 @@ def __init__(self):
import click
"""
)
assert set(imported_modules) == set(["numpy", "pandas", "click"])
assert set(imported_modules) == {"numpy", "pandas", "click"}


def test_import_parser_relative():
imported_modules = ImportParser().get_imported_modules_from_str("""from . import foo\nfrom .foo import bar""")
assert set(imported_modules) == set([])
assert set(imported_modules) == set()


def test_import_parser_ignores_setuptools(tmp_path):
with run_within_dir(tmp_path):
with open("file.py", "w") as f:
f.write("import setuptools\nimport foo")
imported_modules = ImportParser().get_imported_modules_for_list_of_files(["file.py"])
assert set(imported_modules) == set(["foo"])
imported_modules = ImportParser().get_imported_modules_for_list_of_files([Path("file.py")])
assert set(imported_modules) == {"foo"}


def test_import_parser_file_encodings(tmp_path):
Expand Down Expand Up @@ -119,16 +117,16 @@ def test_import_parser_file_encodings(tmp_path):
f.write("""my_string = '🐺'\nimport foo""")

imported_modules = ImportParser().get_imported_modules_from_file(Path("file1.py"))
assert set(imported_modules) == set(["foo"])
assert set(imported_modules) == {"foo"}

imported_modules = ImportParser().get_imported_modules_from_file(Path("file2.py"))
assert set(imported_modules) == set(["foo"])
assert set(imported_modules) == {"foo"}

imported_modules = ImportParser().get_imported_modules_from_file(Path("file3.py"))
assert set(imported_modules) == set(["foo"])
assert set(imported_modules) == {"foo"}

imported_modules = ImportParser().get_imported_modules_from_file(Path("file4.py"))
assert set(imported_modules) == set(["foo"])
assert set(imported_modules) == {"foo"}


def test_import_parser_file_encodings_warning(tmp_path, caplog):
Expand All @@ -142,5 +140,5 @@ def test_import_parser_file_encodings_warning(tmp_path, caplog):
)
with caplog.at_level(logging.WARNING):
imported_modules = mockObject().get_imported_modules_from_file(Path("file1.py"))
assert set(imported_modules) == set([])
assert set(imported_modules) == set()
assert "Warning: File file1.py could not be decoded. Skipping..." in caplog.text
2 changes: 1 addition & 1 deletion tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_top_level():
dependency = Dependency("beautifulsoup4")
dependency.top_levels = ["bs4"]
module = ModuleBuilder("bs4", [dependency]).build()
assert module.package == None
assert module.package is None


def test_stdlib():
Expand Down
6 changes: 0 additions & 6 deletions tests/test_notebook_import_extractor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import os
from contextlib import contextmanager
from pathlib import Path

import pytest

from deptry.notebook_import_extractor import NotebookImportExtractor


Expand Down
1 change: 0 additions & 1 deletion tests/test_pyproject_toml_dependency_getter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from deptry.dependency import Dependency
from deptry.dependency_getter.pyproject_toml import PyprojectTomlDependencyGetter
from deptry.utils import run_within_dir

Expand Down
8 changes: 3 additions & 5 deletions tests/test_python_file_finder.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
from contextlib import contextmanager
from pathlib import Path
from typing import Dict, List

import pytest

from deptry.python_file_finder import PythonFileFinder
from deptry.utils import run_within_dir


def create_files_from_list_of_dicts(paths: List[Dict[str, str]]):
"""
Takes as input an argument paths, which is a list of dicts. Each dict should have two key's;
Takes as input an argument paths, which is a list of dicts. Each dict should have two keys;
'dir' to denote a directory and 'file' to denote the file name. This function creates all files
within their corresponding directories.
"""
for path in paths:
Path(path["dir"]).mkdir(parents=True, exist_ok=True)
open(Path(path["dir"]) / Path(path["file"]), "w").close()
with open(Path(path["dir"]) / Path(path["file"]), "w"):
pass


def test_simple(tmp_path):
Expand Down
1 change: 0 additions & 1 deletion tests/test_requirements_txt_dependency_getter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from deptry.dependency import Dependency
from deptry.dependency_getter.requirements_txt import RequirementsTxtDependencyGetter
from deptry.utils import run_within_dir

Expand Down
2 changes: 0 additions & 2 deletions tests/test_result_logger.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import logging

import pytest

from deptry.result_logger import ResultLogger


Expand Down
1 change: 0 additions & 1 deletion tests/test_transitive_dependencies_finder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from deptry.dependency import Dependency
from deptry.issue_finders.transitive import TransitiveDependenciesFinder
from deptry.module import ModuleBuilder

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extend-exclude =
build/*,
dist/*,
.venv/*,
tests/*,
tests/data/*,
venv/*
max-complexity = 10
max-line-length = 120
Expand Down

0 comments on commit 55aa5fd

Please sign in to comment.