def test_move_file__overwrite(temp_folder): with SetWorkingDirectory(temp_folder): set_file_contents("file_1", "4") set_file_contents("file_2", "5") move_file("file_1", "file_2") assert_equal(os.listdir("."), ["file_2"]) assert_equal(get_file_contents("file_2"), "4")
def test_move_file__move_to_different_folder(temp_folder): with SetWorkingDirectory(temp_folder): set_file_contents("file_1", "3") move_file("file_1", "dst/file_1") assert_equal(os.listdir("."), ["dst"]) assert_equal(os.listdir("dst"), ["file_1"]) assert_equal(get_file_contents("dst/file_1"), "3")
def test_move_file__simple_move_in_cwd(temp_folder): with SetWorkingDirectory(temp_folder): assert_equal(os.listdir("."), []) set_file_contents("file_1", "1") assert_equal(os.listdir("."), ["file_1"]) move_file("file_1", "file_2") assert_equal(os.listdir("."), ["file_2"]) assert_equal(get_file_contents("file_2"), "1")
def test_move_file__move_to_new_folder(temp_folder): assert make_dirs(os.path.join(temp_folder, "src")) file_1 = os.path.join(temp_folder, "src", "file_1") file_2 = os.path.join(temp_folder, "dst", "file_2") set_file_contents(file_1, "2") move_file(file_1, file_2) assert_equal(os.listdir(os.path.dirname(file_1)), []) assert_equal(os.listdir(os.path.dirname(file_2)), ["file_2"]) assert_equal(get_file_contents(file_2), "2")
def test_nodegraph_is_done__output_changes(temp_folder): temp_file_1 = os.path.join(temp_folder, "file_1.txt") temp_file_2 = os.path.join(temp_folder, "file_2.txt") my_node = flexmock(output_files=(temp_file_1, temp_file_2)) assert not NodeGraph.is_done(my_node, FileStatusCache()) set_file_contents(temp_file_1, "foo") assert not NodeGraph.is_done(my_node, FileStatusCache()) set_file_contents(temp_file_2, "bar") assert NodeGraph.is_done(my_node, FileStatusCache())
def test_move_file__simple_move(temp_folder): file_1 = os.path.join(temp_folder, "file_1") file_2 = os.path.join(temp_folder, "file_2") assert_equal(os.listdir(temp_folder), []) set_file_contents(file_1, "1") assert_equal(os.listdir(temp_folder), ["file_1"]) move_file(file_1, file_2) assert_equal(os.listdir(temp_folder), ["file_2"]) assert_equal(get_file_contents(file_2), "1")
def test_copy_file__simple_copy(temp_folder): file_1 = os.path.join(temp_folder, "file_1") file_2 = os.path.join(temp_folder, "file_2") assert_equal(os.listdir(temp_folder), []) set_file_contents(file_1, "1") assert_equal(os.listdir(temp_folder), ["file_1"]) copy_file(file_1, file_2) assert_equal(set(os.listdir(temp_folder)), set(["file_1", "file_2"])) assert_equal(get_file_contents(file_1), "1") assert_equal(get_file_contents(file_2), "1")
def test_atomiccmd__pipes_stdin__temp_file(temp_folder): cmd = AtomicCmd("cat", TEMP_IN_STDIN="infile.fasta", OUT_STDOUT="result.txt") assert_equal(cmd.input_files, frozenset()) set_file_contents(os.path.join(temp_folder, "infile.fasta"), "a\nbc\nd") cmd.run(temp_folder) assert_equal(cmd.join(), [0]) result = get_file_contents(os.path.join(temp_folder, "result.txt")) assert_equal(result, "a\nbc\nd")
def test_copy_file__copy_to_existing_folder(temp_folder): assert make_dirs(os.path.join(temp_folder, "src")) assert make_dirs(os.path.join(temp_folder, "dst")) file_1 = os.path.join(temp_folder, "src", "file_1") file_2 = os.path.join(temp_folder, "dst", "file_2") set_file_contents(file_1, "2") copy_file(file_1, file_2) assert_equal(os.listdir(os.path.dirname(file_1)), ["file_1"]) assert_equal(os.listdir(os.path.dirname(file_2)), ["file_2"]) assert_equal(get_file_contents(file_1), "2") assert_equal(get_file_contents(file_2), "2")
def test_atomiccmd__commit_temp_out(temp_folder): dest, temp = _setup_for_commit(temp_folder, create_cmd=False) cmd = AtomicCmd(("echo", "foo"), OUT_STDOUT=os.path.join(dest, "foo.txt"), TEMP_OUT_FOO="bar.txt") cmd.run(temp) assert_equal(cmd.join(), [0]) set_file_contents(os.path.join(temp, "bar.txt"), "1 2 3") cmd.commit(temp) assert_equal(os.listdir(temp), []) assert_equal(os.listdir(dest), ["foo.txt"])
def test_move_dirs__permission_denied(temp_folder): dst_folder = os.path.join(temp_folder, "dst") file_1 = os.path.join(temp_folder, "file") file_2 = os.path.join(dst_folder, "file") set_file_contents(file_1, "1") # Make destination folder read-only assert make_dirs(os.path.join(temp_folder, "dst")) mode = os.stat(dst_folder).st_mode ro_mode = mode & ~(stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH) os.chmod(dst_folder, ro_mode) # Non ENOENT errors should be re-raised: assert_raises(IOError, move_file, file_1, file_2)
def test_commandnode_teardown__extra_files_in_temp(temp_folder): destination, temp_folder = _setup_temp_folders(temp_folder) cmd = AtomicCmd(("echo", "-n", "1 2 3"), IN_DUMMY=_EMPTY_FILE, OUT_STDOUT=os.path.join(destination, "foo.txt")) cmd.run(temp_folder) assert_equal(cmd.join(), [0]) node = CommandNode(cmd) set_file_contents(os.path.join(temp_folder, "bar.txt"), "1 2 3") temp_files_before = set(os.listdir(temp_folder)) dest_files_before = set(os.listdir(destination)) assert_raises(CmdNodeError, node._teardown, None, temp_folder) assert_equal(temp_files_before, set(os.listdir(temp_folder))) assert_equal(dest_files_before, set(os.listdir(destination)))
def test_try_rmdir__non_file(temp_folder): fpath = os.path.join(temp_folder, "test.txt") set_file_contents(fpath, "1 2 3") assert_raises(OSError, try_rmdir, fpath)
def test_try_remove(temp_folder): fpath = os.path.join(temp_folder, "test.txt") set_file_contents(fpath, "1 2 3") assert try_remove(fpath) assert not os.path.exists(fpath)
def test_try_rmtree(temp_folder): fpath = os.path.join(temp_folder, "testdir") os.mkdir(fpath) set_file_contents(os.path.join(fpath, "file"), "1 2 3") assert try_rmtree(fpath) assert not os.path.exists(fpath)