| 1 | from collections import OrderedDict |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | from labgrid.config import Config |
| 6 | from labgrid.exceptions import InvalidConfigError |
| 7 | |
| 8 | |
| 9 | def test_get_target_option(tmpdir): |
| 10 | p = tmpdir.join("config.yaml") |
| 11 | p.write( |
| 12 | """ |
| 13 | targets: |
| 14 | main: |
| 15 | options: |
| 16 | str: test |
| 17 | list: [1, 2, 3] |
| 18 | dict: |
| 19 | a: 1 |
| 20 | b: 2 |
| 21 | bool: False |
| 22 | int: 0x20 |
| 23 | float: 3.14 |
| 24 | none: null |
| 25 | """ |
| 26 | ) |
| 27 | c = Config(str(p)) |
| 28 | assert c.get_target_option("main", "str") == "test" |
| 29 | assert c.get_target_option("main", "list") == [1, 2, 3] |
| 30 | assert c.get_target_option("main", "dict") == OrderedDict([("a", 1), ("b", 2)]) |
| 31 | assert c.get_target_option("main", "bool") is False |
| 32 | assert c.get_target_option("main", "int") == 0x20 |
| 33 | assert c.get_target_option("main", "float") == 3.14 |
| 34 | assert c.get_target_option("main", "none") is None |
| 35 | |
| 36 | with pytest.raises(KeyError) as err: |
| 37 | c.get_target_option("main", "blah") |
| 38 | assert "No option" in str(err) |
| 39 | |
| 40 | with pytest.raises(KeyError) as err: |
| 41 | c.get_target_option("nonexist", "str") |
| 42 | assert "No target" in str(err) |
| 43 | |
| 44 | |
| 45 | def test_set_target_option(tmpdir): |
| 46 | p = tmpdir.join("config.yaml") |
| 47 | p.write( |
| 48 | """ |
| 49 | targets: |
| 50 | main: |
| 51 | """ |
| 52 | ) |
| 53 | c = Config(str(p)) |
| 54 | |
| 55 | with pytest.raises(KeyError) as err: |
| 56 | c.get_target_option("main", "spam") |
| 57 | assert "No option" in str(err) |
| 58 | |
| 59 | c.set_target_option("main", "spam", "eggs") |
| 60 | assert c.get_target_option("main", "spam") == "eggs" |
| 61 | |
| 62 | obj = object() |
| 63 | c.set_target_option("main", "obj", obj) |
| 64 | assert c.get_target_option("main", "obj") is obj |
| 65 | |
| 66 | |
| 67 | def test_template(tmpdir): |
| 68 | p = tmpdir.join("config.yaml") |
| 69 | p.write( |
| 70 | """ |
| 71 | dict: |
| 72 | list: |
| 73 | - a |
| 74 | - b |
| 75 | - !template $BASE |
| 76 | string: !template ${BASE}/suffix |
| 77 | """ |
| 78 | ) |
| 79 | c = Config(str(p)) |
| 80 | assert "a" in c.data["dict"]["list"] |
| 81 | assert c.data["dict"]["list"][2] == str(tmpdir) |
| 82 | assert c.data["dict"]["string"] == str(tmpdir) + "/suffix" |
| 83 | |
| 84 | |
| 85 | def test_template_bad_placeholder(tmpdir): |
| 86 | p = tmpdir.join("config.yaml") |
| 87 | p.write( |
| 88 | """ |
| 89 | string: !template $ |
| 90 | """ |
| 91 | ) |
| 92 | with pytest.raises(InvalidConfigError) as excinfo: |
| 93 | Config(str(p)) |
| 94 | assert "is invalid" in excinfo.value.msg |
| 95 | assert "template string" in excinfo.value.msg |
| 96 | |
| 97 | |
| 98 | def test_template_bad_key(tmpdir): |
| 99 | p = tmpdir.join("config.yaml") |
| 100 | p.write( |
| 101 | """ |
| 102 | string: !template ${INVALID} |
| 103 | """ |
| 104 | ) |
| 105 | with pytest.raises(InvalidConfigError) as excinfo: |
| 106 | Config(str(p)) |
| 107 | assert "unknown variable" in excinfo.value.msg |
| 108 | |
| 109 | |
| 110 | def test_tool(tmpdir): |
| 111 | t = tmpdir.join("testtool") |
| 112 | t.write("content") |
| 113 | p = tmpdir.join("config.yaml") |
| 114 | p.write( |
| 115 | """ |
| 116 | tools: |
| 117 | testtool: {} |
| 118 | """.format(t) |
| 119 | ) |
| 120 | c = Config(str(p)) |
| 121 | |
| 122 | assert c.get_tool("testtool") == t |
| 123 | |
| 124 | |
| 125 | def test_tool_no_explicit_tool(tmpdir): |
| 126 | t = tmpdir.join("testtool") |
| 127 | t.write("content") |
| 128 | p = tmpdir.join("config.yaml") |
| 129 | p.write( |
| 130 | """ |
| 131 | dict: {} |
| 132 | """ |
| 133 | ) |
| 134 | c = Config(str(p)) |
| 135 | |
| 136 | assert c.get_tool("testtool") == "testtool" |