summaryrefslogtreecommitdiffstats
path: root/config/tests/makefiles/autodeps/check_mkdir.tpy
blob: ad633c781d0212ae14e27446de173814b8bbad63 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#

import os
import sys
import tempfile

from subprocess import call
from shutil     import rmtree

import logging
import unittest


def banner():
    """
    Display interpreter and system info for the test env
    """
    print '*' * 75
    cmd = os.path.basename(__file__)
    print "%s: python version is %s" % (cmd, sys.version)
    print '*' * 75


def myopts(vals):
    """
    Storage for extra command line args passed.

    Returns:
    hash - argparse::Namespace object values
    """

    if not hasattr(myopts, 'vals'):
        if 'argparse' in sys.modules:
            tmp = { } # key existance enables unittest module debug
        else:
            tmp = { 'debug': False, 'verbose': False }

        for k in dir(vals):
            if k[0:1] == '_':
                continue
            tmp[k] = getattr(vals, k)
        myopts.vals = tmp

    return myopts.vals
    

def path2posix(src):
    """
    Normalize directory path syntax

    Keyword arguments:
    src - path to normalize

    Returns:
    scalar - a file path with drive separators and windows slashes removed

    Todo:
    move to {build,config,tools,toolkit}/python for use in a library
    """

    ## (drive, tail) = os.path.splitdrive(src)
    ## Support path testing on all platforms
    drive = ''
    winpath = src.find(':')
    if -1 != winpath and 10 > winpath:
        (drive, tail) = src.split(':', 1)

    if drive:
        todo = [ '', drive.rstrip(':').lstrip('/').lstrip('\\') ]
        todo.extend( tail.lstrip('/').lstrip('\\').split('\\') ) # c:\a => [a]
    else: # os.name == 'posix'
        todo = src.split('\\')

    dst = '/'.join(todo)
    return dst
    

def checkMkdir(work, debug=False):
    """
    Verify arg permutations for directory mutex creation.

    Keyword arguments:
    None

    Returns:
    Exception on error

    Note:
    Exception() rather than self.assertTrue() is used in this test
    function to enable scatch cleanup on test exit/failure conditions.
    Not guaranteed by python closures on early exit.
    """

    logging.debug("Testing: checkMkdir")

    # On Windows, don't convert paths to POSIX
    skipposix = sys.platform == "win32"
    if skipposix:
        path = os.path.abspath(__file__)
        dirname_fun = os.path.dirname
    else:
        path = path2posix(os.path.abspath(__file__))
        import posixpath
        dirname_fun = posixpath.dirname

    src = dirname_fun(path)
    # root is 5 directories up from path
    root = reduce(lambda x, _: dirname_fun(x), xrange(5), path)

    rootP = path2posix(root)
    srcP  = path2posix(src)
    workP = path2posix(work)

    # C:\foo -vs- /c/foo
    # [0] command paths use /c/foo
    # [1] os.path.exists() on mingw() requires C:\
    paths = [
        "mkdir_bycall", # function generated
        "mkdir_bydep",  # explicit dependency
        "mkdir_bygen",  # by GENERATED_DIRS macro
    ]

    ## Use make from the parent "make check" call when available
    cmd = { 'make': 'make' }
    shell0 = os.environ.get('MAKE')
    if shell0:
        shell = os.path.splitext(shell0)[0] # strip: .exe, .py
        if -1 != shell.find('make'):
            print "MAKE COMMAND FOUND: %s" % (shell0)
            cmd['make'] = shell0 if skipposix else path2posix(shell0)

    args = []
    args.append('%s' % (cmd['make']))
    args.append('-C %s'                % (work if skipposix else workP))
    args.append("-f %s/testor.tmpl"    % (src if skipposix else srcP))
    args.append('topsrcdir=%s'         % (root if skipposix else rootP))
    args.append('deps_mkdir_bycall=%s' % paths[0])
    args.append('deps_mkdir_bydep=%s'  % paths[1])
    args.append('deps_mkdir_bygen=%s'  % paths[2])
    args.append('checkup') # target

    # Call will fail on mingw with output redirected ?!?
    if debug:
        pass
    if False: # if not debug:
        args.append('>/dev/null')

    cmd = '%s' % (' '.join(args))
    logging.debug("Running: %s" % (cmd))
    rc = call(cmd, shell=True)
    if rc:
        raise Exception("make failed ($?=%s): cmd=%s" % (rc, cmd))

    for i in paths:
        path = os.path.join(work, i)
        logging.debug("Did testing mkdir(%s) succeed?" % (path))
        if not os.path.exists(path):
            raise Exception("Test path %s does not exist" % (path))


def parseargs():
    """
    Support additional command line arguments for testing
    
    Returns:
    hash - arguments of interested parsed from the command line
    """

    opts = None
    try:
        import argparse2
        parser = argparse.ArgumentParser()
        parser.add_argument('--debug',
                            action="store_true",
                            default=False,
                            help='Enable debug mode')
        # Cannot overload verbose, Verbose: False enables debugging
        parser.add_argument('--verbose',
                            action="store_true",
                            default=False,
                            help='Enable verbose mode')
        parser.add_argument('unittest_args',
                            nargs='*'
                            # help='Slurp/pass remaining args to unittest'
                            )
        opts = parser.parse_args()

    except ImportError:
        pass

    return opts


class TestMakeLogic(unittest.TestCase):
    """
    Test suite used to validate makefile library rules and macros
    """

    def setUp(self):
        opts = myopts(None) # NameSpace object not hash
        self.debug   = opts['debug']
        self.verbose = opts['verbose']

        if self.debug:
            logging.basicConfig(level=logging.DEBUG)

        if self.verbose:
            print
            print "ENVIRONMENT DUMP:"
            print '=' * 75
            for k,v in os.environ.items():
                print "env{%s} => %s" % (k, v)
            print


    def test_path2posix(self):

        todo = {
            '/dev/null'     : '/dev/null',
            'A:\\a\\b\\c'   : '/A/a/b/c',
            'B:/x/y'        : '/B/x/y',
            'C:/x\\y/z'     : '/C/x/y/z',
            '//FOO/bar/tans': '//FOO/bar/tans',
            '//X\\a/b\\c/d' : '//X/a/b/c/d',
            '\\c:mozilla\\sandbox': '/c/mozilla/sandbox',
        }

        for val,exp in todo.items():
            found = path2posix(val)
            tst = "posix2path(%s): %s != %s)" % (val, exp, found)
            self.assertEqual(exp, found, "%s: invalid path detected" % (tst))


    def test_mkdir(self):
        """
        Verify directory creation rules and macros
        """

        failed = True

        # Exception handling is used to cleanup scratch space on error
        try:
            work = tempfile.mkdtemp()
            checkMkdir(work, self.debug)
            failed = False
        finally:
            if os.path.exists(work):
                rmtree(work)

        self.assertFalse(failed, "Unit test failure detected")


if __name__ == '__main__':
    banner()
    opts = parseargs()
    myopts(opts)

    if opts:
        if hasattr(opts, 'unittest_args'):
            sys.argv[1:] = opts.unittest_args
        else:
            sys.argv[1:] = []

    unittest.main()