mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
3.0 KiB
Python
69 lines
3.0 KiB
Python
10 years ago
|
# (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/>.
|
||
|
|
||
10 years ago
|
# Make coding more python3-ish
|
||
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
|
|
||
10 years ago
|
from ansible.compat.tests import unittest
|
||
10 years ago
|
|
||
|
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
|
||
|
from ansible.errors import AnsibleError
|
||
|
|
||
10 years ago
|
from ansible.compat.tests import BUILTINS
|
||
|
from ansible.compat.tests.mock import mock_open, patch
|
||
10 years ago
|
|
||
|
class TestErrors(unittest.TestCase):
|
||
|
|
||
|
def setUp(self):
|
||
10 years ago
|
self.message = 'This is the error message'
|
||
10 years ago
|
|
||
10 years ago
|
self.obj = AnsibleBaseYAMLObject()
|
||
|
|
||
10 years ago
|
def tearDown(self):
|
||
|
pass
|
||
|
|
||
|
def test_basic_error(self):
|
||
|
e = AnsibleError(self.message)
|
||
10 years ago
|
self.assertEqual(e.message, 'ERROR! ' + self.message)
|
||
|
self.assertEqual(e.__repr__(), 'ERROR! ' + self.message)
|
||
10 years ago
|
|
||
10 years ago
|
@patch.object(AnsibleError, '_get_error_lines_from_file')
|
||
10 years ago
|
def test_error_with_object(self, mock_method):
|
||
10 years ago
|
self.obj.ansible_pos = ('foo.yml', 1, 1)
|
||
10 years ago
|
|
||
10 years ago
|
mock_method.return_value = ('this is line 1\n', '')
|
||
10 years ago
|
e = AnsibleError(self.message, self.obj)
|
||
10 years ago
|
|
||
10 years ago
|
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")
|
||
10 years ago
|
|
||
10 years ago
|
def test_get_error_lines_from_file(self):
|
||
10 years ago
|
m = mock_open()
|
||
|
m.return_value.readlines.return_value = ['this is line 1\n']
|
||
|
|
||
10 years ago
|
with patch('{0}.open'.format(BUILTINS), m):
|
||
10 years ago
|
# this line will be found in the file
|
||
10 years ago
|
self.obj.ansible_pos = ('foo.yml', 1, 1)
|
||
10 years ago
|
e = AnsibleError(self.message, self.obj)
|
||
10 years ago
|
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")
|
||
10 years ago
|
|
||
|
# this line will not be found, as it is out of the index range
|
||
10 years ago
|
self.obj.ansible_pos = ('foo.yml', 2, 1)
|
||
10 years ago
|
e = AnsibleError(self.message, self.obj)
|
||
10 years ago
|
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?)")
|
||
10 years ago
|
|