aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot_slave-0.8.8-py2.7.egg/buildslave/test/unit/test_commands_utils.py
blob: 2dae394d8f0ddd80615e45c7e64f3e4ce8ef9d1d (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
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
# This file is part of Buildbot.  Buildbot 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, version 2.
#
# 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, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import os, sys
import shutil

from twisted.trial import unittest
from twisted.python import runtime
import twisted.python.procutils

from buildslave.commands import utils

class GetCommand(unittest.TestCase):

    def setUp(self):
        # monkey-patch 'which' to return something appropriate
        self.which_results = {}
        def which(arg):
            return self.which_results.get(arg, [])
        self.patch(twisted.python.procutils, 'which', which)
        # note that utils.py currently imports which by name, so we
        # patch it there, too
        self.patch(utils, 'which', which)

    def set_which_results(self, results):
        self.which_results = results

    def test_getCommand_empty(self):
        self.set_which_results({
            'xeyes' : [],
        })
        self.assertRaises(RuntimeError, lambda : utils.getCommand('xeyes'))

    def test_getCommand_single(self):
        self.set_which_results({
            'xeyes' : [ '/usr/bin/xeyes' ],
        })
        self.assertEqual(utils.getCommand('xeyes'), '/usr/bin/xeyes')

    def test_getCommand_multi(self):
        self.set_which_results({
            'xeyes' : [ '/usr/bin/xeyes', '/usr/X11/bin/xeyes' ],
        })
        self.assertEqual(utils.getCommand('xeyes'), '/usr/bin/xeyes')

    def test_getCommand_single_exe(self):
        self.set_which_results({
            'xeyes' : [ '/usr/bin/xeyes' ],
            # it should not select this option, since only one matched
            # to begin with
            'xeyes.exe' : [ r'c:\program files\xeyes.exe' ],
        })
        self.assertEqual(utils.getCommand('xeyes'), '/usr/bin/xeyes')

    def test_getCommand_multi_exe(self):
        self.set_which_results({
            'xeyes' : [ r'c:\program files\xeyes.com', r'c:\program files\xeyes.exe' ],
            'xeyes.exe' : [ r'c:\program files\xeyes.exe' ],
        })
        # this one will work out differently depending on platform..
        if runtime.platformType  == 'win32':
            self.assertEqual(utils.getCommand('xeyes'), r'c:\program files\xeyes.exe')
        else:
            self.assertEqual(utils.getCommand('xeyes'), r'c:\program files\xeyes.com')

class RmdirRecursive(unittest.TestCase):

    # this is more complicated than you'd think because Twisted doesn't
    # rmdir its test directory very well, either..

    def setUp(self):
        self.target = 'testdir'
        try:
            if os.path.exists(self.target):
                shutil.rmtree(self.target)
        except:
            # this test will probably fail anyway
            e = sys.exc_info()[0]
            raise unittest.SkipTest("could not clean before test: %s" % (e,))

        # fill it with some files
        os.mkdir(os.path.join(self.target))
        open(    os.path.join(self.target, "a"), "w")
        os.mkdir(os.path.join(self.target, "d"))
        open(    os.path.join(self.target, "d", "a"), "w")
        os.mkdir(os.path.join(self.target, "d", "d"))
        open(    os.path.join(self.target, "d", "d", "a"), "w")

    def tearDown(self):
        try:
            if os.path.exists(self.target):
                shutil.rmtree(self.target)
        except:
            print "\n(target directory was not removed by test, and cleanup failed too)\n"
            raise

    def test_rmdirRecursive_easy(self):
        utils.rmdirRecursive(self.target)
        self.assertFalse(os.path.exists(self.target))

    def test_rmdirRecursive_symlink(self):
        # this was intended as a regression test for #792, but doesn't seem
        # to trigger it.  It can't hurt to check it, all the same.
        if runtime.platformType  == 'win32':
            raise unittest.SkipTest("no symlinks on this platform")
        os.mkdir("noperms")
        open("noperms/x", "w")
        os.chmod("noperms/x", 0)
        try:
            os.symlink("../noperms", os.path.join(self.target, "link"))
            utils.rmdirRecursive(self.target)
            # that shouldn't delete the target of the symlink
            self.assertTrue(os.path.exists("noperms"))
        finally:
            # even Twisted can't clean this up very well, so try hard to
            # clean it up ourselves..
            os.chmod("noperms/x", 0777)
            os.unlink("noperms/x")
            os.rmdir("noperms")

        self.assertFalse(os.path.exists(self.target))