only add data when there is data to add (#54559)

Only add data when there is data to add

  also avoid clobbering existing data with empty file
  fixes #45843

* remove redundant code, update comments
* fix mock dataloader, original does not return None
* added test
This commit is contained in:
Brian Coca
2021-02-11 10:07:20 -05:00
committed by GitHub
parent 4c5ce5a1a9
commit ec8a556538
9 changed files with 60 additions and 19 deletions

View File

@ -0,0 +1,4 @@
- hosts: all
gather_facts: false
roles:
- data

View File

@ -0,0 +1 @@
defined_var: 1

View File

@ -0,0 +1,5 @@
- name: ensure data was correctly defind
assert:
that:
- defined_var is defined
- defined_var == 1

View File

@ -12,3 +12,7 @@ set -eux
# include/import can execute another instance of role
[ "$(ansible-playbook allowed_dupes.yml -i ../../inventory --tags importrole "$@" | grep -c '"msg": "A"')" = "2" ]
[ "$(ansible-playbook allowed_dupes.yml -i ../../inventory --tags includerole "$@" | grep -c '"msg": "A"')" = "2" ]
# ensure role data is merged correctly
ansible-playbook data_integrity.yml -i ../../inventory "$@"

View File

@ -39,10 +39,11 @@ class DictDataLoader(DataLoader):
self._vault_secrets = None
def load_from_file(self, path, cache=True, unsafe=False):
data = None
path = to_text(path)
if path in self._file_mapping:
return self.load(self._file_mapping[path], path)
return None
data = self.load(self._file_mapping[path], path)
return data
# TODO: the real _get_file_contents returns a bytestring, so we actually convert the
# unicode/text it's created with to utf-8