summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_stat.py
blob: f22d8655063442f58f33ced2780b2dcd2c601a55 (plain)
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
import unittest
import os
import stat
from test.support import TESTFN, run_unittest


def get_mode(fname=TESTFN):
    return stat.filemode(os.lstat(fname).st_mode)


class TestFilemode(unittest.TestCase):

    def setUp(self):
        try:
            os.remove(TESTFN)
        except OSError:
            try:
                os.rmdir(TESTFN)
            except OSError:
                pass
    tearDown = setUp

    def test_mode(self):
        with open(TESTFN, 'w'):
            pass
        os.chmod(TESTFN, 0o700)
        self.assertEqual(get_mode(), '-rwx------')
        os.chmod(TESTFN, 0o070)
        self.assertEqual(get_mode(), '----rwx---')
        os.chmod(TESTFN, 0o007)
        self.assertEqual(get_mode(), '-------rwx')
        os.chmod(TESTFN, 0o444)
        self.assertEqual(get_mode(), '-r--r--r--')

    def test_directory(self):
        os.mkdir(TESTFN)
        os.chmod(TESTFN, 0o700)
        self.assertEqual(get_mode(), 'drwx------')

    @unittest.skipUnless(hasattr(os, 'symlink'), 'os.symlink not available')
    def test_link(self):
        os.symlink(os.getcwd(), TESTFN)
        self.assertEqual(get_mode(), 'lrwxrwxrwx')

    @unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
    def test_fifo(self):
        os.mkfifo(TESTFN, 0o700)
        self.assertEqual(get_mode(), 'prwx------')


def test_main():
    run_unittest(TestFilemode)

if __name__ == '__main__':
    test_main()