summaryrefslogtreecommitdiff
path: root/tests/todo/todo_copy.py
blob: 8698381f4d15dae3fe5466323c3f0480f5e1bdf2 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import os
from pathlib import Path

from organize.actions import Copy

USER_DIR = os.path.expanduser("~")

DEFAULT_ARGS = {
    "basedir": Path.home(),
    "path": Path.home() / "test.py",
    "simulate": False,
}


def test_tilde_expansion(mock_exists, mock_samefile, mock_copy, mock_trash, mock_mkdir):
    mock_exists.return_value = False
    mock_samefile.return_value = False
    copy = Copy(dest="~/newname.py", overwrite=False)
    updates = copy.run(**DEFAULT_ARGS)
    mock_mkdir.assert_called_with(exist_ok=True, parents=True)
    mock_exists.assert_called_with()
    mock_trash.assert_not_called()
    mock_copy.assert_called_with(
        src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "newname.py")
    )
    # keep old file path
    assert updates is None


def test_into_folder(mock_exists, mock_samefile, mock_copy, mock_trash, mock_mkdir):
    mock_exists.return_value = False
    mock_samefile.return_value = False
    copy = Copy(dest="~/somefolder/", overwrite=False)
    copy.run(**DEFAULT_ARGS)
    mock_mkdir.assert_called_with(exist_ok=True, parents=True)
    mock_exists.assert_called_with()
    mock_trash.assert_not_called()
    mock_copy.assert_called_with(
        src=os.path.join(USER_DIR, "test.py"),
        dst=os.path.join(USER_DIR, "somefolder", "test.py"),
    )


def test_overwrite(mock_exists, mock_samefile, mock_copy, mock_trash, mock_mkdir):
    mock_exists.return_value = True
    mock_samefile.return_value = False
    copy = Copy(dest="~/somefolder/", overwrite=True)
    copy.run(**DEFAULT_ARGS)
    mock_mkdir.assert_called_with(exist_ok=True, parents=True)
    mock_exists.assert_called_with()
    mock_trash.assert_called_with(os.path.join(USER_DIR, "somefolder", "test.py"))
    mock_copy.assert_called_with(
        src=os.path.join(USER_DIR, "test.py"),
        dst=os.path.join(USER_DIR, "somefolder", "test.py"),
    )


def test_already_exists_multiple_with_separator(
    mock_exists, mock_samefile, mock_copy, mock_trash, mock_mkdir
):
    args = {
        "basedir": Path.home(),
        "path": Path.home() / "test_2.py",
        "simulate": False,
    }
    mock_exists.side_effect = [True, True, True, False]
    mock_samefile.return_value = False
    copy = Copy(dest="~/folder/", overwrite=False, counter_separator="_")
    copy.run(**args)
    mock_mkdir.assert_called_with(exist_ok=True, parents=True)
    mock_exists.assert_called_with()
    mock_trash.assert_not_called()
    mock_copy.assert_called_with(
        src=os.path.join(USER_DIR, "test_2.py"),
        dst=os.path.join(USER_DIR, "folder", "test_5.py"),
    )


def test_makedirs(mock_parent, mock_copy, mock_trash):
    copy = Copy(dest="~/some/new/folder/", overwrite=False)
    copy.run(**DEFAULT_ARGS)
    mock_parent.mkdir.assert_called_with(parents=True, exist_ok=True)
    mock_trash.assert_not_called()
    mock_copy.assert_called_with(
        src=os.path.join(USER_DIR, "test.py"),
        dst=os.path.join(USER_DIR, "some", "new", "folder", "test.py"),
    )


def test_args(mock_exists, mock_samefile, mock_copy, mock_trash, mock_mkdir):
    args = {
        "basedir": Path.home(),
        "path": Path.home() / "test.py",
        "simulate": False,
        "nr": {"upper": 1},
    }
    mock_exists.return_value = False
    mock_samefile.return_value = False
    copy = Copy(dest="~/{nr.upper}-name.py", overwrite=False)
    copy.run(**args)
    mock_mkdir.assert_called_with(exist_ok=True, parents=True)
    mock_exists.assert_called_with()
    mock_trash.assert_not_called()
    mock_copy.assert_called_with(
        src=os.path.join(USER_DIR, "test.py"), dst=os.path.join(USER_DIR, "1-name.py")
    )


def test_path(mock_exists, mock_samefile, mock_copy, mock_trash, mock_mkdir):
    mock_exists.return_value = False
    mock_samefile.return_value = False
    copy = Copy(dest="~/{path.stem}/{path.suffix}/{path.name}", overwrite=False)
    copy.run(**DEFAULT_ARGS)
    mock_mkdir.assert_called_with(exist_ok=True, parents=True)
    mock_exists.assert_called_with()
    mock_trash.assert_not_called()
    mock_copy.assert_called_with(
        src=os.path.join(USER_DIR, "test.py"),
        dst=os.path.join(USER_DIR, "test", ".py", "test.py"),
    )