changeset 292:6a044778371a

Some unittests for the new "strict" YAML parsing mode to prevent duplicate keys within a single YAML document
author Franz Glasner <f.glasner@feldmann-mg.com>
date Wed, 10 Feb 2021 15:04:27 +0100
parents edf5cc1ffd26
children 6f0bf673d4ff
files tests/data/duplicate-keys-2.yml tests/data/duplicate-keys.yml tests/test.py
diffstat 3 files changed, 52 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/data/duplicate-keys-2.yml	Wed Feb 10 15:04:27 2021 +0100
@@ -0,0 +1,13 @@
+# -*- coding: utf-8; mode: yaml; indent-tabs-mode: nil; -*-
+
+# a subordinate duplicate key
+
+%YAML 1.1
+---
+
+toplevel-key:
+  subkey1: the value
+
+  subkey2: 2nd value
+
+  subkey1: not allowed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/data/duplicate-keys.yml	Wed Feb 10 15:04:27 2021 +0100
@@ -0,0 +1,15 @@
+# -*- coding: utf-8; mode: yaml; indent-tabs-mode: nil; -*-
+
+# A top-level duplicate
+
+%YAML 1.1
+---
+
+top:
+  subkey1: the value
+
+other:
+  key2: another value
+
+top:
+  subkey2: the value
--- a/tests/test.py	Wed Feb 10 14:47:41 2021 +0100
+++ b/tests/test.py	Wed Feb 10 15:04:27 2021 +0100
@@ -137,6 +137,30 @@
         cfg = configmix.toml.load(os.path.join(TESTDATADIR, "conf10.toml"))
         self.__check_tree(cfg)
 
+    def test12_yaml_no_duplicate_keys(self):
+        import yaml.constructor as yc
+        with io.open(os.path.join(TESTDATADIR, "duplicate-keys.yml"), "rt",
+                     encoding="utf-8") as f:
+            cfg = configmix.yaml.safe_load(f)
+
+        with io.open(os.path.join(TESTDATADIR, "duplicate-keys.yml"), "rt",
+                     encoding="utf-8") as f:
+            self.assertRaises(yc.ConstructorError,
+                              configmix.yaml.safe_load,
+                              f, strict=True)
+
+    def test13_yaml_no_duplicate_keys_2(self):
+        import yaml.constructor as yc
+        with io.open(os.path.join(TESTDATADIR, "duplicate-keys-2.yml"), "rt",
+                     encoding="utf-8") as f:
+            cfg = configmix.yaml.safe_load(f)
+
+        with io.open(os.path.join(TESTDATADIR, "duplicate-keys-2.yml"), "rt",
+                     encoding="utf-8") as f:
+            self.assertRaises(yc.ConstructorError,
+                              configmix.yaml.safe_load,
+                              f, strict=True)
+
 
 class _T02MixinLoadAndMerge: