2019-09-10 00:54:28 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2008-01-08
|
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
2019-09-10 00:54:28 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
2020-06-26 04:26:48 +00:00
|
|
|
import pytest
|
|
|
|
|
2020-06-27 06:08:12 +00:00
|
|
|
from ..conflict import (
|
|
|
|
get_conflicted_name,
|
|
|
|
get_unconflicted_name,
|
|
|
|
is_conflicted,
|
|
|
|
smart_copy,
|
|
|
|
smart_move,
|
|
|
|
)
|
2022-03-28 04:50:03 +00:00
|
|
|
from pathlib import Path
|
2019-09-10 00:54:28 +00:00
|
|
|
from ..testutil import eq_
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2021-08-21 21:25:33 +00:00
|
|
|
class TestCaseGetConflictedName:
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_simple(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
name = get_conflicted_name(["bar"], "bar")
|
|
|
|
eq_("[000] bar", name)
|
|
|
|
name = get_conflicted_name(["bar", "[000] bar"], "bar")
|
|
|
|
eq_("[001] bar", name)
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_no_conflict(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
name = get_conflicted_name(["bar"], "foobar")
|
|
|
|
eq_("foobar", name)
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_fourth_digit(self):
|
|
|
|
# This test is long because every time we have to add a conflicted name,
|
|
|
|
# a test must be made for every other conflicted name existing...
|
|
|
|
# Anyway, this has very few chances to happen.
|
2020-01-01 02:16:27 +00:00
|
|
|
names = ["bar"] + ["[%03d] bar" % i for i in range(1000)]
|
|
|
|
name = get_conflicted_name(names, "bar")
|
|
|
|
eq_("[1000] bar", name)
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_auto_unconflict(self):
|
|
|
|
# Automatically unconflict the name if it's already conflicted.
|
2020-01-01 02:16:27 +00:00
|
|
|
name = get_conflicted_name([], "[000] foobar")
|
|
|
|
eq_("foobar", name)
|
|
|
|
name = get_conflicted_name(["bar"], "[001] bar")
|
|
|
|
eq_("[000] bar", name)
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2021-08-21 21:25:33 +00:00
|
|
|
class TestCaseGetUnconflictedName:
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_main(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_("foobar", get_unconflicted_name("[000] foobar"))
|
|
|
|
eq_("foobar", get_unconflicted_name("[9999] foobar"))
|
|
|
|
eq_("[000]foobar", get_unconflicted_name("[000]foobar"))
|
|
|
|
eq_("[000a] foobar", get_unconflicted_name("[000a] foobar"))
|
|
|
|
eq_("foobar", get_unconflicted_name("foobar"))
|
|
|
|
eq_("foo [000] bar", get_unconflicted_name("foo [000] bar"))
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2021-08-21 21:25:33 +00:00
|
|
|
class TestCaseIsConflicted:
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_main(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
assert is_conflicted("[000] foobar")
|
|
|
|
assert is_conflicted("[9999] foobar")
|
|
|
|
assert not is_conflicted("[000]foobar")
|
|
|
|
assert not is_conflicted("[000a] foobar")
|
|
|
|
assert not is_conflicted("foobar")
|
|
|
|
assert not is_conflicted("foo [000] bar")
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2021-08-21 21:25:33 +00:00
|
|
|
class TestCaseMoveCopy:
|
2020-06-26 04:26:48 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def do_setup(self, request):
|
|
|
|
tmpdir = request.getfixturevalue("tmpdir")
|
2019-09-10 00:54:28 +00:00
|
|
|
self.path = Path(str(tmpdir))
|
2022-03-28 04:50:03 +00:00
|
|
|
self.path.joinpath("foo").touch()
|
|
|
|
self.path.joinpath("bar").touch()
|
|
|
|
self.path.joinpath("dir").mkdir()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_move_no_conflict(self, do_setup):
|
2022-03-28 04:50:03 +00:00
|
|
|
smart_move(self.path.joinpath("foo"), self.path.joinpath("baz"))
|
|
|
|
assert self.path.joinpath("baz").exists()
|
|
|
|
assert not self.path.joinpath("foo").exists()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2021-08-15 09:10:18 +00:00
|
|
|
def test_copy_no_conflict(self, do_setup): # No need to duplicate the rest of the tests... Let's just test on move
|
2022-03-28 04:50:03 +00:00
|
|
|
smart_copy(self.path.joinpath("foo"), self.path.joinpath("baz"))
|
|
|
|
assert self.path.joinpath("baz").exists()
|
|
|
|
assert self.path.joinpath("foo").exists()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_move_no_conflict_dest_is_dir(self, do_setup):
|
2022-03-28 04:50:03 +00:00
|
|
|
smart_move(self.path.joinpath("foo"), self.path.joinpath("dir"))
|
|
|
|
assert self.path.joinpath("dir", "foo").exists()
|
|
|
|
assert not self.path.joinpath("foo").exists()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_move_conflict(self, do_setup):
|
2022-03-28 04:50:03 +00:00
|
|
|
smart_move(self.path.joinpath("foo"), self.path.joinpath("bar"))
|
|
|
|
assert self.path.joinpath("[000] bar").exists()
|
|
|
|
assert not self.path.joinpath("foo").exists()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_move_conflict_dest_is_dir(self, do_setup):
|
2022-03-28 04:50:03 +00:00
|
|
|
smart_move(self.path.joinpath("foo"), self.path.joinpath("dir"))
|
|
|
|
smart_move(self.path.joinpath("bar"), self.path.joinpath("foo"))
|
|
|
|
smart_move(self.path.joinpath("foo"), self.path.joinpath("dir"))
|
|
|
|
assert self.path.joinpath("dir", "foo").exists()
|
|
|
|
assert self.path.joinpath("dir", "[000] foo").exists()
|
|
|
|
assert not self.path.joinpath("foo").exists()
|
|
|
|
assert not self.path.joinpath("bar").exists()
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_copy_folder(self, tmpdir):
|
|
|
|
# smart_copy also works on folders
|
|
|
|
path = Path(str(tmpdir))
|
2022-03-28 04:50:03 +00:00
|
|
|
path.joinpath("foo").mkdir()
|
|
|
|
path.joinpath("bar").mkdir()
|
|
|
|
smart_copy(path.joinpath("foo"), path.joinpath("bar")) # no crash
|
|
|
|
assert path.joinpath("[000] bar").exists()
|