Skip to content

Commit

Permalink
adds tests for source_[label|url] and py-info
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tal committed Jul 8, 2015
1 parent 7c0e177 commit d06dd05
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Features
- automatic version, author, maintainer and email(s) detection (perfers
__init__.py, __version__.py)
- curses front-end to python classifiers selection
- easy display of installed python package metadata with ``py-info <package>``

Example, "Hello World" application:
-----------------------------------
Expand Down
20 changes: 15 additions & 5 deletions pypackage/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"""


from __future__ import print_function

import sys
import pkg_resources

Expand Down Expand Up @@ -101,12 +103,20 @@ def info():
env = pkg_resources.Environment()
separator = False
for arg in sys.argv[1:]:
pkg = env[pkg_resources.safe_name(arg)][0]
if not pkg:
print("The package {} was not found.".format(arg))
try:
pkg = env[pkg_resources.safe_name(arg)][0]
except IndexError:
print(
"The package {} was not found.".format(arg),
file=sys.stderr,
)
continue
elif pkg.PKG_INFO != "METADATA":
print("The package {} does not use metadata.".format(arg))

if pkg.PKG_INFO != "METADATA":
print(
"The package {} does not use metadata.".format(arg),
file=sys.stderr,
)
continue

# this is without a doubt the dumbest line of code I have written
Expand Down
1 change: 1 addition & 0 deletions pypackage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ class Metadata(object):
name = getattr(self, "name", undef)
version = getattr(self, "version", undef)
summary = getattr(self, "description", undef)
description = summary
home_page = getattr(self, "url", undef)
url = home_page
author = getattr(self, "author", undef)
Expand Down
32 changes: 32 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,38 @@ def only_binary(request, new_module):
return new_module, filename


def random_str(length):
"""Returns a random mixed case string of length provided."""

def _random_chr():
if random.randint(0, 1):
return chr(random.choice(range(65, 91)))
else:
return chr(random.choice(range(97, 122)))
return "".join([_random_chr() for _ in range(length)])


@pytest.fixture
def source_release(request, new_package):
"""Creates a package with source_label and source_url filled in.
Returns:
tuple of module root directory, source_label, source_url
"""

new_module, pkg_root = new_package
source_label = random_str(40)
source_url = "http://{}.com/{}".format(random_str(7), random_str(12))
with open(os.path.join(new_module, META_NAME), "w") as openmeta:
openmeta.write((
'{{"packages": ["find_packages()"], "source_label": "{}", '
'"source_url": "{}"}}'
).format(source_label, source_url))

request.addfinalizer(module_cleanup)
return new_module, source_label, source_url


def module_cleanup():
"""Used to cleanup the testing module."""

Expand Down
47 changes: 47 additions & 0 deletions test/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,52 @@ def test_setup_entry__multilocations(reset_sys_argv):
assert patched_config.call_count == 2


def test_info(reset_sys_argv, capfd):
"""Ensure we're looking up and dumping package metadata to stdout."""

sys.argv = ["py-info", "pytest"]
commands.info()
stdout, stderr = capfd.readouterr()

assert not stderr
assert "Name: pytest" in stdout
assert "License: MIT license" in stdout


def test_info__pkg_not_found(reset_sys_argv, capfd):
"""Ensure the error message when a package is not found."""

sys.argv = ["py-info", "some-random-non-existant-package"]
commands.info()
out, err = capfd.readouterr()

assert not out
assert "The package some-random-non-existant-package was not found." in err


def test_info__pkg_without_metadata(reset_sys_argv, capfd):
"""Verify the error message for a metadata-less package."""

mock_pkg = mock.Mock()
mock_pkg.PKG_INFO = "PKG-INFO"

mockenv = mock.Mock()
mockenv.__getitem__ = mock.Mock(return_value=[mock_pkg])

environment_patch = mock.patch.object(
commands.pkg_resources,
"Environment",
return_value=mockenv,
)

sys.argv = ["py-info", "foo-bar"]
with environment_patch:
commands.info()

out, err = capfd.readouterr()
assert not out
assert "The package foo-bar does not use metadata." in err


if __name__ == "__main__":
pytest.main(["-rx", "-v", "--pdb", __file__])
18 changes: 18 additions & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tests for pypackages' config object and helper functions."""


import io
import os
import json
import mock
import pytest
Expand Down Expand Up @@ -303,5 +305,21 @@ def test_long_read_errors_buried(with_readme):
assert not conf._long_read_in_setup


def test_write_pkg_info_shim(source_release):
"""Confirm the metadata shim to write out our extra metadata params."""

pkg_root, source_label, source_url = source_release
conf = config.get_config(pkg_root)

conf.metadata.write_pkg_info(pkg_root)
pkg_info_file = os.path.join(pkg_root, "PKG-INFO")
with io.open(pkg_info_file, "r", encoding="utf-8") as openinfo:
pkg_info = openinfo.read()

assert pkg_info.startswith("Metadata-Version: 1.0")
assert "Source-Label: {}".format(source_label) in pkg_info
assert "Source-Url: {}".format(source_url) in pkg_info


if __name__ == "__main__":
pytest.main(["-rx", "-v", "--pdb", __file__])
17 changes: 17 additions & 0 deletions test/test_installs.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,22 @@ def test_long_description_as_file(with_readme):
})


def test_source_tags(source_release):
"""Ensure the source url and label are in the package metadata."""

with mock.patch.object(setuptools, "setup") as mocked_setup:
install()

assert sys.argv == ["setup.py", "install"]
package_dir, source_label, source_url = source_release
package = os.path.basename(package_dir)
call_args = mocked_setup.call_args[1]
assert call_args["name"] == package
assert call_args["packages"] == [package]
assert "<pypackage.config" in repr(call_args["metadata"])
assert ".Metadata object" in repr(call_args["metadata"])
assert callable(call_args["metadata"].write_pkg_info)


if __name__ == "__main__":
pytest.main(["-rx", "-v", "--pdb", __file__])

0 comments on commit d06dd05

Please sign in to comment.