# HG changeset patch # User Franz Glasner # Date 1612965867 -3600 # Node ID 6a044778371abbf51481191438b583612f9bfa77 # Parent edf5cc1ffd267b801cb008598e969bc74e333142 Some unittests for the new "strict" YAML parsing mode to prevent duplicate keys within a single YAML document diff -r edf5cc1ffd26 -r 6a044778371a tests/data/duplicate-keys-2.yml --- /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 diff -r edf5cc1ffd26 -r 6a044778371a tests/data/duplicate-keys.yml --- /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 diff -r edf5cc1ffd26 -r 6a044778371a tests/test.py --- 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: