File: test_lib.py

package info (click to toggle)
dtfabric 20251118-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: python: 5,787; sh: 25; makefile: 19
file content (79 lines) | stat: -rw-r--r-- 2,272 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
# -*- coding: utf-8 -*-
"""Shared test case."""

import os
import unittest

from dtfabric import reader
from dtfabric import registry


class BaseTestCase(unittest.TestCase):
  """The base test case."""

  _TEST_DATA_PATH = os.path.join(os.getcwd(), 'test_data')

  # Show full diff results, part of TestCase so does not follow our naming
  # conventions.
  maxDiff = None

  def _CreateDefinitionRegistryFromFile(self, path):
    """Creates a data type definition registry from a file.

    Args:
      path (str): path to the data definition file.

    Returns:
      DataTypeDefinitionsRegistry: data type definition registry or None
          on error.

    Raises:
      SkipTest: if the data definition file does not exist and the test should
          be skipped.
    """
    self._SkipIfPathNotExists(path)

    definitions_registry = registry.DataTypeDefinitionsRegistry()

    self._FillDefinitionRegistryFromFile(definitions_registry, path)

    return definitions_registry

  def _FillDefinitionRegistryFromFile(self, definitions_registry, path):
    """Fills a data type definition registry from a file.

    Args:
      definitions_registry (DataTypeDefinitionsRegistry): data type definitions
          registry.
      path (str): path to the data definition file.
    """
    definitions_reader = reader.YAMLDataTypeDefinitionsFileReader()

    with open(path, 'rb') as file_object:
      definitions_reader.ReadFileObject(definitions_registry, file_object)

  def _GetTestFilePath(self, path_segments):
    """Retrieves the path of a test file in the test data directory.

    Args:
      path_segments (list[str]): path segments inside the test data directory.

    Returns:
      str: path of the test file.
    """
    # Note that we need to pass the individual path segments to os.path.join
    # and not a list.
    return os.path.join(self._TEST_DATA_PATH, *path_segments)

  def _SkipIfPathNotExists(self, path):
    """Skips the test if the path does not exist.

    Args:
      path (str): path of a test file.

    Raises:
      SkipTest: if the path does not exist and the test should be skipped.
    """
    if not os.path.exists(path):
      filename = os.path.basename(path)
      raise unittest.SkipTest(f'missing test file: {filename:s}')