Skip to content

Commit 5768539

Browse files
committed
state: tests: migrate to dir helpers
1 parent 095464d commit 5768539

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

tests/func/test_state.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
import os
2-
31
import mock
42

5-
from dvc.main import main
63
from dvc.path_info import PathInfo
74
from dvc.state import State
85
from dvc.utils import file_md5
96

107

11-
def test_state(dvc_repo, repo_dir):
12-
path = os.path.join(dvc_repo.root_dir, repo_dir.FOO)
8+
def test_state(tmp_dir, dvc):
9+
tmp_dir.gen("foo", "foo content")
10+
path = tmp_dir / "foo"
1311
path_info = PathInfo(path)
1412
md5 = file_md5(path)[0]
1513

16-
state = State(dvc_repo, dvc_repo.config.config)
14+
state = State(dvc, dvc.config.config)
1715

1816
with state:
1917
state.save(path_info, md5)
2018
entry_md5 = state.get(path_info)
2119
assert entry_md5 == md5
2220

23-
os.unlink(path)
24-
with open(path, "a") as fd:
25-
fd.write("1")
21+
path.unlink()
22+
path.write_text("1")
2623

2724
entry_md5 = state.get(path_info)
2825
assert entry_md5 is None
@@ -34,20 +31,17 @@ def test_state(dvc_repo, repo_dir):
3431
assert entry_md5 == md5
3532

3633

37-
def test_state_overflow(dvc_repo):
34+
def test_state_overflow(tmp_dir, dvc):
3835
# NOTE: trying to add more entries than state can handle,
3936
# to see if it will clean up and vacuum successfully
40-
ret = main(["config", "state.row_limit", "10"])
41-
assert ret == 0
37+
dvc.config.set("state", "row_limit", 10)
4238

43-
dname = "dir"
44-
os.mkdir(dname)
39+
path = tmp_dir / "dir"
40+
path.mkdir()
4541
for i in range(20):
46-
with open(os.path.join(dname, str(i)), "w+") as fobj:
47-
fobj.write(str(i))
42+
(path / str(i)).write_text(str(i))
4843

49-
ret = main(["add", "dir"])
50-
assert ret == 0
44+
dvc.add("dir")
5145

5246

5347
def mock_get_inode(inode):
@@ -58,34 +52,33 @@ def get_inode_mocked(path):
5852

5953

6054
@mock.patch("dvc.state.get_inode", autospec=True)
61-
def test_get_state_record_for_inode(get_inode_mock, dvc_repo, repo_dir):
62-
state = State(dvc_repo, dvc_repo.config.config)
55+
def test_get_state_record_for_inode(get_inode_mock, tmp_dir, dvc):
56+
tmp_dir.gen("foo", "foo content")
57+
58+
state = State(dvc, dvc.config.config)
6359
inode = state.MAX_INT + 2
6460
assert inode != state._to_sqlite(inode)
6561

66-
path = os.path.join(dvc_repo.root_dir, repo_dir.FOO)
67-
md5 = file_md5(path)[0]
62+
foo = tmp_dir / "foo"
63+
md5 = file_md5(foo)[0]
6864
get_inode_mock.side_effect = mock_get_inode(inode)
6965

7066
with state:
71-
state.save(PathInfo(path), md5)
67+
state.save(PathInfo(foo), md5)
7268
ret = state.get_state_record_for_inode(inode)
7369
assert ret is not None
7470

7571

76-
def test_remove_unused_links(repo_dir, dvc_repo):
77-
stages = dvc_repo.add(repo_dir.FOO)
78-
assert len(stages) == 1
79-
80-
stages = dvc_repo.add(repo_dir.BAR)
81-
assert len(stages) == 1
72+
def test_remove_unused_links(tmp_dir, dvc):
73+
assert len(tmp_dir.dvc_gen("foo", "foo_content")) == 1
74+
assert len(tmp_dir.dvc_gen("bar", "bar_content")) == 1
8275

8376
cmd_count_links = "SELECT count(*) FROM {}".format(State.LINK_STATE_TABLE)
84-
with dvc_repo.state:
85-
result = dvc_repo.state._execute(cmd_count_links).fetchone()[0]
77+
with dvc.state:
78+
result = dvc.state._execute(cmd_count_links).fetchone()[0]
8679
assert result == 2
8780

88-
dvc_repo.state.remove_unused_links([])
81+
dvc.state.remove_unused_links([])
8982

90-
result = dvc_repo.state._execute(cmd_count_links).fetchone()[0]
83+
result = dvc.state._execute(cmd_count_links).fetchone()[0]
9184
assert result == 0

0 commit comments

Comments
 (0)