Commit 6c8455c8 authored by Stephen Finucane's avatar Stephen Finucane

tests: Increase coverage of requirements parsing

This is mostly an exercise in refactoring, moving tests into a single
test case. An additional test is added, however, verifying that default
requirement files are indeed used if nothing else is provided.

Change-Id: I8dc6cc8c50f1280f24731480501fab61fc401809
parent ec210af0
......@@ -488,70 +488,75 @@ class TestPresenceOfGit(base.BaseTestCase):
self.assertEqual(False, git._git_is_installed())
class TestIndexInRequirements(base.BaseTestCase):
def test_index_in_requirement(self):
tempdir = tempfile.mkdtemp()
requirements = os.path.join(tempdir, 'requirements.txt')
with open(requirements, 'w') as f:
f.write('-i https://myindex.local')
f.write(' --index-url https://myindex.local')
f.write(' --extra-index-url https://myindex.local')
result = packaging.parse_requirements([requirements])
self.assertEqual([], result)
class ParseRequirementsTest(base.BaseTestCase):
class TestNestedRequirements(base.BaseTestCase):
def test_empty_requirements(self):
actual = packaging.parse_requirements([])
self.assertEqual([], actual)
def test_nested_requirement(self):
def test_default_requirements(self):
"""Ensure default files used if no files provided."""
tempdir = tempfile.mkdtemp()
requirements = os.path.join(tempdir, 'requirements.txt')
nested = os.path.join(tempdir, 'nested.txt')
with open(requirements, 'w') as f:
f.write('-r ' + nested)
with open(nested, 'w') as f:
f.write('pbr')
result = packaging.parse_requirements([requirements])
# the defaults are relative to where pbr is called from so we need to
# override them. This is OK, however, as we want to validate that
# defaults are used - not what those defaults are
with mock.patch.object(packaging, 'REQUIREMENTS_FILES', (
requirements,)):
result = packaging.parse_requirements()
self.assertEqual(['pbr'], result)
class ParseRequirementsTest(base.BaseTestCase):
def setUp(self):
super(ParseRequirementsTest, self).setUp()
(fd, self.tmp_file) = tempfile.mkstemp(prefix='openstack',
suffix='.setup')
def test_parse_requirements_override_with_env(self):
with open(self.tmp_file, 'w') as fh:
def test_override_with_env(self):
"""Ensure environment variable used if no files provided."""
_, tmp_file = tempfile.mkstemp(prefix='openstack', suffix='.setup')
with open(tmp_file, 'w') as fh:
fh.write("foo\nbar")
self.useFixture(
fixtures.EnvironmentVariable('PBR_REQUIREMENTS_FILES',
self.tmp_file))
fixtures.EnvironmentVariable('PBR_REQUIREMENTS_FILES', tmp_file))
self.assertEqual(['foo', 'bar'],
packaging.parse_requirements())
def test_parse_requirements_override_with_env_multiple_files(self):
with open(self.tmp_file, 'w') as fh:
def test_override_with_env_multiple_files(self):
_, tmp_file = tempfile.mkstemp(prefix='openstack', suffix='.setup')
with open(tmp_file, 'w') as fh:
fh.write("foo\nbar")
self.useFixture(
fixtures.EnvironmentVariable('PBR_REQUIREMENTS_FILES',
"no-such-file," + self.tmp_file))
"no-such-file," + tmp_file))
self.assertEqual(['foo', 'bar'],
packaging.parse_requirements())
def test_get_requirement_from_file_empty(self):
actual = packaging.get_reqs_from_files([])
self.assertEqual([], actual)
def test_index_present(self):
tempdir = tempfile.mkdtemp()
requirements = os.path.join(tempdir, 'requirements.txt')
with open(requirements, 'w') as f:
f.write('-i https://myindex.local')
f.write(' --index-url https://myindex.local')
f.write(' --extra-index-url https://myindex.local')
result = packaging.parse_requirements([requirements])
self.assertEqual([], result)
def test_nested_requirements(self):
tempdir = tempfile.mkdtemp()
requirements = os.path.join(tempdir, 'requirements.txt')
nested = os.path.join(tempdir, 'nested.txt')
with open(requirements, 'w') as f:
f.write('-r ' + nested)
with open(nested, 'w') as f:
f.write('pbr')
result = packaging.parse_requirements([requirements])
self.assertEqual(['pbr'], result)
def test_parse_requirements_python_version(self):
def test_python_version(self):
with open("requirements-py%d.txt" % sys.version_info[0],
"w") as fh:
fh.write("# this is a comment\nfoobar\n# and another one\nfoobaz")
self.assertEqual(['foobar', 'foobaz'],
packaging.parse_requirements())
def test_parse_requirements_right_python_version(self):
def test_python_version_multiple_options(self):
with open("requirements-py1.txt", "w") as fh:
fh.write("thisisatrap")
with open("requirements-py%d.txt" % sys.version_info[0],
......@@ -609,7 +614,7 @@ class ParseDependencyLinksTest(base.BaseTestCase):
def setUp(self):
super(ParseDependencyLinksTest, self).setUp()
(fd, self.tmp_file) = tempfile.mkstemp(prefix="openstack",
_, self.tmp_file = tempfile.mkstemp(prefix="openstack",
suffix=".setup")
def test_parse_dependency_normal(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment