def test_create_unique_autosave_filename(mocker, in_mapping, on_disk): """Test that AutosaveForStack.create_unique_autosave_filename() returns a file name in the autosave directory with the same base name as the original file name, unless that already exists in the autosave mapping or on disk.""" def new_exists(path): if path == osp.join('autosave', 'ham.py'): return on_disk else: return False mocker.patch('os.path.exists', side_effect=new_exists) addon = AutosaveForStack(mocker.Mock()) if in_mapping: addon.name_mapping = { osp.join('somedir', 'ham.py'): osp.join('autosave', 'ham.py') } autosave_filename = addon.create_unique_autosave_filename( osp.join('orig', 'ham.py'), 'autosave') if in_mapping or on_disk: assert autosave_filename == osp.join('autosave', 'ham-1.py') else: assert autosave_filename == osp.join('autosave', 'ham.py')
def test_get_autosave_filename(mocker, tmpdir): """Test that AutosaveForStack.get_autosave_filename returns a consistent and unique name for the autosave file is returned.""" addon = AutosaveForStack(mocker.Mock()) mocker.patch('spyder.plugins.editor.utils.autosave.get_conf_path', return_value=str(tmpdir)) expected = str(tmpdir.join('foo.py')) assert addon.get_autosave_filename('foo.py') == expected expected2 = str(tmpdir.join('foo-1.py')) assert addon.get_autosave_filename('foo.py') == expected assert addon.get_autosave_filename('ham/foo.py') == expected2
def test_save_autosave_mapping_with_nonempty_mapping(mocker, tmpdir): """Test that save_autosave_mapping() writes the current autosave mapping to the correct file if the mapping is not empty.""" mocker.patch('os.getpid', return_value=42) mocker.patch('spyder.plugins.editor.utils.autosave.get_conf_path', return_value=str(tmpdir)) addon = AutosaveForStack(None) addon.name_mapping = {'orig': 'autosave'} addon.save_autosave_mapping() pidfile = tmpdir.join('pid42.txt') assert ast.literal_eval(pidfile.read()) == addon.name_mapping
def test_autosave_file_renamed(mocker, tmpdir, have_hash): """Test that AutosaveForStack.file_renamed removes the old autosave file, creates a new one, and updates `name_mapping` and `file_hashes`.""" mock_remove = mocker.patch('os.remove') mocker.patch('spyder.plugins.editor.utils.autosave.get_conf_path', return_value=str(tmpdir)) mock_editor = mocker.Mock() mock_fileinfo = mocker.Mock(editor=mock_editor, filename='new_foo.py', newly_created=False) mock_document = mocker.Mock() mock_fileinfo.editor.document.return_value = mock_document mock_stack = mocker.Mock(data=[mock_fileinfo]) mock_stack.has_filename.return_value = 0 mock_stack.compute_hash.return_value = 3 addon = AutosaveForStack(mock_stack) old_autosavefile = str(tmpdir.join('old_foo.py')) new_autosavefile = str(tmpdir.join('new_foo.py')) addon.name_mapping = {'old_foo.py': old_autosavefile} addon.file_hashes = {'old_foo.py': 1, old_autosavefile: 42} if have_hash: addon.file_hashes = {'old_foo.py': 1, old_autosavefile: 42} else: addon.file_hashes = {old_autosavefile: 42} addon.file_renamed('old_foo.py', 'new_foo.py') mock_remove.assert_any_call(old_autosavefile) mock_stack._write_to_file.assert_called_with(mock_fileinfo, new_autosavefile) assert addon.name_mapping == {'new_foo.py': new_autosavefile} if have_hash: assert addon.file_hashes == {'new_foo.py': 1, new_autosavefile: 3} else: assert addon.file_hashes == {new_autosavefile: 3}
def test_save_autosave_mapping_with_empty_mapping(mocker, tmpdir, pidfile_exists): """Test that save_autosave_mapping() does not write the pidfile if the mapping is empty, and that is removes the pidfile if it exists.""" mocker.patch('os.getpid', return_value=42) mocker.patch('spyder.plugins.editor.utils.autosave.get_conf_path', return_value=str(tmpdir)) addon = AutosaveForStack(None) addon.name_mapping = {} pidfile = tmpdir.join('pid42.txt') if pidfile_exists: pidfile.write('This is an ex-parrot!') addon.save_autosave_mapping() assert not pidfile.check()
def test_autosave_remove_autosave_file(mocker, exception): """Test that AutosaveForStack.remove_autosave_file removes the autosave file and that an error dialog is displayed if an exception is raised.""" mock_remove = mocker.patch('os.remove') if exception: mock_remove.side_effect = EnvironmentError() mock_dialog = mocker.patch( 'spyder.plugins.editor.utils.autosave.AutosaveErrorDialog') mock_stack = mocker.Mock() fileinfo = mocker.Mock() fileinfo.filename = 'orig' addon = AutosaveForStack(mock_stack) addon.name_mapping = {'orig': 'autosave'} addon.remove_autosave_file(fileinfo) mock_remove.assert_called_with('autosave') assert mock_dialog.called == exception
def test_autosave(mocker, have_hash): """Test that AutosaveForStack.maybe_autosave writes the contents to the autosave file and updates the file_hashes.""" mock_editor = mocker.Mock() mock_fileinfo = mocker.Mock(editor=mock_editor, filename='orig', newly_created=False) mock_document = mocker.Mock() mock_fileinfo.editor.document.return_value = mock_document mock_stack = mocker.Mock(data=[mock_fileinfo]) addon = AutosaveForStack(mock_stack) addon.name_mapping = {'orig': 'autosave'} addon.file_hashes = {'autosave': 2} if have_hash: addon.file_hashes['orig'] = 1 mock_stack.compute_hash.return_value = 3 addon.maybe_autosave(0) mock_stack._write_to_file.assert_called_with(mock_fileinfo, 'autosave') mock_stack.compute_hash.assert_called_with(mock_fileinfo) if have_hash: assert addon.file_hashes == {'orig': 1, 'autosave': 3} else: assert addon.file_hashes == {'autosave': 3}
def test_autosave_remove_autosave_file(mocker, exception): """Test that AutosaveForStack.remove_autosave_file removes the autosave file, that an error dialog is displayed if an exception is raised, and that the autosave file is removed from `name_mapping` and `file_hashes`.""" mock_remove = mocker.patch('os.remove') if exception: mock_remove.side_effect = OSError() mock_dialog = mocker.patch( 'spyder.plugins.editor.utils.autosave.AutosaveErrorDialog') mock_stack = mocker.Mock() fileinfo = mocker.Mock() fileinfo.filename = 'orig' addon = AutosaveForStack(mock_stack) addon.name_mapping = {'orig': 'autosave'} addon.file_hashes = {'autosave': 42} addon.remove_autosave_file(fileinfo.filename) assert addon.name_mapping == {} assert addon.file_hashes == {} mock_remove.assert_any_call('autosave') assert mock_dialog.called == exception
def test_autosave_remove_all_autosave_files(mocker, exception, errors): """ Test that ``remove_all_autosave_files`` succeeds and handles errors. Check that AutosaveForStack.remove_all_autosave_files removes all autosaves and that an error dialog is displayed if an exception is raised. """ mock_remove = mocker.patch('os.remove') if exception: mock_remove.side_effect = EnvironmentError() mock_dialog = mocker.patch( 'spyder.plugins.editor.utils.autosave.AutosaveErrorDialog') mock_stack = mocker.Mock() addon = AutosaveForStack(mock_stack) addon.name_mapping = {} for idx in range(3): addon.name_mapping['orig_' + str(idx)] = 'autosave_' + str(idx) addon.remove_all_autosave_files(errors=errors) assert addon.name_mapping == {} assert mock_remove.call_count == 3 assert mock_remove.call_args_list == [(('autosave_' + str(idx), ), ) for idx in range(3)] assert mock_dialog.called == (exception and errors == 'raise')