summaryrefslogtreecommitdiff
path: root/js/src/tests/manifest.py
blob: e7161ab5bf7c410e8ccb90248822ed226573e585 (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
# Library for JSTest manifests.
#
# This includes classes for representing and parsing JS manifests.

import os, re, sys
from subprocess import *

from tests import TestCase

class XULInfo:
    def __init__(self, abi, os, isdebug):
        self.abi = abi
        self.os = os
        self.isdebug = isdebug

    def as_js(self):
        """Return JS that when executed sets up variables so that JS expression
        predicates on XUL build info evaluate properly."""

        return 'var xulRuntime = { OS: "%s", XPCOMABI: "%s", shell: true }; var isDebugBuild=%s;' % (
            self.os,
            self.abi,
            str(self.isdebug).lower())

    @classmethod
    def create(cls, jsdir):
        """Create a XULInfo based on the current platform's characteristics."""

        # Our strategy is to find the autoconf.mk generated for the build and
        # read the values from there.
        
        # Find config/autoconf.mk.
        dir = jsdir
        while True:
            path = os.path.join(dir, 'config/autoconf.mk')
            if os.path.isfile(path):
                break
            if os.path.dirname(dir) == dir:
                print "Can't find config/autoconf.mk on a directory containing the JS shell (searched from %s)"%jsdir
                sys.exit(1)
            dir = os.path.dirname(dir)

        # Read the values.
        val_re = re.compile(r'(TARGET_XPCOM_ABI|OS_TARGET|MOZ_DEBUG)\s*=\s*(.*)')
        kw = {}
        for line in open(path):
            m = val_re.match(line)
            if m:
                key, val = m.groups()
                val = val.rstrip()
                if key == 'TARGET_XPCOM_ABI':
                    kw['abi'] = val
                if key == 'OS_TARGET':
                    kw['os'] = val
                if key == 'MOZ_DEBUG':
                    kw['isdebug'] = (val == '1')
        return cls(**kw)

class XULInfoTester:
    def __init__(self, xulinfo, js_bin):
        self.js_prolog = xulinfo.as_js()
        self.js_bin = js_bin
        # Maps JS expr to evaluation result.
        self.cache = {}

    def test(self, cond):
        """Test a XUL predicate condition against this local info."""
        ans = self.cache.get(cond, None)
        if ans is None:
            cmd = [ self.js_bin, '-e', self.js_prolog, '-e', 'print(!!(%s))'%cond ]
            p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
            out, err = p.communicate()
            if out in ('true\n', 'true\r\n'):
                ans = True
            elif out in ('false\n', 'false\r\n'):
                ans = False
            else:
                raise Exception("Failed to test XUL condition '%s'"%cond)
            self.cache[cond] = ans
        return ans

class NullXULInfoTester:
    """Can be used to parse manifests without a JS shell."""
    def test(self, cond):
        return False

def parse(filename, xul_tester, reldir = ''):
    ans = []
    comment_re = re.compile(r'#.*')
    dir = os.path.dirname(filename)

    try:
        f = open(filename)
    except IOError:
        print "warning: include file not found: '%s'"%filename
        return ans

    for line in f:
        sline = comment_re.sub('', line)
        parts = sline.split()
        if len(parts) == 0:
            # line is empty or just a comment, skip
            pass
        elif parts[0] == 'include':
            include_file = parts[1]
            include_reldir = os.path.join(reldir, os.path.dirname(include_file))
            ans += parse(os.path.join(dir, include_file), xul_tester, include_reldir)
        elif parts[0] == 'url-prefix':
            # Doesn't apply to shell tests
            pass
        else:
            script = None
            enable = True
            expect = True
            random = False
            slow = False

            pos = 0
            while pos < len(parts):
                if parts[pos] == 'fails':
                    expect = False
                    pos += 1
                elif parts[pos] == 'skip':
                    expect = enable = False
                    pos += 1
                elif parts[pos] == 'random':
                    random = True
                    pos += 1
                elif parts[pos].startswith('fails-if'):
                    cond = parts[pos][len('fails-if('):-1]
                    if xul_tester.test(cond):
                        expect = False
                    pos += 1
                elif parts[pos].startswith('asserts-if'):
                    # This directive means we may flunk some number of
                    # NS_ASSERTIONs in the browser. For the shell, ignore it.
                    pos += 1
                elif parts[pos].startswith('skip-if'):
                    cond = parts[pos][len('skip-if('):-1]
                    if xul_tester.test(cond):
                        expect = enable = False
                    pos += 1
                elif parts[pos].startswith('random-if'):
                    cond = parts[pos][len('random-if('):-1]
                    if xul_tester.test(cond):
                        random = True
                    pos += 1
                elif parts[pos] == 'script':
                    script = parts[pos+1]
                    pos += 2
                elif parts[pos] == 'slow':
                    slow = True
                    pos += 1
                elif parts[pos] == 'silentfail':
                    # silentfails use tons of memory, and Darwin doesn't support ulimit.
                    if xul_tester.test("xulRuntime.OS == 'Darwin'"):
                        expect = enable = False
                    pos += 1
                else:
                    print 'warning: invalid manifest line element "%s"'%parts[pos]
                    pos += 1

            assert script is not None
            ans.append(TestCase(os.path.join(reldir, script), 
                                enable, expect, random, slow))
    return ans