1 | # SPDX-License-Identifier: BSD-2-Clause |
---|
2 | """ Unit tests for the rtemsspec.items module. """ |
---|
3 | |
---|
4 | # Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de) |
---|
5 | # |
---|
6 | # Redistribution and use in source and binary forms, with or without |
---|
7 | # modification, are permitted provided that the following conditions |
---|
8 | # are met: |
---|
9 | # 1. Redistributions of source code must retain the above copyright |
---|
10 | # notice, this list of conditions and the following disclaimer. |
---|
11 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | # notice, this list of conditions and the following disclaimer in the |
---|
13 | # documentation and/or other materials provided with the distribution. |
---|
14 | # |
---|
15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | # POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |
---|
27 | import os |
---|
28 | import pytest |
---|
29 | |
---|
30 | from rtemsspec.items import EmptyItem, ItemCache, ItemMapper, ItemTemplate |
---|
31 | from rtemsspec.tests.util import create_item_cache_config_and_copy_spec |
---|
32 | |
---|
33 | |
---|
34 | def test_config_error(): |
---|
35 | with pytest.raises(KeyError): |
---|
36 | ItemCache({}) |
---|
37 | |
---|
38 | |
---|
39 | def test_load(tmpdir): |
---|
40 | config = create_item_cache_config_and_copy_spec(tmpdir, "spec-item-cache") |
---|
41 | item_count = 0 |
---|
42 | |
---|
43 | def post_process_load(items): |
---|
44 | nonlocal item_count |
---|
45 | item_count = len(items) |
---|
46 | |
---|
47 | item_cache = ItemCache(config, post_process_load) |
---|
48 | assert item_count == len(item_cache.all) |
---|
49 | assert item_cache.updates |
---|
50 | cache_dir = config["cache-directory"] |
---|
51 | assert os.path.exists(os.path.join(cache_dir, "0", "spec", "spec.pickle")) |
---|
52 | assert os.path.exists( |
---|
53 | os.path.join(cache_dir, "0", "spec", "d", "spec.pickle")) |
---|
54 | assert item_cache["/d/c"]["v"] == "c" |
---|
55 | assert item_cache["/p"]["v"] == "p" |
---|
56 | p = item_cache["/p"] |
---|
57 | assert p["v"] == "p" |
---|
58 | assert p.map("/p") == p |
---|
59 | assert p.map("p") == p |
---|
60 | a = item_cache.all |
---|
61 | assert len(a) == 2 |
---|
62 | assert a["/p"]["v"] == "p" |
---|
63 | assert a["/d/c"]["v"] == "c" |
---|
64 | item_cache_2 = ItemCache(config) |
---|
65 | assert not item_cache_2.updates |
---|
66 | assert item_cache_2["/d/c"]["v"] == "c" |
---|
67 | with open(os.path.join(tmpdir, "spec", "d", "c.yml"), "w+") as out: |
---|
68 | out.write("links:\n- role: null\n uid: ../p\nv: x\n") |
---|
69 | item_cache_3 = ItemCache(config) |
---|
70 | assert item_cache_3.updates |
---|
71 | assert item_cache_3["/d/c"]["v"] == "x" |
---|
72 | item = item_cache_3.add_volatile_item( |
---|
73 | os.path.join(os.path.dirname(__file__), "spec/root.yml"), "/foo/bar") |
---|
74 | assert item.uid == "/foo/bar" |
---|
75 | assert item.type == "" |
---|
76 | assert item["type"] == "spec" |
---|
77 | os.remove(os.path.join(tmpdir, "spec", "d", "c.yml")) |
---|
78 | item_cache_4 = ItemCache(config) |
---|
79 | assert item_cache_4.updates |
---|
80 | with pytest.raises(KeyError): |
---|
81 | item_cache_4["/d/c"] |
---|
82 | |
---|
83 | |
---|
84 | def test_load_link_error(tmpdir): |
---|
85 | config = create_item_cache_config_and_copy_spec(tmpdir, |
---|
86 | "spec-item-cache-2") |
---|
87 | with pytest.raises( |
---|
88 | KeyError, |
---|
89 | match=r"^\"item '/a' links to non-existing item 'nix'\"$"): |
---|
90 | ItemCache(config) |
---|
91 | |
---|
92 | |
---|
93 | def test_load_yaml_error(tmpdir): |
---|
94 | config = create_item_cache_config_and_copy_spec(tmpdir, |
---|
95 | "spec-item-cache-3") |
---|
96 | match = r"""YAML error while loading specification item file '.*invalid.yml': while parsing a block mapping |
---|
97 | expected <block end>, but found ':' |
---|
98 | in "<unicode string>", line 1, column 1: |
---|
99 | : |
---|
100 | \^""" |
---|
101 | with pytest.raises(IOError, match=match): |
---|
102 | ItemCache(config) |
---|
103 | |
---|
104 | |
---|
105 | def get_x_to_b_value(ctx): |
---|
106 | assert ctx.key == "x-to-b" |
---|
107 | return ctx.value["b"] |
---|
108 | |
---|
109 | |
---|
110 | class Mapper(ItemMapper): |
---|
111 | def __init__(self, item): |
---|
112 | super().__init__(item) |
---|
113 | |
---|
114 | def u(self, value): |
---|
115 | return "u" + value |
---|
116 | |
---|
117 | def v(self, value): |
---|
118 | return "v" + value |
---|
119 | |
---|
120 | def dup(self, value): |
---|
121 | return value + value |
---|
122 | |
---|
123 | |
---|
124 | def test_item_mapper(tmpdir): |
---|
125 | config = create_item_cache_config_and_copy_spec(tmpdir, "spec-item-cache") |
---|
126 | item_cache = ItemCache(config) |
---|
127 | item = item_cache["/p"] |
---|
128 | base_mapper = ItemMapper(item) |
---|
129 | assert base_mapper["d/c:v"] == "c" |
---|
130 | mapper = Mapper(item) |
---|
131 | assert mapper.substitute(None) == "" |
---|
132 | assert mapper.substitute(None, prefix="v") == "" |
---|
133 | with mapper.prefix("v"): |
---|
134 | assert mapper[".:."] == "p" |
---|
135 | assert mapper[".:../x/y"] == "z" |
---|
136 | item_2, key_path_2, value_2 = mapper.map(".:.") |
---|
137 | assert item_2 == item |
---|
138 | assert key_path_2 == "/v" |
---|
139 | assert value_2 == "p" |
---|
140 | assert mapper.substitute("$$${.:.}") == "$p" |
---|
141 | assert mapper.substitute("$$${.:.}", prefix="v") == "$p" |
---|
142 | with mapper.prefix("x"): |
---|
143 | with mapper.prefix("y"): |
---|
144 | assert mapper[".:."] == "z" |
---|
145 | assert mapper["."] == "/p" |
---|
146 | assert mapper["d/c"] == "/d/c" |
---|
147 | assert mapper["d/c:v"] == "c" |
---|
148 | assert mapper["d/c:a/b"] == "e" |
---|
149 | assert mapper["d/c:a/b|u"] == "ue" |
---|
150 | mapper.add_get_value(":/a/x-to-b", get_x_to_b_value) |
---|
151 | assert mapper["d/c:a/x-to-b|u|v"] == "vue" |
---|
152 | assert mapper["d/c:a/f[1]"] == 2 |
---|
153 | assert mapper["d/c:a/../a/f[3]/g[0]|dup"] == 8 |
---|
154 | item_3, key_path_3, value_3 = mapper.map("/p:/v") |
---|
155 | assert item_3 == item |
---|
156 | assert key_path_3 == "/v" |
---|
157 | assert value_3 == "p" |
---|
158 | recursive_mapper = ItemMapper(item, recursive=True) |
---|
159 | assert recursive_mapper.substitute("${.:/r1/r2/r3}") == "foobar" |
---|
160 | assert recursive_mapper[".:/r1/r2/r3"] == "foobar" |
---|
161 | match = r"substitution for spec:/p using prefix 'blub' failed for text: \${}" |
---|
162 | with pytest.raises(ValueError, match=match): |
---|
163 | mapper.substitute("${}", item, "blub") |
---|
164 | match = r"item 'boom' relative to spec:/p specified by 'boom:bam' does not exist" |
---|
165 | with pytest.raises(ValueError, match=match): |
---|
166 | mapper.map("boom:bam", item, "blub") |
---|
167 | |
---|
168 | |
---|
169 | def test_empty_item_mapper(): |
---|
170 | item = EmptyItem() |
---|
171 | mapper = ItemMapper(item) |
---|
172 | assert mapper.item == item |
---|
173 | item_2 = EmptyItem() |
---|
174 | mapper.item = item_2 |
---|
175 | assert mapper.item == item_2 |
---|