summaryrefslogtreecommitdiff
path: root/tests/yaml
diff options
context:
space:
mode:
authorPhillip Smyth <phillipsmyth@codethink.co.uk>2018-01-18 18:43:22 +0000
committerJürg Billeter <j@bitron.ch>2018-02-15 17:55:35 +0100
commit57c8b8884828b900d64188a3032f5adb04d46372 (patch)
tree500dd2673adef390bf7580359a0cc6cfb12572e9 /tests/yaml
parent1b664f7170eef19b0a585cfbafa5b17a28349407 (diff)
downloadbuildstream-57c8b8884828b900d64188a3032f5adb04d46372.tar.gz
_yaml.py: Prevent ruamel from removing underscores
The ruamel parser interprets values such as 1_2_3 as numbers, ignoring the underscores. However, for `track` we need the value as a verbatim string. This change configures ruamel to parse all values as strings, leaving potential conversions to node_get() with the `expected_type` parameter. This fixes #166 and adds tests
Diffstat (limited to 'tests/yaml')
-rw-r--r--tests/yaml/data/convert_value_to_str.yaml4
-rw-r--r--tests/yaml/yaml.py40
2 files changed, 44 insertions, 0 deletions
diff --git a/tests/yaml/data/convert_value_to_str.yaml b/tests/yaml/data/convert_value_to_str.yaml
new file mode 100644
index 000000000..4a59e3287
--- /dev/null
+++ b/tests/yaml/data/convert_value_to_str.yaml
@@ -0,0 +1,4 @@
+Test1: 1_23_4
+Test2: 1.23.4
+Test3: 1.20
+Test4: OneTwoThree
diff --git a/tests/yaml/yaml.py b/tests/yaml/yaml.py
index a462e18df..3b9f385ed 100644
--- a/tests/yaml/yaml.py
+++ b/tests/yaml/yaml.py
@@ -350,3 +350,43 @@ def test_list_composition_twice(datafiles, filename1, filename2,
assert child['mood'] == mood
assert_provenance(prov_file, prov_line, prov_col, child, 'mood')
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR))
+def test_convert_value_to_string(datafiles):
+ conf_file = os.path.join(datafiles.dirname,
+ datafiles.basename,
+ 'convert_value_to_str.yaml')
+
+ # Run file through yaml to convert it
+ test_dict = _yaml.load(conf_file)
+
+ user_config = _yaml.node_get(test_dict, str, "Test1")
+ assert isinstance(user_config, str)
+ assert user_config == "1_23_4"
+
+ user_config = _yaml.node_get(test_dict, str, "Test2")
+ assert isinstance(user_config, str)
+ assert user_config == "1.23.4"
+
+ user_config = _yaml.node_get(test_dict, str, "Test3")
+ assert isinstance(user_config, str)
+ assert user_config == "1.20"
+
+ user_config = _yaml.node_get(test_dict, str, "Test4")
+ assert isinstance(user_config, str)
+ assert user_config == "OneTwoThree"
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR))
+def test_value_doesnt_match_expected(datafiles):
+ conf_file = os.path.join(datafiles.dirname,
+ datafiles.basename,
+ 'convert_value_to_str.yaml')
+
+ # Run file through yaml to convert it
+ test_dict = _yaml.load(conf_file)
+
+ with pytest.raises(LoadError) as exc:
+ user_config = _yaml.node_get(test_dict, int, "Test4")
+ assert exc.value.reason == LoadErrorReason.INVALID_DATA