-
Notifications
You must be signed in to change notification settings - Fork 8
/
tests.py
152 lines (104 loc) · 5.47 KB
/
tests.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
#!/usr/bin/env python
import os
import sys
import tempfile
import time
import unittest
import hardlink
testdata1 = "1234" * 1024 + "abc"
testdata2 = "1234" * 1024 + "xyz"
def get_inode(filename):
return os.lstat(filename).st_ino
class TestHappy(unittest.TestCase):
def setUp(self):
self.root = tempfile.mkdtemp()
os.chdir(self.root)
self.testfs = {
"dir1/name1.ext": testdata1,
"dir1/name2.ext": testdata1,
"dir1/name3.ext": testdata2,
"dir2/name1.ext": testdata1,
"dir3/name1.ext": testdata2,
"dir3/name1.noext": testdata1,
"dir4/name1.ext": testdata1,
}
for dir in ("dir1", "dir2", "dir3", "dir4", "dir5"):
os.mkdir(dir)
for filename, contents in self.testfs.items():
with open(filename, "w") as f:
f.write(contents)
now = time.time()
other = now - 2
for filename in ("dir1/name1.ext", "dir1/name2.ext", "dir1/name3.ext",
"dir2/name1.ext", "dir3/name1.ext", "dir3/name1.noext"):
os.utime(filename, (now, now))
os.utime("dir4/name1.ext", (other, other))
# os.chown("dir5/name1.ext", os.getuid(), ...)
# -c, --content-only Only file contents have to match
os.link("dir1/name1.ext", "dir1/link")
self.verify_file_contents()
def verify_file_contents(self):
for filename, contents in self.testfs.items():
with open(filename, "r") as f:
actual = f.read()
self.assertEqual(actual, contents)
# Bug? Should hardlink to the file with most existing links?
# self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir1/link"))
def test_hardlink_tree_dryrun(self):
sys.argv = ["hardlink.py", "-v", "0", "--no-stats", "--dry-run", self.root]
hardlink.main()
self.verify_file_contents()
self.assertEqual(os.lstat("dir1/name1.ext").st_nlink, 2) # Existing link
self.assertEqual(os.lstat("dir1/name2.ext").st_nlink, 1)
self.assertEqual(os.lstat("dir1/name3.ext").st_nlink, 1)
self.assertEqual(os.lstat("dir2/name1.ext").st_nlink, 1)
self.assertEqual(os.lstat("dir3/name1.ext").st_nlink, 1)
self.assertEqual(os.lstat("dir3/name1.noext").st_nlink, 1)
self.assertEqual(os.lstat("dir4/name1.ext").st_nlink, 1)
def test_hardlink_tree(self):
sys.argv = ["hardlink.py", "-v", "0", "--no-stats", self.root]
hardlink.main()
self.verify_file_contents()
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir1/name2.ext"))
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir2/name1.ext"))
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir3/name1.noext"))
self.assertEqual(get_inode("dir1/name3.ext"), get_inode("dir3/name1.ext"))
self.assertNotEqual(get_inode("dir1/name1.ext"), get_inode("dir4/name1.ext"))
def test_hardlink_tree_filenames_equal(self):
sys.argv = ["hardlink.py", "-v", "0", "--no-stats", "--filenames-equal", self.root]
hardlink.main()
self.verify_file_contents()
self.assertNotEqual(get_inode("dir1/name1.ext"), get_inode("dir1/name2.ext"))
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir2/name1.ext"))
self.assertNotEqual(get_inode("dir1/name1.ext"), get_inode("dir3/name1.noext"))
self.assertNotEqual(get_inode("dir1/name3.ext"), get_inode("dir3/name1.ext"))
self.assertNotEqual(get_inode("dir1/name1.ext"), get_inode("dir4/name1.ext"))
def test_hardlink_tree_exclude(self):
sys.argv = ["hardlink.py", "-v", "0", "--no-stats", "--exclude", ".*noext$", self.root]
hardlink.main()
self.verify_file_contents()
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir1/name2.ext"))
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir2/name1.ext"))
self.assertNotEqual(get_inode("dir1/name1.ext"), get_inode("dir3/name1.noext"))
self.assertEqual(get_inode("dir1/name3.ext"), get_inode("dir3/name1.ext"))
self.assertNotEqual(get_inode("dir1/name1.ext"), get_inode("dir4/name1.ext"))
def test_hardlink_tree_timestamp_ignore(self):
sys.argv = ["hardlink.py", "-v", "0", "--no-stats", "--timestamp-ignore", self.root]
hardlink.main()
self.verify_file_contents()
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir1/name2.ext"))
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir2/name1.ext"))
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir3/name1.noext"))
self.assertEqual(get_inode("dir1/name3.ext"), get_inode("dir3/name1.ext"))
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir4/name1.ext"))
def test_hardlink_tree_match(self):
sys.argv = ["hardlink.py", "-v", "0", "--no-stats", "--match", "*.ext", self.root]
hardlink.main()
self.verify_file_contents()
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir1/name2.ext"))
self.assertEqual(get_inode("dir1/name1.ext"), get_inode("dir2/name1.ext"))
self.assertNotEqual(get_inode("dir1/name1.ext"), get_inode("dir3/name1.noext"))
self.assertEqual(get_inode("dir1/name3.ext"), get_inode("dir3/name1.ext"))
self.assertNotEqual(get_inode("dir1/name1.ext"), get_inode("dir4/name1.ext"))
if __name__ == '__main__':
unittest.main()