• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The ChromiumOS Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Tests for constraint_suite_discovery."""
5
6import os
7import sys
8import unittest
9
10from checker.constraint_suite import ConstraintSuite
11from checker.constraint_suite_discovery import discover_suites
12
13TESTDATA_PATH = os.path.join(os.path.dirname(__file__), 'testdata')
14
15
16class DiscoverSuitesTest(unittest.TestCase):
17  """Tests for discover_suites."""
18
19  def test_discover_suites(self):
20    """Tests that ConstraintSuites are found and loaded."""
21    suites = discover_suites(directory=TESTDATA_PATH)
22
23    self.assertEqual(len(suites), 2)
24
25    self.assertEqual(type(suites[0]).__name__, 'Example1ConstraintSuite')
26    self.assertEqual(type(suites[1]).__name__, 'Example2ConstraintSuite')
27
28    for suite in suites:
29      self.assertIsInstance(suite, ConstraintSuite)
30      self.assertTrue(hasattr(suite, 'check_first_thing'))
31      self.assertTrue(hasattr(suite, 'check_second_thing'))
32
33  def test_discover_suites_path_unmodified(self):
34    """Tests that discover_suites doesn't modify sys.path."""
35    path_before = sys.path
36    discover_suites(directory=TESTDATA_PATH)
37    self.assertEqual(sys.path, path_before)
38
39  def test_discover_suites_invalid_pattern(self):
40    """Tests non-Python patterns raise an error."""
41    with self.assertRaisesRegex(ValueError, 'pattern must end with ".py"'):
42      discover_suites(directory=TESTDATA_PATH, pattern='*.txt')
43