Ejemplo n.º 1
0
def test_mkdir_create_parents(state, repl, capsys):
    root = Folder.get_root()

    repl.onecmd("mkdir /a1/b2/c3/d4")
    out, err = capsys.readouterr()
    assert "Cannot create folder" in out
    assert Folder.find_by_path("/a1/b2/c3/d4", state.cwd) is None

    repl.onecmd("mkdir -p /a1/b2/c3/d4")
    out, err = capsys.readouterr()
    assert Folder.find_by_path("/a1", state.cwd) is not None
    assert Folder.find_by_path("/a1/b2", state.cwd) is not None
    assert Folder.find_by_path("/a1/b2/c3", state.cwd) is not None
    assert Folder.find_by_path("/a1/b2/c3/d4", state.cwd) is not None
Ejemplo n.º 2
0
def test_ls(tree, state, repl, capsys, sample_jobs, monkeypatch):
    repl.do_ls(".")
    out, err = capsys.readouterr()
    assert all(f.name in out for f in state.cwd.children)
    assert all(f"{j.job_id}" in out for j in state.cwd.jobs)

    state.mkdir("f4")
    repl.onecmd("ls f4")

    j1 = Job.get(1)
    j1.batch_job_id = None
    j1.save()
    state.mv(j1, "f1")

    repl.onecmd("ls")
    out, err = capsys.readouterr()
    assert all(f.name in out for f in state.cwd.children)
    assert all(f"{j}" in out for j in [2, 3])

    repl.onecmd("ls --recursive")
    out, err = capsys.readouterr()
    # assert all(f.name in out for f in state.cwd.children)
    all_jobs = [j.job_id for j in Job.select()]
    assert all(f"{j}" in out for j in all_jobs)

    repl.do_ls("/nope")
    out, err = capsys.readouterr()
    assert "not exist" in out

    state.cwd = Folder.find_by_path("/f2", state.cwd)
    repl.do_ls(".")
    out, err = capsys.readouterr()
    assert all(f.name in out for f in state.cwd.children)
    assert all(f"{j.job_id}" in out for j in state.cwd.jobs)

    with pytest.raises(UsageError):
        repl.onecmd("ls --nope")
    out, err = capsys.readouterr()
    assert "No such option" in out, out

    with monkeypatch.context() as m:

        job = state.create_job(command="sleep 1")
        job.batch_job_id = None

        m.setattr(state, "refresh_jobs", Mock(return_value=[]))
        m.setattr(state, "ls", Mock(return_value=([], [job])))

        repl.onecmd("ls -R /")

        state.ls.assert_called_once()
        assert state.refresh_jobs.call_count == 1
Ejemplo n.º 3
0
def test_ls(tree, state, sample_jobs):
    root = tree

    folders, jobs = state.ls(".")
    assert all(a == b for a, b in zip(folders, root.children))
    assert all(a == b for a, b in zip(jobs, root.jobs))

    with pytest.raises(pw.DoesNotExist):
        state.ls("/nope")

    f2 = Folder.find_by_path("/f2", state.cwd)
    state.cwd = f2
    folders, jobs = state.ls(".")
    assert all(a == b for a, b in zip(folders, f2.children))
    assert all(a == b for a, b in zip(jobs, f2.jobs))
Ejemplo n.º 4
0
def test_complete_path(state, tree, repl):
    root = Folder.get_root()
    alts = complete_path(root, "f")
    assert alts == ["f1/", "f2/", "f3/"]

    alts = complete_path(root, "f1")
    assert alts == ["f1/"]

    alts = complete_path(root, "f2/")
    assert alts == ["alpha/", "beta/", "gamma/"]

    state.cwd = Folder.find_by_path("/f2", state.cwd)

    alts = complete_path(root.subfolder("f2"), "a")
    assert alts == ["alpha/"]
Ejemplo n.º 5
0
def sample_jobs(tree, state):
    driver = state.default_driver
    jobs = []
    job = lambda f: jobs.append(
        driver.create_job(command="sleep 0.1", folder=Folder.find_by_path(f, state.cwd))
    )
    root = tree

    for i in range(3):
        job("/")

    for i in range(4):
        job("/f1")

    for i in range(4):
        job("/f2")

    for i in range(2):
        job("/f2/beta")

    return jobs
Ejemplo n.º 6
0
def test_mkdir_create_parent(state):
    root = Folder.get_root()

    with pytest.raises(CannotCreateError):
        state.mkdir("/a1/b2/c3/d4")
    assert Folder.find_by_path("/a1/b2/c3/d4", state.cwd) is None

    state.mkdir("/a1/b2/c3/d4", create_parent=True)
    assert Folder.find_by_path("/a1", state.cwd) is not None
    assert Folder.find_by_path("/a1/b2", state.cwd) is not None
    assert Folder.find_by_path("/a1/b2/c3", state.cwd) is not None
    assert Folder.find_by_path("/a1/b2/c3/d4", state.cwd) is not None

    state.mkdir("/a1/b2/c3/d5", create_parent=True)

    state.mkdir("beta", create_parent=True)
    assert Folder.find_by_path("/beta", state.cwd) is not None

    state.mkdir("basic", create_parent=True)
    assert Folder.find_by_path("/basic", state.cwd) is not None

    state.mkdir("/basic2", create_parent=True)
    assert Folder.find_by_path("/basic2", state.cwd) is not None
Ejemplo n.º 7
0
def test_find_by_path(db):
    root = Folder.get_root()

    f1 = root.add_folder("f1")
    f2 = f1.add_folder("f2")
    f3 = f2.add_folder("f3")
    f4 = f2.add_folder("f4")

    # Check it works with implicit CWD
    assert Folder.find_by_path("/") == root
    assert Folder.find_by_path("../blubb") == None
    assert Folder.find_by_path("/f1") == f1
    assert Folder.find_by_path("/f1/f2") == f2
    assert Folder.find_by_path("/f1/f2/f3") == f3
    assert Folder.find_by_path("/f1/f2/f4") == f4

    for f in [root, f1, f2, f3, f4]:
        # absolute paths work regardless of cwd
        assert Folder.find_by_path("/", f) == root
        assert Folder.find_by_path("/f1", f) == f1
        assert Folder.find_by_path("/f1/f2", f) == f2
        assert Folder.find_by_path("/f1/f2/f3", f) == f3
        assert Folder.find_by_path("/f1/f2/f4", f) == f4
        # self referential paths work everywhere
        assert Folder.find_by_path(".", f) == f
        assert Folder.find_by_path(f.path, f) == f
        assert Folder.find_by_path(f.path + "/", f) == f

        # for root, it's None
        assert Folder.find_by_path("..", f) == f.parent

        assert Folder.find_by_path("nope", f) is None
        assert Folder.find_by_path("../nope", f) is None

    assert Folder.find_by_path("f1", root) == f1
    assert Folder.find_by_path("f1") == f1
    assert Folder.find_by_path("../", f1) == root
    assert Folder.find_by_path("../f1", f1) == f1
    assert Folder.find_by_path("../f1/f2", f1) == f2
    assert Folder.find_by_path("f2", f1) == f2
    assert Folder.find_by_path("f3", f2) == f3
    assert Folder.find_by_path("f4", f2) == f4
    assert Folder.find_by_path("../f4", f3) == f4
    assert Folder.find_by_path("../f3", f4) == f3