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
This commit is contained in:
Quentin Stafford-Fraser 2019-03-21 06:10:09 +00:00 committed by Paulus Schoutsen
parent 4b1de61110
commit 88be786e82
2 changed files with 3 additions and 3 deletions

View File

@ -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

View File

@ -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):