2014-10-16 00:35:16 +02:00
# (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/>.
2014-10-16 01:25:48 +02:00
# Make coding more python3-ish
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
2014-10-21 07:14:30 +02:00
from ansible . compat . tests import unittest
2014-10-16 00:35:16 +02:00
from ansible . parsing . yaml . objects import AnsibleBaseYAMLObject
from ansible . errors import AnsibleError
2014-10-21 07:14:30 +02:00
from ansible . compat . tests import BUILTINS
from ansible . compat . tests . mock import mock_open , patch
2014-10-16 00:35:16 +02:00
class TestErrors ( unittest . TestCase ) :
def setUp ( self ) :
2014-10-23 22:39:27 +02:00
self . message = ' This is the error message '
2014-10-16 00:35:16 +02:00
2014-10-16 02:59:51 +02:00
self . obj = AnsibleBaseYAMLObject ( )
2014-10-16 00:35:16 +02:00
def tearDown ( self ) :
pass
def test_basic_error ( self ) :
e = AnsibleError ( self . message )
2014-11-14 23:14:08 +01:00
self . assertEqual ( e . message , ' ERROR! ' + self . message )
self . assertEqual ( e . __repr__ ( ) , ' ERROR! ' + self . message )
2014-10-16 00:35:16 +02:00
2014-10-23 22:39:27 +02:00
@patch.object ( AnsibleError , ' _get_error_lines_from_file ' )
2014-10-16 02:59:51 +02:00
def test_error_with_object ( self , mock_method ) :
2015-04-01 23:54:22 +02:00
self . obj . ansible_pos = ( ' foo.yml ' , 1 , 1 )
2014-10-16 00:35:16 +02:00
2014-10-23 22:39:27 +02:00
mock_method . return_value = ( ' this is line 1 \n ' , ' ' )
2014-10-16 02:59:51 +02:00
e = AnsibleError ( self . message , self . obj )
2014-10-16 00:35:16 +02:00
2014-11-14 23:14:08 +01:00
self . assertEqual ( e . message , " ERROR! This is the error message \n \n The error appears to have been in ' foo.yml ' : line 1, column 1, but may \n be elsewhere in the file depending on the exact syntax problem. \n \n The offending line appears to be: \n \n \n this is line 1 \n ^ here \n " )
2014-10-16 02:59:51 +02:00
2014-10-23 22:39:27 +02:00
def test_get_error_lines_from_file ( self ) :
2014-10-16 02:59:51 +02:00
m = mock_open ( )
m . return_value . readlines . return_value = [ ' this is line 1 \n ' ]
2014-10-21 07:24:09 +02:00
with patch ( ' {0} .open ' . format ( BUILTINS ) , m ) :
2014-10-16 02:59:51 +02:00
# this line will be found in the file
2015-04-01 23:54:22 +02:00
self . obj . ansible_pos = ( ' foo.yml ' , 1 , 1 )
2014-10-16 02:59:51 +02:00
e = AnsibleError ( self . message , self . obj )
2014-11-14 23:14:08 +01:00
self . assertEqual ( e . message , " ERROR! This is the error message \n \n The error appears to have been in ' foo.yml ' : line 1, column 1, but may \n be elsewhere in the file depending on the exact syntax problem. \n \n The offending line appears to be: \n \n \n this is line 1 \n ^ here \n " )
2014-10-16 02:59:51 +02:00
# this line will not be found, as it is out of the index range
2015-04-01 23:54:22 +02:00
self . obj . ansible_pos = ( ' foo.yml ' , 2 , 1 )
2014-10-16 02:59:51 +02:00
e = AnsibleError ( self . message , self . obj )
2014-11-14 23:14:08 +01:00
self . assertEqual ( e . message , " ERROR! This is the error message \n \n The error appears to have been in ' foo.yml ' : line 2, column 1, but may \n be elsewhere in the file depending on the exact syntax problem. \n \n (specified line no longer in file, maybe it changed?) " )
2015-04-01 23:54:22 +02:00