Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(718)

Side by Side Diff: test/cflags/gyptest-cflags.py

Issue 228323002: Use context manager to manage OS environment in tests (Closed) Base URL: https://chromium.googlesource.com/external/gyp.git@master
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/cxxflags/gyptest-cxxflags.py » ('j') | test/cxxflags/gyptest-cxxflags.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 # Copyright (c) 2012 Google Inc. All rights reserved. 3 # Copyright (c) 2012 Google Inc. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 """ 7 """
8 Verifies build of an executable with C++ define specified by a gyp define, and 8 Verifies build of an executable with C++ define specified by a gyp define, and
9 the use of the environment during regeneration when the gyp file changes. 9 the use of the environment during regeneration when the gyp file changes.
10 """ 10 """
11 11
12 import os 12 import os
13 import sys 13 import sys
14 import TestGyp 14 import TestGyp
15 15
16 env_stack = []
17
18
19 def PushEnv():
20 env_copy = os.environ.copy()
21 env_stack.append(env_copy)
22
23 def PopEnv():
24 os.environ.clear()
25 os.environ.update(env_stack.pop())
26
27 formats = ['make', 'ninja'] 16 formats = ['make', 'ninja']
28 17
29 test = TestGyp.TestGyp(formats=formats) 18 test = TestGyp.TestGyp(formats=formats)
30 19
31 try: 20 # First set CFLAGS to blank in case the platform doesn't support unsetenv.
32 PushEnv() 21 with TestGyp.LocalEnv({'CFLAGS': '',
33 os.environ['CFLAGS'] = '' 22 'GYP_CROSSCOMPILE': '1'}):
34 os.environ['GYP_CROSSCOMPILE'] = '1'
35 test.run_gyp('cflags.gyp') 23 test.run_gyp('cflags.gyp')
36 test.build('cflags.gyp') 24 test.build('cflags.gyp')
37 finally:
38 # We clear the environ after calling gyp. When the auto-regeneration happens,
39 # the same define should be reused anyway. Reset to empty string first in
40 # case the platform doesn't support unsetenv.
41 PopEnv()
42
43 25
44 expect = """FOO not defined\n""" 26 expect = """FOO not defined\n"""
45 test.run_built_executable('cflags', stdout=expect) 27 test.run_built_executable('cflags', stdout=expect)
46 test.run_built_executable('cflags_host', stdout=expect) 28 test.run_built_executable('cflags_host', stdout=expect)
47 29
48 test.sleep() 30 test.sleep()
49 31
50 try: 32 with TestGyp.LocalEnv({'CFLAGS': '-DFOO=1',
51 PushEnv() 33 'GYP_CROSSCOMPILE': '1'}):
52 os.environ['CFLAGS'] = '-DFOO=1'
53 os.environ['GYP_CROSSCOMPILE'] = '1'
54 test.run_gyp('cflags.gyp') 34 test.run_gyp('cflags.gyp')
55 test.build('cflags.gyp') 35 test.build('cflags.gyp')
56 finally:
57 # We clear the environ after calling gyp. When the auto-regeneration happens,
58 # the same define should be reused anyway. Reset to empty string first in
59 # case the platform doesn't support unsetenv.
60 PopEnv()
61
62 36
63 expect = """FOO defined\n""" 37 expect = """FOO defined\n"""
64 test.run_built_executable('cflags', stdout=expect) 38 test.run_built_executable('cflags', stdout=expect)
65 39
66 # Environment variables shouldn't influence the flags for the host. 40 # Environment variables shouldn't influence the flags for the host.
67 expect = """FOO not defined\n""" 41 expect = """FOO not defined\n"""
68 test.run_built_executable('cflags_host', stdout=expect) 42 test.run_built_executable('cflags_host', stdout=expect)
69 43
70 test.sleep() 44 test.sleep()
71 45
72 try: 46 with TestGyp.LocalEnv({'CFLAGS': ''}):
73 PushEnv()
74 os.environ['CFLAGS'] = ''
75 test.run_gyp('cflags.gyp') 47 test.run_gyp('cflags.gyp')
76 test.build('cflags.gyp') 48 test.build('cflags.gyp')
77 finally:
78 # We clear the environ after calling gyp. When the auto-regeneration happens,
79 # the same define should be reused anyway. Reset to empty string first in
80 # case the platform doesn't support unsetenv.
81 PopEnv()
82
83 49
84 expect = """FOO not defined\n""" 50 expect = """FOO not defined\n"""
85 test.run_built_executable('cflags', stdout=expect) 51 test.run_built_executable('cflags', stdout=expect)
86 52
87 test.sleep() 53 test.sleep()
88 54
89 try: 55 with TestGyp.LocalEnv({'CFLAGS': '-DFOO=1'}):
90 PushEnv()
91 os.environ['CFLAGS'] = '-DFOO=1'
92 test.run_gyp('cflags.gyp') 56 test.run_gyp('cflags.gyp')
93 test.build('cflags.gyp') 57 test.build('cflags.gyp')
94 finally:
95 # We clear the environ after calling gyp. When the auto-regeneration happens,
96 # the same define should be reused anyway. Reset to empty string first in
97 # case the platform doesn't support unsetenv.
98 PopEnv()
99
100 58
101 expect = """FOO defined\n""" 59 expect = """FOO defined\n"""
102 test.run_built_executable('cflags', stdout=expect) 60 test.run_built_executable('cflags', stdout=expect)
103 61
104 test.pass_test() 62 test.pass_test()
OLDNEW
« no previous file with comments | « no previous file | test/cxxflags/gyptest-cxxflags.py » ('j') | test/cxxflags/gyptest-cxxflags.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698