Improved mocking of Python builtins in unit tests.

This commit is contained in:
Dan Helfman 2015-06-14 11:00:46 -07:00
parent c3613e0637
commit d9125451f5
5 changed files with 22 additions and 9 deletions

4
NEWS
View file

@ -1,3 +1,7 @@
0.0.7-dev
* Improved mocking of Python builtins in unit tests.
0.0.6 0.0.6
* New configuration section for customizing which Attic consistency checks run, if any. * New configuration section for customizing which Attic consistency checks run, if any.

View file

@ -143,7 +143,7 @@ def parse_configuration(config_filename):
Raise IOError if the file cannot be read, or ValueError if the format is not as expected. Raise IOError if the file cannot be read, or ValueError if the format is not as expected.
''' '''
parser = ConfigParser() parser = ConfigParser()
parser.readfp(open(config_filename)) parser.read(config_filename)
validate_configuration_format(parser, CONFIG_FORMAT) validate_configuration_format(parser, CONFIG_FORMAT)

View file

@ -0,0 +1,11 @@
from flexmock import flexmock
import sys
def builtins_mock():
try:
# Python 2
return flexmock(sys.modules['__builtin__'])
except KeyError:
# Python 3
return flexmock(sys.modules['builtins'])

View file

@ -3,6 +3,7 @@ from collections import OrderedDict
from flexmock import flexmock from flexmock import flexmock
from atticmatic import attic as module from atticmatic import attic as module
from atticmatic.tests.builtins import builtins_mock
def insert_subprocess_mock(check_call_command, **kwargs): def insert_subprocess_mock(check_call_command, **kwargs):
@ -18,7 +19,7 @@ def insert_subprocess_never():
def insert_platform_mock(): def insert_platform_mock():
flexmock(module).platform = flexmock().should_receive('node').and_return('host').mock flexmock(module.platform).should_receive('node').and_return('host')
def insert_datetime_mock(): def insert_datetime_mock():
@ -166,8 +167,8 @@ def test_check_archives_should_call_attic_with_parameters():
) )
insert_platform_mock() insert_platform_mock()
insert_datetime_mock() insert_datetime_mock()
flexmock(module).open = lambda filename, mode: stdout builtins_mock().should_receive('open').and_return(stdout)
flexmock(module).os = flexmock().should_receive('devnull').mock flexmock(module.os).should_receive('devnull')
module.check_archives( module.check_archives(
verbose=False, verbose=False,
@ -204,5 +205,3 @@ def test_check_archives_without_any_checks_should_bail():
repository='repo', repository='repo',
consistency_config=consistency_config, consistency_config=consistency_config,
) )

View file

@ -197,9 +197,8 @@ def test_parse_section_options_for_missing_section_should_return_empty_dict():
def insert_mock_parser(): def insert_mock_parser():
parser = flexmock() parser = flexmock()
parser.should_receive('readfp') parser.should_receive('read')
flexmock(module).open = lambda filename: None module.ConfigParser = lambda: parser
flexmock(module).ConfigParser = parser
return parser return parser