| Matthew Treinish | 8708621 | 2013-10-28 20:21:54 +0000 | [diff] [blame^] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 | 
|  | 2 |  | 
|  | 3 | # Copyright 2013 IBM Corp. | 
|  | 4 | # | 
|  | 5 | #    Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 6 | #    not use this file except in compliance with the License. You may obtain | 
|  | 7 | #    a copy of the License at | 
|  | 8 | # | 
|  | 9 | #         http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | # | 
|  | 11 | #    Unless required by applicable law or agreed to in writing, software | 
|  | 12 | #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 13 | #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | 
|  | 14 | #    License for the specific language governing permissions and limitations | 
|  | 15 | #    under the License. | 
|  | 16 |  | 
|  | 17 | import os | 
|  | 18 |  | 
|  | 19 | import fixtures | 
|  | 20 | import mox | 
|  | 21 | import stubout | 
|  | 22 | import testtools | 
|  | 23 |  | 
|  | 24 |  | 
|  | 25 | class MoxStubout(fixtures.Fixture): | 
|  | 26 | """Deal with code around mox and stubout as a fixture.""" | 
|  | 27 |  | 
|  | 28 | def setUp(self): | 
|  | 29 | super(MoxStubout, self).setUp() | 
|  | 30 | # emulate some of the mox stuff, we can't use the metaclass | 
|  | 31 | # because it screws with our generators | 
|  | 32 | self.mox = mox.Mox() | 
|  | 33 | self.stubs = stubout.StubOutForTesting() | 
|  | 34 | self.addCleanup(self.stubs.UnsetAll) | 
|  | 35 | self.addCleanup(self.stubs.SmartUnsetAll) | 
|  | 36 | self.addCleanup(self.mox.UnsetStubs) | 
|  | 37 | self.addCleanup(self.mox.VerifyAll) | 
|  | 38 |  | 
|  | 39 |  | 
|  | 40 | class TestCase(testtools.TestCase): | 
|  | 41 |  | 
|  | 42 | def setUp(self): | 
|  | 43 | super(TestCase, self).setUp() | 
|  | 44 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or | 
|  | 45 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): | 
|  | 46 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream | 
|  | 47 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) | 
|  | 48 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or | 
|  | 49 | os.environ.get('OS_STDERR_CAPTURE') == '1'): | 
|  | 50 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream | 
|  | 51 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) | 
|  | 52 |  | 
|  | 53 | mox_fixture = self.useFixture(MoxStubout()) | 
|  | 54 | self.mox = mox_fixture.mox | 
|  | 55 | self.stubs = mox_fixture.stubs |