statusfile_unittest.py 4.27 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python
# Copyright 2016 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

6

7 8 9
from __future__ import absolute_import
import os
import sys
10 11
import unittest

12 13 14 15 16 17
TOOLS_PATH = os.path.dirname(os.path.dirname(os.path.dirname(
    os.path.abspath(__file__))))
sys.path.append(TOOLS_PATH)

from testrunner.local import statusfile
from testrunner.local.utils import Freeze
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97


TEST_VARIABLES = {
  'system': 'linux',
  'mode': 'release',
}


TEST_STATUS_FILE = """
[
[ALWAYS, {
  'foo/bar': [PASS, SKIP],
  'baz/bar': [PASS, FAIL],
  'foo/*': [PASS, SLOW],
}],  # ALWAYS

['%s', {
  'baz/bar': [PASS, SLOW],
  'foo/*': [FAIL],
}],
]
"""


def make_variables():
  variables = {}
  variables.update(TEST_VARIABLES)
  return variables


class UtilsTest(unittest.TestCase):
  def test_freeze(self):
    self.assertEqual(2, Freeze({1: [2]})[1][0])
    self.assertEqual(set([3]), Freeze({1: [2], 2: set([3])})[2])

    with self.assertRaises(Exception):
      Freeze({1: [], 2: set([3])})[2] = 4
    with self.assertRaises(Exception):
      Freeze({1: [], 2: set([3])}).update({3: 4})
    with self.assertRaises(Exception):
      Freeze({1: [], 2: set([3])})[1].append(2)
    with self.assertRaises(Exception):
      Freeze({1: [], 2: set([3])})[2] |= set([3])

    # Sanity check that we can do the same calls on a non-frozen object.
    {1: [], 2: set([3])}[2] = 4
    {1: [], 2: set([3])}.update({3: 4})
    {1: [], 2: set([3])}[1].append(2)
    {1: [], 2: set([3])}[2] |= set([3])


class StatusFileTest(unittest.TestCase):
  def test_eval_expression(self):
    variables = make_variables()
    variables.update(statusfile.VARIABLES)

    self.assertTrue(
        statusfile._EvalExpression(
            'system==linux and mode==release', variables))
    self.assertTrue(
        statusfile._EvalExpression(
            'system==linux or variant==default', variables))
    self.assertFalse(
        statusfile._EvalExpression(
            'system==linux and mode==debug', variables))
    self.assertRaises(
        AssertionError,
        lambda: statusfile._EvalExpression(
            'system==linux and mode==foo', variables))
    self.assertRaises(
        SyntaxError,
        lambda: statusfile._EvalExpression(
            'system==linux and mode=release', variables))
    self.assertEquals(
        statusfile.VARIANT_EXPRESSION,
        statusfile._EvalExpression(
            'system==linux and variant==default', variables)
    )

  def test_read_statusfile_section_true(self):
98
    rules, prefix_rules = statusfile.ReadStatusFile(
99 100 101 102 103 104 105 106 107 108 109
        TEST_STATUS_FILE % 'system==linux', make_variables())

    self.assertEquals(
        {
          'foo/bar': set(['PASS', 'SKIP']),
          'baz/bar': set(['PASS', 'FAIL', 'SLOW']),
        },
        rules[''],
    )
    self.assertEquals(
        {
110
          'foo/': set(['SLOW', 'FAIL']),
111
        },
112
        prefix_rules[''],
113 114
    )
    self.assertEquals({}, rules['default'])
115
    self.assertEquals({}, prefix_rules['default'])
116 117

  def test_read_statusfile_section_false(self):
118
    rules, prefix_rules = statusfile.ReadStatusFile(
119 120 121 122 123 124 125 126 127 128 129
        TEST_STATUS_FILE % 'system==windows', make_variables())

    self.assertEquals(
        {
          'foo/bar': set(['PASS', 'SKIP']),
          'baz/bar': set(['PASS', 'FAIL']),
        },
        rules[''],
    )
    self.assertEquals(
        {
130
          'foo/': set(['PASS', 'SLOW']),
131
        },
132
        prefix_rules[''],
133 134
    )
    self.assertEquals({}, rules['default'])
135
    self.assertEquals({}, prefix_rules['default'])
136 137

  def test_read_statusfile_section_variant(self):
138
    rules, prefix_rules = statusfile.ReadStatusFile(
139 140 141 142 143 144 145 146 147 148 149 150 151
        TEST_STATUS_FILE % 'system==linux and variant==default',
        make_variables(),
    )

    self.assertEquals(
        {
          'foo/bar': set(['PASS', 'SKIP']),
          'baz/bar': set(['PASS', 'FAIL']),
        },
        rules[''],
    )
    self.assertEquals(
        {
152
          'foo/': set(['PASS', 'SLOW']),
153
        },
154
        prefix_rules[''],
155 156 157 158 159 160 161 162 163
    )
    self.assertEquals(
        {
          'baz/bar': set(['PASS', 'SLOW']),
        },
        rules['default'],
    )
    self.assertEquals(
        {
164
          'foo/': set(['FAIL']),
165
        },
166
        prefix_rules['default'],
167 168 169 170 171
    )


if __name__ == '__main__':
    unittest.main()