summaryrefslogtreecommitdiff
path: root/tests/actions/test_move.py
blob: 042430d6badf6f1f26dbeead2a13a7d80895f52b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pytest
from conftest import make_files, read_files
from fs.base import FS

from organize import core

files = {
    "test.txt": "",
    "file.txt": "Hello world\nAnother line",
    "another.txt": "",
    "folder": {
        "x.txt": "",
    },
}


@pytest.fixture
def testfiles(testfs) -> FS:
    make_files(testfs, files)
    yield testfs


def test_copy_on_itself(testfiles):
    config = """
    rules:
      - locations: "/"
        actions:
          - move: "/"
    """
    core.run(config, simulate=True, working_dir=testfiles)
    result = read_files(testfiles)
    assert result == files


@pytest.mark.parametrize(
    "mode,files,test_txt_content",
    [
        ("skip", ["file.txt", "test.txt"], "old"),
        ("overwrite", ["test.txt"], "new"),
        ("rename_new", ["test.txt", "test 1.txt"], "old"),
        ("rename_existing", ["test.txt", "test 1.txt"], "new"),
    ],
)
def test_move_conflict(testfs: FS, mode, files, test_txt_content):
    config = """
    rules:
      - locations: "/"
        filters:
          - name: file
        actions:
          - move:
              dest: "test.txt"
              on_conflict: {}
    """.format(
        mode
    )
    testfs.writetext("file.txt", "new")
    testfs.writetext("test.txt", "old")
    core.run(config, simulate=False, working_dir=testfs)
    assert set(testfs.listdir("/")) == set(files)
    assert testfs.readtext("test.txt") == test_txt_content