Skip to content

Commit

Permalink
Convert all unit test with statements to be nested
Browse files Browse the repository at this point in the history
Python 2.6 does not support multi-clause 'with' statements, so all of these
needed to be converted to nested with statements. Created some trouble with
keeping things within 80 chars for PEP8 compliance.
  • Loading branch information
sirosen committed Jul 27, 2014
1 parent 4c392eb commit 5771e80
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 181 deletions.
8 changes: 4 additions & 4 deletions tests/unit/block/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def mock_expanduser(string):
dummy_exec_context = ExecutionContext()
dummy_logger = Logger(dummy_exec_context)

with mock.patch('os.path.expanduser', mock_expanduser), \
mock.patch('salve.exec_context', dummy_exec_context), \
mock.patch('salve.logger', dummy_logger):
dummy_conf = salve.settings.config.SALVEConfig()
with mock.patch('os.path.expanduser', mock_expanduser):
with mock.patch('salve.exec_context', dummy_exec_context):
with mock.patch('salve.logger', dummy_logger):
dummy_conf = salve.settings.config.SALVEConfig()

# must be set after conf is created, otherwise they will be overidden by
# config initialization
Expand Down
21 changes: 11 additions & 10 deletions tests/unit/block/directory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def dir_create_chown_as_root():
b.set('target', '/p/q/r')
b.set('user', 'user1')
b.set('group', 'nogroup')
with mock.patch('salve.util.ugo.is_root', lambda: True), \
mock.patch('salve.logger', dummy_logger):
dir_act = b.compile()
with mock.patch('salve.util.ugo.is_root', lambda: True):
with mock.patch('salve.logger', dummy_logger):
dir_act = b.compile()

assert isinstance(dir_act, action.ActionList)
assert len(dir_act.actions) == 2
Expand All @@ -120,9 +120,9 @@ def empty_dir_copy_compile():
b.set('action', 'copy')
b.set('source', '/a/b/c')
b.set('target', '/p/q/r')
with mock.patch('os.walk', lambda d: []), \
mock.patch('salve.logger', dummy_logger):
dir_act = b.compile()
with mock.patch('os.walk', lambda d: []):
with mock.patch('salve.logger', dummy_logger):
dir_act = b.compile()

assert isinstance(dir_act, action.ActionList)
assert len(dir_act.actions) == 1
Expand All @@ -145,10 +145,11 @@ def dir_copy_chown_as_root():
b.set('target', '/p/q/r')
b.set('user', 'user1')
b.set('group', 'nogroup')
with mock.patch('salve.util.ugo.is_root', lambda: True), \
mock.patch('os.walk', lambda d: []), \
mock.patch('salve.logger', dummy_logger):
al = b.compile()

with mock.patch('salve.util.ugo.is_root', lambda: True):
with mock.patch('os.walk', lambda d: []):
with mock.patch('salve.logger', dummy_logger):
al = b.compile()

assert isinstance(al, action.ActionList)
assert len(al.actions) == 2
Expand Down
66 changes: 34 additions & 32 deletions tests/unit/block/file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def file_copy_compile():
b.set('group', 'nogroup')
b.set('mode', '600')

with mock.patch('salve.logger', dummy_logger), \
mock.patch('salve.exec_context', dummy_exec_context):
act = b.compile()
with mock.patch('salve.logger', dummy_logger):
with mock.patch('salve.exec_context', dummy_exec_context):
act = b.compile()

assert isinstance(act, action.ActionList)
assert len(act.actions) == 4
Expand Down Expand Up @@ -78,10 +78,10 @@ def file_create_compile():
b.set('user', 'user1')
b.set('group', 'nogroup')
b.set('mode', '0600')
with mock.patch('os.path.exists', lambda f: True), \
mock.patch('salve.util.ugo.is_root', lambda: False), \
mock.patch('salve.logger', dummy_logger):
file_act = b.compile()
with mock.patch('os.path.exists', lambda f: True):
with mock.patch('salve.util.ugo.is_root', lambda: False):
with mock.patch('salve.logger', dummy_logger):
file_act = b.compile()

assert isinstance(file_act, action.ActionList)
assert len(file_act.actions) == 3
Expand Down Expand Up @@ -123,10 +123,10 @@ def file_copy_nouser():
b.set('group', 'nogroup')
b.set('mode', '0600')

with mock.patch('salve.util.ugo.is_root', lambda: True), \
mock.patch('salve.exec_context', dummy_exec_context), \
mock.patch('salve.logger', dummy_logger):
file_act = b.compile()
with mock.patch('salve.util.ugo.is_root', lambda: True):
with mock.patch('salve.exec_context', dummy_exec_context):
with mock.patch('salve.logger', dummy_logger):
file_act = b.compile()

