Permalink
Browse files

Raise a controlled exception if popen doesn't find the command

Fix issue #11.
  • Loading branch information...
1 parent 449117d commit d323122933b09483f199814bfb9c6ddd57824491 @dvarrazzo committed Mar 13, 2012
Showing with 27 additions and 3 deletions.
  1. +7 −0 CHANGES
  2. +9 −3 pgxnclient/commands/__init__.py
  3. +3 −0 pgxnclient/errors.py
  4. +8 −0 pgxnclient/tests/test_commands.py
View
@@ -3,6 +3,13 @@
PGXN Client changes log
-----------------------
+pgxnclient 1.0.2
+================
+
+- Don't crash with a traceback if some external command is not found
+ (ticket #11).
+
+
pgxnclient 1.0.1
================
@@ -22,7 +22,7 @@
from pgxnclient import Spec, SemVer
from pgxnclient.api import Api
from pgxnclient.i18n import _, gettext
-from pgxnclient.errors import NotFound, PgxnClientException, ResourceNotFound, UserAbort
+from pgxnclient.errors import NotFound, PgxnClientException, ProcessError, ResourceNotFound, UserAbort
logger = logging.getLogger('pgxnclient.commands')
@@ -204,14 +204,20 @@ def confirm(self, prompt):
else:
prompt = _("Please answer yes or no")
- def popen(self, *args, **kwargs):
+ def popen(self, cmd, *args, **kwargs):
"""
Excecute subprocess.Popen.
Commands should use this method instead of importing subprocess.Popen:
this allows replacement with a mock in the test suite.
"""
- return Popen(*args, **kwargs)
+ try:
+ return Popen(cmd, *args, **kwargs)
+ except OSError, e:
+ if not isinstance(cmd, basestring):
+ cmd = ' '.join(cmd)
+ msg = _("%s running command: %s") % (e, cmd)
+ raise ProcessError(msg)
from pgxnclient.errors import BadSpecError
View
@@ -22,6 +22,9 @@ class UserAbort(PgxnClientException):
class BadSpecError(PgxnClientException):
"""A bad package specification."""
+class ProcessError(PgxnClientException):
+ """An error raised calling an external program."""
+
class NotFound(PgxnException):
"""Something requested by the user not found on PGXN"""
@@ -69,6 +69,14 @@ def test_info_case_insensitive(self):
"""))
+class CommandTestCase(unittest.TestCase):
+ def test_popen_raises(self):
+ from pgxnclient.commands import Command
+ c = Command([])
+ self.assertRaises(PgxnClientException,
+ c.popen, "this-script-doesnt-exist")
+
+
class DownloadTestCase(unittest.TestCase):
@patch('pgxnclient.api.get_file')
def test_download_latest(self, mock):

0 comments on commit d323122

Please sign in to comment.