forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_scm.py
169 lines (120 loc) · 4.71 KB
/
test_scm.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import pytest
from git import Repo
from dvc.scm import Git
from dvc.scm import NoSCM
from dvc.scm import SCM
from dvc.scm.base import SCMError
from dvc.system import System
from dvc.compat import fspath
from tests.basic_env import TestGit
from tests.basic_env import TestGitSubmodule
from tests.utils import get_gitignore_content
def test_init_none(tmp_dir):
assert isinstance(SCM(fspath(tmp_dir), no_scm=True), NoSCM)
def test_init_git(tmp_dir):
Repo.init(fspath(tmp_dir))
assert isinstance(SCM(fspath(tmp_dir)), Git)
def test_init_no_git(tmp_dir):
with pytest.raises(SCMError):
SCM(fspath(tmp_dir))
def test_init_sub_dir(tmp_dir):
Repo.init(fspath(tmp_dir))
subdir = tmp_dir / "dir"
subdir.mkdir()
scm = SCM(fspath(subdir))
assert scm.root_dir == fspath(tmp_dir)
class TestSCMGit(TestGit):
def test_commit(self):
G = Git(self._root_dir)
G.add(["foo"])
G.commit("add")
self.assertIn("foo", self.git.git.ls_files())
def test_is_tracked(self):
foo = os.path.abspath(self.FOO)
G = Git(self._root_dir)
G.add([self.FOO, self.UNICODE])
self.assertTrue(G.is_tracked(foo))
self.assertTrue(G.is_tracked(self.FOO))
self.assertTrue(G.is_tracked(self.UNICODE))
G.commit("add")
self.assertTrue(G.is_tracked(foo))
self.assertTrue(G.is_tracked(self.FOO))
G.repo.index.remove([self.FOO], working_tree=True)
self.assertFalse(G.is_tracked(foo))
self.assertFalse(G.is_tracked(self.FOO))
self.assertFalse(G.is_tracked("not-existing-file"))
class TestSCMGitSubmodule(TestGitSubmodule):
def test_git_submodule(self):
self.assertIsInstance(SCM(os.curdir), Git)
def test_commit_in_submodule(self):
G = Git(self._root_dir)
G.add(["foo"])
G.commit("add")
self.assertTrue("foo" in self.git.git.ls_files())
def _count_gitignore_entries(line):
lines = get_gitignore_content()
return lines.count(line)
def test_ignore(tmp_dir, scm):
foo = fspath(tmp_dir / "foo")
target = "/foo"
scm.ignore(foo)
assert (tmp_dir / ".gitignore").is_file()
assert _count_gitignore_entries(target) == 1
scm.ignore(foo)
assert (tmp_dir / ".gitignore").is_file()
assert _count_gitignore_entries(target) == 1
scm.ignore_remove(foo)
assert _count_gitignore_entries(target) == 0
def test_ignored(tmp_dir, scm):
tmp_dir.gen({"dir1": {"file1.jpg": "cont", "file2.txt": "cont"}})
tmp_dir.gen({".gitignore": "dir1/*.jpg"})
assert scm._ignored(fspath(tmp_dir / "dir1" / "file1.jpg"))
assert not scm._ignored(fspath(tmp_dir / "dir1" / "file2.txt"))
def test_get_gitignore(tmp_dir, scm):
tmp_dir.gen({"file1": "contents", "dir": {}})
data_dir = fspath(tmp_dir / "file1")
entry, gitignore = scm._get_gitignore(data_dir)
assert entry == "/file1"
assert gitignore == fspath(tmp_dir / ".gitignore")
data_dir = fspath(tmp_dir / "dir")
entry, gitignore = scm._get_gitignore(data_dir)
assert entry == "/dir"
assert gitignore == fspath(tmp_dir / ".gitignore")
def test_get_gitignore_symlink(tmp_dir, scm):
tmp_dir.gen({"dir": {"subdir": {"data": "contents"}}})
link = fspath(tmp_dir / "link")
target = fspath(tmp_dir / "dir" / "subdir" / "data")
System.symlink(target, link)
entry, gitignore = scm._get_gitignore(link)
assert entry == "/link"
assert gitignore == fspath(tmp_dir / ".gitignore")
def test_get_gitignore_subdir(tmp_dir, scm):
tmp_dir.gen({"dir1": {"file1": "cont", "dir2": {}}})
data_dir = fspath(tmp_dir / "dir1" / "file1")
entry, gitignore = scm._get_gitignore(data_dir)
assert entry == "/file1"
assert gitignore == fspath(tmp_dir / "dir1" / ".gitignore")
data_dir = fspath(tmp_dir / "dir1" / "dir2")
entry, gitignore = scm._get_gitignore(data_dir)
assert entry == "/dir2"
assert gitignore == fspath(tmp_dir / "dir1" / ".gitignore")
def test_gitignore_should_end_with_newline(tmp_dir, scm):
tmp_dir.gen({"foo": "foo", "bar": "bar"})
foo = fspath(tmp_dir / "foo")
bar = fspath(tmp_dir / "bar")
gitignore = tmp_dir / ".gitignore"
scm.ignore(foo)
assert gitignore.read_text().endswith("\n")
scm.ignore(bar)
assert gitignore.read_text().endswith("\n")
def test_gitignore_should_append_newline_to_gitignore(tmp_dir, scm):
tmp_dir.gen({"foo": "foo", "bar": "bar"})
bar_path = fspath(tmp_dir / "bar")
gitignore = tmp_dir / ".gitignore"
gitignore.write_text("/foo")
assert not gitignore.read_text().endswith("\n")
scm.ignore(bar_path)
contents = gitignore.read_text()
assert gitignore.read_text().endswith("\n")
assert contents.splitlines() == ["/foo", "/bar"]