Home | History | Annotate | Download | only in net
      1 # Copyright (c) 2010, Google Inc. All rights reserved.
      2 #
      3 # Redistribution and use in source and binary forms, with or without
      4 # modification, are permitted provided that the following conditions are
      5 # met:
      6 #
      7 #     * Redistributions of source code must retain the above copyright
      8 # notice, this list of conditions and the following disclaimer.
      9 #     * Redistributions in binary form must reproduce the above
     10 # copyright notice, this list of conditions and the following disclaimer
     11 # in the documentation and/or other materials provided with the
     12 # distribution.
     13 #     * Neither the name of Google Inc. nor the names of its
     14 # contributors may be used to endorse or promote products derived from
     15 # this software without specific prior written permission.
     16 #
     17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 import json
     30 import logging
     31 
     32 from webkitpy.common.memoized import memoized
     33 from webkitpy.layout_tests.layout_package import json_results_generator
     34 from webkitpy.layout_tests.models import test_expectations
     35 from webkitpy.layout_tests.models.test_expectations import TestExpectations
     36 
     37 _log = logging.getLogger(__name__)
     38 
     39 
     40 # These are helper functions for navigating the results json structure.
     41 def for_each_test(tree, handler, prefix=''):
     42     for key in tree:
     43         new_prefix = (prefix + '/' + key) if prefix else key
     44         if 'actual' not in tree[key]:
     45             for_each_test(tree[key], handler, new_prefix)
     46         else:
     47             handler(new_prefix, tree[key])
     48 
     49 
     50 def result_for_test(tree, test):
     51     parts = test.split('/')
     52     for part in parts:
     53         if part not in tree:
     54             return None
     55         tree = tree[part]
     56     return tree
     57 
     58 
     59 class JSONTestResult(object):
     60     def __init__(self, test_name, result_dict):
     61         self._test_name = test_name
     62         self._result_dict = result_dict
     63 
     64     def did_pass_or_run_as_expected(self):
     65         return self.did_pass() or self.did_run_as_expected()
     66 
     67     def did_pass(self):
     68         return test_expectations.PASS in self._actual_as_tokens()
     69 
     70     def did_run_as_expected(self):
     71         return 'is_unexpected' not in self._result_dict
     72 
     73     def _tokenize(self, results_string):
     74         tokens = map(TestExpectations.expectation_from_string, results_string.split(' '))
     75         if None in tokens:
     76             _log.warning("Unrecognized result in %s" % results_string)
     77         return set(tokens)
     78 
     79     @memoized
     80     def _actual_as_tokens(self):
     81         actual_results = self._result_dict['actual']
     82         return self._tokenize(actual_results)
     83 
     84 
     85 # FIXME: This should be unified with ResultsSummary or other NRWT layout tests code
     86 # in the layout_tests package.
     87 # This doesn't belong in common.net, but we don't have a better place for it yet.
     88 class LayoutTestResults(object):
     89     @classmethod
     90     def results_from_string(cls, string):
     91         if not string:
     92             return None
     93 
     94         content_string = json_results_generator.strip_json_wrapper(string)
     95         json_dict = json.loads(content_string)
     96         if not json_dict:
     97             return None
     98         return cls(json_dict)
     99 
    100     def __init__(self, parsed_json):
    101         self._results = parsed_json
    102 
    103     def run_was_interrupted(self):
    104         return self._results["interrupted"]
    105 
    106     def builder_name(self):
    107         return self._results["builder_name"]
    108 
    109     def blink_revision(self):
    110         return int(self._results["blink_revision"])
    111 
    112     def actual_results(self, test):
    113         result = result_for_test(self._results["tests"], test)
    114         if result:
    115             return result["actual"]
    116         return ""
    117