-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_srcflib_unix.py
97 lines (78 loc) · 3.2 KB
/
test_srcflib_unix.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
import os
import os.path
import pwd
import stat
import tempfile
import unittest
from unittest.mock import Mock, patch
from srcflib.plumbing.common import State
from srcflib.plumbing import unix
class TestFilesystem(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.user = unix.User(pwd.getpwuid(os.getuid()))
def setUp(self):
self.tempdir = tempfile.TemporaryDirectory()
self.path = os.path.join(self.tempdir.name, "test")
def tearDown(self):
self.tempdir.cleanup()
def test_mkdir(self):
result = unix.mkdir(self.path, self.user)
self.assertEqual(result.state, State.created)
self.assertTrue(os.path.isdir(self.path))
def test_mkdir_group_write(self):
unix.mkdir(self.path, self.user, 0o775)
stats = os.stat(self.path)
mode = stat.S_IMODE(stats.st_mode)
self.assertTrue(mode, 0o775)
def test_mkdir_set_gid(self):
unix.mkdir(self.path, self.user, 0o2700)
stats = os.stat(self.path)
mode = stat.S_IMODE(stats.st_mode)
self.assertTrue(mode, 0o2700)
def test_mkdir_user(self):
if self.user.pw_uid != 0:
self.skipTest("Requires chown, must run as root")
nobody = unix.get_user("nobody")
unix.mkdir(self.path, nobody)
stats = os.stat(self.path)
self.assertTrue(stats.st_uid, nobody.pw_uid)
def test_mkdir_change_perms(self):
unix.mkdir(self.path, self.user, 0o700)
result = unix.mkdir(self.path, self.user, 0o770)
self.assertEqual(result.state, State.success)
def test_mkdir_change_user(self):
if self.user.pw_uid != 0:
self.skipTest("Requires chown, must run as root")
unix.mkdir(self.path, self.user)
nobody = unix.get_user("nobody")
result = unix.mkdir(self.path, nobody)
self.assertEqual(result.state, State.success)
def test_mkdir_unchanged(self):
unix.mkdir(self.path, self.user)
result = unix.mkdir(self.path, self.user)
self.assertEqual(result.state, State.unchanged)
def test_symlink(self):
result = unix.symlink(self.path, "target")
self.assertEqual(result.state, State.created)
self.assertTrue(os.path.islink(self.path))
self.assertEqual(os.readlink(self.path), "target")
def test_symlink_unchanged(self):
unix.symlink(self.path, "target")
result = unix.symlink(self.path, "target")
self.assertEqual(result.state, State.unchanged)
@patch("{}.LOG".format(unix.__spec__.name))
def test_symlink_existing_link(self, log: Mock):
unix.symlink(self.path, "target")
result = unix.symlink(self.path, "not-target")
log.warning.assert_called_with("Not overwriting existing file %r", self.path)
self.assertEqual(result.state, State.unchanged)
@patch("{}.LOG".format(unix.__spec__.name))
def test_symlink_existing_file(self, log: Mock):
with open(self.path, "w"):
pass
result = unix.symlink(self.path, "target")
log.warning.assert_called_with("Not overwriting existing file %r", self.path)
self.assertEqual(result.state, State.unchanged)
if __name__ == "__main__":
unittest.main()