@ -19,6 +19,8 @@
from __future__ import ( absolute_import , division , print_function )
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
__metaclass__ = type
import re
import textwrap
import types
import types
from ansible . compat . tests import unittest
from ansible . compat . tests import unittest
@ -134,9 +136,15 @@ class TestCallbackDumpResults(unittest.TestCase):
# that try except orig appeared in 61d01f549f2143fd9adfa4ffae42f09d24649c26
# that try except orig appeared in 61d01f549f2143fd9adfa4ffae42f09d24649c26
# in 2013 so maybe a < py2.6 issue
# in 2013 so maybe a < py2.6 issue
class TestCallbackDiff ( unittest . TestCase ) :
class TestCallbackDiff ( unittest . TestCase ) :
def setUp ( self ) :
self . cb = CallbackBase ( )
def _strip_color ( self , s ) :
return re . sub ( ' \033 \\ [[^m]*m ' , ' ' , s )
def test_difflist ( self ) :
def test_difflist ( self ) :
# TODO: split into smaller tests?
# TODO: split into smaller tests?
cb = CallbackBase ( )
difflist = [ { ' before ' : [ ' preface \n The Before String \n postscript ' ] ,
difflist = [ { ' before ' : [ ' preface \n The Before String \n postscript ' ] ,
' after ' : [ ' preface \n The After String \n postscript ' ] ,
' after ' : [ ' preface \n The After String \n postscript ' ] ,
' before_header ' : ' just before ' ,
' before_header ' : ' just before ' ,
@ -153,13 +161,158 @@ class TestCallbackDiff(unittest.TestCase):
{ ' before_header ' : ' just before ' } ,
{ ' before_header ' : ' just before ' } ,
{ ' after_header ' : ' just after ' } ]
{ ' after_header ' : ' just after ' } ]
res = cb . _get_diff ( difflist )
res = self . cb . _get_diff ( difflist )
self . assertIn ( ' Before String ' , res )
self . assertIn ( ' Before String ' , res )
self . assertIn ( ' After String ' , res )
self . assertIn ( ' After String ' , res )
self . assertIn ( ' just before ' , res )
self . assertIn ( ' just before ' , res )
self . assertIn ( ' just after ' , res )
self . assertIn ( ' just after ' , res )
def test_simple_diff ( self ) :
self . assertMultiLineEqual (
self . _strip_color ( self . cb . _get_diff ( {
' before_header ' : ' somefile.txt ' ,
' after_header ' : ' generated from template somefile.j2 ' ,
' before ' : ' one \n two \n three \n ' ,
' after ' : ' one \n three \n four \n ' ,
} ) ) ,
textwrap . dedent ( ''' \
- - - before : somefile . txt
+ + + after : generated from template somefile . j2
@ @ - 1 , 3 + 1 , 3 @ @
one
- two
three
+ four
''' ))
def test_new_file ( self ) :
self . assertMultiLineEqual (
self . _strip_color ( self . cb . _get_diff ( {
' before_header ' : ' somefile.txt ' ,
' after_header ' : ' generated from template somefile.j2 ' ,
' before ' : ' ' ,
' after ' : ' one \n two \n three \n ' ,
} ) ) ,
textwrap . dedent ( ''' \
- - - before : somefile . txt
+ + + after : generated from template somefile . j2
@ @ - 0 , 0 + 1 , 3 @ @
+ one
+ two
+ three
''' ))
def test_clear_file ( self ) :
self . assertMultiLineEqual (
self . _strip_color ( self . cb . _get_diff ( {
' before_header ' : ' somefile.txt ' ,
' after_header ' : ' generated from template somefile.j2 ' ,
' before ' : ' one \n two \n three \n ' ,
' after ' : ' ' ,
} ) ) ,
textwrap . dedent ( ''' \
- - - before : somefile . txt
+ + + after : generated from template somefile . j2
@ @ - 1 , 3 + 0 , 0 @ @
- one
- two
- three
''' ))
def test_no_trailing_newline_before ( self ) :
self . assertMultiLineEqual (
self . _strip_color ( self . cb . _get_diff ( {
' before_header ' : ' somefile.txt ' ,
' after_header ' : ' generated from template somefile.j2 ' ,
' before ' : ' one \n two \n three ' ,
' after ' : ' one \n two \n three \n ' ,
} ) ) ,
textwrap . dedent ( ''' \
- - - before : somefile . txt
+ + + after : generated from template somefile . j2
@ @ - 1 , 3 + 1 , 3 @ @
one
two
- three
\\ No newline at end of file
+ three
''' ))
def test_no_trailing_newline_after ( self ) :
self . assertMultiLineEqual (
self . _strip_color ( self . cb . _get_diff ( {
' before_header ' : ' somefile.txt ' ,
' after_header ' : ' generated from template somefile.j2 ' ,
' before ' : ' one \n two \n three \n ' ,
' after ' : ' one \n two \n three ' ,
} ) ) ,
textwrap . dedent ( ''' \
- - - before : somefile . txt
+ + + after : generated from template somefile . j2
@ @ - 1 , 3 + 1 , 3 @ @
one
two
- three
+ three
\\ No newline at end of file
''' ))
def test_no_trailing_newline_both ( self ) :
self . assertMultiLineEqual (
self . cb . _get_diff ( {
' before_header ' : ' somefile.txt ' ,
' after_header ' : ' generated from template somefile.j2 ' ,
' before ' : ' one \n two \n three ' ,
' after ' : ' one \n two \n three ' ,
} ) ,
' ' )
def test_no_trailing_newline_both_with_some_changes ( self ) :
self . assertMultiLineEqual (
self . _strip_color ( self . cb . _get_diff ( {
' before_header ' : ' somefile.txt ' ,
' after_header ' : ' generated from template somefile.j2 ' ,
' before ' : ' one \n two \n three ' ,
' after ' : ' one \n five \n three ' ,
} ) ) ,
textwrap . dedent ( ''' \
- - - before : somefile . txt
+ + + after : generated from template somefile . j2
@ @ - 1 , 3 + 1 , 3 @ @
one
- two
+ five
three
\\ No newline at end of file
''' ))
def test_diff_dicts ( self ) :
self . assertMultiLineEqual (
self . _strip_color ( self . cb . _get_diff ( {
' before ' : dict ( one = 1 , two = 2 , three = 3 ) ,
' after ' : dict ( one = 1 , three = 3 , four = 4 ) ,
} ) ) ,
textwrap . dedent ( ''' \
- - - before
+ + + after
@ @ - 1 , 5 + 1 , 5 @ @
{
+ " four " : 4 ,
" one " : 1 ,
- " three " : 3 ,
- " two " : 2
+ " three " : 3
}
''' ))
class TestCallbackOnMethods ( unittest . TestCase ) :
class TestCallbackOnMethods ( unittest . TestCase ) :
def _find_on_methods ( self , callback ) :
def _find_on_methods ( self , callback ) :