def test_create_bin_with_rel_path(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) clean_path.move(file_1, user_config_manager.get_property('bin_path')) bin_config_manager.history_add(file_1) new_bin_rel_path = os.path.relpath( os.path.join(self.test_folder_path, 'new_bin_rel_path'), os.getcwd()) self.assertEqual(bin_command.create_bin(new_bin_rel_path), 0, msg='Bin command should return success code: 0.') self.assertEqual(len(bin_config_manager.get_property('history')), 0, msg="New bin should be empty.") self.assertEqual(user_config_manager.get_property('bin_path'), os.path.abspath(new_bin_rel_path), msg="New path should be writen to user config file")
def test_move_bin_with_relative_path(self): file_1 = os.path.join(self.test_folder_path, 'file_1') bin_move_rel_path = os.path.relpath( os.path.join(self.test_folder_path, 'bin_move_path'), os.getcwd()) os.mknod(file_1) bin_config_manager.history_add(file_1) clean_path.move(file_1, user_config_manager.get_property('bin_path')) self.assertEqual(bin_command.move_bin(bin_move_rel_path), 0) self.assertFalse( os.path.exists(os.path.join(self.test_bin_path, 'file_1'))) self.assertTrue( os.path.exists( os.path.join(user_config_manager.get_property('bin_path'), 'file_1'))) self.assertEqual(user_config_manager.get_property('bin_path'), os.path.abspath(bin_move_rel_path)) self.assertEqual(len(bin_config_manager.get_property('history')), 1)
def test_copy_bin(self): file_1 = os.path.join(self.test_folder_path, 'file_1') bin_copy_path = os.path.join(self.test_folder_path, 'bin_copy_path') os.mknod(file_1) bin_config_manager.history_add(file_1) clean_path.move(file_1, user_config_manager.get_property('bin_path')) self.assertEqual(bin_command.copy_bin(bin_copy_path), 0, msg="Should return success code: 0.") self.assertEqual( len(bin_config_manager.get_property('history')), 1, ) self.assertTrue(os.path.exists(os.path.join(bin_copy_path, 'file_1')), msg="Should copy innear files too") self.assertEqual(user_config_manager.get_property('bin_path'), self.test_bin_path)
def test_initial_structure(self): self.assertTrue(os.path.exists(self.test_bin_path), "Initial Structure Exception.") self.assertEqual(user_config_manager.get_property('bin_path'), self.test_bin_path, msg="Test bin does not exists.") self.assertEqual(len(bin_config_manager.get_property('history')), 0, msg="Initial Exception: len should equal to 0.")
def setUp(self): self.test_folder_path = TEST_FOLDER_PATH self.test_bin_path = TEST_BIN_PATH self.real_bin_path = user_config_manager.get_property('bin_path') self.real_bin_history = bin_config_manager.get_property('history') user_config_manager.set_property('bin_path', self.test_bin_path) bin_config_manager.set_property('history', []) os.mkdir(self.test_folder_path) os.mkdir(self.test_bin_path)
def test__get_restore_paths(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) delete_command.delete([file_1]) self.assertFalse(os.path.exists(file_1)) self.assertTrue(os.path.exists(os.path.join( user_config_manager.get_property('bin_path'), os.path.basename(file_1) ))) self.assertEqual( ['file_1'], restore_command._get_restore_paths([ os.path.join( user_config_manager.get_property('bin_path'), 'file_*' ) ]) )
def test_execution_manager_with_create_bin(self): new_bin_location = os.path.join(self.test_folder_path, 'new_bin') self.options.update({'mods': ['create_bin'], 'path': new_bin_location}) self.assertEqual(execution_manager.execute_command( self.paths, self.options), 0, msg="Execute command should return success code: 0.") self.assertEqual(user_config_manager.get_property('bin_path'), new_bin_location) self.assertEqual(len(bin_config_manager.get_property('history')), 0)
def test__copy_to_bin(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) delete_command._copy_to_bin([file_1]) self.assertTrue(os.path.exists(file_1), msg='Should not remove current file.') self.assertEqual(bin_config_manager.history_get('file_1')['src_dir'], os.path.dirname(file_1), msg='Should copy file to bin.') self.assertTrue(os.path.exists( os.path.join(user_config_manager.get_property('bin_path'), 'file_1')), msg='File should be coped to bin.')
def test_restore_with_different_src_bin_names(self): file_1 = os.path.join(self.test_folder_path, 'file_1') file_new_base_name = 'file_2' os.mknod(file_1) delete_command.delete([file_1]) self.assertFalse(os.path.exists(file_1)) self.assertTrue(os.path.exists(os.path.join( user_config_manager.get_property('bin_path'), os.path.basename(file_1) ))) history_item = bin_config_manager.history_get(os.path.basename(file_1)) history_item['src_name'] = 'file_2' history_item['date'] = '1997.12.17' bin_config_manager.set_property('history', [history_item]) self.assertEqual( restore_command.restore( [os.path.join( user_config_manager.get_property('bin_path'), bin_config_manager.history_get('file_1')['bin_name'] )] ), 0 ) self.assertTrue( os.path.isfile( os.path.join( os.path.dirname(file_1), file_new_base_name ) ) ) self.assertFalse( os.path.exists( file_1 ) )
def _valid_command(paths, options): try: if not app_config_manager.is_valid(): raise Exception(ERROR_MESSAGES['app_config_error']) elif not user_config_manager.is_valid(): raise Exception(ERROR_MESSAGES['user_config_error']) elif not bin_config_manager.is_valid(): raise Exception(ERROR_MESSAGES['bin_config_error']) elif not os.path.isdir(user_config_manager.get_property('bin_path')): if 'bincreate' not in options['mods']: raise Exception(ERROR_MESSAGES['bin_not_exists']) return True except Exception as e: print(e) return False
def test_execution_manager_with_empty_bin_with_second_key_format(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) delete_command.delete([file_1]) self.options.update({'mods': ['binempty']}) self.assertEqual(execution_manager.execute_command( self.paths, self.options), 0, msg="Execute command should return success code: 0.") self.assertEqual(len(bin_config_manager.get_property('history')), 0) self.assertFalse(os.path.exists(file_1)) self.assertFalse( os.path.exists( os.path.join(user_config_manager.get_property('bin_path'), os.path.basename(file_1))))
def test_delete_with_file(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) delete_command.delete([file_1]) self.assertEqual(delete_command.delete([file_1]), 0, msg='Should return success code: 0.') self.assertFalse(os.path.exists(file_1)) self.assertTrue( os.path.exists( os.path.join(user_config_manager.get_property('bin_path'), 'file_1'))) self.assertEqual( bin_config_manager.history_get('file_1')['src_dir'], os.path.dirname(file_1))
def test_execution_manager_with_copy_bin(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) delete_command.delete([file_1]) copy_bin_location = os.path.join(self.test_folder_path, 'copy_bin') self.options.update({'mods': ['copy_bin'], 'path': copy_bin_location}) self.assertEqual(execution_manager.execute_command( self.paths, self.options), 0, msg="Execute command should return success code: 0.") self.assertTrue( os.path.exists( os.path.join(self.test_bin_path, os.path.basename(file_1)))) self.assertTrue( os.path.exists( os.path.join(copy_bin_location, os.path.basename(file_1)))) self.assertEqual(user_config_manager.get_property('bin_path'), self.test_bin_path)
def test_execution_manager_with_move_bin(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) delete_command.delete([file_1]) new_bin_location = os.path.join(self.test_folder_path, 'new_bin') self.options.update({'mods': ['move_bin'], 'path': new_bin_location}) self.assertEqual(execution_manager.execute_command( self.paths, self.options), 0, msg="Execute command should return success code: 0.") self.assertEqual(user_config_manager.get_property('bin_path'), new_bin_location) self.assertTrue( os.path.exists( os.path.join(new_bin_location, os.path.basename(file_1)))) self.assertEqual( bin_config_manager.history_get( os.path.basename(file_1))['src_dir'], os.path.dirname(file_1))
def _move_from_bin(paths, options=None): """Copy From Bin Function Do not use this function outside of module. Function allow copy bin files to destination. Parameters: paths: what to copy. options: list of copy politics. """ # Go through every path in paths and move every element # from bin folder for path in paths: # Move obj to src location. clean_path.move( os.path.join( user_config_manager.get_property('bin_path'), bin_config_manager.history_get(path, options)['bin_name'] ), os.path.join( bin_config_manager.history_get(path, options)['src_dir'], bin_config_manager.history_get(path, options)['src_name'], ), options ) # Delete element from history list bin_config_manager.history_del(path, options) # Print Progress bar of operation get_log().info( INFO_MESSAGES['progress_res'].format( ascii_bar.get_progress_bar( paths.index(path) + 1, len(paths) ), path ) )
def empty_bin(options=None): """Copy Function Function allow to empty bin folder and history too. Parameters: options: list of copy politics. Returns: value: 0 - successful, 1 - fail. """ try: # Check if command run in confirm mod and if it's so, ask # user about activity confirmation. if not confirm_question("Try to empty bin: \n{}\n", "no", options): return 1 # Check if command run in dry mode and if so skip main management # operation and only print info if not bin_config_manager.is_dry_mode(options): history_items = bin_config_manager.get_property('history') # Go through over every single history item and # delete it from history and from actual bin folder for history_item in history_items: bin_config_manager.history_del(history_item['bin_name']) clean_path.delete( os.path.join(user_config_manager.get_property('bin_path'), history_item['bin_name'])) get_log().info(INFO_MESSAGES['progress_del'].format( ascii_bar.get_progress_bar( history_items.index(history_item) + 1, len(history_items)), history_item['bin_name'], )) # Print info about what was done while operation get_log().info(INFO_MESSAGES['bin_empty']) return 0 except Exception as e: get_log().error(e) return 1
def test__move_from_bin(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) self.assertTrue(os.path.exists(file_1)) delete_command.delete([file_1]) self.assertFalse(os.path.exists(file_1)) restore_command._move_from_bin([os.path.basename(file_1)]) self.assertTrue(os.path.exists(file_1)) self.assertFalse( os.path.exists( os.path.join( user_config_manager.get_property('bin_path'), os.path.basename(file_1) ) ) ) self.assertEqual( len(bin_config_manager.get_property('history')), 0, msg="While restore, fail occupies." )
def get_bin_path(options=None): """Get Bin Path Function Function print to stdout path of bin location or if it's not exists, print error message. Parameters: options: list of bin path getting politics. Returns: value: 0 - successful, 1 - fail. """ try: bin_path = user_config_manager.get_property('bin_path') if bin_path: get_log().info(INFO_MESSAGES['bin_path'].format(bin_path)) else: get_log().warning(ERROR_MESSAGES['bin_not_exists']) return 0 except Exception as e: get_log().error(e) return 1
def test_execution_manager_with_delete_folder(self): dir_1 = os.path.join(self.test_folder_path, 'dir_1') file_1 = os.path.join(dir_1, 'file_1') os.mkdir(dir_1) os.mknod(file_1) self.assertTrue(os.path.exists(file_1)) self.options.update({'mods': ['remove']}) self.paths = [dir_1] self.assertEqual(execution_manager.execute_command( self.paths, self.options), 0, msg="Execute command should return success code: 0.") self.assertFalse(os.path.exists(dir_1)) self.assertEqual(len(bin_config_manager.get_property('history')), 1) self.assertEqual( bin_config_manager.history_get(os.path.basename(dir_1))['src_dir'], os.path.dirname(dir_1)) self.assertTrue( os.path.exists( os.path.join( user_config_manager.get_property('bin_path'), bin_config_manager.history_get( os.path.basename(dir_1))['bin_name'])))
def _copy_by_rename_way(path, options): """Copy By Renam Way Function allow to delete file/dir... to bin by rename way if name collision exists. Arguments: path: path to source. options: list of optional parameters. """ src_name = os.path.basename(path) src_dir = os.path.dirname(path) bin_name = os.path.basename(path) if bin_config_manager.history_get(bin_name, options): count = 0 while True: tmp_bin_name = HISTORY_COPY_NAME_FORMAT.format(bin_name, count) if not bin_config_manager.history_get(tmp_bin_name, options): break count += 1 bin_name = tmp_bin_name clean_path.copy( path, os.path.join(user_config_manager.get_property('bin_path'), bin_name), options ) bin_config_manager.history_add( { 'src_name': src_name, 'src_dir': src_dir, 'bin_name': bin_name }, options )