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""" 8Verifies build of an executable with C++ define specified by a gyp define using 9various special characters such as quotes, commas, etc. 10""" 11 12import os 13import TestGyp 14 15test = TestGyp.TestGyp() 16 17# Tests string literals, percents, and backslash escapes. 18try: 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') 23finally: 24 del os.environ['GYP_DEFINES'] 25 26test.build('defines-escaping.gyp') 27 28expect = """ 29Simple test of %s with a literal 30""" 31test.run_built_executable('defines_escaping', stdout=expect) 32 33 34# Test multiple comma-and-space-separated string literals. 35try: 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') 39finally: 40 del os.environ['GYP_DEFINES'] 41 42test.sleep() 43test.touch('defines-escaping.c') 44test.build('defines-escaping.gyp') 45 46expect = """ 47foo and bar 48""" 49test.run_built_executable('defines_escaping', stdout=expect) 50 51 52# Test string literals containing quotes. 53try: 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') 62finally: 63 del os.environ['GYP_DEFINES'] 64 65test.sleep() 66test.touch('defines-escaping.c') 67test.build('defines-escaping.gyp') 68 69expect = """ 70"These," "words," "are," "in," "quotes." 71""" 72test.run_built_executable('defines_escaping', stdout=expect) 73 74 75# Test string literals containing single quotes. 76try: 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') 85finally: 86 del os.environ['GYP_DEFINES'] 87 88test.sleep() 89test.touch('defines-escaping.c') 90test.build('defines-escaping.gyp') 91 92expect = """ 93'These,' 'words,' 'are,' 'in,' 'quotes.' 94""" 95test.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). 100try: 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') 107finally: 108 del os.environ['GYP_DEFINES'] 109 110test.sleep() 111test.touch('defines-escaping.c') 112test.build('defines-escaping.gyp') 113 114expect = r""" 115\"1 visible slash\" 116\\"2 visible slashes\\" 117\\\"3 visible slashes\\\" 118""" 119test.run_built_executable('defines_escaping', stdout=expect) 120 121 122# Test that various scary sequences are passed unfettered. 123try: 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') 128finally: 129 del os.environ['GYP_DEFINES'] 130 131test.sleep() 132test.touch('defines-escaping.c') 133test.build('defines-escaping.gyp') 134 135expect = """ 136$foo, " `foo`; 137""" 138test.run_built_executable('defines_escaping', stdout=expect) 139 140 141# VisualStudio 2010 can't handle passing %PATH% 142if 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). 161try: 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') 168finally: 169 del os.environ['GYP_DEFINES'] 170 171test.sleep() 172test.touch('defines-escaping.c') 173test.build('defines-escaping.gyp') 174 175expect = r""" 176\, \\; 177"\, \\;" 178""" 179test.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 184test.pass_test() 185