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): ...@@ -488,70 +488,75 @@ class TestPresenceOfGit(base.BaseTestCase):
self.assertEqual(False, git._git_is_installed()) self.assertEqual(False, git._git_is_installed())
class TestIndexInRequirements(base.BaseTestCase): class ParseRequirementsTest(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 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() tempdir = tempfile.mkdtemp()
requirements = os.path.join(tempdir, 'requirements.txt') requirements = os.path.join(tempdir, 'requirements.txt')
nested = os.path.join(tempdir, 'nested.txt')
with open(requirements, 'w') as f: with open(requirements, 'w') as f:
f.write('-r ' + nested)
with open(nested, 'w') as f:
f.write('pbr') 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) self.assertEqual(['pbr'], result)
def test_override_with_env(self):
class ParseRequirementsTest(base.BaseTestCase): """Ensure environment variable used if no files provided."""
_, tmp_file = tempfile.mkstemp(prefix='openstack', suffix='.setup')
def setUp(self): with open(tmp_file, 'w') as fh:
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:
fh.write("foo\nbar") fh.write("foo\nbar")
self.useFixture( self.useFixture(
fixtures.EnvironmentVariable('PBR_REQUIREMENTS_FILES', fixtures.EnvironmentVariable('PBR_REQUIREMENTS_FILES', tmp_file))
self.tmp_file))
self.assertEqual(['foo', 'bar'], self.assertEqual(['foo', 'bar'],
packaging.parse_requirements()) packaging.parse_requirements())
def test_parse_requirements_override_with_env_multiple_files(self): def test_override_with_env_multiple_files(self):
with open(self.tmp_file, 'w') as fh: _, tmp_file = tempfile.mkstemp(prefix='openstack', suffix='.setup')
with open(tmp_file, 'w') as fh:
fh.write("foo\nbar") fh.write("foo\nbar")
self.useFixture( self.useFixture(
fixtures.EnvironmentVariable('PBR_REQUIREMENTS_FILES', fixtures.EnvironmentVariable('PBR_REQUIREMENTS_FILES',
"no-such-file," + self.tmp_file)) "no-such-file," + tmp_file))
self.assertEqual(['foo', 'bar'], self.assertEqual(['foo', 'bar'],
packaging.parse_requirements()) packaging.parse_requirements())
def test_get_requirement_from_file_empty(self): def test_index_present(self):
actual = packaging.get_reqs_from_files([]) tempdir = tempfile.mkdtemp()
self.assertEqual([], actual) 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], with open("requirements-py%d.txt" % sys.version_info[0],
"w") as fh: "w") as fh:
fh.write("# this is a comment\nfoobar\n# and another one\nfoobaz") fh.write("# this is a comment\nfoobar\n# and another one\nfoobaz")
self.assertEqual(['foobar', 'foobaz'], self.assertEqual(['foobar', 'foobaz'],
packaging.parse_requirements()) 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: with open("requirements-py1.txt", "w") as fh:
fh.write("thisisatrap") fh.write("thisisatrap")
with open("requirements-py%d.txt" % sys.version_info[0], with open("requirements-py%d.txt" % sys.version_info[0],
...@@ -609,8 +614,8 @@ class ParseDependencyLinksTest(base.BaseTestCase): ...@@ -609,8 +614,8 @@ class ParseDependencyLinksTest(base.BaseTestCase):
def setUp(self): def setUp(self):
super(ParseDependencyLinksTest, self).setUp() super(ParseDependencyLinksTest, self).setUp()
(fd, self.tmp_file) = tempfile.mkstemp(prefix="openstack", _, self.tmp_file = tempfile.mkstemp(prefix="openstack",
suffix=".setup") suffix=".setup")
def test_parse_dependency_normal(self): def test_parse_dependency_normal(self):
with open(self.tmp_file, "w") as fh: with open(self.tmp_file, "w") as fh:
......
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