@ -12,10 +12,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import io
import mock
import six
from os_testr import regex_builder as os_testr
from os_testr . tests import base
@ -35,82 +34,85 @@ class TestConstructRegex(base.TestCase):
self . assertEqual ( result , ' fake_regex ' )
def test_blacklist_regex_with_comments ( self ) :
blacklist_file = six . StringIO ( )
for i in range ( 4 ) :
blacklist_file . write ( ' fake_regex_ %s # A Comment \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None , None , False )
self . assertEqual (
result ,
" ^((?!fake_regex_3|fake_regex_2| fake_regex_1|fake_regex_0).)*$" )
with io . StringIO ( ) as blacklist_file :
for i in range ( 4 ) :
blacklist_file . write ( u ' fake_regex_ %s # A Comment \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex (
' fake_path ' , None , None , False )
self . assertEqual ( result , " ^((?!fake_regex_3|fake_regex_2| "
" fake_regex_1|fake_regex_0).)*$" )
def test_whitelist_regex_with_comments ( self ) :
whitelist_file = six . StringIO ( )
for i in range ( 4 ) :
whitelist_file . write ( ' fake_regex_ %s # A Comment \n ' % i )
whitelist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = whitelist_file ) :
result = os_testr . construct_regex ( None , ' fake_path ' , None , False )
self . assertEqual (
result ,
" fake_regex_0|fake_regex_1|fake_regex_2|fake_regex_3 " )
with io . StringIO ( ) as whitelist_file :
for i in range ( 4 ) :
whitelist_file . write ( u ' fake_regex_ %s # A Comment \n ' % i )
whitelist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = whitelist_file ) :
result = os_testr . construct_regex (
None , ' fake_path ' , None , False )
self . assertEqual (
result ,
" fake_regex_0|fake_regex_1|fake_regex_2|fake_regex_3 " )
def test_blacklist_regex_without_comments ( self ) :
blacklist_file = six . StringIO ( )
for i in range ( 4 ) :
blacklist_file . write ( ' fake_regex_ %s \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None , None , False )
self . assertEqual (
result ,
" ^((?!fake_regex_3|fake_regex_2| fake_regex_1|fake_regex_0).)*$" )
with io . StringIO ( ) as blacklist_file :
for i in range ( 4 ) :
blacklist_file . write ( u ' fake_regex_ %s \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex (
' fake_path ' , None , None , False )
self . assertEqual ( result , " ^((?!fake_regex_3|fake_regex_2| "
" fake_regex_1|fake_regex_0).)*$" )
def test_blacklist_regex_with_comments_and_regex ( self ) :
blacklist_file = six . StringIO ( )
for i in range ( 4 ) :
blacklist_file . write ( ' fake_regex_ %s # Comments \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None ,
' fake_regex ' , False )
expected_regex = ( " ^((?!fake_regex_3|fake_regex_2|fake_regex_1| "
" fake_regex_0).)*$fake_regex " )
self . assertEqual ( result , expected_regex )
with io . StringIO ( ) as blacklist_file :
for i in range ( 4 ) :
blacklist_file . write ( u ' fake_regex_ %s # Comments \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None ,
' fake_regex ' , False )
expected_regex = (
" ^((?!fake_regex_3|fake_regex_2|fake_regex_1| "
" fake_regex_0).)*$fake_regex " )
self . assertEqual ( result , expected_regex )
def test_blacklist_regex_without_comments_and_regex ( self ) :
blacklist_file = six . StringIO ( )
for i in range ( 4 ) :
blacklist_file . write ( ' fake_regex_ %s \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None ,
' fake_regex ' , False )
expected_regex = ( " ^((?!fake_regex_3|fake_regex_2|fake_regex_1| "
" fake_regex_0).)*$fake_regex " )
self . assertEqual ( result , expected_regex )
with io . StringIO ( ) as blacklist_file :
for i in range ( 4 ) :
blacklist_file . write ( u ' fake_regex_ %s \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None ,
' fake_regex ' , False )
expected_regex = (
" ^((?!fake_regex_3|fake_regex_2|fake_regex_1| "
" fake_regex_0).)*$fake_regex " )
self . assertEqual ( result , expected_regex )
@mock.patch.object ( os_testr , ' print_skips ' )
def test_blacklist_regex_with_comment_print_skips ( self , print_mock ) :
blacklist_file = six . StringIO ( )
for i in range ( 4 ) :
blacklist_file . write ( ' fake_regex_ %s # Comment \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None ,
None , True )
expected_regex = ( " ^((?!fake_regex_3|fake_regex_2|fake_regex_1| "
" fake_regex_0).)*$ " )
with io . StringIO ( ) as blacklist_file :
for i in range ( 4 ) :
blacklist_file . write ( u ' fake_regex_ %s # Comment \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None ,
None , True )
expected_regex = ( " ^((?!fake_regex_3|fake_regex_2|fake_regex_1| "
" fake_regex_0).)*$ " )
self . assertEqual ( result , expected_regex )
calls = print_mock . mock_calls
self . assertEqual ( len ( calls ) , 4 )
@ -122,17 +124,17 @@ class TestConstructRegex(base.TestCase):
@mock.patch.object ( os_testr , ' print_skips ' )
def test_blacklist_regex_without_comment_print_skips ( self , print_mock ) :
blacklist_file = six . StringIO ( )
for i in range ( 4 ) :
blacklist_file . write ( ' fake_regex_ %s \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None ,
None , True )
expected_regex = ( " ^((?!fake_regex_3|fake_regex_2| "
" fake_regex_1|fake_regex_0).)*$ " )
with io . StringIO ( ) as blacklist_file :
for i in range ( 4 ) :
blacklist_file . write ( u ' fake_regex_ %s \n ' % i )
blacklist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = blacklist_file ) :
result = os_testr . construct_regex ( ' fake_path ' , None ,
None , True )
expected_regex = ( " ^((?!fake_regex_3|fake_regex_2| "
" fake_regex_1|fake_regex_0).)*$ " )
self . assertEqual ( result , expected_regex )
calls = print_mock . mock_calls
self . assertEqual ( len ( calls ) , 4 )
@ -145,29 +147,30 @@ class TestConstructRegex(base.TestCase):
class TestWhitelistFile ( base . TestCase ) :
def test_read_whitelist_file ( self ) :
file_contents = """ regex_a
file_contents = u """ regex_a
regex_b """
whitelist_file = six . StringIO ( )
whitelist_file . write ( file_contents )
whitelist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = whitelist_file ) :
regex = os_testr . get_regex_from_whitelist_file ( ' /path/to/not_used ' )
self . assertEqual ( ' regex_a|regex_b ' , regex )
with io . StringIO ( ) as whitelist_file :
whitelist_file . write ( file_contents )
whitelist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = whitelist_file ) :
regex = os_testr . get_regex_from_whitelist_file (
' /path/to/not_used ' )
self . assertEqual ( ' regex_a|regex_b ' , regex )
def test_whitelist_regex_without_comments_and_regex ( self ) :
file_contents = """ regex_a
file_contents = u """ regex_a
regex_b """
whitelist_file = six . StringIO ( )
whitelist_file . write ( file_contents )
whitelist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = whitelist_file ) :
result = os_testr . construct_regex ( None , ' fake_path ' ,
None , False )
expected_regex = ' regex_a|regex_b '
self . assertEqual ( result , expected_regex )
with io . StringIO ( ) as whitelist_file :
whitelist_file . write ( file_contents )
whitelist_file . seek ( 0 )
with mock . patch ( ' six.moves.builtins.open ' ,
return_value = whitelist_file ) :
result = os_testr . construct_regex ( None , ' fake_path ' ,
None , False )
expected_regex = ' regex_a|regex_b '
self . assertEqual ( result , expected_regex )
class TestGetTestList ( base . TestCase ) :