Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
a50332fc
Commit
a50332fc
authored
Oct 09, 2014
by
James Cammarata
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding v2 error line support, and tests
parent
383a44a4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
91 additions
and
6 deletions
+91
-6
test/v2/errors/__init__.py
+18
-0
test/v2/errors/test_errors.py
+33
-0
v2/ansible/errors/__init__.py
+37
-6
v2/ansible/parsing/yaml/objects.py
+3
-0
No files found.
test/v2/errors/__init__.py
0 → 100644
View file @
a50332fc
# (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/>.
test/v2/errors/test_errors.py
0 → 100644
View file @
a50332fc
# TODO: header
import
unittest
from
mock
import
mock_open
,
patch
from
ansible.parsing.yaml.objects
import
AnsibleBaseYAMLObject
from
ansible.errors
import
AnsibleError
class
TestErrors
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
message
=
'this is the error message'
def
tearDown
(
self
):
pass
def
test_basic_error
(
self
):
e
=
AnsibleError
(
self
.
message
)
assert
e
.
message
==
self
.
message
def
test_error_with_object
(
self
):
obj
=
AnsibleBaseYAMLObject
()
obj
.
_data_source
=
'foo.yml'
obj
.
_line_number
=
1
obj
.
_column_number
=
1
m
=
mock_open
()
m
.
return_value
.
readlines
.
return_value
=
[
'this is line 1
\n
'
,
'this is line 2
\n
'
,
'this is line 3
\n
'
]
with
patch
(
'__builtin__.open'
,
m
):
e
=
AnsibleError
(
self
.
message
,
obj
)
assert
e
.
message
==
'this is the error message
\n
The error occurred on line 1 of the file foo.yml:
\n
this is line 1
\n
^'
v2/ansible/errors/__init__.py
View file @
a50332fc
...
...
@@ -15,15 +15,46 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import
os
from
ansible.parsing.yaml.objects
import
AnsibleBaseYAMLObject
class
AnsibleError
(
Exception
):
def
__init__
(
self
,
message
,
object
=
None
):
self
.
message
=
message
self
.
object
=
object
def
__init__
(
self
,
message
,
obj
=
None
):
self
.
_obj
=
obj
if
isinstance
(
self
.
_obj
,
AnsibleBaseYAMLObject
):
extended_error
=
self
.
_get_extended_error
()
if
extended_error
:
self
.
message
=
'
%
s
\n
%
s'
%
(
message
,
extended_error
)
else
:
self
.
message
=
message
def
__repr__
(
self
):
return
self
.
message
def
_get_line_from_file
(
self
,
filename
,
line_number
):
with
open
(
filename
,
'r'
)
as
f
:
lines
=
f
.
readlines
()
if
line_number
<
len
(
lines
):
return
lines
[
line_number
]
return
None
def
_get_extended_error
(
self
):
error_message
=
''
# TODO: nice __repr__ message that includes the line number if the object
# it was constructed with had the line number
try
:
(
src_file
,
line_number
,
col_number
)
=
self
.
_obj
.
get_position_info
()
error_message
+=
'The error occurred on line
%
d of the file
%
s:
\n
'
%
(
line_number
,
src_file
)
if
src_file
not
in
(
'<string>'
,
'<unicode>'
):
responsible_line
=
self
.
_get_line_from_file
(
src_file
,
line_number
-
1
)
if
responsible_line
:
error_message
+=
responsible_line
error_message
+=
(
' '
*
(
col_number
-
1
))
+
'^'
except
IOError
:
error_message
+=
'
\n
(could not open file to display line)'
except
IndexError
:
error_message
+=
'
\n
(specified line no longer in file, maybe it changed?)'
# TODO: tests for the line number functionality
return
error_message
class
AnsibleParserError
(
AnsibleError
):
''' something was detected early that is wrong about a playbook or data file '''
...
...
v2/ansible/parsing/yaml/objects.py
View file @
a50332fc
...
...
@@ -8,6 +8,9 @@ class AnsibleBaseYAMLObject(object):
_line_number
=
None
_column_number
=
None
def
get_position_info
(
self
):
return
(
self
.
_data_source
,
self
.
_line_number
,
self
.
_column_number
)
class
AnsibleMapping
(
AnsibleBaseYAMLObject
,
dict
):
''' sub class for dictionaries '''
pass
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment