From a50feeb6821e25201121997e214c237d9210152a Mon Sep 17 00:00:00 2001 From: Jun Wang Date: Fri, 18 Mar 2016 21:49:38 +0800 Subject: [PATCH] Modify unit test cases which called 'assert_raises' function to fix pre-review test bugs on CentOS_6. Change-Id: Ibb930eb107c18526876fc58428db359f9d31e716 --- tests/component/rpm/test_buildpackage_rpm.py | 4 ++ tests/component/rpm/test_import_orig_rpm.py | 4 ++ tests/component/rpm/test_import_srpm.py | 4 ++ tests/component/rpm/test_pq_rpm.py | 4 ++ tests/component/rpm/test_rpm_ch.py | 4 ++ tests/test_rpm.py | 56 ++++++++++++++++++++++++++++ tests/test_rpm_changelog.py | 20 ++++++++++ 7 files changed, 96 insertions(+) diff --git a/tests/component/rpm/test_buildpackage_rpm.py b/tests/component/rpm/test_buildpackage_rpm.py index 88a66fc..652b152 100644 --- a/tests/component/rpm/test_buildpackage_rpm.py +++ b/tests/component/rpm/test_buildpackage_rpm.py @@ -101,8 +101,12 @@ class TestGbpRpm(RpmRepoTestBase): def test_invalid_args(self): """Check graceful exit when called with invalid args""" GitRepository.create('.') + assert_raises(SystemExit, mock_gbp, ['--git-invalid-arg']) + + """ with assert_raises(SystemExit): mock_gbp(['--git-invalid-arg']) + """ def test_outside_repo(self): """Run outside a git repository""" diff --git a/tests/component/rpm/test_import_orig_rpm.py b/tests/component/rpm/test_import_orig_rpm.py index a85366b..ebf592a 100644 --- a/tests/component/rpm/test_import_orig_rpm.py +++ b/tests/component/rpm/test_import_orig_rpm.py @@ -142,8 +142,12 @@ class TestImportOrig(ImportOrigTestBase): self._check_repo_state(repo, None, []) # Test invalid cmdline options + assert_raises(SystemExit, mock_import, ['--invalid-arg=123']) + + """ with assert_raises(SystemExit): mock_import(['--invalid-arg=123']) + """ def test_import_outside_repo(self): """Test importing when not in a git repository""" diff --git a/tests/component/rpm/test_import_srpm.py b/tests/component/rpm/test_import_srpm.py index 8c6408f..6635400 100644 --- a/tests/component/rpm/test_import_srpm.py +++ b/tests/component/rpm/test_import_srpm.py @@ -45,8 +45,12 @@ class TestImportPacked(ComponentTestBase): def test_invalid_args(self): """See that import-srpm fails gracefully if called with invalid args""" eq_(mock_import([]), 1) + assert_raises(SystemExit, mock_import, ['--invalid-arg=123']) + + """ with assert_raises(SystemExit): mock_import(['--invalid-arg=123']) + """ def test_basic_import(self): """Test importing of non-native src.rpm""" diff --git a/tests/component/rpm/test_pq_rpm.py b/tests/component/rpm/test_pq_rpm.py index 293244b..c14ea68 100644 --- a/tests/component/rpm/test_pq_rpm.py +++ b/tests/component/rpm/test_pq_rpm.py @@ -52,8 +52,12 @@ class TestPqRpm(RpmRepoTestBase): self._clear_log() # Test invalid cmdline options + assert_raises(SystemExit, mock_pq, ['--invalid-arg=123']) + + """ with assert_raises(SystemExit): mock_pq(['--invalid-arg=123']) + """ def test_import_outside_repo(self): """Run pq-rpm when not in a git repository""" diff --git a/tests/component/rpm/test_rpm_ch.py b/tests/component/rpm/test_rpm_ch.py index db03c84..4b3354b 100644 --- a/tests/component/rpm/test_rpm_ch.py +++ b/tests/component/rpm/test_rpm_ch.py @@ -56,8 +56,12 @@ class TestRpmCh(RpmRepoTestBase): """See that git-rpm-ch fails gracefully when called with invalid args""" GitRepository.create('.') + assert_raises(SystemExit, mock_ch, ['--invalid-opt']) + + """ with assert_raises(SystemExit): mock_ch(['--invalid-opt']) + """ def test_import_outside_repo(self): """Run git-rpm-ch when not in a git repository""" diff --git a/tests/test_rpm.py b/tests/test_rpm.py index 1379926..3150acb 100644 --- a/tests/test_rpm.py +++ b/tests/test_rpm.py @@ -154,10 +154,15 @@ class TestSpecFile(object): def test_parse_raw(self): """Test parsing of a valid spec file""" + assert_raises(NoSpecError, SpecFile, None, None) + assert_raises(NoSpecError, SpecFile, 'filename', 'filedata') + + """ with assert_raises(NoSpecError): SpecFile(None, None) with assert_raises(NoSpecError): SpecFile('filename', 'filedata') + """ spec_filepath = os.path.join(SPEC_DIR, 'gbp-test.spec') with open(spec_filepath, 'r') as spec_fd: @@ -229,22 +234,36 @@ class TestSpecFile(object): assert spec.protected('_patches')() == {} prev = spec.protected('_delete_tag')('invalidtag', None) + assert_raises(GbpError, spec.protected('_set_tag'), + 'Version', None, '', prev) + assert_raises(GbpError, spec.set_tag, + 'invalidtag', None, 'value') + + """ with assert_raises(GbpError): # Check that setting empty value fails spec.protected('_set_tag')('Version', None, '', prev) with assert_raises(GbpError): # Check that setting invalid tag with public method fails spec.set_tag('invalidtag', None, 'value') + """ # Mangle macros prev = spec.protected('_delete_special_macro')('patch', -1) spec.protected('_delete_special_macro')('patch', 123) spec.protected('_set_special_macro')('patch', 0, 'my new args', prev) + assert_raises(GbpError, spec.protected('_delete_special_macro'), + 'invalidmacro', 0) + assert_raises(GbpError, spec.protected('_set_special_macro'), + 'invalidmacro', 0, 'args', prev) + + """ with assert_raises(GbpError): spec.protected('_delete_special_macro')('invalidmacro', 0) with assert_raises(GbpError): spec.protected('_set_special_macro')('invalidmacro', 0, 'args', prev) + """ # Check resulting spec file spec.write_spec_file() @@ -256,12 +275,21 @@ class TestSpecFile(object): spec = SpecFileTester(spec_filepath) # Unknown/invalid section name + assert_raises(GbpError, spec.protected('_set_section'), + 'patch', 'new content\n') + """ with assert_raises(GbpError): spec.protected('_set_section')('patch', 'new content\n') + """ # Multiple sections with the same name + assert_raises(GbpError, spec.protected('_set_section'), + 'files', '%{_sysconfdir}/foo\n') + + """ with assert_raises(GbpError): spec.protected('_set_section')('files', '%{_sysconfdir}/foo\n') + """ def test_changelog(self): """Test changelog methods""" @@ -370,13 +398,27 @@ class TestUtilityFunctions(object): def test_guess_spec(self): """Test guess_spec() function""" # Spec not found + assert_raises(NoSpecError, guess_spec, + DATA_DIR, recursive=False) + + """ with assert_raises(NoSpecError): guess_spec(DATA_DIR, recursive=False) + """ + # Multiple spec files + assert_raises(NoSpecError, guess_spec, + DATA_DIR, recursive=True) + assert_raises(NoSpecError, guess_spec, + SPEC_DIR, recursive=False) + + """ with assert_raises(NoSpecError): guess_spec(DATA_DIR, recursive=True) with assert_raises(NoSpecError): guess_spec(SPEC_DIR, recursive=False) + """ + # Spec found spec = guess_spec(SPEC_DIR, recursive=False, preferred_name = 'gbp-test2.spec') @@ -398,10 +440,18 @@ class TestUtilityFunctions(object): repo.commit_all('Add spec file') # Spec not found + assert_raises(NoSpecError, guess_spec_repo, + repo, 'HEAD~1', recursive=True) + assert_raises(NoSpecError, guess_spec_repo, + repo, 'HEAD', recursive=False) + + """ with assert_raises(NoSpecError): guess_spec_repo(repo, 'HEAD~1', recursive=True) with assert_raises(NoSpecError): guess_spec_repo(repo, 'HEAD', recursive=False) + """ + # Spec found spec = guess_spec_repo(repo, 'HEAD', 'packaging', recursive=False) spec = guess_spec_repo(repo, 'HEAD', recursive=True) @@ -410,8 +460,14 @@ class TestUtilityFunctions(object): assert spec.specpath == 'packaging/gbp-test.spec' # Test spec_from_repo() + assert_raises(NoSpecError, spec_from_repo, + repo, 'HEAD~1', 'packaging/gbp-test.spec') + + """ with assert_raises(NoSpecError): spec_from_repo(repo, 'HEAD~1', 'packaging/gbp-test.spec') + """ + spec = spec_from_repo(repo, 'HEAD', 'packaging/gbp-test.spec') assert spec.specfile == 'gbp-test.spec' diff --git a/tests/test_rpm_changelog.py b/tests/test_rpm_changelog.py index 2973293..22739f0 100644 --- a/tests/test_rpm_changelog.py +++ b/tests/test_rpm_changelog.py @@ -40,8 +40,12 @@ class TestChangelogHeader(object): """Test missing properties""" time = datetime(2014, 01, 29, 12, 13, 14) header = _ChangelogHeader(RpmPkgPolicy, time, name="John", revision="1") + assert_raises(ChangelogError, str, header) + + """ with assert_raises(ChangelogError): str(header) + """ def test_container(self): """Test the container methods of the class""" @@ -190,6 +194,16 @@ class TestChangelogParser(object): def test_parse_section_fail(self): """Basic tests for failures of changelog section parsing""" + assert_raises(ChangelogError, self.parser.parse_section, + self.cl_broken_header_1) + assert_raises(ChangelogError, self.parser.parse_section, + self.cl_broken_header_2) + assert_raises(ChangelogError, self.parser.parse_section, + self.cl_broken_header_3) + assert_raises(ChangelogError, self.parser.parse_section, + self.cl_broken_header_4) + + """ with assert_raises(ChangelogError): self.parser.parse_section(self.cl_broken_header_1) @@ -201,11 +215,17 @@ class TestChangelogParser(object): with assert_raises(ChangelogError): self.parser.parse_section(self.cl_broken_header_4) + """ def test_parse_changelog_fail(self): """Basic tests for changelog parsing failures""" + assert_raises(ChangelogError, self.parser.raw_parse_string, + self.cl_broken_header_5) + + """ with assert_raises(ChangelogError): self.parser.raw_parse_string(self.cl_broken_header_5) + """ class TestChangelog(object): -- 2.7.4