summaryrefslogtreecommitdiffstats
path: root/third_party/python/gyp/test/defines-escaping/gyptest-defines-escaping.py
blob: eb18a3d369f48d7c266d421158d79cb3f616ed11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python

# Copyright (c) 2010 Google Inc. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""
Verifies build of an executable with C++ define specified by a gyp define using
various special characters such as quotes, commas, etc.
"""

import os
import TestGyp

test = TestGyp.TestGyp()

# Tests string literals, percents, and backslash escapes.
try:
  os.environ['GYP_DEFINES'] = (
      r"""test_format='\n%s\n' """
      r"""test_args='"Simple test of %s with a literal"'""")
  test.run_gyp('defines-escaping.gyp')
finally:
  del os.environ['GYP_DEFINES']

test.build('defines-escaping.gyp')

expect = """
Simple test of %s with a literal
"""
test.run_built_executable('defines_escaping', stdout=expect)


# Test multiple comma-and-space-separated string literals.
try:
  os.environ['GYP_DEFINES'] = \
      r"""test_format='\n%s and %s\n' test_args='"foo", "bar"'"""
  test.run_gyp('defines-escaping.gyp')
finally:
  del os.environ['GYP_DEFINES']

test.sleep()
test.touch('defines-escaping.c')
test.build('defines-escaping.gyp')

expect = """
foo and bar
"""
test.run_built_executable('defines_escaping', stdout=expect)


# Test string literals containing quotes.
try:
  os.environ['GYP_DEFINES'] = (
      r"""test_format='\n%s %s %s %s %s\n' """
      r"""test_args='"\"These,\"","""
                r""" "\"words,\"","""
                r""" "\"are,\"","""
                r""" "\"in,\"","""
                r""" "\"quotes.\""'""")
  test.run_gyp('defines-escaping.gyp')
finally:
  del os.environ['GYP_DEFINES']

test.sleep()
test.touch('defines-escaping.c')
test.build('defines-escaping.gyp')

expect = """
"These," "words," "are," "in," "quotes."
"""
test.run_built_executable('defines_escaping', stdout=expect)


# Test string literals containing single quotes.
try:
  os.environ['GYP_DEFINES'] = (
      r"""test_format='\n%s %s %s %s %s\n' """
      r"""test_args="\"'These,'\","""
                r""" \"'words,'\","""
                r""" \"'are,'\","""
                r""" \"'in,'\","""
                r""" \"'quotes.'\"" """)
  test.run_gyp('defines-escaping.gyp')
finally:
  del os.environ['GYP_DEFINES']

test.sleep()
test.touch('defines-escaping.c')
test.build('defines-escaping.gyp')

expect = """
'These,' 'words,' 'are,' 'in,' 'quotes.'
"""
test.run_built_executable('defines_escaping', stdout=expect)


# Test string literals containing different numbers of backslashes before quotes
# (to exercise Windows' quoting behaviour).
try:
  os.environ['GYP_DEFINES'] = (
      r"""test_format='\n%s\n%s\n%s\n' """
      r"""test_args='"\\\"1 visible slash\\\"","""
                r""" "\\\\\"2 visible slashes\\\\\"","""
                r""" "\\\\\\\"3 visible slashes\\\\\\\""'""")
  test.run_gyp('defines-escaping.gyp')
finally:
  del os.environ['GYP_DEFINES']

test.sleep()
test.touch('defines-escaping.c')
test.build('defines-escaping.gyp')

expect = r"""
\"1 visible slash\"
\\"2 visible slashes\\"
\\\"3 visible slashes\\\"
"""
test.run_built_executable('defines_escaping', stdout=expect)


# Test that various scary sequences are passed unfettered.
try:
  os.environ['GYP_DEFINES'] = (
      r"""test_format='\n%s\n' """
      r"""test_args='"$foo, " `foo`;"'""")
  test.run_gyp('defines-escaping.gyp')
finally:
  del os.environ['GYP_DEFINES']

test.sleep()
test.touch('defines-escaping.c')
test.build('defines-escaping.gyp')

expect = """
$foo, " `foo`;
"""
test.run_built_executable('defines_escaping', stdout=expect)


# VisualStudio 2010 can't handle passing %PATH%
if not (test.format == 'msvs' and test.uses_msbuild):
  try:
    os.environ['GYP_DEFINES'] = (
        """test_format='%s' """
        """test_args='"%PATH%"'""")
    test.run_gyp('defines-escaping.gyp')
  finally:
    del os.environ['GYP_DEFINES']

  test.sleep()
  test.touch('defines-escaping.c')
  test.build('defines-escaping.gyp')

  expect = "%PATH%"
  test.run_built_executable('defines_escaping', stdout=expect)


# Test commas and semi-colons preceded by backslashes (to exercise Windows'
# quoting behaviour).
try:
  os.environ['GYP_DEFINES'] = (
      r"""test_format='\n%s\n%s\n' """
      r"""test_args='"\\, \\\\;","""
                # Same thing again, but enclosed in visible quotes.
                r""" "\"\\, \\\\;\""'""")
  test.run_gyp('defines-escaping.gyp')
finally:
  del os.environ['GYP_DEFINES']

test.sleep()
test.touch('defines-escaping.c')
test.build('defines-escaping.gyp')

expect = r"""
\, \\;
"\, \\;"
"""
test.run_built_executable('defines_escaping', stdout=expect)

# We deliberately do not test having an odd number of quotes in a string
# literal because that isn't feasible in MSVS.

test.pass_test()