def test_move_loaded_directory_with_one_file(self, mock_labbook, mock_config_file, sample_src_file): lb = mock_labbook[2] new_file_data = FO.insert_file(lb, "code", sample_src_file) base_name = os.path.basename(new_file_data['key']) assert os.path.exists(os.path.join(lb.root_dir, 'code', base_name)) # make new subdir os.makedirs(os.path.join(lb.root_dir, 'code', 'subdir')) # .. and then put a file in it mv_file_res = FO.move_file(lb, "code", base_name, os.path.join('subdir', base_name)) # Should be 2, because it returns the info of the directory it was moved into assert len(mv_file_res) == 1 assert mv_file_res[0]['key'] == f'subdir/{base_name}' assert mv_file_res[0]['is_dir'] == False # Move "subdir" into "target_dir", there should be two activity records FO.makedir(lb, "code/target_dir", create_activity_record=True) mv_dir_res = FO.move_file(lb, "code", 'subdir', 'target_dir') assert len(mv_dir_res) == 2 assert mv_dir_res[0]['key'] == 'target_dir/subdir/' assert mv_dir_res[0]['is_dir'] is True assert mv_dir_res[1]['key'] == f'target_dir/subdir/{base_name}' assert mv_dir_res[1]['is_dir'] is False assert not os.path.exists(os.path.join(lb.root_dir, 'code', 'subdir')) assert os.path.exists(os.path.join(lb.root_dir, 'code', 'target_dir/subdir'))
def mutate_and_get_payload(cls, root, info, owner, labbook_name, section, src_path, dst_path, client_mutation_id=None, **kwargs): username = get_logged_in_username() lb = InventoryManager().load_labbook(username, owner, labbook_name, author=get_logged_in_author()) with lb.lock(): mv_results = FileOperations.move_file(lb, section, src_path, dst_path) file_edges = list() for file_dict in mv_results: file_edges.append(LabbookFile(owner=owner, name=labbook_name, section=section, key=file_dict['key'], is_dir=file_dict['is_dir'], is_favorite=file_dict['is_favorite'], modified_at=file_dict['modified_at'], size=str(file_dict['size']))) cursors = [base64.b64encode("{}".format(cnt).encode("UTF-8")).decode("UTF-8") for cnt, x in enumerate(file_edges)] edge_objs = [LabbookFileConnection.Edge(node=e, cursor=c) for e, c in zip(file_edges, cursors)] return MoveLabbookFile(updated_edges=edge_objs)
def test_move_single_file(self, mock_labbook, mock_config_file, sample_src_file): lb = mock_labbook[2] f = FO.insert_file(lb, 'code', sample_src_file)['key'] FO.makedir(lb, 'code/target_dir') results = FO.move_file(lb, 'code', f, 'target_dir') assert len(results) == 1 pprint.pprint(results) assert results[0]['is_dir'] == False assert results[0]['key'] == 'target_dir/' + os.path.basename(sample_src_file)
def test_move_empty_directory(self, mock_labbook, mock_config_file, sample_src_file): lb = mock_labbook[2] FO.makedir(lb, 'code/stable_dir') FO.makedir(lb, 'code/empty_dir') # We'll move "empty_dir" into "stable_dir" - there should only be one element in returned list res = FO.move_file(lb, 'code', 'empty_dir', 'stable_dir') assert len(res) == 1 assert res[0]['is_dir'] is True assert res[0]['key'] == 'stable_dir/empty_dir/'
def test_move_single_file_to_section_top(self, mock_labbook, mock_config_file, sample_src_file): lb = mock_labbook[2] FO.makedir(lb, 'code/inner_dir') f = FO.insert_file(lb, 'code', sample_src_file, 'inner_dir')['key'] # Move file to top of code section results = FO.move_file(lb, 'code', f, dst_rel_path='') # Results should be returned for "code" -- the file just moved there and the assert len(results) == 1 assert results[0]['is_dir'] == False assert results[0]['key'] == os.path.basename(f)
def test_move_loaded_directory_with_full_tree(self, mock_labbook, mock_config_file, sample_src_file): lb = mock_labbook[2] FO.makedir(lb, 'code/level_1/level_2A', create_activity_record=True) FO.makedir(lb, 'code/level_1/level_2B', create_activity_record=True) FO.makedir(lb, 'code/target_dir', create_activity_record=True) FO.makedir(lb, 'code/target_dir/existing_dir_counted_anyway', create_activity_record=True) FO.makedir(lb, 'code/this-dir-must-be-ignored', create_activity_record=True) FO.insert_file(lb, 'code', sample_src_file, dst_path='level_1/level_2B') # Move "level_1" into target_dir results = FO.move_file(lb, 'code', 'level_1', 'target_dir') assert len(results) == 4
def test_move_file_as_rename_in_same_dir(self, mock_labbook, sample_src_file): lb = mock_labbook[2] # insert file new_file_data = FO.insert_file(lb, "code", sample_src_file, '') base_name = os.path.basename(new_file_data['key']) assert os.path.exists(os.path.join(lb.root_dir, 'code', base_name)) assert new_file_data['key'] == base_name # move to rename moved_rel_path = os.path.join(f'{base_name}.MOVED') r = FO.move_file(lb, 'code', new_file_data['key'], moved_rel_path) assert len(r) == 1 assert not os.path.exists(os.path.join(lb.root_dir, 'code', base_name)) assert os.path.exists(os.path.join(lb.root_dir, 'code', f'{base_name}.MOVED')) assert os.path.isfile(os.path.join(lb.root_dir, 'code', f'{base_name}.MOVED'))
def test_with_the_whole_suite_of_file_operations_on_an_UNTRACKED_labbook( self, mock_labbook): x, y, lb = mock_labbook hash_0 = lb.git.commit_hash FileOperations.set_untracked(labbook=lb, section='input') hash_1 = lb.git.commit_hash assert hash_0 != hash_1 with open('/tmp/unittestfile', 'wb') as f: f.write('àbčdęfghįjkłmñöpqrštūvwxÿż0123456789'.encode('utf-8')) assert not os.path.exists( os.path.join(lb.root_dir, 'input', 'unittestfile')) r = FileOperations.put_file(lb, section="input", src_file=f.name, dst_path='') assert os.path.exists( os.path.join(lb.root_dir, 'input', 'unittestfile')) hash_2 = lb.git.commit_hash FileOperations.delete_files(lb, section='input', relative_paths=['unittestfile']) hash_3 = lb.git.commit_hash target_path = os.path.join(lb.root_dir, 'input', 'unittestfile') assert not os.path.exists(target_path) assert lb.is_repo_clean # Hash_2 == hash_3 because we delete a file in an UNTRACKED section assert hash_2 == hash_3 FileOperations.makedir(lb, 'input/sample-untracked-dir/nested-dir') hash_4 = lb.git.commit_hash assert hash_3 == hash_4 with open('/tmp/unittestfile', 'wb') as f: f.write('aaaaaæ'.encode('utf-8')) FileOperations.put_file(lb, section='input', src_file=f.name, dst_path='sample-untracked-dir/nested-dir') hash_5 = lb.git.commit_hash assert hash_4 == hash_5 FileOperations.move_file( lb, section='input', src_rel_path='sample-untracked-dir/nested-dir/unittestfile', dst_rel_path='unittestfile') assert not os.path.exists( os.path.join(lb.root_dir, 'input', 'sample-untracked-dir/nested-dir/unittestfile')) assert os.path.exists( os.path.join(lb.root_dir, 'input', 'unittestfile')) hash_6 = lb.git.commit_hash assert hash_5 == hash_6 FileOperations.delete_files( lb, section='input', relative_paths=['sample-untracked-dir/nested-dir']) hash_7 = lb.git.commit_hash assert hash_6 == hash_7