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
417bf1c8
Commit
417bf1c8
authored
Sep 02, 2015
by
Toshio Kuratomi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unittest the _count_trailing_newlines function
parent
7ed746ad
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
18 deletions
+45
-18
lib/ansible/template/__init__.py
+17
-17
test/units/template/test_template_utilities.py
+28
-1
No files found.
lib/ansible/template/__init__.py
View file @
417bf1c8
...
@@ -90,6 +90,21 @@ def _preserve_backslashes(data, jinja_env):
...
@@ -90,6 +90,21 @@ def _preserve_backslashes(data, jinja_env):
return
data
return
data
def
_count_newlines_from_end
(
in_str
):
'''
Counts the number of newlines at the end of a string. This is used during
the jinja2 templating to ensure the count matches the input, since some newlines
may be thrown away during the templating.
'''
i
=
len
(
in_str
)
while
i
>
0
:
if
in_str
[
i
-
1
]
!=
'
\n
'
:
break
i
-=
1
return
len
(
in_str
)
-
i
class
Templar
:
class
Templar
:
'''
'''
The main class for templating, with the main entry-point of template().
The main class for templating, with the main entry-point of template().
...
@@ -130,21 +145,6 @@ class Templar:
...
@@ -130,21 +145,6 @@ class Templar:
self
.
SINGLE_VAR
=
re
.
compile
(
r"^
%
s\s*(\w*)\s*
%
s$"
%
(
self
.
environment
.
variable_start_string
,
self
.
environment
.
variable_end_string
))
self
.
SINGLE_VAR
=
re
.
compile
(
r"^
%
s\s*(\w*)\s*
%
s$"
%
(
self
.
environment
.
variable_start_string
,
self
.
environment
.
variable_end_string
))
def
_count_newlines_from_end
(
self
,
in_str
):
'''
Counts the number of newlines at the end of a string. This is used during
the jinja2 templating to ensure the count matches the input, since some newlines
may be thrown away during the templating.
'''
i
=
len
(
in_str
)
while
i
>
0
:
if
in_str
[
i
-
1
]
!=
'
\n
'
:
break
i
-=
1
return
len
(
in_str
)
-
i
def
_get_filters
(
self
):
def
_get_filters
(
self
):
'''
'''
Returns filter plugins, after loading and caching them if need be
Returns filter plugins, after loading and caching them if need be
...
@@ -318,7 +318,7 @@ class Templar:
...
@@ -318,7 +318,7 @@ class Templar:
# For preserving the number of input newlines in the output (used
# For preserving the number of input newlines in the output (used
# later in this method)
# later in this method)
data_newlines
=
self
.
_count_newlines_from_end
(
data
)
data_newlines
=
_count_newlines_from_end
(
data
)
if
fail_on_undefined
is
None
:
if
fail_on_undefined
is
None
:
fail_on_undefined
=
self
.
_fail_on_undefined_errors
fail_on_undefined
=
self
.
_fail_on_undefined_errors
...
@@ -389,7 +389,7 @@ class Templar:
...
@@ -389,7 +389,7 @@ class Templar:
# that version being present, modify our code to set that when
# that version being present, modify our code to set that when
# initializing self.environment and remove a single trailing
# initializing self.environment and remove a single trailing
# newline here if preserve_newlines is False.
# newline here if preserve_newlines is False.
res_newlines
=
self
.
_count_newlines_from_end
(
res
)
res_newlines
=
_count_newlines_from_end
(
res
)
if
data_newlines
>
res_newlines
:
if
data_newlines
>
res_newlines
:
res
+=
'
\n
'
*
(
data_newlines
-
res_newlines
)
res
+=
'
\n
'
*
(
data_newlines
-
res_newlines
)
...
...
test/units/template/test_
jinja_backslash
.py
→
test/units/template/test_
template_utilities
.py
View file @
417bf1c8
...
@@ -22,8 +22,10 @@ __metaclass__ = type
...
@@ -22,8 +22,10 @@ __metaclass__ = type
import
jinja2
import
jinja2
from
ansible.compat.tests
import
unittest
from
ansible.compat.tests
import
unittest
from
ansible.template
import
_preserve_backslashes
from
ansible.template
import
_preserve_backslashes
,
_count_newlines_from_end
# These are internal utility functions only needed for templating. They're
# algorithmic so good candidates for unittesting by themselves
class
TestBackslashEscape
(
unittest
.
TestCase
):
class
TestBackslashEscape
(
unittest
.
TestCase
):
...
@@ -83,3 +85,28 @@ class TestBackslashEscape(unittest.TestCase):
...
@@ -83,3 +85,28 @@ class TestBackslashEscape(unittest.TestCase):
args
=
test
[
'args'
]
args
=
test
[
'args'
]
self
.
assertEquals
(
template
.
render
(
**
args
),
test
[
'expectation'
])
self
.
assertEquals
(
template
.
render
(
**
args
),
test
[
'expectation'
])
class
TestCountNewlines
(
unittest
.
TestCase
):
def
setUp
(
self
):
pass
def
tearDown
(
self
):
pass
def
test_short_string
(
self
):
self
.
assertEquals
(
_count_newlines_from_end
(
'The quick
\n
'
),
1
)
def
test_one_newline
(
self
):
self
.
assertEquals
(
_count_newlines_from_end
(
'The quick brown fox jumped over the lazy dog'
*
1000
+
'
\n
'
),
1
)
def
test_multiple_newlines
(
self
):
self
.
assertEquals
(
_count_newlines_from_end
(
'The quick brown fox jumped over the lazy dog'
*
1000
+
'
\n\n\n
'
),
3
)
def
test_zero_newlines
(
self
):
self
.
assertEquals
(
_count_newlines_from_end
(
'The quick brown fox jumped over the lazy dog'
*
1000
+
'
\n\n\n
'
),
3
)
def
test_all_newlines
(
self
):
self
.
assertEquals
(
_count_newlines_from_end
(
'
\n
'
*
10
),
10
)
def
test_mostly_newlines
(
self
):
self
.
assertEquals
(
_count_newlines_from_end
(
'The quick brown fox jumped over the lazy dog'
+
'
\n
'
*
1000
),
1000
)
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