2 # Copyright 2013 The Swarming Authors. All rights reserved.
3 # Use of this source code is governed under the Apache License, Version 2.0 that
4 # can be found in the LICENSE file.
16 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
17 GOOGLETEST_DIR = os.path.dirname(BASE_DIR)
18 ROOT_DIR = os.path.dirname(GOOGLETEST_DIR)
20 sys.path.insert(0, ROOT_DIR)
21 sys.path.insert(0, os.path.join(ROOT_DIR, 'tests'))
25 import trace_test_util
28 class FixTestCases(unittest.TestCase):
30 self.tempdir = tempfile.mkdtemp(prefix='fix_test_case')
31 self.srcdir = os.path.join(self.tempdir, 'srcdir')
37 # If -v is used, this means the user wants to do further analysis on
39 print('Leaking %s' % self.tempdir)
41 shutil.rmtree(self.tempdir)
45 cmd = cmd + ['--verbose'] * 3
47 proc = subprocess.Popen(
48 [sys.executable] + cmd,
50 stdout=subprocess.PIPE,
51 stderr=subprocess.STDOUT)
52 out = proc.communicate()[0]
57 self.assertEqual(0, proc.returncode)
60 @trace_test_util.check_can_trace
61 def test_simple(self):
62 # Create a directory with nothing in it and progressively add more stuff.
63 isolate = os.path.join(self.srcdir, 'gtest_fake_pass.isolate')
64 condition = 'OS=="linux" and chromeos==1'
65 with open(isolate, 'w') as f:
66 # Write a minimal .isolate file.
72 'run_test_cases.py', 'gtest_fake_pass.py',
80 os.path.join(BASE_DIR, 'gtest_fake', filename),
81 os.path.join(self.srcdir, filename))
82 _copy('gtest_fake_base.py')
83 _copy('gtest_fake_pass.py')
85 os.path.join(GOOGLETEST_DIR, 'run_test_cases.py'),
86 os.path.join(self.srcdir, 'run_test_cases.py'))
87 # Deploy run_isolated with dependencies as zip into srcdir.
88 run_isolated.get_as_zip_package(executable=False).zip_into_file(
89 os.path.join(self.srcdir, 'run_isolated.zip'))
91 logging.debug('1. Create a .isolated file out of the .isolate file.')
92 isolated = os.path.join(self.srcdir, 'gtest_fake_pass.isolated')
95 os.path.join(ROOT_DIR, 'isolate.py'),
96 'check', '-i', isolate, '-s', isolated,
97 '--config-variable', 'OS', 'linux',
98 '--config-variable', 'chromeos', '1',
101 self.assertEqual('', out)
103 logging.debug('2. Run fix_test_cases.py on it.')
104 # Give up on looking at stdout.
106 os.path.join(GOOGLETEST_DIR, 'fix_test_cases.py'),
108 '--trace-blacklist', '.*\\.run_test_cases',
112 logging.debug('3. Asserting the content of the .isolated file.')
113 with open(isolated) as f:
114 actual_isolated = json.load(f)
115 gtest_fake_base_py = os.path.join(self.srcdir, 'gtest_fake_base.py')
116 gtest_fake_pass_py = os.path.join(self.srcdir, 'gtest_fake_pass.py')
117 run_isolated_zip = os.path.join(self.srcdir, 'run_isolated.zip')
118 run_test_cases_py = os.path.join(self.srcdir, 'run_test_cases.py')
120 expected_isolated = {
122 u'command': [u'run_test_cases.py', u'gtest_fake_pass.py'],
124 u'gtest_fake_base.py': {
126 u'h': unicode(isolateserver.hash_file(gtest_fake_base_py, algo)),
127 u's': os.stat(gtest_fake_base_py).st_size,
129 u'gtest_fake_pass.py': {
131 u'h': unicode(isolateserver.hash_file(gtest_fake_pass_py, algo)),
132 u's': os.stat(gtest_fake_pass_py).st_size,
134 u'run_isolated.zip': {
136 u'h': unicode(isolateserver.hash_file(run_isolated_zip, algo)),
137 u's': os.stat(run_isolated_zip).st_size,
139 u'run_test_cases.py': {
141 u'h': unicode(isolateserver.hash_file(run_test_cases_py, algo)),
142 u's': os.stat(run_test_cases_py).st_size,
145 u'relative_cwd': u'.',
146 u'version': unicode(isolateserver.ISOLATED_FILE_VERSION),
148 if sys.platform == 'win32':
149 for value in expected_isolated['files'].itervalues():
150 self.assertTrue(value.pop('m'))
151 self.assertEqual(expected_isolated, actual_isolated)
153 # Now verify the .isolate file was updated! (That's the magical part where
155 with open(isolate) as f:
156 actual = eval(f.read(), {'__builtins__': None}, None)
162 'run_test_cases.py', 'gtest_fake_pass.py'
164 'isolate_dependency_tracked': [
165 'gtest_fake_base.py',
166 'gtest_fake_pass.py',
174 self.assertEqual(expected, actual)
177 if __name__ == '__main__':
178 VERBOSE = '-v' in sys.argv
180 unittest.TestCase.maxDiff = None
181 logging.basicConfig(level=logging.DEBUG if VERBOSE else logging.ERROR)