def test_history_add(self): file_1 = os.path.join(self.test_bin_path, 'file_1') bin_config_manager.history_add(file_1) self.assertEqual(len(bin_config_manager.get_property('history')), 1, msg="History len should be 1 after adding element.") self.assertEqual( bin_config_manager.get_property('history')[0]['bin_name'], os.path.basename(file_1), msg="Wrong history item name.") self.assertEqual( bin_config_manager.get_property('history')[0]['src_dir'], os.path.dirname(file_1), msg="Wrong history item src_dir.")
def test_restore_with_file(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)) 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.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 test_empty_bin(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) bin_config_manager.history_add(file_1) clean_path.move(file_1, user_config_manager.get_property('bin_path')) self.assertTrue( os.path.exists( os.path.join(user_config_manager.get_property('bin_path'), 'file_1'))) self.assertFalse(os.path.exists(file_1), msg="File should be in bin.") self.assertEqual(bin_config_manager.history_get('file_1')['src_dir'], os.path.dirname(file_1), msg="Initial preparations fails.") self.assertEqual(bin_command.empty_bin(), 0, msg='Bin command should return success code: 0.') self.assertTrue(os.path.isdir( user_config_manager.get_property('bin_path')), msg="Binempty should not delete existing bin folder.") self.assertFalse( os.path.exists( os.path.join(user_config_manager.get_property('bin_path'), 'file_1'))) self.assertEqual(len(bin_config_manager.get_property('history')), 0)
def test_history_delete(self): file_1 = os.path.join(self.test_bin_path, 'file_1') bin_config_manager.history_add(file_1) bin_config_manager.history_del(os.path.basename(file_1)) self.assertEqual(len(bin_config_manager.get_property('history')), 0, msg="History len should be 0 after removing element.")
def test_execution_manager_with_restore(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) self.assertEqual(delete_command.delete([file_1]), 0, msg="Execute command should return success code: 0.") self.paths = [ os.path.join(user_config_manager.get_property('bin_path'), os.path.basename(file_1)) ] self.options.update({'mods': ['restore']}) self.assertEqual(execution_manager.execute_command( self.paths, self.options), 0, msg="Execute command should return success code: 0.") 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)
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_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 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_move_bin(self): file_1 = os.path.join(self.test_folder_path, 'file_1') bin_move_path = os.path.join(self.test_folder_path, 'bin_move_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.move_bin(bin_move_path), 0) self.assertFalse( os.path.exists(os.path.join(self.test_bin_path, 'file_1'))) self.assertTrue(os.path.exists(os.path.join(bin_move_path, 'file_1'))) self.assertEqual(user_config_manager.get_property('bin_path'), bin_move_path) self.assertEqual(len(bin_config_manager.get_property('history')), 1)
def test_history_empty(self): files = [ os.path.join(self.test_bin_path, str(idx)) for idx in range(5) ] for file in files: bin_config_manager.history_add(file) self.assertEqual( len(bin_config_manager.get_property('history')), 5, msg= "History should be equal to 5 after adding 5 different elements to it." ) history = bin_config_manager.get_property('history') [ self.assertEqual( os.path.join(history[i]['src_dir'], history[i]['bin_name']), files[i]) for i in range(5) ] bin_config_manager.history_empty() self.assertEqual(len(bin_config_manager.get_property('history')), 0, msg="History should be len 0 after history_empty.")
def test_execution_manager_with_delete(self): file_1 = os.path.join(self.test_folder_path, 'file_1') os.mknod(file_1) self.assertTrue(os.path.exists(file_1)) self.options.update({'mods': ['remove']}) self.paths = [file_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(file_1)) self.assertEqual(len(bin_config_manager.get_property('history')), 1) self.assertEqual( bin_config_manager.history_get( os.path.basename(file_1))['src_dir'], os.path.dirname(file_1))
def test_create_bin(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_location = os.path.join(self.test_folder_path, 'new_bin_location') self.assertEqual(bin_command.create_bin(new_bin_location), 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'), new_bin_location, msg="New path should be writen to user config file")
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 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 print_bin(options): """Print Bin Function Function print to stdout objects of bin. Parameters: options: list of bin path getting politics. Returns: value: 0 - successful, 1 - fail. """ try: history_list = bin_config_manager.get_property('history') for history_item in history_list: get_log().info(INFO_MESSAGES['list_item'].format( history_item['bin_name'])) get_log().info(INFO_MESSAGES['bin_restore']) return 0 except Exception as e: get_log().error(e) return 1