Home | History | Annotate | Download | only in defines-escaping
      1 #!/usr/bin/env python
      2 
      3 # Copyright (c) 2010 Google Inc. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 """
      8 Verifies build of an executable with C++ define specified by a gyp define using
      9 various special characters such as quotes, commas, etc.
     10 """
     11 
     12 import os
     13 import TestGyp
     14 
     15 test = TestGyp.TestGyp()
     16 
     17 # Tests string literals, percents, and backslash escapes.
     18 try:
     19   os.environ['GYP_DEFINES'] = (
     20       r"""test_format='\n%s\n' """
     21       r"""test_args='"Simple test of %s with a literal"'""")
     22   test.run_gyp('defines-escaping.gyp')
     23 finally:
     24   del os.environ['GYP_DEFINES']
     25 
     26 test.build('defines-escaping.gyp')
     27 
     28 expect = """
     29 Simple test of %s with a literal
     30 """
     31 test.run_built_executable('defines_escaping', stdout=expect)
     32 
     33 
     34 # Test multiple comma-and-space-separated string literals.
     35 try:
     36   os.environ['GYP_DEFINES'] = \
     37       r"""test_format='\n%s and %s\n' test_args='"foo", "bar"'"""
     38   test.run_gyp('defines-escaping.gyp')
     39 finally:
     40   del os.environ['GYP_DEFINES']
     41 
     42 test.sleep()
     43 test.touch('defines-escaping.c')
     44 test.build('defines-escaping.gyp')
     45 
     46 expect = """
     47 foo and bar
     48 """
     49 test.run_built_executable('defines_escaping', stdout=expect)
     50 
     51 
     52 # Test string literals containing quotes.
     53 try:
     54   os.environ['GYP_DEFINES'] = (
     55       r"""test_format='\n%s %s %s %s %s\n' """
     56       r"""test_args='"\"These,\"","""
     57                 r""" "\"words,\"","""
     58                 r""" "\"are,\"","""
     59                 r""" "\"in,\"","""
     60                 r""" "\"quotes.\""'""")
     61   test.run_gyp('defines-escaping.gyp')
     62 finally:
     63   del os.environ['GYP_DEFINES']
     64 
     65 test.sleep()
     66 test.touch('defines-escaping.c')
     67 test.build('defines-escaping.gyp')
     68 
     69 expect = """
     70 "These," "words," "are," "in," "quotes."
     71 """
     72 test.run_built_executable('defines_escaping', stdout=expect)
     73 
     74 
     75 # Test string literals containing single quotes.
     76 try:
     77   os.environ['GYP_DEFINES'] = (
     78       r"""test_format='\n%s %s %s %s %s\n' """
     79       r"""test_args="\"'These,'\","""
     80                 r""" \"'words,'\","""
     81                 r""" \"'are,'\","""
     82                 r""" \"'in,'\","""
     83                 r""" \"'quotes.'\"" """)
     84   test.run_gyp('defines-escaping.gyp')
     85 finally:
     86   del os.environ['GYP_DEFINES']
     87 
     88 test.sleep()
     89 test.touch('defines-escaping.c')
     90 test.build('defines-escaping.gyp')
     91 
     92 expect = """
     93 'These,' 'words,' 'are,' 'in,' 'quotes.'
     94 """
     95 test.run_built_executable('defines_escaping', stdout=expect)
     96 
     97 
     98 # Test string literals containing different numbers of backslashes before quotes
     99 # (to exercise Windows' quoting behaviour).
    100 try:
    101   os.environ['GYP_DEFINES'] = (
    102       r"""test_format='\n%s\n%s\n%s\n' """
    103       r"""test_args='"\\\"1 visible slash\\\"","""
    104                 r""" "\\\\\"2 visible slashes\\\\\"","""
    105                 r""" "\\\\\\\"3 visible slashes\\\\\\\""'""")
    106   test.run_gyp('defines-escaping.gyp')
    107 finally:
    108   del os.environ['GYP_DEFINES']
    109 
    110 test.sleep()
    111 test.touch('defines-escaping.c')
    112 test.build('defines-escaping.gyp')
    113 
    114 expect = r"""
    115 \"1 visible slash\"
    116 \\"2 visible slashes\\"
    117 \\\"3 visible slashes\\\"
    118 """
    119 test.run_built_executable('defines_escaping', stdout=expect)
    120 
    121 
    122 # Test that various scary sequences are passed unfettered.
    123 try:
    124   os.environ['GYP_DEFINES'] = (
    125       r"""test_format='\n%s\n' """
    126       r"""test_args='"$foo, " `foo`;"'""")
    127   test.run_gyp('defines-escaping.gyp')
    128 finally:
    129   del os.environ['GYP_DEFINES']
    130 
    131 test.sleep()
    132 test.touch('defines-escaping.c')
    133 test.build('defines-escaping.gyp')
    134 
    135 expect = """
    136 $foo, " `foo`;
    137 """
    138 test.run_built_executable('defines_escaping', stdout=expect)
    139 
    140 
    141 # VisualStudio 2010 can't handle passing %PATH%
    142 if not (test.format == 'msvs' and test.uses_msbuild):
    143   try:
    144     os.environ['GYP_DEFINES'] = (
    145         """test_format='%s' """
    146         """test_args='"%PATH%"'""")
    147     test.run_gyp('defines-escaping.gyp')
    148   finally:
    149     del os.environ['GYP_DEFINES']
    150 
    151   test.sleep()
    152   test.touch('defines-escaping.c')
    153   test.build('defines-escaping.gyp')
    154 
    155   expect = "%PATH%"
    156   test.run_built_executable('defines_escaping', stdout=expect)
    157 
    158 
    159 # Test commas and semi-colons preceded by backslashes (to exercise Windows'
    160 # quoting behaviour).
    161 try:
    162   os.environ['GYP_DEFINES'] = (
    163       r"""test_format='\n%s\n%s\n' """
    164       r"""test_args='"\\, \\\\;","""
    165                 # Same thing again, but enclosed in visible quotes.
    166                 r""" "\"\\, \\\\;\""'""")
    167   test.run_gyp('defines-escaping.gyp')
    168 finally:
    169   del os.environ['GYP_DEFINES']
    170 
    171 test.sleep()
    172 test.touch('defines-escaping.c')
    173 test.build('defines-escaping.gyp')
    174 
    175 expect = r"""
    176 \, \\;
    177 "\, \\;"
    178 """
    179 test.run_built_executable('defines_escaping', stdout=expect)
    180 
    181 # We deliberately do not test having an odd number of quotes in a string
    182 # literal because that isn't feasible in MSVS.
    183 
    184 test.pass_test()
    185