1# Copyright 2019 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'''This module implements the StoryExpectations class which is a wrapper
6around typ's expectations_parser module.
7
8Example:
9  expectations = typ_expectations.StoryExpectations(benchmark1)
10  expectations.GetBenchmarkExpectationsFromParser(file_content)
11  disabled_benchmark = expectations.IsBenchmarkDisabled()
12  disabled_story = expectations.IsStoryDisabled('story1')
13'''
14
15from typ import expectations_parser
16from typ import json_results
17
18
19ResultType = json_results.ResultType
20
21# TODO(crbug.com/999335): This set includes all the legal tags that can be
22# used in expectations.config. We will add a presubmit check which will make
23# sure that the set of tags below and set of all the tags used in
24# expectations.config stay in sync.
25SYSTEM_CONDITION_TAGS = frozenset([
26    'android', 'android-go', 'android-low-end', 'android-nexus-5',
27    'android-nexus-5x', 'android-nexus-6', 'android-pixel-2',
28    'chromeos', 'chromeos-board-amd64-generic', 'chromeos-board-betty',
29    'chromeos-board-betty-pi-arc', 'chromeos-board-eve', 'chromeos-board-kevin',
30    'chromeos-local', 'chromeos-remote', 'desktop', 'fuchsia',
31    'fuchsia-board-astro', 'fuchsia-board-qemu-x64', 'linux', 'mac',
32    'mac-10.12', 'win', 'win10', 'win7', 'android-not-webview',
33    'android-webview', 'mobile', 'android-marshmallow',
34    'android-lollipop', 'android-nougat', 'android-oreo', 'android-pie',
35    'android-10', 'android-webview-google', 'reference', 'android-chromium',
36    'ubuntu', 'android-kitkat', 'highsierra', 'sierra', 'smart-display',
37    'web-engine-shell', 'mac-10.11', 'release', 'exact', 'debug',
38    'android-weblayer', 'win-laptop'
39])
40
41
42class StoryExpectations(object):
43
44  def __init__(self, benchmark_name):
45    self._tags = []
46    self._benchmark_name = benchmark_name
47    self._typ_expectations = (
48        expectations_parser.TestExpectations())
49
50  def GetBenchmarkExpectationsFromParser(self, raw_data):
51    error, message = self._typ_expectations.parse_tagged_list(raw_data)
52    assert not error, 'Expectations parser error: %s' % message
53
54  def SetTags(self, tags):
55    self._typ_expectations.set_tags(tags)
56
57  def _IsStoryOrBenchmarkDisabled(self, story_name):
58    exp = self._typ_expectations.expectations_for(story_name)
59    expected_results, reasons = exp.results, exp.reason
60    if ResultType.Skip in expected_results:
61      return reasons if reasons else 'No reason given'
62    return ''
63
64  def IsBenchmarkDisabled(self):
65    return self._IsStoryOrBenchmarkDisabled(self._benchmark_name + '/')
66
67  def IsStoryDisabled(self, story):
68    return self._IsStoryOrBenchmarkDisabled(
69        self._benchmark_name + '/' + story.name)
70