add unit tests and fix exception clause compatibility

Exceptions are now compatible with both python 2.4 and 3.3,
and we have unit tests to detect regressions.

The syntax for capturing exceptions changed in python 3, but
we need to continue supporting python 2.4 syntax since many
people use tito on RHEL 5, which has python 2.4.

http://docs.python.org/dev/howto/pyporting.html#capturing-the-currently-raised-exception
This commit is contained in:
Paul Morgan 2014-02-15 22:55:14 +00:00
parent 1597d7956a
commit 1182b13713
5 changed files with 41 additions and 5 deletions

11
HACKING
View file

@ -9,6 +9,17 @@ See http://docs.python.org/dev/howto/pyporting.html
and http://python3porting.com/differences.html
for tips on writing portable Python code.
In particular, you must capture exceptions in a way that's
compatible with both python 2.4 and 3.x. For example:
try:
raise Exception()
except Exception:
import sys
exc = sys.exc_info()[1]
# Current exception is 'exc'.
pass
Tests
-----

View file

@ -227,13 +227,15 @@ class BuilderBase(object):
except (KeyboardInterrupt, SystemExit):
print ""
exit(1)
except RunCommandException, err:
except RunCommandException:
err = sys.exc_info()[1]
msg = str(err)
if (re.search('Failed build dependencies', err.output)):
msg = "Please run 'yum-builddep %s' as root." % \
find_spec_file(self.relative_project_dir)
error_out('%s' % msg)
except Exception, err:
except Exception:
err = sys.exc_info()[1]
error_out('%s' % str(err))
print(output)
files_written = find_wrote_in_rpmbuild_output(output)

View file

@ -709,7 +709,8 @@ class TagModule(BaseCliModule):
try:
return tagger.run(self.options)
except TitoException, e:
except TitoException:
e = sys.exc_info()[1]
error_out(e.message)
def _validate_options(self):

View file

@ -16,6 +16,7 @@ Code for submitting builds for release.
import copy
import os
import sys
import commands
import tempfile
import subprocess
@ -454,7 +455,7 @@ class YumRepoReleaser(RsyncReleaser):
if artifact.endswith(".rpm") and not artifact.endswith(".src.rpm"):
try:
header = self._read_rpm_header(rpm_ts, artifact)
except rpm.error, e:
except rpm.error:
continue
self.new_rpm_dep_sets[header['name']] = header.dsOfHeader()
@ -467,7 +468,8 @@ class YumRepoReleaser(RsyncReleaser):
full_path = os.path.join(temp_dir, filename)
try:
hdr = self._read_rpm_header(rpm_ts, full_path)
except rpm.error, e:
except rpm.error:
e = sys.exc_info()[1]
print "error reading rpm header in '%s': %s" % (full_path, e)
continue
if hdr['name'] in self.new_rpm_dep_sets:

View file

@ -64,3 +64,23 @@ class TestPep8(TitoUnitTestFixture):
self.assertEqual(result, 0,
"Found PEP8 errors that may break your code in Python 3.")
class UglyHackishTest(TitoUnitTestFixture):
def setUp(self):
TitoUnitTestFixture.setUp(self)
os.chdir(REPO_DIR)
def test_exceptions_2_dot_4(self):
# detect 'except rpm.error as e:'
regex = "'^[[:space:]]*except .* as .*:'"
cmd = "find . -type f -regex '.*\.py$' -exec egrep %s {} + | wc -l" % regex
result = int(getoutput(cmd))
self.assertEqual(result, 0, "Found except clause not supported in Python 2.4")
def test_exceptions_3(self):
# detect 'except rpm.error, e:'
regex = "'^[[:space:]]*except [^,]+,[[:space:]]*[[:alpha:]]+:'"
cmd = "find . -type f -regex '.*\.py$' -exec egrep %s {} + | wc -l" % regex
result = int(getoutput(cmd))
self.assertEqual(result, 0, "Found except clause not supported in Python 3")