summaryrefslogtreecommitdiffstats
path: root/test/units/errors
diff options
context:
space:
mode:
authorJames Cammarata <jimi@sngx.net>2015-05-04 04:47:26 +0200
committerJames Cammarata <jimi@sngx.net>2015-05-04 04:47:26 +0200
commitce3ef7f4c16e47d5a0b5600e1c56c177b7c93f0d (patch)
treeebb90eaba034dfb4ea8a3afe746a87f9b7dee66f /test/units/errors
parentFix module arg parsing when 'args' are present but not a dict (v2) (diff)
downloadansible-ce3ef7f4c16e47d5a0b5600e1c56c177b7c93f0d.tar.xz
ansible-ce3ef7f4c16e47d5a0b5600e1c56c177b7c93f0d.zip
Making the switch to v2
Diffstat (limited to 'test/units/errors')
-rw-r--r--test/units/errors/__init__.py22
-rw-r--r--test/units/errors/test_errors.py68
2 files changed, 90 insertions, 0 deletions
diff --git a/test/units/errors/__init__.py b/test/units/errors/__init__.py
new file mode 100644
index 0000000000..20207b272d
--- /dev/null
+++ b/test/units/errors/__init__.py
@@ -0,0 +1,22 @@
+# (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/errors/test_errors.py b/test/units/errors/test_errors.py
new file mode 100644
index 0000000000..3993ea5061
--- /dev/null
+++ b/test/units/errors/test_errors.py
@@ -0,0 +1,68 @@
+# (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
+
+from ansible.compat.tests import unittest
+
+from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
+from ansible.errors import AnsibleError
+
+from ansible.compat.tests import BUILTINS
+from ansible.compat.tests.mock import mock_open, patch
+
+class TestErrors(unittest.TestCase):
+
+ def setUp(self):
+ self.message = 'This is the error message'
+
+ self.obj = AnsibleBaseYAMLObject()
+
+ def tearDown(self):
+ pass
+
+ def test_basic_error(self):
+ e = AnsibleError(self.message)
+ self.assertEqual(e.message, 'ERROR! ' + self.message)
+ self.assertEqual(e.__repr__(), 'ERROR! ' + self.message)
+
+ @patch.object(AnsibleError, '_get_error_lines_from_file')
+ def test_error_with_object(self, mock_method):
+ self.obj.ansible_pos = ('foo.yml', 1, 1)
+
+ mock_method.return_value = ('this is line 1\n', '')
+ e = AnsibleError(self.message, self.obj)
+
+ self.assertEqual(e.message, "ERROR! This is the error message\n\nThe error appears to have been in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis is line 1\n^ here\n")
+
+ def test_get_error_lines_from_file(self):
+ m = mock_open()
+ m.return_value.readlines.return_value = ['this is line 1\n']
+
+ with patch('{0}.open'.format(BUILTINS), m):
+ # this line will be found in the file
+ self.obj.ansible_pos = ('foo.yml', 1, 1)
+ e = AnsibleError(self.message, self.obj)
+ self.assertEqual(e.message, "ERROR! This is the error message\n\nThe error appears to have been in 'foo.yml': line 1, column 1, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\nthis is line 1\n^ here\n")
+
+ # this line will not be found, as it is out of the index range
+ self.obj.ansible_pos = ('foo.yml', 2, 1)
+ e = AnsibleError(self.message, self.obj)
+ self.assertEqual(e.message, "ERROR! This is the error message\n\nThe error appears to have been in 'foo.yml': line 2, column 1, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\n(specified line no longer in file, maybe it changed?)")
+