1 # Library for JSTest manifests.
3 # This includes classes for representing and parsing JS manifests.
6 from subprocess import *
8 from tests import TestCase
11 def __init__(self, abi, os, isdebug):
14 self.isdebug = isdebug
17 """Return JS that when executed sets up variables so that JS expression
18 predicates on XUL build info evaluate properly."""
20 return 'var xulRuntime = { OS: "%s", XPCOMABI: "%s", shell: true }; var isDebugBuild=%s;' % (
23 str(self.isdebug).lower())
26 def create(cls, jsdir):
27 """Create a XULInfo based on the current platform's characteristics."""
29 # Our strategy is to find the autoconf.mk generated for the build and
30 # read the values from there.
32 # Find config/autoconf.mk.
35 path = os.path.join(dir, 'config/autoconf.mk')
36 if os.path.isfile(path):
38 if os.path.dirname(dir) == dir:
39 print "Can't find config/autoconf.mk on a directory containing the JS shell (searched from %s)"%jsdir
41 dir = os.path.dirname(dir)
44 val_re = re.compile(r'(TARGET_XPCOM_ABI|OS_TARGET|MOZ_DEBUG)\s*=\s*(.*)')
46 for line in open(path):
47 m = val_re.match(line)
51 if key == 'TARGET_XPCOM_ABI':
53 if key == 'OS_TARGET':
55 if key == 'MOZ_DEBUG':
56 kw['isdebug'] = (val == '1')
60 def __init__(self, xulinfo, js_bin):
61 self.js_prolog = xulinfo.as_js()
63 # Maps JS expr to evaluation result.
67 """Test a XUL predicate condition against this local info."""
68 ans = self.cache.get(cond, None)
70 cmd = [ self.js_bin, '-e', self.js_prolog, '-e', 'print(!!(%s))'%cond ]
71 p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
72 out, err = p.communicate()
73 if out in ('true\n', 'true\r\n'):
75 elif out in ('false\n', 'false\r\n'):
78 raise Exception("Failed to test XUL condition '%s'"%cond)
79 self.cache[cond] = ans
82 class NullXULInfoTester:
83 """Can be used to parse manifests without a JS shell."""
87 def parse(filename, xul_tester, reldir = ''):
89 comment_re = re.compile(r'#.*')
90 dir = os.path.dirname(filename)
95 print "warning: include file not found: '%s'"%filename
99 sline = comment_re.sub('', line)
100 parts = sline.split()
102 # line is empty or just a comment, skip
104 elif parts[0] == 'include':
105 include_file = parts[1]
106 include_reldir = os.path.join(reldir, os.path.dirname(include_file))
107 ans += parse(os.path.join(dir, include_file), xul_tester, include_reldir)
108 elif parts[0] == 'url-prefix':
109 # Doesn't apply to shell tests
119 while pos < len(parts):
120 if parts[pos] == 'fails':
123 elif parts[pos] == 'skip':
124 expect = enable = False
126 elif parts[pos] == 'random':
129 elif parts[pos].startswith('fails-if'):
130 cond = parts[pos][len('fails-if('):-1]
131 if xul_tester.test(cond):
134 elif parts[pos].startswith('asserts-if'):
135 # This directive means we may flunk some number of
136 # NS_ASSERTIONs in the browser. For the shell, ignore it.
138 elif parts[pos].startswith('skip-if'):
139 cond = parts[pos][len('skip-if('):-1]
140 if xul_tester.test(cond):
141 expect = enable = False
143 elif parts[pos].startswith('random-if'):
144 cond = parts[pos][len('random-if('):-1]
145 if xul_tester.test(cond):
148 elif parts[pos] == 'script':
149 script = parts[pos+1]
151 elif parts[pos] == 'slow':
154 elif parts[pos] == 'silentfail':
155 # silentfails use tons of memory, and Darwin doesn't support ulimit.
156 if xul_tester.test("xulRuntime.OS == 'Darwin'"):
157 expect = enable = False
160 print 'warning: invalid manifest line element "%s"'%parts[pos]
163 assert script is not None
164 ans.append(TestCase(os.path.join(reldir, script),
165 enable, expect, random, slow))