From 2bcd5744de9818a0b009feb32247e1a40bada0d6 Mon Sep 17 00:00:00 2001 From: Andrew Halberstadt Date: Tue, 24 Sep 2024 14:50:23 -0400 Subject: [PATCH] feat(config): implement 'get()' for GraphConfig --- src/taskgraph/config.py | 3 +++ test/test_config.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/test_config.py diff --git a/src/taskgraph/config.py b/src/taskgraph/config.py index ac384eab8..76b882b6b 100644 --- a/src/taskgraph/config.py +++ b/src/taskgraph/config.py @@ -106,6 +106,9 @@ def __getitem__(self, name): def __contains__(self, name): return name in self._config + def get(self, name): + return self._config.get(name) + def register(self): """ Add the project's taskgraph directory to the python path, and register diff --git a/test/test_config.py b/test/test_config.py new file mode 100644 index 000000000..596036f34 --- /dev/null +++ b/test/test_config.py @@ -0,0 +1,28 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import pytest + +from taskgraph.config import GraphConfig + + +def test_graph_config_basic(): + graph_config = GraphConfig({"foo": "bar"}, "root") + + assert "foo" in graph_config + assert graph_config["foo"] == "bar" + assert graph_config.get("foo") == "bar" + + assert "missing" not in graph_config + assert graph_config.get("missing") is None + + with pytest.raises(KeyError): + graph_config["missing"] + + # does not support assignment + with pytest.raises(TypeError): + graph_config["foo"] = "different" + + with pytest.raises(TypeError): + graph_config["baz"] = 2