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 coverage for _project – 100% coverage baby! #431

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Empty file.
54 changes: 54 additions & 0 deletions src/towncrier/test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
import sys

from subprocess import check_output
from unittest import skipIf

from twisted.trial.unittest import TestCase

from .._project import get_project_name, get_version
from .helpers import write


try:
from importlib.metadata import version
except ImportError:
version = None


class VersionFetchingTests(TestCase):
Expand Down Expand Up @@ -40,6 +48,52 @@ def test_tuple(self):
version = get_version(temp, "mytestproja")
self.assertEqual(version, "1.3.12")

@skipIf(version is None, "Needs importlib.metadata.")
def test_incremental(self):
"""
An incremental Version __version__ is picked up.
"""
pkg = "../src"

self.assertEqual(version("towncrier"), get_version(pkg, "towncrier"))
self.assertEqual("towncrier", get_project_name(pkg, "towncrier"))

def _setup_missing(self):
"""
Create a minimalistic project with missing metadata in a temporary
directory.
"""
tmp_dir = self.mktemp()
pkg = os.path.join(tmp_dir, "missing")
os.makedirs(pkg)
init = os.path.join(tmp_dir, "__init__.py")

write(init, "# nope\n")

return tmp_dir

def test_missing_version(self):
"""
Missing __version__ string leads to an exception.
"""
tmp_dir = self._setup_missing()

with self.assertRaises(Exception) as e:
get_version(tmp_dir, "missing")

self.assertEqual(
("No __version__, I don't know how else to look",), e.exception.args
)

def test_missing_version_project_name(self):
"""
Missing __version__ string leads to the package name becoming the
project name.
"""
tmp_dir = self._setup_missing()

self.assertEqual("Missing", get_project_name(tmp_dir, "missing"))

def test_unknown_type(self):
"""
A __version__ of unknown type will lead to an exception.
Expand Down