1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5import re
6
7class TPSTestPhase(object):
8
9    lineRe = re.compile(
10        r'^(.*?)test phase (?P<matchphase>[^\s]+): (?P<matchstatus>.*)$')
11
12    def __init__(self, phase, profile, testname, testpath, logfile, env,
13                 firefoxRunner, logfn, ignore_unused_engines=False):
14        self.phase = phase
15        self.profile = profile
16        self.testname = str(testname) # this might be passed in as unicode
17        self.testpath = testpath
18        self.logfile = logfile
19        self.env = env
20        self.firefoxRunner = firefoxRunner
21        self.log = logfn
22        self.ignore_unused_engines = ignore_unused_engines
23        self._status = None
24        self.errline = ''
25
26    @property
27    def status(self):
28        return self._status if self._status else 'unknown'
29
30    def run(self):
31        # launch Firefox
32        args = [ '-tps', self.testpath,
33                 '-tpsphase', self.phase,
34                 '-tpslogfile', self.logfile ]
35
36        if self.ignore_unused_engines:
37            args.append('--ignore-unused-engines')
38
39        self.log('\nLaunching Firefox for phase %s with args %s\n' %
40                 (self.phase, str(args)))
41        self.firefoxRunner.run(env=self.env,
42                               args=args,
43                               profile=self.profile)
44
45        # parse the logfile and look for results from the current test phase
46        found_test = False
47        f = open(self.logfile, 'r')
48        for line in f:
49
50            # skip to the part of the log file that deals with the test we're running
51            if not found_test:
52                if line.find('Running test %s' % self.testname) > -1:
53                    found_test = True
54                else:
55                    continue
56
57            # look for the status of the current phase
58            match = self.lineRe.match(line)
59            if match:
60                if match.group('matchphase') == self.phase:
61                    self._status = match.group('matchstatus')
62                    break
63
64            # set the status to FAIL if there is TPS error
65            if line.find('CROSSWEAVE ERROR: ') > -1 and not self._status:
66                self._status = 'FAIL'
67                self.errline = line[line.find('CROSSWEAVE ERROR: ') + len('CROSSWEAVE ERROR: '):]
68
69        f.close()
70