From 1578b445368e49c765b6fe4df741b44755f0eb83 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 14 Jun 2015 11:00:46 -0700 Subject: [PATCH] Improved mocking of Python builtins in unit tests. --- NEWS | 4 ++++ atticmatic/config.py | 2 +- atticmatic/tests/builtins.py | 11 +++++++++++ atticmatic/tests/unit/test_attic.py | 9 ++++----- atticmatic/tests/unit/test_config.py | 5 ++--- 5 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 atticmatic/tests/builtins.py diff --git a/NEWS b/NEWS index 5dbc3bc..302227f 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +0.0.7-dev + + * Improved mocking of Python builtins in unit tests. + 0.0.6 * New configuration section for customizing which Attic consistency checks run, if any. diff --git a/atticmatic/config.py b/atticmatic/config.py index 8254c88..7474f05 100644 --- a/atticmatic/config.py +++ b/atticmatic/config.py @@ -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. ''' parser = ConfigParser() - parser.readfp(open(config_filename)) + parser.read(config_filename) validate_configuration_format(parser, CONFIG_FORMAT) diff --git a/atticmatic/tests/builtins.py b/atticmatic/tests/builtins.py new file mode 100644 index 0000000..ff48f93 --- /dev/null +++ b/atticmatic/tests/builtins.py @@ -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']) diff --git a/atticmatic/tests/unit/test_attic.py b/atticmatic/tests/unit/test_attic.py index 26c6299..ab52850 100644 --- a/atticmatic/tests/unit/test_attic.py +++ b/atticmatic/tests/unit/test_attic.py @@ -3,6 +3,7 @@ from collections import OrderedDict from flexmock import flexmock from atticmatic import attic as module +from atticmatic.tests.builtins import builtins_mock def insert_subprocess_mock(check_call_command, **kwargs): @@ -18,7 +19,7 @@ def insert_subprocess_never(): 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(): @@ -166,8 +167,8 @@ def test_check_archives_should_call_attic_with_parameters(): ) insert_platform_mock() insert_datetime_mock() - flexmock(module).open = lambda filename, mode: stdout - flexmock(module).os = flexmock().should_receive('devnull').mock + builtins_mock().should_receive('open').and_return(stdout) + flexmock(module.os).should_receive('devnull') module.check_archives( verbose=False, @@ -204,5 +205,3 @@ def test_check_archives_without_any_checks_should_bail(): repository='repo', consistency_config=consistency_config, ) - - diff --git a/atticmatic/tests/unit/test_config.py b/atticmatic/tests/unit/test_config.py index 1c9fcb6..02f1f52 100644 --- a/atticmatic/tests/unit/test_config.py +++ b/atticmatic/tests/unit/test_config.py @@ -197,9 +197,8 @@ def test_parse_section_options_for_missing_section_should_return_empty_dict(): def insert_mock_parser(): parser = flexmock() - parser.should_receive('readfp') - flexmock(module).open = lambda filename: None - flexmock(module).ConfigParser = parser + parser.should_receive('read') + module.ConfigParser = lambda: parser return parser