1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Utility module for testharness."""
6
7
8# const definitions
9TESTHARNESSREPORT_HEADER = 'This is a testharness.js-based test.'
10TESTHARNESSREPORT_FOOTER = 'Harness: the test ran to completion.'
11
12
13def is_testharness_output(content_text):
14    """
15    Returns whether the content_text in parameter is a testharness output.
16    """
17
18    # Leading and trailing white spaces are accepted.
19    lines = content_text.strip().splitlines()
20    lines = [line.strip() for line in lines]
21
22    # A testharness output is defined as containing the header and the footer.
23    found_header = False
24    found_footer = False
25    for line in lines:
26        if line == TESTHARNESSREPORT_HEADER:
27            found_header = True
28        elif line == TESTHARNESSREPORT_FOOTER:
29            found_footer = True
30
31    return found_header and found_footer
32
33
34def is_testharness_output_passing(content_text):
35    """
36    Returns whether the content_text in parameter is a passing testharness output.
37
38    Note:
39        It is expected that the |content_text| is a testharness output.
40    """
41
42    # Leading and trailing white spaces are accepted.
43    lines = content_text.strip().splitlines()
44    lines = [line.strip() for line in lines]
45
46    # The check is very conservative and rejects any unexpected content in the output.
47    for line in lines:
48        # There should be no empty lines.
49        if len(line) == 0:
50            return False
51
52        # Those lines are expected to be exactly equivalent.
53        if line == TESTHARNESSREPORT_HEADER or \
54           line == TESTHARNESSREPORT_FOOTER:
55            continue
56
57        # Those are expected passing output.
58        if line.startswith('CONSOLE') or \
59           line.startswith('PASS'):
60            continue
61
62        # Those are expected failing output.
63        if line.startswith('FAIL') or \
64           line.startswith('TIMEOUT') or \
65           line.startswith('NOTRUN') or \
66           line.startswith('Harness Error. harness_status = '):
67            return False
68
69        # Unexpected output should be considered as a failure.
70        return False
71
72    return True
73