From 88be786e82106297e091191b6fe4247b231419a4 Mon Sep 17 00:00:00 2001 From: Quentin Stafford-Fraser Date: Thu, 21 Mar 2019 06:10:09 +0000 Subject: [PATCH] Make !include_dir_list use alphanumeric order (#21902) * Make YAML includes such as !include_dir_list incorporate files in alphabetical order * Test for !include_dir_list sorting --- homeassistant/util/yaml.py | 2 +- tests/util/test_yaml.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/util/yaml.py b/homeassistant/util/yaml.py index c988cb811b2..15bf73f459d 100644 --- a/homeassistant/util/yaml.py +++ b/homeassistant/util/yaml.py @@ -144,7 +144,7 @@ def _find_files(directory: str, pattern: str) -> Iterator[str]: """Recursively load files in a directory.""" for root, dirs, files in os.walk(directory, topdown=True): dirs[:] = [d for d in dirs if _is_file_valid(d)] - for basename in files: + for basename in sorted(files): if _is_file_valid(basename) and fnmatch.fnmatch(basename, pattern): filename = os.path.join(root, basename) yield filename diff --git a/tests/util/test_yaml.py b/tests/util/test_yaml.py index 2eab75cb92a..46dc3c045b2 100644 --- a/tests/util/test_yaml.py +++ b/tests/util/test_yaml.py @@ -95,7 +95,7 @@ class TestYaml(unittest.TestCase): def test_include_dir_list(self, mock_walk): """Test include dir list yaml.""" mock_walk.return_value = [ - ['/tmp', [], ['one.yaml', 'two.yaml']], + ['/tmp', [], ['two.yaml', 'one.yaml']], ] with patch_yaml_files({ @@ -105,7 +105,7 @@ class TestYaml(unittest.TestCase): conf = "key: !include_dir_list /tmp" with io.StringIO(conf) as file: doc = yaml.yaml.safe_load(file) - assert sorted(doc["key"]) == sorted(["one", "two"]) + assert doc["key"] == sorted(["one", "two"]) @patch('homeassistant.util.yaml.os.walk') def test_include_dir_list_recursive(self, mock_walk):