assert isinstance(file_act, action.ActionList)
assert len(file_act.actions) == 3
Expand Down Expand Up @@ -160,10 +160,10 @@ def file_create_nouser():
b.set('mode', '0600')

# skip backup just to generate a simpler action
with mock.patch('salve.util.ugo.is_root', lambda: True), \
mock.patch('os.path.exists', lambda f: False), \
mock.patch('salve.logger', dummy_logger):
file_act = b.compile()
with mock.patch('salve.util.ugo.is_root', lambda: True):
with mock.patch('os.path.exists', lambda f: False):
with mock.patch('salve.logger', dummy_logger):
file_act = b.compile()

assert isinstance(file_act, action.ActionList)
assert len(file_act.actions) == 2
Expand Down Expand Up @@ -192,11 +192,11 @@ def file_copy_nogroup():
b.set('mode', '0600')

# skip backup just to generate a simpler action
with mock.patch('salve.util.ugo.is_root', lambda: True), \
mock.patch('os.path.exists', lambda f: False), \
mock.patch('salve.exec_context', dummy_exec_context), \
mock.patch('salve.logger', dummy_logger):
file_act = b.compile()
with mock.patch('salve.util.ugo.is_root', lambda: True):
with mock.patch('os.path.exists', lambda f: False):
with mock.patch('salve.exec_context', dummy_exec_context):
with mock.patch('salve.logger', dummy_logger):
file_act = b.compile()

assert isinstance(file_act, action.ActionList)
assert len(file_act.actions) == 3
Expand Down Expand Up @@ -230,10 +230,10 @@ def file_create_nogroup():
b.set('mode', '0600')

# skip backup just to generate a simpler action
with mock.patch('salve.util.ugo.is_root', lambda: True), \
mock.patch('os.path.exists', lambda f: False), \
mock.patch('salve.logger', dummy_logger):
file_act = b.compile()
with mock.patch('salve.util.ugo.is_root', lambda: True):
with mock.patch('os.path.exists', lambda f: False):
with mock.patch('salve.logger', dummy_logger):
file_act = b.compile()

assert isinstance(file_act, action.ActionList)
assert len(file_act.actions) == 2
Expand All @@ -260,11 +260,12 @@ def file_copy_nomode():
b.set('target', '/p/q/r')
b.set('user', 'user1')
b.set('group', 'nogroup')

# skip chown, for simplicity
with mock.patch('salve.util.ugo.is_root', lambda: False), \
mock.patch('salve.logger', dummy_logger), \
mock.patch('salve.exec_context', dummy_exec_context):
file_act = b.compile()
with mock.patch('salve.util.ugo.is_root', lambda: False):
with mock.patch('salve.logger', dummy_logger):
with mock.patch('salve.exec_context', dummy_exec_context):
file_act = b.compile()

assert isinstance(file_act, action.ActionList)
assert len(file_act.actions) == 3
Expand Down Expand Up @@ -298,11 +299,12 @@ def file_create_nomode():
b.set('target', '/p/q/r')
b.set('user', 'user1')
b.set('group', 'nogroup')

# skip chown and backup just to generate a simpler action
with mock.patch('salve.util.ugo.is_root', lambda: False), \
mock.patch('os.path.exists', lambda f: False), \
mock.patch('salve.logger', dummy_logger):
act = b.compile()
with mock.patch('salve.util.ugo.is_root', lambda: False):
with mock.patch('os.path.exists', lambda f: False):
with mock.patch('salve.logger', dummy_logger):
act = b.compile()

assert isinstance(act, action.ActionList)
assert len(act.actions) == 2
Expand Down
12 changes: 7 additions & 5 deletions tests/unit/block/manifest_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ def sub_block_compile():
assert file_block.get('source') == get_full_path('valid1.manifest')
target_loc = os.path.join(locations.get_salve_root(), 'a/b/c')
assert file_block.get('target') == target_loc
with mock.patch('salve.logger', dummy_logger), \
mock.patch('salve.exec_context', dummy_exec_context), \
mock.patch('os.path.exists', lambda f: True), \
mock.patch('os.access', lambda f, p: True):
act = b.compile()

with mock.patch('salve.logger', dummy_logger):
with mock.patch('salve.exec_context', dummy_exec_context):
with mock.patch('os.path.exists', lambda f: True):
with mock.patch('os.access', lambda f, p: True):
act = b.compile()

assert isinstance(act, salve.execute.action.ActionList)
assert len(act.actions) == 2
assert isinstance(act.actions[0], salve.execute.action.ActionList)
Expand Down
46 changes: 26 additions & 20 deletions tests/unit/execute/backup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ def mock_exists(path):
act = backup.FileBackupAction(filename,
dummy_file_context)

