forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_scm_context.py
144 lines (104 loc) · 4.37 KB
/
test_scm_context.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import re
import pytest
from dvc.repo.scm_context import SCMContext
from dvc.scm import Git, NoSCM
def pytest_generate_tests(metafunc):
if "scm_context" in metafunc.fixturenames:
metafunc.parametrize("scm_context", ["scm", "no_scm"], indirect=True)
@pytest.fixture
def scm_context(request, mocker):
spec = Git if getattr(request, "param", "scm") == "scm" else NoSCM
# we'll test `ignore` and `ignore_remove` in a functional test.
return SCMContext(
scm=mocker.MagicMock(
spec=spec,
**{
"ignore_remove.return_value": ".gitignore",
"ignore.return_value": ".gitignore",
},
)
)
def test_scm_track_file(scm_context):
scm_context.track_file("foo")
assert scm_context.files_to_track == {"foo"}
scm_context.track_file("bar")
assert scm_context.files_to_track == {"foo", "bar"}
def test_scm_track_changed_files(scm_context):
scm_context.track_changed_files()
scm_context.scm.add.assert_not_called()
scm_context.track_file("foo")
scm_context.track_changed_files()
scm_context.scm.add.assert_called_once_with({"foo"})
def test_ignore(scm_context):
scm_context.ignore("foo")
scm_context.scm.ignore.assert_called_once_with("foo")
assert scm_context.files_to_track == {".gitignore"}
assert scm_context.ignored_paths == ["foo"]
def test_ignore_remove(scm_context):
scm_context.ignore_remove("foo")
scm_context.scm.ignore_remove.assert_called_once_with("foo")
assert scm_context.files_to_track == {".gitignore"}
def test_scm_context_reset_on_exit(scm_context):
with scm_context:
scm_context.ignore("foo")
scm_context.track_file("bar")
assert not scm_context.files_to_track
assert not scm_context.ignored_paths
def test_scm_context_autostage_changed_files(scm_context):
scm_context.autostage = True
with scm_context:
scm_context.track_file("foo")
assert scm_context.files_to_track == {"foo"}
assert not scm_context.files_to_track
assert not scm_context.ignored_paths
scm_context.scm.add.assert_called_once_with({"foo"})
def test_scm_context_clears_ignores_on_error(scm_context):
class CustomException(Exception):
pass
with pytest.raises(CustomException), scm_context(): # noqa: PT012
scm_context.ignore("foo")
assert scm_context.ignored_paths == ["foo"]
raise CustomException
scm_context.scm.ignore_remove.assert_called_once_with("foo")
assert scm_context.files_to_track == {".gitignore"}
assert not scm_context.ignored_paths
@pytest.mark.parametrize("autostage", [True, False])
@pytest.mark.parametrize("quiet", [True, False])
def test_scm_context_on_no_files_to_track(caplog, scm_context, autostage, quiet):
with scm_context(autostage=autostage, quiet=quiet):
pass
scm_context.scm.assert_not_called()
assert not caplog.text
@pytest.mark.parametrize("quiet", [True, False])
def test_scm_context_remind_to_track(caplog, scm_context, quiet):
with scm_context(quiet=quiet) as context:
context.track_file("foo")
context.track_file("lorem ipsum")
assert context.files_to_track == {"foo", "lorem ipsum"}
if quiet or isinstance(scm_context.scm, NoSCM):
assert not caplog.text
else:
assert "To track the changes with git, run:" in caplog.text
match = re.search(r"git add(?: (('.*?')|(\S+)))*", caplog.text)
assert match
assert set(match.groups()) == {"'lorem ipsum'", "foo"}
def test_scm_context_remind_disable(caplog, scm_context):
with scm_context(quiet=True) as context:
context.track_file("foo")
assert context.files_to_track == {"foo"}
assert not caplog.text
assert scm_context.quiet is False
scm_context.quiet = True
with scm_context() as context:
context.track_file("foo")
assert context.files_to_track == {"foo"}
assert not caplog.text
def test_scm_context_decorator(scm_context, mocker):
from dvc.repo.scm_context import scm_context as decorator
repo = mocker.MagicMock(scm_context=scm_context)
def test_method(repo, *args, **kwargs):
scm_context.track_file("foo")
method = mocker.MagicMock(wraps=test_method)
decorator(method, autostage=True)(repo, "arg", kw=1)
method.assert_called_once_with(repo, "arg", kw=1)
scm_context.scm.add.assert_called_once_with({"foo"})