Merge lp:~salgado/linaro-image-tools/fix-mocking into lp:linaro-image-tools/11.11

Proposed by Guilherme Salgado
Status: Merged
Merged at revision: 181
Proposed branch: lp:~salgado/linaro-image-tools/fix-mocking
Merge into: lp:linaro-image-tools/11.11
Diff against target: 121 lines (+35/-23)
2 files modified
media_create/tests/fixtures.py (+25/-6)
media_create/tests/test_media_create.py (+10/-17)
To merge this branch: bzr merge lp:~salgado/linaro-image-tools/fix-mocking
Reviewer Review Type Date Requested Status
James Westby (community) Approve
Review via email: mp+42656@code.launchpad.net

Description of the change

Use a fixture instead of a contextmanager to mock do_run in tests.

This is to make sure the mocking is always undone at the end of the test.

To post a comment you must log in.
Revision history for this message
James Westby (james-w) wrote :

Hi,

I think this is a good change, thanks.

47 +class MockSomethingFixture(object):

Maybe MockAttributeFixture?

We might also want a specific subclass for do_run, as I expect that will
be used a lot?

Thanks,

James

review: Approve
Revision history for this message
Guilherme Salgado (salgado) wrote :

On Fri, 2010-12-03 at 18:11 +0000, James Westby wrote:
> Review: Approve
> Hi,
>
> I think this is a good change, thanks.
>
> 47 +class MockSomethingFixture(object):
>
> Maybe MockAttributeFixture?

I thought about that but then it may mislead one into thinking it can
not mock functions/objects.

>
> We might also want a specific subclass for do_run, as I expect that will
> be used a lot?

Yes, that makes sense. I've created that.

Thanks for the review!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'media_create/tests/fixtures.py'
--- media_create/tests/fixtures.py 2010-11-30 06:08:21 +0000
+++ media_create/tests/fixtures.py 2010-12-03 17:24:08 +0000
@@ -1,10 +1,9 @@
1#!/usr/bin/python
2
3import os1import os
4import shutil2import shutil
5import subprocess3import subprocess
6import tempfile4import tempfile
75
6
8class CreateTempDirFixture(object):7class CreateTempDirFixture(object):
98
10 def __init__(self):9 def __init__(self):
@@ -16,7 +15,7 @@
16 def tearDown(self):15 def tearDown(self):
17 if os.path.exists(self.tempdir):16 if os.path.exists(self.tempdir):
18 shutil.rmtree(self.tempdir)17 shutil.rmtree(self.tempdir)
19 18
20 def get_temp_dir(self):19 def get_temp_dir(self):
21 return self.tempdir20 return self.tempdir
2221
@@ -26,16 +25,36 @@
26 def __init__(self, dir):25 def __init__(self, dir):
27 self.dir = dir26 self.dir = dir
28 self.tarball = os.path.join(self.dir + 'tarball.tar.gz')27 self.tarball = os.path.join(self.dir + 'tarball.tar.gz')
29 28
30 def setUp(self):29 def setUp(self):
31 # Create gzipped tar archive.30 # Create gzipped tar archive.
32 args = ['tar', '-czf', self.tarball, self.dir]31 args = ['tar', '-czf', self.tarball, self.dir]
33 proc = subprocess.Popen(args)32 proc = subprocess.Popen(args)
34 proc.wait()33 proc.wait()
35 34
36 def tearDown(self):35 def tearDown(self):
37 if os.path.exists(self.tarball):36 if os.path.exists(self.tarball):
38 os.remove(self.tarball)37 os.remove(self.tarball)
39 38
40 def get_tarball(self):39 def get_tarball(self):
41 return self.tarball40 return self.tarball
41
42
43class MockSomethingFixture(object):
44 """A fixture which mocks something on the given object.
45
46 Replaces attr_name on obj with the given mock, undoing that upon
47 tearDown().
48 """
49
50 def __init__(self, obj, attr_name, mock):
51 self.obj = obj
52 self.attr_name = attr_name
53 self.mock = mock
54 self.orig_attr = getattr(obj, attr_name)
55
56 def setUp(self):
57 setattr(self.obj, self.attr_name, self.mock)
58
59 def tearDown(self):
60 setattr(self.obj, self.attr_name, self.orig_attr)
4261
=== modified file 'media_create/tests/test_media_create.py'
--- media_create/tests/test_media_create.py 2010-12-01 19:08:25 +0000
+++ media_create/tests/test_media_create.py 2010-12-03 17:24:08 +0000
@@ -17,6 +17,7 @@
17from media_create.tests.fixtures import (17from media_create.tests.fixtures import (
18 CreateTempDirFixture,18 CreateTempDirFixture,
19 CreateTarballFixture,19 CreateTarballFixture,
20 MockSomethingFixture,
20 )21 )
2122
2223
@@ -104,14 +105,6 @@
104 self.assertEqual(rc, 0)105 self.assertEqual(rc, 0)
105106
106107
107@contextmanager
108def do_run_mocked(mock):
109 orig_func = cmd_runner.do_run
110 cmd_runner.do_run = mock
111 yield
112 cmd_runner.do_run = orig_func
113
114
115class MockDoRun(object):108class MockDoRun(object):
116 """A mock for do_run() which just stores the args given to it."""109 """A mock for do_run() which just stores the args given to it."""
117 args = None110 args = None
@@ -120,20 +113,20 @@
120 return 0113 return 0
121114
122115
123class TestCmdRunner(TestCase):116class TestCmdRunner(TestCaseWithFixtures):
124117
125 def test_run(self):118 def test_run(self):
126 mock = MockDoRun()119 fixture = MockSomethingFixture(cmd_runner, 'do_run', MockDoRun())
127 with do_run_mocked(mock):120 self.useFixture(fixture)
128 return_code = cmd_runner.run(['foo', 'bar', 'baz'])121 return_code = cmd_runner.run(['foo', 'bar', 'baz'])
129 self.assertEqual(0, return_code)122 self.assertEqual(0, return_code)
130 self.assertEqual(['foo', 'bar', 'baz'], mock.args)123 self.assertEqual(['foo', 'bar', 'baz'], fixture.mock.args)
131124
132 def test_run_as_root(self):125 def test_run_as_root(self):
133 mock = MockDoRun()126 fixture = MockSomethingFixture(cmd_runner, 'do_run', MockDoRun())
134 with do_run_mocked(mock):127 self.useFixture(fixture)
135 cmd_runner.run(['foo', 'bar'], as_root=True)128 cmd_runner.run(['foo', 'bar'], as_root=True)
136 self.assertEqual(['sudo', 'foo', 'bar'], mock.args)129 self.assertEqual(['sudo', 'foo', 'bar'], fixture.mock.args)
137130
138 def test_run_succeeds_on_zero_return_code(self):131 def test_run_succeeds_on_zero_return_code(self):
139 return_code = cmd_runner.run(['true'])132 return_code = cmd_runner.run(['true'])

Subscribers

People subscribed via source and target branches