with mock.patch('salve.execute.copy.FileCopyAction.execute', mock_cp),\
mock.patch(
'salve.execute.backup.FileBackupAction.verify_can_exec',
lambda self: self.verification_codes.OK), \
mock.patch('salve.execute.backup.FileBackupAction.write_log',
lambda self: None), \
mock.patch('os.path.exists', mock_exists):
act()
cp_exec_name = 'salve.execute.copy.FileCopyAction.execute'
backup_verify_name = \
'salve.execute.backup.FileBackupAction.verify_can_exec'
backup_writelog_name = \
'salve.execute.backup.FileBackupAction.write_log'
with mock.patch(cp_exec_name, mock_cp):
with mock.patch(backup_verify_name, lambda self:
self.verification_codes.OK):
with mock.patch(backup_writelog_name, lambda self: None):
with mock.patch('os.path.exists', mock_exists):
act()

assert os.path.basename(act.dst) == ('9bfabef5ffd7f5df84171393643' +
'e7ceeba916e64876ace612ca8d2' +
Expand Down Expand Up @@ -95,14 +98,17 @@ def mock_exists(path):
act = backup.FileBackupAction(linkname,
dummy_file_context)

with mock.patch('salve.execute.copy.FileCopyAction.execute', mock_cp),\
mock.patch('salve.execute.backup.FileBackupAction.write_log',
lambda self: None), \
mock.patch(
'salve.execute.backup.FileBackupAction.verify_can_exec',
lambda self: self.verification_codes.OK), \
mock.patch('os.path.exists', mock_exists):
act()
cp_exec_name = 'salve.execute.copy.FileCopyAction.execute'
backup_writelog_name = \
'salve.execute.backup.FileBackupAction.write_log'
backup_verify_name = \
'salve.execute.backup.FileBackupAction.verify_can_exec'
with mock.patch(cp_exec_name, mock_cp):
with mock.patch(backup_writelog_name, lambda self: None):
with mock.patch(backup_verify_name,
lambda self: self.verification_codes.OK):
with mock.patch('os.path.exists', mock_exists):
act()

assert os.path.basename(act.dst) == ('55ae75d991c770d8f3ef07cbfde' +
'124ffce9c420da5db6203afab70' +
Expand Down Expand Up @@ -159,10 +165,10 @@ def file_write_log(self):

mo = mock.mock_open()
mm = mock.MagicMock()
with mock.patch('salve.execute.backup.open', mo, create=True), \
mock.patch('salve.execute.backup.print', mm, create=True), \
mock.patch('time.strftime', lambda s: 'NOW'):
act.write_log()
with mock.patch('salve.execute.backup.open', mo, create=True):
with mock.patch('salve.execute.backup.print', mm, create=True):
with mock.patch('time.strftime', lambda s: 'NOW'):
act.write_log()

mo.assert_called_once_with('/etc/salve/backup.log', 'a')
assert mm.call_args[0][0] == ('NOW abc ' + filename)
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/execute/copy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ def mock_name_to_uid(username):
def mock_name_to_gid(groupname):
return 2

with mock.patch('shutil.copytree', mock_copytree), \
mock.patch('os.access', lambda x, y: True):
dcp = copy.DirCopyAction('a',
'b/c',
self.dummy_file_context)
dcp()
with mock.patch('shutil.copytree', mock_copytree):
with mock.patch('os.access', lambda x, y: True):
dcp = copy.DirCopyAction('a',
'b/c',
self.dummy_file_context)
dcp()

assert log['mock_cp'] == ('a', 'b/c', True)

Expand Down
16 changes: 8 additions & 8 deletions tests/unit/execute/create_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def filecreate_execute(self):
builtin_patch = mock.patch('__builtin__.open', mock_open,
create=True)

with builtin_patch, \
mock.patch('os.access', lambda x, y: True):
fc = create.FileCreateAction(a_name, dummy_file_context)
fc()
with builtin_patch:
with mock.patch('os.access', lambda x, y: True):
fc = create.FileCreateAction(a_name, dummy_file_context)
fc()

mock_open.assert_called_once_with(a_name, 'w')
handle = mock_open()
Expand All @@ -58,10 +58,10 @@ def dircreate_execute(self):
mock_mkdirs = mock.Mock()
a_name = self.get_fullname('a')

with mock.patch('os.makedirs', mock_mkdirs), \
mock.patch('os.access', lambda x, y: True):
dc = create.DirCreateAction(a_name, dummy_file_context)
dc()
with mock.patch('os.makedirs', mock_mkdirs):
with mock.patch('os.access', lambda x, y: True):
dc = create.DirCreateAction(a_name, dummy_file_context)
dc()

mock_mkdirs.assert_called_once_with(a_name)

Expand Down
Loading

0 comments on commit 5771e80

Please sign in to comment.