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
1=== modified file 'media_create/tests/fixtures.py'
2--- media_create/tests/fixtures.py 2010-11-30 06:08:21 +0000
3+++ media_create/tests/fixtures.py 2010-12-03 17:24:08 +0000
4@@ -1,10 +1,9 @@
5-#!/usr/bin/python
6-
7 import os
8 import shutil
9 import subprocess
10 import tempfile
11
12+
13 class CreateTempDirFixture(object):
14
15 def __init__(self):
16@@ -16,7 +15,7 @@
17 def tearDown(self):
18 if os.path.exists(self.tempdir):
19 shutil.rmtree(self.tempdir)
20-
21+
22 def get_temp_dir(self):
23 return self.tempdir
24
25@@ -26,16 +25,36 @@
26 def __init__(self, dir):
27 self.dir = dir
28 self.tarball = os.path.join(self.dir + 'tarball.tar.gz')
29-
30+
31 def setUp(self):
32 # Create gzipped tar archive.
33 args = ['tar', '-czf', self.tarball, self.dir]
34 proc = subprocess.Popen(args)
35 proc.wait()
36-
37+
38 def tearDown(self):
39 if os.path.exists(self.tarball):
40 os.remove(self.tarball)
41-
42+
43 def get_tarball(self):
44 return self.tarball
45+
46+
47+class MockSomethingFixture(object):
48+ """A fixture which mocks something on the given object.
49+
50+ Replaces attr_name on obj with the given mock, undoing that upon
51+ tearDown().
52+ """
53+
54+ def __init__(self, obj, attr_name, mock):
55+ self.obj = obj
56+ self.attr_name = attr_name
57+ self.mock = mock
58+ self.orig_attr = getattr(obj, attr_name)
59+
60+ def setUp(self):
61+ setattr(self.obj, self.attr_name, self.mock)
62+
63+ def tearDown(self):
64+ setattr(self.obj, self.attr_name, self.orig_attr)
65
66=== modified file 'media_create/tests/test_media_create.py'
67--- media_create/tests/test_media_create.py 2010-12-01 19:08:25 +0000
68+++ media_create/tests/test_media_create.py 2010-12-03 17:24:08 +0000
69@@ -17,6 +17,7 @@
70 from media_create.tests.fixtures import (
71 CreateTempDirFixture,
72 CreateTarballFixture,
73+ MockSomethingFixture,
74 )
75
76
77@@ -104,14 +105,6 @@
78 self.assertEqual(rc, 0)
79
80
81-@contextmanager
82-def do_run_mocked(mock):
83- orig_func = cmd_runner.do_run
84- cmd_runner.do_run = mock
85- yield
86- cmd_runner.do_run = orig_func
87-
88-
89 class MockDoRun(object):
90 """A mock for do_run() which just stores the args given to it."""
91 args = None
92@@ -120,20 +113,20 @@
93 return 0
94
95
96-class TestCmdRunner(TestCase):
97+class TestCmdRunner(TestCaseWithFixtures):
98
99 def test_run(self):
100- mock = MockDoRun()
101- with do_run_mocked(mock):
102- return_code = cmd_runner.run(['foo', 'bar', 'baz'])
103+ fixture = MockSomethingFixture(cmd_runner, 'do_run', MockDoRun())
104+ self.useFixture(fixture)
105+ return_code = cmd_runner.run(['foo', 'bar', 'baz'])
106 self.assertEqual(0, return_code)
107- self.assertEqual(['foo', 'bar', 'baz'], mock.args)
108+ self.assertEqual(['foo', 'bar', 'baz'], fixture.mock.args)
109
110 def test_run_as_root(self):
111- mock = MockDoRun()
112- with do_run_mocked(mock):
113- cmd_runner.run(['foo', 'bar'], as_root=True)
114- self.assertEqual(['sudo', 'foo', 'bar'], mock.args)
115+ fixture = MockSomethingFixture(cmd_runner, 'do_run', MockDoRun())
116+ self.useFixture(fixture)
117+ cmd_runner.run(['foo', 'bar'], as_root=True)
118+ self.assertEqual(['sudo', 'foo', 'bar'], fixture.mock.args)
119
120 def test_run_succeeds_on_zero_return_code(self):
121 return_code = cmd_runner.run(['true'])

Subscribers

People subscribed via source and target branches