mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Exclude dirs/files prefixed with . (#3986)
This commit is contained in:
parent
3230869f74
commit
1db18478d2
@ -64,11 +64,17 @@ def _include_yaml(loader: SafeLineLoader,
|
|||||||
return load_yaml(fname)
|
return load_yaml(fname)
|
||||||
|
|
||||||
|
|
||||||
def _find_files(directory, pattern):
|
def _is_file_valid(name: str) -> bool:
|
||||||
|
"""Decide if a file is valid."""
|
||||||
|
return not name.startswith('.')
|
||||||
|
|
||||||
|
|
||||||
|
def _find_files(directory: str, pattern: str):
|
||||||
"""Recursively load files in a directory."""
|
"""Recursively load files in a directory."""
|
||||||
for root, _dirs, files in os.walk(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 files:
|
||||||
if fnmatch.fnmatch(basename, pattern):
|
if _is_file_valid(basename) and fnmatch.fnmatch(basename, pattern):
|
||||||
filename = os.path.join(root, basename)
|
filename = os.path.join(root, basename)
|
||||||
yield filename
|
yield filename
|
||||||
|
|
||||||
|
@ -76,10 +76,13 @@ class TestYaml(unittest.TestCase):
|
|||||||
@patch('homeassistant.util.yaml.os.walk')
|
@patch('homeassistant.util.yaml.os.walk')
|
||||||
def test_include_dir_list(self, mock_walk):
|
def test_include_dir_list(self, mock_walk):
|
||||||
"""Test include dir list yaml."""
|
"""Test include dir list yaml."""
|
||||||
mock_walk.return_value = [['/tmp', [], ['one.yaml', 'two.yaml']]]
|
mock_walk.return_value = [
|
||||||
|
['/tmp', [], ['one.yaml', 'two.yaml']],
|
||||||
|
]
|
||||||
|
|
||||||
with patch_yaml_files({
|
with patch_yaml_files({
|
||||||
'/tmp/one.yaml': 'one', '/tmp/two.yaml': 'two'
|
'/tmp/one.yaml': 'one',
|
||||||
|
'/tmp/two.yaml': 'two',
|
||||||
}):
|
}):
|
||||||
conf = "key: !include_dir_list /tmp"
|
conf = "key: !include_dir_list /tmp"
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
@ -90,27 +93,35 @@ class TestYaml(unittest.TestCase):
|
|||||||
def test_include_dir_list_recursive(self, mock_walk):
|
def test_include_dir_list_recursive(self, mock_walk):
|
||||||
"""Test include dir recursive list yaml."""
|
"""Test include dir recursive list yaml."""
|
||||||
mock_walk.return_value = [
|
mock_walk.return_value = [
|
||||||
['/tmp', ['tmp2'], ['zero.yaml']],
|
['/tmp', ['tmp2', '.ignore', 'ignore'], ['zero.yaml']],
|
||||||
['/tmp/tmp2', [], ['one.yaml', 'two.yaml']],
|
['/tmp/tmp2', [], ['one.yaml', 'two.yaml']],
|
||||||
['/tmp/ignore', [], ['.ignore.yaml']]
|
['/tmp/ignore', [], ['.ignore.yaml']]
|
||||||
]
|
]
|
||||||
|
|
||||||
with patch_yaml_files({
|
with patch_yaml_files({
|
||||||
'/tmp/zero.yaml': 'zero', '/tmp/tmp2/one.yaml': 'one',
|
'/tmp/zero.yaml': 'zero',
|
||||||
|
'/tmp/tmp2/one.yaml': 'one',
|
||||||
'/tmp/tmp2/two.yaml': 'two'
|
'/tmp/tmp2/two.yaml': 'two'
|
||||||
}):
|
}):
|
||||||
conf = "key: !include_dir_list /tmp"
|
conf = "key: !include_dir_list /tmp"
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
|
assert '.ignore' in mock_walk.return_value[0][1], \
|
||||||
|
"Expecting .ignore in here"
|
||||||
doc = yaml.yaml.safe_load(file)
|
doc = yaml.yaml.safe_load(file)
|
||||||
|
assert 'tmp2' in mock_walk.return_value[0][1]
|
||||||
|
assert '.ignore' not in mock_walk.return_value[0][1]
|
||||||
assert sorted(doc["key"]) == sorted(["zero", "one", "two"])
|
assert sorted(doc["key"]) == sorted(["zero", "one", "two"])
|
||||||
|
|
||||||
@patch('homeassistant.util.yaml.os.walk')
|
@patch('homeassistant.util.yaml.os.walk')
|
||||||
def test_include_dir_named(self, mock_walk):
|
def test_include_dir_named(self, mock_walk):
|
||||||
"""Test include dir named yaml."""
|
"""Test include dir named yaml."""
|
||||||
mock_walk.return_value = [['/tmp', [], ['first.yaml', 'second.yaml']]]
|
mock_walk.return_value = [
|
||||||
|
['/tmp', [], ['first.yaml', 'second.yaml']]
|
||||||
|
]
|
||||||
|
|
||||||
with patch_yaml_files({
|
with patch_yaml_files({
|
||||||
'/tmp/first.yaml': 'one', '/tmp/second.yaml': 'two'
|
'/tmp/first.yaml': 'one',
|
||||||
|
'/tmp/second.yaml': 'two'
|
||||||
}):
|
}):
|
||||||
conf = "key: !include_dir_named /tmp"
|
conf = "key: !include_dir_named /tmp"
|
||||||
correct = {'first': 'one', 'second': 'two'}
|
correct = {'first': 'one', 'second': 'two'}
|
||||||
@ -122,19 +133,24 @@ class TestYaml(unittest.TestCase):
|
|||||||
def test_include_dir_named_recursive(self, mock_walk):
|
def test_include_dir_named_recursive(self, mock_walk):
|
||||||
"""Test include dir named yaml."""
|
"""Test include dir named yaml."""
|
||||||
mock_walk.return_value = [
|
mock_walk.return_value = [
|
||||||
['/tmp', ['tmp2'], ['first.yaml']],
|
['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']],
|
||||||
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
|
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
|
||||||
['/tmp/ignore', [], ['.ignore.yaml']]
|
['/tmp/ignore', [], ['.ignore.yaml']]
|
||||||
]
|
]
|
||||||
|
|
||||||
with patch_yaml_files({
|
with patch_yaml_files({
|
||||||
'/tmp/first.yaml': 'one', '/tmp/tmp2/second.yaml': 'two',
|
'/tmp/first.yaml': 'one',
|
||||||
|
'/tmp/tmp2/second.yaml': 'two',
|
||||||
'/tmp/tmp2/third.yaml': 'three'
|
'/tmp/tmp2/third.yaml': 'three'
|
||||||
}):
|
}):
|
||||||
conf = "key: !include_dir_named /tmp"
|
conf = "key: !include_dir_named /tmp"
|
||||||
correct = {'first': 'one', 'second': 'two', 'third': 'three'}
|
correct = {'first': 'one', 'second': 'two', 'third': 'three'}
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
|
assert '.ignore' in mock_walk.return_value[0][1], \
|
||||||
|
"Expecting .ignore in here"
|
||||||
doc = yaml.yaml.safe_load(file)
|
doc = yaml.yaml.safe_load(file)
|
||||||
|
assert 'tmp2' in mock_walk.return_value[0][1]
|
||||||
|
assert '.ignore' not in mock_walk.return_value[0][1]
|
||||||
assert doc["key"] == correct
|
assert doc["key"] == correct
|
||||||
|
|
||||||
@patch('homeassistant.util.yaml.os.walk')
|
@patch('homeassistant.util.yaml.os.walk')
|
||||||
@ -155,18 +171,23 @@ class TestYaml(unittest.TestCase):
|
|||||||
def test_include_dir_merge_list_recursive(self, mock_walk):
|
def test_include_dir_merge_list_recursive(self, mock_walk):
|
||||||
"""Test include dir merge list yaml."""
|
"""Test include dir merge list yaml."""
|
||||||
mock_walk.return_value = [
|
mock_walk.return_value = [
|
||||||
['/tmp', ['tmp2'], ['first.yaml']],
|
['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']],
|
||||||
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
|
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
|
||||||
['/tmp/ignore', [], ['.ignore.yaml']]
|
['/tmp/ignore', [], ['.ignore.yaml']]
|
||||||
]
|
]
|
||||||
|
|
||||||
with patch_yaml_files({
|
with patch_yaml_files({
|
||||||
'/tmp/first.yaml': '- one', '/tmp/tmp2/second.yaml': '- two',
|
'/tmp/first.yaml': '- one',
|
||||||
|
'/tmp/tmp2/second.yaml': '- two',
|
||||||
'/tmp/tmp2/third.yaml': '- three\n- four'
|
'/tmp/tmp2/third.yaml': '- three\n- four'
|
||||||
}):
|
}):
|
||||||
conf = "key: !include_dir_merge_list /tmp"
|
conf = "key: !include_dir_merge_list /tmp"
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
|
assert '.ignore' in mock_walk.return_value[0][1], \
|
||||||
|
"Expecting .ignore in here"
|
||||||
doc = yaml.yaml.safe_load(file)
|
doc = yaml.yaml.safe_load(file)
|
||||||
|
assert 'tmp2' in mock_walk.return_value[0][1]
|
||||||
|
assert '.ignore' not in mock_walk.return_value[0][1]
|
||||||
assert sorted(doc["key"]) == sorted(["one", "two",
|
assert sorted(doc["key"]) == sorted(["one", "two",
|
||||||
"three", "four"])
|
"three", "four"])
|
||||||
|
|
||||||
@ -192,7 +213,7 @@ class TestYaml(unittest.TestCase):
|
|||||||
def test_include_dir_merge_named_recursive(self, mock_walk):
|
def test_include_dir_merge_named_recursive(self, mock_walk):
|
||||||
"""Test include dir merge named yaml."""
|
"""Test include dir merge named yaml."""
|
||||||
mock_walk.return_value = [
|
mock_walk.return_value = [
|
||||||
['/tmp', ['tmp2'], ['first.yaml']],
|
['/tmp', ['tmp2', '.ignore', 'ignore'], ['first.yaml']],
|
||||||
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
|
['/tmp/tmp2', [], ['second.yaml', 'third.yaml']],
|
||||||
['/tmp/ignore', [], ['.ignore.yaml']]
|
['/tmp/ignore', [], ['.ignore.yaml']]
|
||||||
]
|
]
|
||||||
@ -204,7 +225,11 @@ class TestYaml(unittest.TestCase):
|
|||||||
}):
|
}):
|
||||||
conf = "key: !include_dir_merge_named /tmp"
|
conf = "key: !include_dir_merge_named /tmp"
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
|
assert '.ignore' in mock_walk.return_value[0][1], \
|
||||||
|
"Expecting .ignore in here"
|
||||||
doc = yaml.yaml.safe_load(file)
|
doc = yaml.yaml.safe_load(file)
|
||||||
|
assert 'tmp2' in mock_walk.return_value[0][1]
|
||||||
|
assert '.ignore' not in mock_walk.return_value[0][1]
|
||||||
assert doc["key"] == {
|
assert doc["key"] == {
|
||||||
"key1": "one",
|
"key1": "one",
|
||||||
"key2": "two",
|
"key2": "two",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user