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

Side by Side Diff: test/lib/TestGyp.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: Revised docs and which build 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 | « test/cxxflags/gyptest-cxxflags.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 Google Inc. All rights reserved. 1 # Copyright (c) 2012 Google Inc. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """ 5 """
6 TestGyp.py: a testing framework for GYP integration tests. 6 TestGyp.py: a testing framework for GYP integration tests.
7 """ 7 """
8 8
9 import collections 9 import collections
10 from contextlib import contextmanager
10 import itertools 11 import itertools
11 import os 12 import os
12 import re 13 import re
13 import shutil 14 import shutil
14 import stat 15 import stat
15 import subprocess 16 import subprocess
16 import sys 17 import sys
17 import tempfile 18 import tempfile
18 19
19 import TestCmd 20 import TestCmd
20 import TestCommon 21 import TestCommon
21 from TestCommon import __all__ 22 from TestCommon import __all__
22 23
23 __all__.extend([ 24 __all__.extend([
24 'TestGyp', 25 'TestGyp',
25 ]) 26 ])
26 27
28
27 def remove_debug_line_numbers(contents): 29 def remove_debug_line_numbers(contents):
28 """Function to remove the line numbers from the debug output 30 """Function to remove the line numbers from the debug output
29 of gyp and thus remove the exremem fragility of the stdout 31 of gyp and thus reduce the extreme fragility of the stdout
30 comparison tests. 32 comparison tests.
31 """ 33 """
32 lines = contents.splitlines() 34 lines = contents.splitlines()
33 # split each line on ":" 35 # split each line on ":"
34 lines = [l.split(":", 3) for l in lines] 36 lines = [l.split(":", 3) for l in lines]
35 # join each line back together while ignoring the 37 # join each line back together while ignoring the
36 # 3rd column which is the line number 38 # 3rd column which is the line number
37 lines = [len(l) > 3 and ":".join(l[3:]) or l for l in lines] 39 lines = [len(l) > 3 and ":".join(l[3:]) or l for l in lines]
38 return "\n".join(lines) 40 return "\n".join(lines)
39 41
42
40 def match_modulo_line_numbers(contents_a, contents_b): 43 def match_modulo_line_numbers(contents_a, contents_b):
41 """File contents matcher that ignores line numbers.""" 44 """File contents matcher that ignores line numbers."""
42 contents_a = remove_debug_line_numbers(contents_a) 45 contents_a = remove_debug_line_numbers(contents_a)
43 contents_b = remove_debug_line_numbers(contents_b) 46 contents_b = remove_debug_line_numbers(contents_b)
44 return TestCommon.match_exact(contents_a, contents_b) 47 return TestCommon.match_exact(contents_a, contents_b)
45 48
49
50 @contextmanager
51 def LocalEnv(local_env):
52 """Context manager to provide a local OS environment."""
53 old_env = os.environ.copy()
54 os.environ.update(local_env)
55 try:
56 yield
57 finally:
58 os.environ.clear()
59 os.environ.update(old_env)
60
61
46 class TestGypBase(TestCommon.TestCommon): 62 class TestGypBase(TestCommon.TestCommon):
47 """ 63 """
48 Class for controlling end-to-end tests of gyp generators. 64 Class for controlling end-to-end tests of gyp generators.
49 65
50 Instantiating this class will create a temporary directory and 66 Instantiating this class will create a temporary directory and
51 arrange for its destruction (via the TestCmd superclass) and 67 arrange for its destruction (via the TestCmd superclass) and
52 copy all of the non-gyptest files in the directory hierarchy of the 68 copy all of the non-gyptest files in the directory hierarchy of the
53 executing script. 69 executing script.
54 70
55 The default behavior is to test the 'gyp' or 'gyp.bat' file in the 71 The default behavior is to test the 'gyp' or 'gyp.bat' file in the
(...skipping 1052 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 1124
1109 def TestGyp(*args, **kw): 1125 def TestGyp(*args, **kw):
1110 """ 1126 """
1111 Returns an appropriate TestGyp* instance for a specified GYP format. 1127 Returns an appropriate TestGyp* instance for a specified GYP format.
1112 """ 1128 """
1113 format = kw.pop('format', os.environ.get('TESTGYP_FORMAT')) 1129 format = kw.pop('format', os.environ.get('TESTGYP_FORMAT'))
1114 for format_class in format_class_list: 1130 for format_class in format_class_list:
1115 if format == format_class.format: 1131 if format == format_class.format:
1116 return format_class(*args, **kw) 1132 return format_class(*args, **kw)
1117 raise Exception, "unknown format %r" % format 1133 raise Exception, "unknown format %r" % format
OLDNEW
« no previous file with comments | « test/cxxflags/gyptest-cxxflags.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698