diff options
author | Stephen Finucane <sfinucan@redhat.com> | 2017-12-06 11:26:15 +0000 |
---|---|---|
committer | Stephen Finucane <sfinucan@redhat.com> | 2017-12-06 11:34:25 +0000 |
commit | 6c8455c8c53bd0be32e13b9ca4efad4e25015b5c (patch) | |
tree | 2f27f1018d7c25e0cc5d611f812592996e12945f | |
parent | ec210af0f57e726c0bc3f897bc4c99542cf38882 (diff) | |
download | pbr-6c8455c8c53bd0be32e13b9ca4efad4e25015b5c.tar.gz |
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
-rw-r--r-- | pbr/tests/test_packaging.py | 85 |
1 files changed, 45 insertions, 40 deletions
diff --git a/pbr/tests/test_packaging.py b/pbr/tests/test_packaging.py index 64f63ce..526e12d 100644 --- a/pbr/tests/test_packaging.py +++ b/pbr/tests/test_packaging.py @@ -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,8 +614,8 @@ class ParseDependencyLinksTest(base.BaseTestCase): def setUp(self): super(ParseDependencyLinksTest, self).setUp() - (fd, self.tmp_file) = tempfile.mkstemp(prefix="openstack", - suffix=".setup") + _, self.tmp_file = tempfile.mkstemp(prefix="openstack", + suffix=".setup") def test_parse_dependency_normal(self): with open(self.tmp_file, "w") as fh: |