gyptest-defines-escaping.py 4.6 KB

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