ยปCore Development>Code coverage>Lib/test/test_stat.py

Python code coverage for Lib/test/test_stat.py

#countcontent
1n/aimport unittest
2n/aimport os
3n/aimport sys
4n/afrom test.support import TESTFN, import_fresh_module, android_not_root
5n/a
6n/ac_stat = import_fresh_module('stat', fresh=['_stat'])
7n/apy_stat = import_fresh_module('stat', blocked=['_stat'])
8n/a
9n/aclass TestFilemode:
10n/a statmod = None
11n/a
12n/a file_flags = {'SF_APPEND', 'SF_ARCHIVED', 'SF_IMMUTABLE', 'SF_NOUNLINK',
13n/a 'SF_SNAPSHOT', 'UF_APPEND', 'UF_COMPRESSED', 'UF_HIDDEN',
14n/a 'UF_IMMUTABLE', 'UF_NODUMP', 'UF_NOUNLINK', 'UF_OPAQUE'}
15n/a
16n/a formats = {'S_IFBLK', 'S_IFCHR', 'S_IFDIR', 'S_IFIFO', 'S_IFLNK',
17n/a 'S_IFREG', 'S_IFSOCK'}
18n/a
19n/a format_funcs = {'S_ISBLK', 'S_ISCHR', 'S_ISDIR', 'S_ISFIFO', 'S_ISLNK',
20n/a 'S_ISREG', 'S_ISSOCK'}
21n/a
22n/a stat_struct = {
23n/a 'ST_MODE': 0,
24n/a 'ST_INO': 1,
25n/a 'ST_DEV': 2,
26n/a 'ST_NLINK': 3,
27n/a 'ST_UID': 4,
28n/a 'ST_GID': 5,
29n/a 'ST_SIZE': 6,
30n/a 'ST_ATIME': 7,
31n/a 'ST_MTIME': 8,
32n/a 'ST_CTIME': 9}
33n/a
34n/a # permission bit value are defined by POSIX
35n/a permission_bits = {
36n/a 'S_ISUID': 0o4000,
37n/a 'S_ISGID': 0o2000,
38n/a 'S_ENFMT': 0o2000,
39n/a 'S_ISVTX': 0o1000,
40n/a 'S_IRWXU': 0o700,
41n/a 'S_IRUSR': 0o400,
42n/a 'S_IREAD': 0o400,
43n/a 'S_IWUSR': 0o200,
44n/a 'S_IWRITE': 0o200,
45n/a 'S_IXUSR': 0o100,
46n/a 'S_IEXEC': 0o100,
47n/a 'S_IRWXG': 0o070,
48n/a 'S_IRGRP': 0o040,
49n/a 'S_IWGRP': 0o020,
50n/a 'S_IXGRP': 0o010,
51n/a 'S_IRWXO': 0o007,
52n/a 'S_IROTH': 0o004,
53n/a 'S_IWOTH': 0o002,
54n/a 'S_IXOTH': 0o001}
55n/a
56n/a # defined by the Windows API documentation
57n/a file_attributes = {
58n/a 'FILE_ATTRIBUTE_ARCHIVE': 32,
59n/a 'FILE_ATTRIBUTE_COMPRESSED': 2048,
60n/a 'FILE_ATTRIBUTE_DEVICE': 64,
61n/a 'FILE_ATTRIBUTE_DIRECTORY': 16,
62n/a 'FILE_ATTRIBUTE_ENCRYPTED': 16384,
63n/a 'FILE_ATTRIBUTE_HIDDEN': 2,
64n/a 'FILE_ATTRIBUTE_INTEGRITY_STREAM': 32768,
65n/a 'FILE_ATTRIBUTE_NORMAL': 128,
66n/a 'FILE_ATTRIBUTE_NOT_CONTENT_INDEXED': 8192,
67n/a 'FILE_ATTRIBUTE_NO_SCRUB_DATA': 131072,
68n/a 'FILE_ATTRIBUTE_OFFLINE': 4096,
69n/a 'FILE_ATTRIBUTE_READONLY': 1,
70n/a 'FILE_ATTRIBUTE_REPARSE_POINT': 1024,
71n/a 'FILE_ATTRIBUTE_SPARSE_FILE': 512,
72n/a 'FILE_ATTRIBUTE_SYSTEM': 4,
73n/a 'FILE_ATTRIBUTE_TEMPORARY': 256,
74n/a 'FILE_ATTRIBUTE_VIRTUAL': 65536}
75n/a
76n/a def setUp(self):
77n/a try:
78n/a os.remove(TESTFN)
79n/a except OSError:
80n/a try:
81n/a os.rmdir(TESTFN)
82n/a except OSError:
83n/a pass
84n/a tearDown = setUp
85n/a
86n/a def get_mode(self, fname=TESTFN, lstat=True):
87n/a if lstat:
88n/a st_mode = os.lstat(fname).st_mode
89n/a else:
90n/a st_mode = os.stat(fname).st_mode
91n/a modestr = self.statmod.filemode(st_mode)
92n/a return st_mode, modestr
93n/a
94n/a def assertS_IS(self, name, mode):
95n/a # test format, lstrip is for S_IFIFO
96n/a fmt = getattr(self.statmod, "S_IF" + name.lstrip("F"))
97n/a self.assertEqual(self.statmod.S_IFMT(mode), fmt)
98n/a # test that just one function returns true
99n/a testname = "S_IS" + name
100n/a for funcname in self.format_funcs:
101n/a func = getattr(self.statmod, funcname, None)
102n/a if func is None:
103n/a if funcname == testname:
104n/a raise ValueError(funcname)
105n/a continue
106n/a if funcname == testname:
107n/a self.assertTrue(func(mode))
108n/a else:
109n/a self.assertFalse(func(mode))
110n/a
111n/a def test_mode(self):
112n/a with open(TESTFN, 'w'):
113n/a pass
114n/a if os.name == 'posix':
115n/a os.chmod(TESTFN, 0o700)
116n/a st_mode, modestr = self.get_mode()
117n/a self.assertEqual(modestr, '-rwx------')
118n/a self.assertS_IS("REG", st_mode)
119n/a self.assertEqual(self.statmod.S_IMODE(st_mode),
120n/a self.statmod.S_IRWXU)
121n/a
122n/a os.chmod(TESTFN, 0o070)
123n/a st_mode, modestr = self.get_mode()
124n/a self.assertEqual(modestr, '----rwx---')
125n/a self.assertS_IS("REG", st_mode)
126n/a self.assertEqual(self.statmod.S_IMODE(st_mode),
127n/a self.statmod.S_IRWXG)
128n/a
129n/a os.chmod(TESTFN, 0o007)
130n/a st_mode, modestr = self.get_mode()
131n/a self.assertEqual(modestr, '-------rwx')
132n/a self.assertS_IS("REG", st_mode)
133n/a self.assertEqual(self.statmod.S_IMODE(st_mode),
134n/a self.statmod.S_IRWXO)
135n/a
136n/a os.chmod(TESTFN, 0o444)
137n/a st_mode, modestr = self.get_mode()
138n/a self.assertS_IS("REG", st_mode)
139n/a self.assertEqual(modestr, '-r--r--r--')
140n/a self.assertEqual(self.statmod.S_IMODE(st_mode), 0o444)
141n/a else:
142n/a os.chmod(TESTFN, 0o700)
143n/a st_mode, modestr = self.get_mode()
144n/a self.assertEqual(modestr[:3], '-rw')
145n/a self.assertS_IS("REG", st_mode)
146n/a self.assertEqual(self.statmod.S_IFMT(st_mode),
147n/a self.statmod.S_IFREG)
148n/a
149n/a def test_directory(self):
150n/a os.mkdir(TESTFN)
151n/a os.chmod(TESTFN, 0o700)
152n/a st_mode, modestr = self.get_mode()
153n/a self.assertS_IS("DIR", st_mode)
154n/a if os.name == 'posix':
155n/a self.assertEqual(modestr, 'drwx------')
156n/a else:
157n/a self.assertEqual(modestr[0], 'd')
158n/a
159n/a @unittest.skipUnless(hasattr(os, 'symlink'), 'os.symlink not available')
160n/a def test_link(self):
161n/a try:
162n/a os.symlink(os.getcwd(), TESTFN)
163n/a except (OSError, NotImplementedError) as err:
164n/a raise unittest.SkipTest(str(err))
165n/a else:
166n/a st_mode, modestr = self.get_mode()
167n/a self.assertEqual(modestr[0], 'l')
168n/a self.assertS_IS("LNK", st_mode)
169n/a
170n/a @unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
171n/a @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
172n/a def test_fifo(self):
173n/a os.mkfifo(TESTFN, 0o700)
174n/a st_mode, modestr = self.get_mode()
175n/a self.assertEqual(modestr, 'prwx------')
176n/a self.assertS_IS("FIFO", st_mode)
177n/a
178n/a @unittest.skipUnless(os.name == 'posix', 'requires Posix')
179n/a def test_devices(self):
180n/a if os.path.exists(os.devnull):
181n/a st_mode, modestr = self.get_mode(os.devnull, lstat=False)
182n/a self.assertEqual(modestr[0], 'c')
183n/a self.assertS_IS("CHR", st_mode)
184n/a # Linux block devices, BSD has no block devices anymore
185n/a for blockdev in ("/dev/sda", "/dev/hda"):
186n/a if os.path.exists(blockdev):
187n/a st_mode, modestr = self.get_mode(blockdev, lstat=False)
188n/a self.assertEqual(modestr[0], 'b')
189n/a self.assertS_IS("BLK", st_mode)
190n/a break
191n/a
192n/a def test_module_attributes(self):
193n/a for key, value in self.stat_struct.items():
194n/a modvalue = getattr(self.statmod, key)
195n/a self.assertEqual(value, modvalue, key)
196n/a for key, value in self.permission_bits.items():
197n/a modvalue = getattr(self.statmod, key)
198n/a self.assertEqual(value, modvalue, key)
199n/a for key in self.file_flags:
200n/a modvalue = getattr(self.statmod, key)
201n/a self.assertIsInstance(modvalue, int)
202n/a for key in self.formats:
203n/a modvalue = getattr(self.statmod, key)
204n/a self.assertIsInstance(modvalue, int)
205n/a for key in self.format_funcs:
206n/a func = getattr(self.statmod, key)
207n/a self.assertTrue(callable(func))
208n/a self.assertEqual(func(0), 0)
209n/a
210n/a @unittest.skipUnless(sys.platform == "win32",
211n/a "FILE_ATTRIBUTE_* constants are Win32 specific")
212n/a def test_file_attribute_constants(self):
213n/a for key, value in sorted(self.file_attributes.items()):
214n/a self.assertTrue(hasattr(self.statmod, key), key)
215n/a modvalue = getattr(self.statmod, key)
216n/a self.assertEqual(value, modvalue, key)
217n/a
218n/a
219n/aclass TestFilemodeCStat(TestFilemode, unittest.TestCase):
220n/a statmod = c_stat
221n/a
222n/a formats = TestFilemode.formats | {'S_IFDOOR', 'S_IFPORT', 'S_IFWHT'}
223n/a format_funcs = TestFilemode.format_funcs | {'S_ISDOOR', 'S_ISPORT',
224n/a 'S_ISWHT'}
225n/a
226n/a
227n/aclass TestFilemodePyStat(TestFilemode, unittest.TestCase):
228n/a statmod = py_stat
229n/a
230n/a
231n/aif __name__ == '__main__':
232n/a unittest.main()