Skip to content

Commit

Permalink
add tests (and fix bugs found in testing)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Sep 3, 2023
1 parent 7e14793 commit 98b99cf
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 10 deletions.
17 changes: 10 additions & 7 deletions pip_abandoned/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ def get_archived_packages(dist_urls, api_data):


def is_inactive(distribution):
return "Development Status :: 7 - Inactive" in distribution.metadata.get_all(
"Classifier"
)
classifiers = distribution.metadata.get_all("Classifier") or []
return "Development Status :: 7 - Inactive" in classifiers


def output_archived_table(packages):
Expand Down Expand Up @@ -145,10 +144,14 @@ def search(gh_token, path, verbosity):
url = get_github_repo_url(distribution)
if url:
dist_urls.append((distribution, url))
query = get_graphql_query(dist_urls)
archived_packages = get_archived_packages(
dist_urls, query_github_api(gh_token, query)
)

if len(dist_urls) > 0:
query = get_graphql_query(dist_urls)
archived_packages = get_archived_packages(
dist_urls, query_github_api(gh_token, query)
)
else:
archived_packages = []

if len(inactive_packages) == 0:
console.print(
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dev = [
"black==23.7.0",
"pytest==7.4.0",
"pytest-cov==4.1.0",
"responses==0.23.3",
]

[project.urls]
Expand Down
4 changes: 4 additions & 0 deletions tests/fixture_data/home-page-1.0.0.dist-info/METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Metadata-Version: 2.1
Name: home-page
Version: 1.0.0
Home-page: https://github.com/chris48s/does-not-exist
4 changes: 4 additions & 0 deletions tests/fixture_data/inactive-1.0.0.dist-info/METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Metadata-Version: 2.1
Name: inactive
Version: 1.0.0
Classifier: Development Status :: 7 - Inactive
7 changes: 7 additions & 0 deletions tests/fixture_data/multiple-matches-1.0.0.dist-info/METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Metadata-Version: 2.1
Name: project-urls
Version: 1.0.0
Home-page: https://example.com
Project-URL: Homepage, https://github.com/chris48s/does-not-exist
Project-URL: Documentation, https://chris48s.github.io/does-not-exist
Project-URL: Source, https://github.com/chris48s/completely-different-repo
6 changes: 6 additions & 0 deletions tests/fixture_data/project-urls-1.0.0.dist-info/METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Metadata-Version: 2.1
Name: project-urls
Version: 1.0.0
Home-page: https://example.com
Project-URL: Documentation, https://chris48s.github.io/does-not-exist
Project-URL: Source, https://github.com/chris48s/does-not-exist
187 changes: 184 additions & 3 deletions tests/test_pip_abandoned.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,184 @@
def test_TODO():
# TODO: write some tests
assert True
from contextlib import redirect_stdout
from importlib.metadata import Distribution
from io import StringIO
from pathlib import Path
from unittest.mock import patch

import pytest
import responses
from rich.console import Console

from pip_abandoned import lib

# Disable Rich formatting so we can more easily make assertions about text output
lib.console = Console(force_terminal=True, _environ={"TERM": "dumb"}, soft_wrap=True)


def get_dist_fixture(name):
return Distribution.at(Path(".") / "tests" / "fixture_data" / name)


@pytest.fixture
def mock_distributions_no_packages():
with patch("pip_abandoned.lib.distributions") as mock:
mock.return_value = []
yield mock


@pytest.fixture
def mock_distributions_inactive():
with patch("pip_abandoned.lib.distributions") as mock:
mock.return_value = [get_dist_fixture("inactive-1.0.0.dist-info")]
yield mock


@pytest.fixture
def mock_distributions_homepage():
with patch("pip_abandoned.lib.distributions") as mock:
mock.return_value = [get_dist_fixture("home-page-1.0.0.dist-info")]
yield mock


@pytest.fixture
def mock_distributions_project_urls():
with patch("pip_abandoned.lib.distributions") as mock:
mock.return_value = [get_dist_fixture("project-urls-1.0.0.dist-info")]
yield mock


@pytest.fixture
def mock_distributions_inactive_and_homepage():
with patch("pip_abandoned.lib.distributions") as mock:
mock.return_value = [
get_dist_fixture("inactive-1.0.0.dist-info"),
get_dist_fixture("home-page-1.0.0.dist-info"),
]
yield mock


class TestSearch:
def test_no_packages(self, mock_distributions_no_packages):
with pytest.raises(Exception) as exc:
lib.search("fake_token", "/fake/path", 0)
assert "Couldn't find any packages in /fake/path" in str(exc)

def test_inactive(self, mock_distributions_inactive):
with StringIO() as buf, redirect_stdout(buf):
exit_code = lib.search("fake_token", "/fake/path", 0)
stdout = buf.getvalue()
assert (
"Packages with the trove classifier 'Development Status :: 7 - Inactive' were found"
in stdout
)
assert "No packages associated with archived GitHub repos were found" in stdout
assert exit_code == 1

@responses.activate
def test_home_page_archived(self, mock_distributions_homepage):
responses.add(
responses.POST,
"https://api.github.com/graphql",
json={"data": {"home_page": {"isArchived": True}}},
status=200,
)
with StringIO() as buf, redirect_stdout(buf):
exit_code = lib.search("fake_token", "/fake/path", 0)
stdout = buf.getvalue()
assert (
"No packages with the trove classifier 'Development Status :: 7 - Inactive' were found"
in stdout
)
assert "Packages associated with archived GitHub repos were found:" in stdout
assert exit_code == 1

@responses.activate
def test_home_page_not_archived(self, mock_distributions_homepage):
responses.add(
responses.POST,
"https://api.github.com/graphql",
json={"data": {"home_page": {"isArchived": False}}},
status=200,
)
with StringIO() as buf, redirect_stdout(buf):
exit_code = lib.search("fake_token", "/fake/path", 0)
stdout = buf.getvalue()
assert (
"No packages with the trove classifier 'Development Status :: 7 - Inactive' were found"
in stdout
)
assert "No packages associated with archived GitHub repos were found" in stdout
assert exit_code == 0

@responses.activate
def test_project_urls_archived(self, mock_distributions_project_urls):
responses.add(
responses.POST,
"https://api.github.com/graphql",
json={"data": {"project_urls": {"isArchived": True}}},
status=200,
)
with StringIO() as buf, redirect_stdout(buf):
exit_code = lib.search("fake_token", "/fake/path", 0)
stdout = buf.getvalue()
assert (
"No packages with the trove classifier 'Development Status :: 7 - Inactive' were found"
in stdout
)
assert "Packages associated with archived GitHub repos were found:" in stdout
assert exit_code == 1

@responses.activate
def test_project_urls_not_archived(self, mock_distributions_project_urls):
responses.add(
responses.POST,
"https://api.github.com/graphql",
json={"data": {"project_urls_": {"isArchived": False}}},
status=200,
)
with StringIO() as buf, redirect_stdout(buf):
exit_code = lib.search("fake_token", "/fake/path", 0)
stdout = buf.getvalue()
assert (
"No packages with the trove classifier 'Development Status :: 7 - Inactive' were found"
in stdout
)
assert "No packages associated with archived GitHub repos were found" in stdout
assert exit_code == 0

@responses.activate
def test_inactive_and_archived(self, mock_distributions_inactive_and_homepage):
responses.add(
responses.POST,
"https://api.github.com/graphql",
json={"data": {"home_page": {"isArchived": True}}},
status=200,
)
with StringIO() as buf, redirect_stdout(buf):
exit_code = lib.search("fake_token", "/fake/path", 0)
stdout = buf.getvalue()
assert (
"Packages with the trove classifier 'Development Status :: 7 - Inactive' were found"
in stdout
)
assert "Packages associated with archived GitHub repos were found:" in stdout
assert exit_code == 1


class TestGetGitHubRepo:
def test_no_matches(self):
dist = get_dist_fixture("inactive-1.0.0.dist-info")
assert lib.get_github_repo_url(dist) is None

def test_home_page_match(self):
dist = get_dist_fixture("home-page-1.0.0.dist-info")
expected = "https://github.com/chris48s/does-not-exist"
assert lib.get_github_repo_url(dist) == expected

def test_project_urls_one_match(self):
dist = get_dist_fixture("project-urls-1.0.0.dist-info")
expected = "https://github.com/chris48s/does-not-exist"
assert lib.get_github_repo_url(dist) == expected

def test_project_urls_multiple_matches(self):
dist = get_dist_fixture("multiple-matches-1.0.0.dist-info")
assert lib.get_github_repo_url(dist) is None

0 comments on commit 98b99cf

Please sign in to comment.