# found in the LICENSE file.
"""
-Verifies build of an executable with C++ define specified by a gyp define, and
-the use of the environment during regeneration when the gyp file changes.
+Verifies the use of the environment during regeneration when the gyp file
+changes, specifically via build of an executable with C preprocessor
+definition specified by CFLAGS.
+
+In this test, gyp and build both run in same local environment.
"""
-import os
-import sys
import TestGyp
-env_stack = []
-
-
-def PushEnv():
- env_copy = os.environ.copy()
- env_stack.append(env_copy)
-
-def PopEnv():
- os.environ.clear()
- os.environ.update(env_stack.pop())
-
-formats = ['make', 'ninja']
+# CPPFLAGS works in ninja but not make; CFLAGS works in both
+FORMATS = ('make', 'ninja')
-test = TestGyp.TestGyp(formats=formats)
+test = TestGyp.TestGyp(formats=FORMATS)
-try:
- PushEnv()
- os.environ['CFLAGS'] = ''
- os.environ['GYP_CROSSCOMPILE'] = '1'
+# First set CFLAGS to blank in case the platform doesn't support unsetenv.
+with TestGyp.LocalEnv({'CFLAGS': '',
+ 'GYP_CROSSCOMPILE': '1'}):
test.run_gyp('cflags.gyp')
test.build('cflags.gyp')
-finally:
- # We clear the environ after calling gyp. When the auto-regeneration happens,
- # the same define should be reused anyway. Reset to empty string first in
- # case the platform doesn't support unsetenv.
- PopEnv()
-
expect = """FOO not defined\n"""
test.run_built_executable('cflags', stdout=expect)
test.sleep()
-try:
- PushEnv()
- os.environ['CFLAGS'] = '-DFOO=1'
- os.environ['GYP_CROSSCOMPILE'] = '1'
+with TestGyp.LocalEnv({'CFLAGS': '-DFOO=1',
+ 'GYP_CROSSCOMPILE': '1'}):
test.run_gyp('cflags.gyp')
test.build('cflags.gyp')
-finally:
- # We clear the environ after calling gyp. When the auto-regeneration happens,
- # the same define should be reused anyway. Reset to empty string first in
- # case the platform doesn't support unsetenv.
- PopEnv()
-
expect = """FOO defined\n"""
test.run_built_executable('cflags', stdout=expect)
test.sleep()
-try:
- PushEnv()
- os.environ['CFLAGS'] = ''
+with TestGyp.LocalEnv({'CFLAGS': ''}):
test.run_gyp('cflags.gyp')
test.build('cflags.gyp')
-finally:
- # We clear the environ after calling gyp. When the auto-regeneration happens,
- # the same define should be reused anyway. Reset to empty string first in
- # case the platform doesn't support unsetenv.
- PopEnv()
-
expect = """FOO not defined\n"""
test.run_built_executable('cflags', stdout=expect)
test.sleep()
-try:
- PushEnv()
- os.environ['CFLAGS'] = '-DFOO=1'
+with TestGyp.LocalEnv({'CFLAGS': '-DFOO=1'}):
test.run_gyp('cflags.gyp')
test.build('cflags.gyp')
-finally:
- # We clear the environ after calling gyp. When the auto-regeneration happens,
- # the same define should be reused anyway. Reset to empty string first in
- # case the platform doesn't support unsetenv.
- PopEnv()
-
expect = """FOO defined\n"""
test.run_built_executable('cflags', stdout=expect)