summaryrefslogtreecommitdiff
path: root/test/units/mock
diff options
context:
space:
mode:
authorJames Cammarata <jimi@sngx.net>2015-05-03 21:47:26 -0500
committerJames Cammarata <jimi@sngx.net>2015-05-03 21:47:26 -0500
commitce3ef7f4c16e47d5a0b5600e1c56c177b7c93f0d (patch)
treeebb90eaba034dfb4ea8a3afe746a87f9b7dee66f /test/units/mock
parent8cf4452d48e583cfd59f96e67cfd34a1c35226e7 (diff)
downloadansible-ce3ef7f4c16e47d5a0b5600e1c56c177b7c93f0d.tar.gz
Making the switch to v2
Diffstat (limited to 'test/units/mock')
-rw-r--r--test/units/mock/__init__.py20
-rw-r--r--test/units/mock/loader.py83
2 files changed, 103 insertions, 0 deletions
diff --git a/test/units/mock/__init__.py b/test/units/mock/__init__.py
new file mode 100644
index 0000000000..ae8ccff595
--- /dev/null
+++ b/test/units/mock/__init__.py
@@ -0,0 +1,20 @@
+# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
+#
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+# Make coding more python3-ish
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
diff --git a/test/units/mock/loader.py b/test/units/mock/loader.py
new file mode 100644
index 0000000000..cf9d7ea72d
--- /dev/null
+++ b/test/units/mock/loader.py
@@ -0,0 +1,83 @@
+# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
+#
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
+
+# Make coding more python3-ish
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import os
+
+from ansible.parsing import DataLoader
+
+class DictDataLoader(DataLoader):
+
+ def __init__(self, file_mapping=dict()):
+ assert type(file_mapping) == dict
+
+ self._file_mapping = file_mapping
+ self._build_known_directories()
+
+ super(DictDataLoader, self).__init__()
+
+ def load_from_file(self, path):
+ if path in self._file_mapping:
+ return self.load(self._file_mapping[path], path)
+ return None
+
+ def path_exists(self, path):
+ return path in self._file_mapping or path in self._known_directories
+
+ def is_file(self, path):
+ return path in self._file_mapping
+
+ def is_directory(self, path):
+ return path in self._known_directories
+
+ def list_directory(self, path):
+ return [x for x in self._known_directories]
+
+ def _add_known_directory(self, directory):
+ if directory not in self._known_directories:
+ self._known_directories.append(directory)
+
+ def _build_known_directories(self):
+ self._known_directories = []
+ for path in self._file_mapping:
+ dirname = os.path.dirname(path)
+ while dirname not in ('/', ''):
+ self._add_known_directory(dirname)
+ dirname = os.path.dirname(dirname)
+
+ def push(self, path, content):
+ rebuild_dirs = False
+ if path not in self._file_mapping:
+ rebuild_dirs = True
+
+ self._file_mapping[path] = content
+
+ if rebuild_dirs:
+ self._build_known_directories()
+
+ def pop(self, path):
+ if path in self._file_mapping:
+ del self._file_mapping[path]
+ self._build_known_directories()
+
+ def clear(self):
+ self._file_mapping = dict()
+ self._known_directories = []
+