Back to index

plone3  3.1.7
test_differ.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ Unit tests for differ module.
00014 
00015 $Id: test_differ.py 71241 2006-11-21 13:15:05Z yuppie $
00016 """
00017 
00018 import unittest
00019 from Testing.ZopeTestCase import ZopeTestCase
00020 
00021 from DateTime.DateTime import DateTime
00022 from OFS.Folder import Folder
00023 from OFS.Image import File
00024 
00025 
00026 class DummySite( Folder ):
00027 
00028     pass
00029 
00030 
00031 class Test_unidiff( unittest.TestCase ):
00032 
00033     def test_unidiff_both_text( self ):
00034 
00035         from Products.GenericSetup.differ import unidiff
00036 
00037         diff_lines = unidiff( ONE_FOUR, ZERO_FOUR )
00038         diff_text = '\n'.join( diff_lines )
00039         self.assertEqual( diff_text, DIFF_TEXT )
00040 
00041     def test_unidiff_both_lines( self ):
00042 
00043         from Products.GenericSetup.differ import unidiff
00044 
00045         diff_lines = unidiff( ONE_FOUR.splitlines(), ZERO_FOUR.splitlines() )
00046         diff_text = '\n'.join( diff_lines )
00047         self.assertEqual( diff_text, DIFF_TEXT )
00048 
00049     def test_unidiff_mixed( self ):
00050 
00051         from Products.GenericSetup.differ import unidiff
00052 
00053         diff_lines = unidiff( ONE_FOUR, ZERO_FOUR.splitlines() )
00054         diff_text = '\n'.join( diff_lines )
00055         self.assertEqual( diff_text, DIFF_TEXT )
00056 
00057     def test_unidiff_ignore_blanks( self ):
00058 
00059         from Products.GenericSetup.differ import unidiff
00060 
00061         double_spaced = ONE_FOUR.replace( '\n', '\n\n' )
00062         diff_lines = unidiff( double_spaced
00063                             , ZERO_FOUR.splitlines()
00064                             , ignore_blanks=True
00065                             )
00066 
00067         diff_text = '\n'.join( diff_lines )
00068         self.assertEqual( diff_text, DIFF_TEXT )
00069 
00070 ZERO_FOUR = """\
00071 zero
00072 one
00073 tree
00074 four
00075 """
00076 
00077 ONE_FOUR = """\
00078 one
00079 two
00080 three
00081 four
00082 """
00083 
00084 DIFF_TEXT = """\
00085 --- original None
00086 +++ modified None
00087 @@ -1,4 +1,4 @@
00088 +zero
00089  one
00090 -two
00091 -three
00092 +tree
00093  four\
00094 """
00095 
00096 class ConfigDiffTests(ZopeTestCase):
00097 
00098     site = None
00099     tool = None
00100 
00101     def _getTargetClass( self ):
00102 
00103         from Products.GenericSetup.differ import ConfigDiff
00104         return ConfigDiff
00105 
00106     def _makeOne( self, lhs, rhs, *args, **kw ):
00107 
00108         return self._getTargetClass()( lhs, rhs, *args, **kw )
00109 
00110     def _makeSite( self ):
00111 
00112         if self.site is not None:
00113             return
00114 
00115         site = self.site = DummySite('site').__of__(self.app)
00116         site._setObject( 'setup_tool', Folder( 'setup_tool' ) )
00117         self.tool = tool = site._getOb( 'setup_tool' )
00118 
00119         tool._setObject( 'snapshots', Folder( 'snapshots' ) )
00120 
00121     def _makeContext( self, context_id ):
00122 
00123         from Products.GenericSetup.context import SnapshotImportContext
00124 
00125         self._makeSite()
00126 
00127         if context_id not in self.tool.snapshots.objectIds():
00128             self.tool.snapshots._setObject( context_id, Folder( context_id ) )
00129 
00130         ctx = SnapshotImportContext( self.tool, context_id )
00131 
00132         return ctx.__of__( self.tool )
00133 
00134     def _makeDirectory( self, snapshot_id, subdir ):
00135 
00136         self._makeSite()
00137         folder = self.tool.snapshots._getOb( snapshot_id )
00138 
00139         for element in subdir.split( '/' ):
00140 
00141             try:
00142                 folder = folder._getOb( element )
00143             except AttributeError:
00144                 folder._setObject( element, Folder( element ) )
00145                 folder = folder._getOb( element )
00146 
00147         return folder
00148 
00149     def _makeFile( self
00150                  , snapshot_id
00151                  , filename
00152                  , contents
00153                  , content_type='text/plain'
00154                  , mod_time=None
00155                  , subdir=None
00156                  ):
00157 
00158         self._makeSite()
00159         snapshots = self.tool.snapshots
00160         snapshot = snapshots._getOb( snapshot_id )
00161 
00162         if subdir is not None:
00163             folder = self._makeDirectory( snapshot_id, subdir )
00164         else:
00165             folder = snapshot
00166 
00167         file = File( filename, '', contents, content_type )
00168         folder._setObject( filename, file )
00169 
00170         if mod_time is not None:
00171 
00172             def __faux_mod_time():
00173                 return mod_time
00174 
00175             folder.bobobase_modification_time = \
00176             file.bobobase_modification_time = __faux_mod_time
00177 
00178         return folder._getOb( filename )
00179 
00180     def test_compare_empties( self ):
00181 
00182         lhs = self._makeContext( 'lhs' )
00183         rhs = self._makeContext( 'rhs' )
00184 
00185         cd = self._makeOne( lhs, rhs )
00186 
00187         diffs = cd.compare()
00188 
00189         self.assertEqual( diffs, '' )
00190 
00191     def test_compare_identical( self ):
00192 
00193         lhs = self._makeContext( 'lhs' )
00194         rhs = self._makeContext( 'rhs' )
00195 
00196         self._makeFile( 'lhs', 'test.txt', 'ABCDEF' )
00197         self._makeFile( 'lhs', 'again.txt', 'GHIJKL', subdir='sub' )
00198         self._makeFile( 'rhs', 'test.txt', 'ABCDEF' )
00199         self._makeFile( 'rhs', 'again.txt', 'GHIJKL', subdir='sub' )
00200 
00201         cd = self._makeOne( lhs, rhs )
00202 
00203         diffs = cd.compare()
00204 
00205         self.assertEqual( diffs, '' )
00206 
00207     def test_compare_changed_file( self ):
00208 
00209         BEFORE = DateTime( '2004-01-01T00:00:00Z' )
00210         AFTER = DateTime( '2004-02-29T23:59:59Z' )
00211 
00212         lhs = self._makeContext( 'lhs' )
00213         rhs = self._makeContext( 'rhs' )
00214 
00215         self._makeFile( 'lhs', 'test.txt', 'ABCDEF\nWXYZ', mod_time=BEFORE )
00216         self._makeFile( 'lhs', 'again.txt', 'GHIJKL', subdir='sub' )
00217         self._makeFile( 'rhs', 'test.txt', 'ABCDEF\nQRST', mod_time=AFTER )
00218         self._makeFile( 'rhs', 'again.txt', 'GHIJKL', subdir='sub' )
00219 
00220         cd = self._makeOne( lhs, rhs )
00221 
00222         diffs = cd.compare()
00223 
00224         self.assertEqual( diffs, TEST_TXT_DIFFS % ( BEFORE, AFTER ) )
00225 
00226     def test_compare_changed_file_ignore_blanks( self ):
00227 
00228         BEFORE = DateTime( '2004-01-01T00:00:00Z' )
00229         AFTER = DateTime( '2004-02-29T23:59:59Z' )
00230 
00231         lhs = self._makeContext( 'lhs' )
00232         rhs = self._makeContext( 'rhs' )
00233 
00234         self._makeFile( 'lhs', 'test.txt', 'ABCDEF\nWXYZ', mod_time=BEFORE )
00235         self._makeFile( 'rhs', 'test.txt', 'ABCDEF\n\n\nWXYZ', mod_time=AFTER )
00236 
00237         cd = self._makeOne( lhs, rhs, ignore_blanks=True )
00238 
00239         diffs = cd.compare()
00240 
00241         self.assertEqual( diffs, '' )
00242 
00243     def test_compare_changed_file_explicit_skip( self ):
00244 
00245         BEFORE = DateTime( '2004-01-01T00:00:00Z' )
00246         AFTER = DateTime( '2004-02-29T23:59:59Z' )
00247 
00248         lhs = self._makeContext( 'lhs' )
00249         rhs = self._makeContext( 'rhs' )
00250 
00251         self._makeFile( 'lhs', 'test.txt', 'ABCDEF\nWXYZ', subdir='skipme'
00252                       , mod_time=BEFORE )
00253         self._makeFile( 'lhs', 'again.txt', 'GHIJKL', subdir='sub' )
00254         self._makeFile( 'rhs', 'test.txt', 'ABCDEF\nQRST', subdir='skipme'
00255                       , mod_time=AFTER )
00256         self._makeFile( 'rhs', 'again.txt', 'GHIJKL', subdir='sub' )
00257 
00258         cd = self._makeOne( lhs, rhs, skip=[ 'skipme' ] )
00259 
00260         diffs = cd.compare()
00261 
00262         self.assertEqual( diffs, '' )
00263 
00264     def test_compare_changed_file_implicit_skip( self ):
00265 
00266         BEFORE = DateTime( '2004-01-01T00:00:00Z' )
00267         AFTER = DateTime( '2004-02-29T23:59:59Z' )
00268 
00269         lhs = self._makeContext( 'lhs' )
00270         rhs = self._makeContext( 'rhs' )
00271 
00272         self._makeFile( 'lhs', 'test.txt', 'ABCDEF\nWXYZ', subdir='CVS'
00273                       , mod_time=BEFORE )
00274         self._makeFile( 'lhs', 'again.txt', 'GHIJKL', subdir='.svn'
00275                       , mod_time=BEFORE )
00276 
00277         self._makeFile( 'rhs', 'test.txt', 'ABCDEF\nQRST', subdir='CVS'
00278                       , mod_time=AFTER )
00279         self._makeFile( 'rhs', 'again.txt', 'MNOPQR', subdir='.svn'
00280                       , mod_time=AFTER )
00281 
00282         cd = self._makeOne( lhs, rhs )
00283 
00284         diffs = cd.compare()
00285 
00286         self.assertEqual( diffs, '' )
00287 
00288     def test_compare_added_file_no_missing_as_empty( self ):
00289 
00290         lhs = self._makeContext( 'lhs' )
00291         rhs = self._makeContext( 'rhs' )
00292 
00293         self._makeFile( 'lhs', 'test.txt', 'ABCDEF\nWXYZ' )
00294         self._makeDirectory( 'lhs', subdir='sub' )
00295         self._makeFile( 'rhs', 'test.txt', 'ABCDEF\nWXYZ' )
00296         self._makeFile( 'rhs', 'again.txt', 'GHIJKL', subdir='sub' )
00297 
00298         cd = self._makeOne( lhs, rhs )
00299 
00300         diffs = cd.compare()
00301 
00302         self.assertEqual( diffs, ADDED_FILE_DIFFS_NO_MAE )
00303 
00304     def test_compare_added_file_missing_as_empty( self ):
00305 
00306         AFTER = DateTime( '2004-02-29T23:59:59Z' )
00307         lhs = self._makeContext( 'lhs' )
00308         rhs = self._makeContext( 'rhs' )
00309 
00310         self._makeFile( 'lhs', 'test.txt', 'ABCDEF\nWXYZ' )
00311         self._makeDirectory( 'lhs', subdir='sub' )
00312         self._makeFile( 'rhs', 'test.txt', 'ABCDEF\nWXYZ' )
00313         self._makeFile( 'rhs', 'again.txt', 'GHIJKL', subdir='sub'
00314                       , mod_time=AFTER )
00315 
00316         cd = self._makeOne( lhs, rhs, missing_as_empty=True )
00317 
00318         diffs = cd.compare()
00319 
00320         self.assertEqual( diffs, ADDED_FILE_DIFFS_MAE % AFTER )
00321 
00322     def test_compare_removed_file_no_missing_as_empty( self ):
00323 
00324         lhs = self._makeContext( 'lhs' )
00325         rhs = self._makeContext( 'rhs' )
00326 
00327         self._makeFile( 'lhs', 'test.txt', 'ABCDEF\nWXYZ' )
00328         self._makeFile( 'lhs', 'again.txt', 'GHIJKL', subdir='sub' )
00329         self._makeFile( 'rhs', 'test.txt', 'ABCDEF\nWXYZ' )
00330         self._makeDirectory( 'rhs', subdir='sub' )
00331 
00332         cd = self._makeOne( lhs, rhs )
00333 
00334         diffs = cd.compare()
00335 
00336         self.assertEqual( diffs, REMOVED_FILE_DIFFS_NO_MAE )
00337 
00338     def test_compare_removed_file_missing_as_empty( self ):
00339 
00340         BEFORE = DateTime( '2004-01-01T00:00:00Z' )
00341         lhs = self._makeContext( 'lhs' )
00342         rhs = self._makeContext( 'rhs' )
00343 
00344         self._makeFile( 'lhs', 'test.txt', 'ABCDEF\nWXYZ' )
00345         self._makeFile( 'lhs', 'again.txt', 'GHIJKL', subdir='sub'
00346                       , mod_time=BEFORE )
00347         self._makeFile( 'rhs', 'test.txt', 'ABCDEF\nWXYZ' )
00348         self._makeDirectory( 'rhs', subdir='sub' )
00349 
00350         cd = self._makeOne( lhs, rhs, missing_as_empty=True )
00351 
00352         diffs = cd.compare()
00353 
00354         self.assertEqual( diffs, REMOVED_FILE_DIFFS_MAE % BEFORE )
00355 
00356 
00357 TEST_TXT_DIFFS = """\
00358 Index: test.txt
00359 ===================================================================
00360 --- test.txt %s
00361 +++ test.txt %s
00362 @@ -1,2 +1,2 @@
00363  ABCDEF
00364 -WXYZ
00365 +QRST\
00366 """
00367 
00368 ADDED_FILE_DIFFS_NO_MAE = """\
00369 ** File sub/again.txt added
00370 """
00371 
00372 ADDED_FILE_DIFFS_MAE = """\
00373 Index: sub/again.txt
00374 ===================================================================
00375 --- sub/again.txt 0
00376 +++ sub/again.txt %s
00377 @@ -1,0 +1,1 @@
00378 +GHIJKL\
00379 """
00380 
00381 REMOVED_FILE_DIFFS_NO_MAE = """\
00382 ** File sub/again.txt removed
00383 """
00384 
00385 REMOVED_FILE_DIFFS_MAE = """\
00386 Index: sub/again.txt
00387 ===================================================================
00388 --- sub/again.txt %s
00389 +++ sub/again.txt 0
00390 @@ -1,1 +1,0 @@
00391 -GHIJKL\
00392 """
00393 
00394 
00395 def test_suite():
00396     return unittest.TestSuite((
00397         unittest.makeSuite(Test_unidiff),
00398         unittest.makeSuite(ConfigDiffTests),
00399         ))
00400 
00401 if __name__ == '__main__':
00402     unittest.main(defaultTest='test_suite')