File: TestGeneral.py

package info (click to toggle)
bleachbit 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,116 kB
  • sloc: python: 8,664; xml: 1,876; makefile: 163; sh: 9
file content (122 lines) | stat: -rw-r--r-- 3,730 bytes parent folder | download
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
# vim: ts=4:sw=4:expandtab

# BleachBit
# Copyright (C) 2008-2016 Andrew Ziem
# http://www.bleachbit.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


"""
Test case for module General
"""


import sys
import unittest

sys.path.append('.')
from bleachbit.General import *


class GeneralTestCase(unittest.TestCase):

    """Test case for module General"""

    def test_boolstr_to_bool(self):
        """Test case for method boolstr_to_bool"""
        tests = (('True', True),
                 ('true', True),
                 ('False', False),
                 ('false', False))

        for test in tests:
            self.assertEqual(boolstr_to_bool(test[0]), test[1])

    def test_getrealuid(self):
        """Test for getrealuid()"""
        if 'posix' != os.name:
            self.assertRaises(RuntimeError, getrealuid)
            return
        uid = getrealuid()
        self.assert_(isinstance(uid, int))
        self.assert_(0 <= uid <= 65535)
        if sudo_mode():
            self.assert_(uid > 0)
        print "debug: os.getenv('LOGNAME') =", os.getenv('LOGNAME')
        print "debug: os.getenv('SUDO_UID') =", os.getenv('SUDO_UID')
        print 'debug: os.geteuid() =', os.geteuid()
        print 'debug: os.getuid() =', os.getuid()
        try:
            print 'debug: os.login() =', os.getlogin()
        except:
            traceback.print_exc()
            print 'debug: os.login() raised exception'

    def test_makedirs(self):
        """Unit test for makedirs"""
        def cleanup(dir):
            if not os.path.lexists(dir):
                return
            os.rmdir(dir)
            os.rmdir(os.path.dirname(dir))
            self.assert_(not os.path.lexists(dir))

        if 'nt' == os.name:
            dir = 'c:\\temp\\bleachbit-test-makedirs\\a'
        if 'posix' == os.name:
            dir = '/tmp/bleachbit-test-makedirs/a'
        cleanup(dir)
        # directory does not exist
        makedirs(dir)
        self.assert_(os.path.lexists(dir))
        # directory already exists
        makedirs(dir)
        self.assert_(os.path.lexists(dir))
        # clean up
        cleanup(dir)

    def test_run_external(self):
        """Unit test for run_external"""
        if 'nt' == os.name:
            args = ['cmd.exe', '/c', 'dir', '%windir%\system32', '/s', '/b']
        elif 'posix' == os.name:
            args = ['find', '/usr/bin']
        (rc, stdout, stderr) = run_external(args)
        self.assertEqual(0, rc)
        self.assertEqual(0, len(stderr))

        args = ['cmddoesnotexist']
        self.assertRaises(OSError, run_external, args)

        if 'nt' == os.name:
            args = ['cmd.exe', '/c', 'dir', 'c:\doesnotexist']
        elif 'posix' == os.name:
            args = ['ls', '/doesnotexist']
        (rc, stdout, stderr) = run_external(args)
        self.assertNotEqual(0, rc)

    def test_sudo_mode(self):
        """Unit test for sudo_mode"""
        if not 'posix' == os.name:
            return
        self.assert_(isinstance(sudo_mode(), bool))


def suite():
    return unittest.makeSuite(GeneralTestCase)


if __name__ == '__main__':
    unittest.main()