File: config_include_test.py

package info (click to toggle)
cmake-format 0.6.13-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,436 kB
  • sloc: python: 16,990; makefile: 14
file content (120 lines) | stat: -rw-r--r-- 3,257 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Test the include mechanism for loading configs
"""

from __future__ import print_function
from __future__ import unicode_literals

import io
import os
import shutil
import tempfile
import unittest

from cmakelang.format import __main__


class TestConfigInclude(unittest.TestCase):

  outdir = None

  def _list2reason(self, exc_list):
    if exc_list and exc_list[-1][0] is self:
      return exc_list[-1][1]
    return None

  def _test_passed(self):
    # Taken from https://stackoverflow.com/a/39606065/141023
    if hasattr(self._outcome, 'errors'):
        # Python 3.4 - 3.10  (These two methods have no side effects)
        result = self.defaultTestResult()
        self._feedErrorsToResult(result, self._outcome.errors)
    else:
        # Python 3.11+
        result = self._outcome.result

    error = self._list2reason(result.errors)
    failure = self._list2reason(result.failures)
    return not error and not failure

    # # demo:   report short info immediately (not important)
    # if not ok:
    #   typ, text = ('ERROR', error) if error else ('FAIL', failure)
    #   msg = [x for x in text.split('\n')[1:] if not x.startswith(' ')][0]
    #   print("\n%s: %s\n     %s" % (typ, self.id(), msg))

  def setUp(self):
    self.outdir = tempfile.mkdtemp(
        prefix="cmake-format-{}-".format(self._testMethodName))

  def tearDown(self):
    if self._test_passed():
      shutil.rmtree(self.outdir)

  def test_single_include(self):
    outpath = os.path.join(self.outdir, "config-2.py")
    with io.open(outpath, "w") as outfile:
      outfile.write("""
var_a = "hello"
      """)

    outpath = os.path.join(self.outdir, "config-1.py")
    with io.open(outpath, "w") as outfile:
      outfile.write("""
include = ["config-2.py"]
var_b = "world"
      """)

    config_dict = __main__.get_configdict([outpath])
    self.assertIn("var_a", config_dict)
    self.assertIn("var_b", config_dict)

  def test_nested_include(self):
    outpath = os.path.join(self.outdir, "config-3.py")
    with io.open(outpath, "w") as outfile:
      outfile.write("""
var_a = "hello"
      """)

    outpath = os.path.join(self.outdir, "config-2.py")
    with io.open(outpath, "w") as outfile:
      outfile.write("""
include = ["config-3.py"]
var_b = "world"
      """)

    outpath = os.path.join(self.outdir, "config-1.py")
    with io.open(outpath, "w") as outfile:
      outfile.write("""
include = ["config-2.py"]
var_c = "foobar"
      """)

    config_dict = __main__.get_configdict([outpath])
    self.assertIn("var_a", config_dict)
    self.assertIn("var_b", config_dict)
    self.assertIn("var_c", config_dict)

  def test_relative_path(self):
    subdir_path = os.path.join(self.outdir, "subdir")
    os.makedirs(subdir_path)
    outpath = os.path.join(subdir_path, "config-2.py")
    with io.open(outpath, "w") as outfile:
      outfile.write("""
var_a = "hello"
      """)

    outpath = os.path.join(self.outdir, "config-1.py")
    with io.open(outpath, "w") as outfile:
      outfile.write("""
include = ["subdir/config-2.py"]
var_b = "world"
      """)

    config_dict = __main__.get_configdict([outpath])
    self.assertIn("var_a", config_dict)
    self.assertIn("var_b", config_dict)


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