def test_folder_count_elements(): pictures = Folder('pictures') pic_1 = File(pictures, 'pic_1', 'jpg', 999) pic_2 = File(pictures, 'pic_2', 'jpg', 999) pic_3 = File(pictures, 'pic_3', 'jpg', 999) pic_4 = File(pictures, 'pic_4', 'jpg', 999) assert pictures.count_elements() == 4
def test_folder_find_folder(): home = Folder('home') file1 = File(home, 'file1', 'txt', 1000) folder1 = Folder('folder1', home) file2 = File(folder1, 'file2', 'txt', 1000) wanted = home.find('folder1') assert wanted == folder1
def test_folder_count_only_files(): pictures = Folder('pictures') pic_1 = File(pictures, 'pic_1', 'jpg', 999) pic_2 = File(pictures, 'pic_2', 'jpg', 999) pic_3 = File(pictures, 'pic_3', 'jpg', 999) holiday = Folder('holiday', pictures) assert pictures.count_only_files() == 3
def test_folder_list_elements(): home = Folder('home') file1 = File(home, 'file1', 'txt', 1000) folder1 = Folder('folder1', home) file2 = File(folder1, 'file2', 'txt', 1000) tree = "home\n -file1\n folder1\n -file2" assert tree == home.list_elements()
def test_folder_find_no_matches(): home = Folder('home') file1 = File(home, 'file1', 'txt', 1000) folder1 = Folder('folder1', home) file2 = File(folder1, 'file2', 'txt', 1000) wanted = home.find('fileX') assert wanted is None
def test_command_wc(): pictures = Folder('pictures') pic_1 = File(pictures, 'pic_1', 'jpg', 999) pic_2 = File(pictures, 'pic_2', 'jpg', 999) pic_3 = File(pictures, 'pic_3', 'jpg', 999) request = "wc pictures".split() num_of_elements = wc(pictures, request) assert num_of_elements == 3
def test_command_wc_only_files(): pictures = Folder('pictures') pic_1 = File(pictures, 'pic_1', 'jpg', 999) pic_2 = File(pictures, 'pic_2', 'jpg', 999) pic_3 = File(pictures, 'pic_3', 'jpg', 999) holiday = Folder("holiday", pictures) request = "wc -f pictures".split() num_of_elements = wc(pictures, request) assert num_of_elements == 3
def test_folder_count_size_recursive(): home = Folder('home') lab10 = File(home, 'lab10', 'py', 100) lab11 = File(home, 'lab11', 'py', 200) project = Folder('project', home) file1 = File(project, 'file1', 'py', 100) file2 = File(project, 'file2', 'py', 150) size = home.count_size_recursive() assert size == 550
def test_count_only_files_recursive(): pictures = Folder('pictures') pic_1 = File(pictures, 'pic_1', 'jpg', 999) pic_2 = File(pictures, 'pic_2', 'jpg', 999) pic_3 = File(pictures, 'pic_3', 'jpg', 999) holiday = Folder('holiday', pictures) pic_4 = File(holiday, 'pic_4', 'jpg', 999) pic_5 = File(holiday, 'pic_5', 'jpg', 999) assert pictures.count_only_files_recursive() == 5
def test_copy_file_into_working_dir(): home = Folder('home') lab10 = File(home, 'lab10', 'py', 100) assert len(home.content) == 1 lab10.copy(home, 'lab10_copy') if_copy_exists = True if len(home.content) == 2 else False assert if_copy_exists is True assert home.content[1].name == 'lab10_copy' assert home.content[1].size == lab10.size assert home.content[1]._type == lab10._type
def test_copy_file_to_another_dir(): home = Folder('home') new_folder = Folder('new_folder') lab10 = File(home, 'lab10', 'py', 100) assert len(new_folder.content) == 0 lab10.copy(new_folder, 'lab10_copy') if_copy_exists = True if len(new_folder.content) == 1 else False assert if_copy_exists is True assert new_folder.content[0].name == 'lab10_copy' assert new_folder.content[0].size == lab10.size assert new_folder.content[0]._type == lab10._type
def test_command_wc_recursive_only_files(): pictures = Folder('pictures') pic_1 = File(pictures, 'pic_1', 'jpg', 999) pic_2 = File(pictures, 'pic_2', 'jpg', 999) pic_3 = File(pictures, 'pic_3', 'jpg', 999) holiday = Folder('holiday', pictures) pic_4 = File(holiday, 'pic_4', 'jpg', 999) pic_5 = File(holiday, 'pic_5', 'jpg', 999) request = "wc -r -f pictures".split() num_of_elements = wc(pictures, request) assert num_of_elements == 5
def test_create_file(): home = Folder('home') lab10 = File(home, 'lab10', 'py', 100) assert lab10.name == 'lab10' assert lab10._type == 'py' assert lab10.size == 100 assert lab10.directory == home
def test_cat_folder(): home = Folder('home') working_dir = Folder('working_dir') working_dir = home folder1 = Folder('folder1', home) file1 = File(folder1, 'file1', 'txt', 1000) assert cat(working_dir, 'folder1') == str(folder1)
def test_copy_file(): home = Folder('home') movies = Folder('movies') movie = File(movies, 'movie', 'mp4', 1510) assert home.content == [] movies.copy(home, 'movies_copy') assert len(home.content) == 1 assert home.content[0].name == 'movies_copy'
def test_ls(): pictures = Folder('pictures') pic_1 = File(pictures, 'pic_1', 'jpg', 999) pic_2 = File(pictures, 'pic_2', 'jpg', 999) pic_3 = File(pictures, 'pic_3', 'jpg', 999) holiday = Folder('holiday', pictures) pic_4 = File(holiday, 'pic_4', 'jpg', 999) pic_5 = File(holiday, 'pic_5', 'jpg', 999) tree = """pictures -pic_1 -pic_2 -pic_3 holiday -pic_4 -pic_5""" request = "ls pictures".split() assert tree == ls(pictures, request)
def test_create_folder_with_content(): laby = Folder('laby') lab10 = File(laby, 'lab10', 'py', 100) laby_copy = Folder('laby_copy', None, [lab10], laby.size) assert laby_copy.name == 'laby_copy' assert laby_copy.directory is None assert laby_copy.content[0] == lab10 assert laby_copy.size == 100
def test_folder_delete_element(): home = Folder('home') file = File(home, 'file', 'exe', 1510) assert home.size == 1510 assert len(home.content) == 1 home.delete_element(file) assert home.size == 0 assert home.content == []
def test_folder_add_element(): home = Folder('home') user_one = Folder('user_one') file = File(user_one, 'file', 'exe', 1510) assert home.size == 0 assert home.content == [] home.add_element(file) assert len(home.content) == 1 assert home.size == 1510
def test_command_rm(monkeypatch): home = Folder('home') working_dir = Folder('working_dir') working_dir = home folder1 = Folder('folder1', home) file1 = File(home, 'file1', 'txt', 1000) assert len(home.content) == 2 input_decision = StringIO('Y\n') monkeypatch.setattr('sys.stdin', input_decision) rm(file1, working_dir, home) assert len(home.content) == 1
def test_copy_file_to_working_dir(): home = Folder('home') working_dir = Folder('working_dir') working_dir = home folder1 = Folder('folder1', home) File(folder1, 'file1', 'txt', 10) request = 'cp file1 file1_copy'.split() assert len(home.content) == 1 cp(working_dir, request, home) assert len(home.content) == 2 assert home.content[1].name == "file1_copy"
def test_move_folder_to_working_dir(): home = Folder('home') working_dir = Folder('working_dir') working_dir = home folder1 = Folder('folder1', home) File(folder1, 'file1', 'txt', 10) request = 'mv folder1 folder1_copy'.split() assert len(home.content) == 1 mv(working_dir, request, home) assert len(home.content) == 1 assert home.content[0].name == "folder1_copy"
def main(): home = Folder('home') working_dir = Folder('working_dir') working_dir = home answer = True starting_msg = "File system is working. Type help if needed or exit to end program." print(starting_msg) while answer: try: path = get_path(working_dir, home) answer = input(path).split() if answer[0] == "cd": new_working_dir = cd(answer, working_dir, home) if new_working_dir: working_dir = new_working_dir else: print("Incorrect usage of: cd. Try again") elif answer[0] == 'mkdir': # make folder try: Folder(answer[1], working_dir) except Exception: print("Incorrect usage of: mkdir. Try again") elif answer[0] == 'mk': # make file try: File(working_dir, answer[1], answer[2], answer[3]) except Exception: print("Incorrect usage of: mk. Try again") elif answer[0] == 'rm': # delete file or folder if len(answer) == 2: to_be_deleted = working_dir.find(answer[1]) if to_be_deleted: print(rm(to_be_deleted, working_dir, home)) else: print(f"Error. {answer[1]} - Element does not exist") else: print("Incorrect usage of: rm. Try again") elif answer[0] == 'ls': # print structure print(ls(working_dir, answer)) elif answer[0] == 'cat': # print file/folder info print(cat(working_dir, answer[1])) elif answer[0] == 'size': # print folder size try: size = working_dir.find(answer[1]).count_size_recursive() print(f'Size: {size} kB') except AttributeError: print("Incorrect usage of: size. Try again") elif answer[0] == 'wc': # count elements in folder try: print(wc(working_dir, answer)) except Exception: print("Incorrect usage of: wc. Try again") elif answer[0] == 'pwd': print(working_dir.name) elif answer[0] == 'cp': try: cp(working_dir, answer, home) except Exception: print("Incorrect usage of: cp. Try again") elif answer[0] == 'mv': try: mv(working_dir, answer, home) except Exception: print("Incorrect usage of: mv. Try again") elif answer[0] == 'help': print(help()) elif answer[0] == 'exit': # end program answer = False else: print("Command not found") except IndexError: print("No command was given. Try again.") main()
def test_cat_file(): home = Folder('home') working_dir = Folder('working_dir') working_dir = home file1 = File(home, 'file1', 'txt', 1000) assert cat(working_dir, 'file1') == str(file1)
def test_folder_create_folder_size(): home = Folder('home') lab10 = File(home, 'lab10', 'py', 100) lab11 = File(home, 'lab11', 'py', 200) lab12 = File(home, 'lab12', 'py', 1) assert home.size == 301
def test_create_file_incorrect_size(): home = Folder('folder') with pytest.raises(IncorrectSizeError): File(home, 'file', 'txt', 'abc') File(home, "file_b", 'csv', -10)
def test_str_file(): home = Folder('home') lab10 = File(home, 'lab10', 'py', 100) description = str(lab10) assert description == 'Name:lab10 Type:py Size:100 In directory: home'
def test_file_type(): home = Folder('home') file = File(home, 'file', 'txt', 1000) assert file.type() == 'txt'