blob: f3a017966435b93d530ec221960ea4aab77277e7 [file] [log] [blame]
Rupert Swarbrick592cd5d2020-06-10 16:45:03 +01001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4
5'''pytest-based testing for functions in utils.py'''
6
7import os
8import pytest
9from .utils import _subst_wildcards, subst_wildcards
10
11
12def test_subst_wildcards():
13 '''Pytest-compatible test for the subst_wildcards function.'''
14 # Basic checks
15 assert subst_wildcards('foo {x} baz', {'x': 'bar'}) == 'foo bar baz'
16
17 # Stringify
18 assert (subst_wildcards('{a}, {b}, {c}, {d}',
19 {'a': 'a', 'b': True, 'c': 42, 'd': ['{b}', 10]}) ==
20 'a, 1, 42, 1 10')
21
22 # Ignored wildcards (with or without a match in mdict)
23 assert (subst_wildcards('{a} {b}', {'a': 'aye', 'b': 'bee'},
24 ignored_wildcards=['a']) ==
25 '{a} bee')
26 assert (subst_wildcards('{a} {b}', {'b': 'bee'},
27 ignored_wildcards=['a']) ==
28 '{a} bee')
29
30 # Environment variables. We will always have PWD and can probably assume
31 # that this won't itself have any braced substrings.
32 assert (subst_wildcards('{PWD}', {}) == os.environ['PWD'])
33
34 # Missing variable with ignore_error=False, running _subst_wildcards
35 # instead so that we can catch the error. (We assume that 'biggles' isn't
36 # in the environment)
37 with pytest.raises(ValueError) as excinfo:
38 _subst_wildcards('{biggles} {b}', {'b': 'bee'}, [], False, [])
39 assert "unknown wildcard, '{biggles}'" in str(excinfo.value)
40
41 # ignore_error=True.
42 assert (subst_wildcards('{biggles} {b}', {'b': 'bee'},
43 ignore_error=True) ==
44 '{biggles} bee')
45
46 # Check we support (non-circular) recursion
47 assert (subst_wildcards('{a}', {'a': '{b}', 'b': 'c'}) == 'c')
48
49 # Check we spot circular recursion
50 with pytest.raises(ValueError) as excinfo:
51 _subst_wildcards('{a}', {'a': '{b}', 'b': '{a}'}, [], False, [])
52 assert "circular expansion of wildcard '{a}'" in str(excinfo.value)
53
54 # Check we also complain about circular recursion with ignore_error
55 with pytest.raises(ValueError) as excinfo:
56 _subst_wildcards('{a}', {'a': '{b}', 'b': '{a}'}, [], True, [])
57 assert "circular expansion of wildcard '{a}'" in str(excinfo.value)
58
59 # Computed variable names (probably not a great idea, but it's probably
60 # good to check this works the way we think)
61 assert subst_wildcards('{a}b}', {'a': 'a {', 'b': 'bee'}) == 'a bee'
62
63 # Some eval_cmd calls (using echo, which should always work)
64 assert (subst_wildcards('{eval_cmd}echo foo {b}', {'b': 'bar'}) ==
65 'foo bar')
66
Srikrishna Iyer9bff8ea2021-04-02 15:14:04 -070067 assert (subst_wildcards('foo {eval_cmd}echo {b}', {'b': 'bar'}) ==
68 'foo bar')
69
Rupert Swarbrick592cd5d2020-06-10 16:45:03 +010070 # Make sure that nested commands work
71 assert (subst_wildcards('{eval_cmd} {eval_cmd} echo echo a', {}) == 'a')
72
73 # Recursive expansion
74 assert (subst_wildcards('{var}',
75 {
76 'var': '{{foo}_xyz_{bar}}',
77 'foo': 'p',
78 'bar': 'q',
79 'p_xyz_q': 'baz'
80 }) == 'baz')