Merge lp:~frankban/python-shelltoolbox/apt-and-run into lp:python-shelltoolbox

Proposed by Francesco Banconi
Status: Merged
Approved by: Gary Poster
Approved revision: 15
Merged at revision: 15
Proposed branch: lp:~frankban/python-shelltoolbox/apt-and-run
Merge into: lp:python-shelltoolbox
Diff against target: 97 lines (+37/-3)
3 files modified
setup.py (+1/-1)
shelltoolbox/__init__.py (+4/-2)
tests.py (+32/-0)
To merge this branch: bzr merge lp:~frankban/python-shelltoolbox/apt-and-run
Reviewer Review Type Date Requested Status
Gary Poster (community) Approve
Review via email: mp+98188@code.launchpad.net

Description of the change

== Changes ==

The helper *apt_get_install* now accepts a *caller* keyword argument that defaults to *run*.
This is required by lpsetup, and this way we can easily test the function too.

Fixed a problem in *run* helper error handling: calling an invalid command and passing stdout (or stderr) = None resulted in a concatenation of str and None.

Bumped version up a little.

== Tests ==

$ python tests.py
...........................................................
----------------------------------------------------------------------
Ran 59 tests in 0.147s

OK

To post a comment you must log in.
Revision history for this message
Gary Poster (gary) wrote :

Nice changes. Thank you.

review: Approve
Revision history for this message
Francesco Banconi (frankban) wrote :

Thanks Gary.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'setup.py'
--- setup.py 2012-03-05 15:16:17 +0000
+++ setup.py 2012-03-19 11:52:20 +0000
@@ -9,7 +9,7 @@
9ez_setup.use_setuptools()9ez_setup.use_setuptools()
1010
1111
12__version__ = '0.2.0'12__version__ = '0.2.1'
1313
14from setuptools import setup14from setuptools import setup
1515
1616
=== modified file 'shelltoolbox/__init__.py'
--- shelltoolbox/__init__.py 2012-03-07 10:08:35 +0000
+++ shelltoolbox/__init__.py 2012-03-19 11:52:20 +0000
@@ -71,10 +71,11 @@
7171
72 :raises: subprocess.CalledProcessError72 :raises: subprocess.CalledProcessError
73 """73 """
74 caller = kwargs.pop('caller', run)
74 debian_frontend = kwargs.pop('DEBIAN_FRONTEND', 'noninteractive')75 debian_frontend = kwargs.pop('DEBIAN_FRONTEND', 'noninteractive')
75 with environ(DEBIAN_FRONTEND=debian_frontend, **kwargs):76 with environ(DEBIAN_FRONTEND=debian_frontend, **kwargs):
76 cmd = ('apt-get', '-y', 'install') + args77 cmd = ('apt-get', '-y', 'install') + args
77 return run(*cmd)78 return caller(*cmd)
7879
7980
80def bzr_whois(user):81def bzr_whois(user):
@@ -417,8 +418,9 @@
417 close_fds=kwargs.pop('close_fds', True), **kwargs)418 close_fds=kwargs.pop('close_fds', True), **kwargs)
418 stdout, stderr = process.communicate()419 stdout, stderr = process.communicate()
419 if process.returncode:420 if process.returncode:
421 output = ''.join(filter(None, [stdout, stderr]))
420 raise subprocess.CalledProcessError(422 raise subprocess.CalledProcessError(
421 process.returncode, repr(args), output=stdout+stderr)423 process.returncode, repr(args), output=output)
422 return stdout424 return stdout
423425
424426
425427
=== modified file 'tests.py'
--- tests.py 2012-03-07 10:08:35 +0000
+++ tests.py 2012-03-19 11:52:20 +0000
@@ -13,6 +13,7 @@
13import unittest13import unittest
1414
15from shelltoolbox import (15from shelltoolbox import (
16 apt_get_install,
16 cd,17 cd,
17 command,18 command,
18 DictDiffer,19 DictDiffer,
@@ -34,6 +35,33 @@
34 )35 )
3536
3637
38class TestAptGetInstall(unittest.TestCase):
39
40 packages = ('package1', 'package2')
41
42 def _get_caller(self, **kwargs):
43 def caller(*args):
44 for k, v in kwargs.items():
45 self.assertEqual(v, os.getenv(k))
46 return caller
47
48 def test_caller(self):
49 # Ensure the correct command line is passed to caller.
50 cmd = apt_get_install(*self.packages, caller=lambda *args: args)
51 expected = ('apt-get', '-y', 'install') + self.packages
52 self.assertTupleEqual(expected, cmd)
53
54 def test_non_interactive_dpkg(self):
55 # Ensure dpkg is called in non interactive mode.
56 caller = self._get_caller(DEBIAN_FRONTEND='noninteractive')
57 apt_get_install(*self.packages, caller=caller)
58
59 def test_env_vars(self):
60 # Ensure apt can be run using custom environment variables.
61 caller = self._get_caller(DEBIAN_FRONTEND='noninteractive', LANG='C')
62 apt_get_install(*self.packages, caller=caller, LANG='C')
63
64
37class TestCdContextManager(unittest.TestCase):65class TestCdContextManager(unittest.TestCase):
3866
39 def test_cd(self):67 def test_cd(self):
@@ -335,6 +363,10 @@
335 self.assertEqual("['ls', '--not a valid switch']", exception.cmd)363 self.assertEqual("['ls', '--not a valid switch']", exception.cmd)
336 self.assertIn('unrecognized option', exception.output)364 self.assertIn('unrecognized option', exception.output)
337365
366 def testErrorRaisedStdoutNotRedirected(self):
367 with self.assertRaises(CalledProcessError):
368 run('ls', '--not a valid switch', stdout=None)
369
338 def testNoneArguments(self):370 def testNoneArguments(self):
339 # Ensure None is ignored when passed as positional argument.371 # Ensure None is ignored when passed as positional argument.
340 self.assertIn('Usage:', run('/bin/ls', None, '--help', None))372 self.assertIn('Usage:', run('/bin/ls', None, '--help', None))

Subscribers

People subscribed via source and target branches