PageRenderTime 102ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/gtest/test/run_tests_util_test.py

http://github.com/tomahawk-player/tomahawk
Python | 676 lines | 590 code | 39 blank | 47 comment | 18 complexity | 24a9b0af4ad94a054dfe2f72b84b7e0d MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2009 Google Inc. All Rights Reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are
  7. # met:
  8. #
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following disclaimer
  13. # in the documentation and/or other materials provided with the
  14. # distribution.
  15. # * Neither the name of Google Inc. nor the names of its
  16. # contributors may be used to endorse or promote products derived from
  17. # this software without specific prior written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. """Tests for run_tests_util.py test runner script."""
  31. __author__ = 'vladl@google.com (Vlad Losev)'
  32. import os
  33. import re
  34. import sets
  35. import unittest
  36. import run_tests_util
  37. GTEST_DBG_DIR = 'scons/build/dbg/gtest/scons'
  38. GTEST_OPT_DIR = 'scons/build/opt/gtest/scons'
  39. GTEST_OTHER_DIR = 'scons/build/other/gtest/scons'
  40. def AddExeExtension(path):
  41. """Appends .exe to the path on Windows or Cygwin."""
  42. if run_tests_util.IS_WINDOWS or run_tests_util.IS_CYGWIN:
  43. return path + '.exe'
  44. else:
  45. return path
  46. class FakePath(object):
  47. """A fake os.path module for testing."""
  48. def __init__(self, current_dir=os.getcwd(), known_paths=None):
  49. self.current_dir = current_dir
  50. self.tree = {}
  51. self.path_separator = os.sep
  52. # known_paths contains either absolute or relative paths. Relative paths
  53. # are absolutized with self.current_dir.
  54. if known_paths:
  55. self._AddPaths(known_paths)
  56. def _AddPath(self, path):
  57. ends_with_slash = path.endswith('/')
  58. path = self.abspath(path)
  59. if ends_with_slash:
  60. path += self.path_separator
  61. name_list = path.split(self.path_separator)
  62. tree = self.tree
  63. for name in name_list[:-1]:
  64. if not name:
  65. continue
  66. if name in tree:
  67. tree = tree[name]
  68. else:
  69. tree[name] = {}
  70. tree = tree[name]
  71. name = name_list[-1]
  72. if name:
  73. if name in tree:
  74. assert tree[name] == 1
  75. else:
  76. tree[name] = 1
  77. def _AddPaths(self, paths):
  78. for path in paths:
  79. self._AddPath(path)
  80. def PathElement(self, path):
  81. """Returns an internal representation of directory tree entry for path."""
  82. tree = self.tree
  83. name_list = self.abspath(path).split(self.path_separator)
  84. for name in name_list:
  85. if not name:
  86. continue
  87. tree = tree.get(name, None)
  88. if tree is None:
  89. break
  90. return tree
  91. # Silences pylint warning about using standard names.
  92. # pylint: disable-msg=C6409
  93. def normpath(self, path):
  94. return os.path.normpath(path)
  95. def abspath(self, path):
  96. return self.normpath(os.path.join(self.current_dir, path))
  97. def isfile(self, path):
  98. return self.PathElement(self.abspath(path)) == 1
  99. def isdir(self, path):
  100. return type(self.PathElement(self.abspath(path))) == type(dict())
  101. def basename(self, path):
  102. return os.path.basename(path)
  103. def dirname(self, path):
  104. return os.path.dirname(path)
  105. def join(self, *kargs):
  106. return os.path.join(*kargs)
  107. class FakeOs(object):
  108. """A fake os module for testing."""
  109. P_WAIT = os.P_WAIT
  110. def __init__(self, fake_path_module):
  111. self.path = fake_path_module
  112. # Some methods/attributes are delegated to the real os module.
  113. self.environ = os.environ
  114. # pylint: disable-msg=C6409
  115. def listdir(self, path):
  116. assert self.path.isdir(path)
  117. return self.path.PathElement(path).iterkeys()
  118. def spawnv(self, wait, executable, *kargs):
  119. assert wait == FakeOs.P_WAIT
  120. return self.spawn_impl(executable, kargs)
  121. class GetTestsToRunTest(unittest.TestCase):
  122. """Exercises TestRunner.GetTestsToRun."""
  123. def NormalizeGetTestsToRunResults(self, results):
  124. """Normalizes path data returned from GetTestsToRun for comparison."""
  125. def NormalizePythonTestPair(pair):
  126. """Normalizes path data in the (directory, python_script) pair."""
  127. return (os.path.normpath(pair[0]), os.path.normpath(pair[1]))
  128. def NormalizeBinaryTestPair(pair):
  129. """Normalizes path data in the (directory, binary_executable) pair."""
  130. directory, executable = map(os.path.normpath, pair)
  131. # On Windows and Cygwin, the test file names have the .exe extension, but
  132. # they can be invoked either by name or by name+extension. Our test must
  133. # accommodate both situations.
  134. if run_tests_util.IS_WINDOWS or run_tests_util.IS_CYGWIN:
  135. executable = re.sub(r'\.exe$', '', executable)
  136. return (directory, executable)
  137. python_tests = sets.Set(map(NormalizePythonTestPair, results[0]))
  138. binary_tests = sets.Set(map(NormalizeBinaryTestPair, results[1]))
  139. return (python_tests, binary_tests)
  140. def AssertResultsEqual(self, results, expected):
  141. """Asserts results returned by GetTestsToRun equal to expected results."""
  142. self.assertEqual(self.NormalizeGetTestsToRunResults(results),
  143. self.NormalizeGetTestsToRunResults(expected),
  144. 'Incorrect set of tests returned:\n%s\nexpected:\n%s' %
  145. (results, expected))
  146. def setUp(self):
  147. self.fake_os = FakeOs(FakePath(
  148. current_dir=os.path.abspath(os.path.dirname(run_tests_util.__file__)),
  149. known_paths=[AddExeExtension(GTEST_DBG_DIR + '/gtest_unittest'),
  150. AddExeExtension(GTEST_OPT_DIR + '/gtest_unittest'),
  151. 'test/gtest_color_test.py']))
  152. self.fake_configurations = ['dbg', 'opt']
  153. self.test_runner = run_tests_util.TestRunner(script_dir='.',
  154. injected_os=self.fake_os,
  155. injected_subprocess=None)
  156. def testBinaryTestsOnly(self):
  157. """Exercises GetTestsToRun with parameters designating binary tests only."""
  158. # A default build.
  159. self.AssertResultsEqual(
  160. self.test_runner.GetTestsToRun(
  161. ['gtest_unittest'],
  162. '',
  163. False,
  164. available_configurations=self.fake_configurations),
  165. ([],
  166. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]))
  167. # An explicitly specified directory.
  168. self.AssertResultsEqual(
  169. self.test_runner.GetTestsToRun(
  170. [GTEST_DBG_DIR, 'gtest_unittest'],
  171. '',
  172. False,
  173. available_configurations=self.fake_configurations),
  174. ([],
  175. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]))
  176. # A particular configuration.
  177. self.AssertResultsEqual(
  178. self.test_runner.GetTestsToRun(
  179. ['gtest_unittest'],
  180. 'other',
  181. False,
  182. available_configurations=self.fake_configurations),
  183. ([],
  184. [(GTEST_OTHER_DIR, GTEST_OTHER_DIR + '/gtest_unittest')]))
  185. # All available configurations
  186. self.AssertResultsEqual(
  187. self.test_runner.GetTestsToRun(
  188. ['gtest_unittest'],
  189. 'all',
  190. False,
  191. available_configurations=self.fake_configurations),
  192. ([],
  193. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest'),
  194. (GTEST_OPT_DIR, GTEST_OPT_DIR + '/gtest_unittest')]))
  195. # All built configurations (unbuilt don't cause failure).
  196. self.AssertResultsEqual(
  197. self.test_runner.GetTestsToRun(
  198. ['gtest_unittest'],
  199. '',
  200. True,
  201. available_configurations=self.fake_configurations + ['unbuilt']),
  202. ([],
  203. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest'),
  204. (GTEST_OPT_DIR, GTEST_OPT_DIR + '/gtest_unittest')]))
  205. # A combination of an explicit directory and a configuration.
  206. self.AssertResultsEqual(
  207. self.test_runner.GetTestsToRun(
  208. [GTEST_DBG_DIR, 'gtest_unittest'],
  209. 'opt',
  210. False,
  211. available_configurations=self.fake_configurations),
  212. ([],
  213. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest'),
  214. (GTEST_OPT_DIR, GTEST_OPT_DIR + '/gtest_unittest')]))
  215. # Same test specified in an explicit directory and via a configuration.
  216. self.AssertResultsEqual(
  217. self.test_runner.GetTestsToRun(
  218. [GTEST_DBG_DIR, 'gtest_unittest'],
  219. 'dbg',
  220. False,
  221. available_configurations=self.fake_configurations),
  222. ([],
  223. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]))
  224. # All built configurations + explicit directory + explicit configuration.
  225. self.AssertResultsEqual(
  226. self.test_runner.GetTestsToRun(
  227. [GTEST_DBG_DIR, 'gtest_unittest'],
  228. 'opt',
  229. True,
  230. available_configurations=self.fake_configurations),
  231. ([],
  232. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest'),
  233. (GTEST_OPT_DIR, GTEST_OPT_DIR + '/gtest_unittest')]))
  234. def testPythonTestsOnly(self):
  235. """Exercises GetTestsToRun with parameters designating Python tests only."""
  236. # A default build.
  237. self.AssertResultsEqual(
  238. self.test_runner.GetTestsToRun(
  239. ['gtest_color_test.py'],
  240. '',
  241. False,
  242. available_configurations=self.fake_configurations),
  243. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py')],
  244. []))
  245. # An explicitly specified directory.
  246. self.AssertResultsEqual(
  247. self.test_runner.GetTestsToRun(
  248. [GTEST_DBG_DIR, 'test/gtest_color_test.py'],
  249. '',
  250. False,
  251. available_configurations=self.fake_configurations),
  252. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py')],
  253. []))
  254. # A particular configuration.
  255. self.AssertResultsEqual(
  256. self.test_runner.GetTestsToRun(
  257. ['gtest_color_test.py'],
  258. 'other',
  259. False,
  260. available_configurations=self.fake_configurations),
  261. ([(GTEST_OTHER_DIR, 'test/gtest_color_test.py')],
  262. []))
  263. # All available configurations
  264. self.AssertResultsEqual(
  265. self.test_runner.GetTestsToRun(
  266. ['test/gtest_color_test.py'],
  267. 'all',
  268. False,
  269. available_configurations=self.fake_configurations),
  270. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py'),
  271. (GTEST_OPT_DIR, 'test/gtest_color_test.py')],
  272. []))
  273. # All built configurations (unbuilt don't cause failure).
  274. self.AssertResultsEqual(
  275. self.test_runner.GetTestsToRun(
  276. ['gtest_color_test.py'],
  277. '',
  278. True,
  279. available_configurations=self.fake_configurations + ['unbuilt']),
  280. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py'),
  281. (GTEST_OPT_DIR, 'test/gtest_color_test.py')],
  282. []))
  283. # A combination of an explicit directory and a configuration.
  284. self.AssertResultsEqual(
  285. self.test_runner.GetTestsToRun(
  286. [GTEST_DBG_DIR, 'gtest_color_test.py'],
  287. 'opt',
  288. False,
  289. available_configurations=self.fake_configurations),
  290. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py'),
  291. (GTEST_OPT_DIR, 'test/gtest_color_test.py')],
  292. []))
  293. # Same test specified in an explicit directory and via a configuration.
  294. self.AssertResultsEqual(
  295. self.test_runner.GetTestsToRun(
  296. [GTEST_DBG_DIR, 'gtest_color_test.py'],
  297. 'dbg',
  298. False,
  299. available_configurations=self.fake_configurations),
  300. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py')],
  301. []))
  302. # All built configurations + explicit directory + explicit configuration.
  303. self.AssertResultsEqual(
  304. self.test_runner.GetTestsToRun(
  305. [GTEST_DBG_DIR, 'gtest_color_test.py'],
  306. 'opt',
  307. True,
  308. available_configurations=self.fake_configurations),
  309. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py'),
  310. (GTEST_OPT_DIR, 'test/gtest_color_test.py')],
  311. []))
  312. def testCombinationOfBinaryAndPythonTests(self):
  313. """Exercises GetTestsToRun with mixed binary/Python tests."""
  314. # Use only default configuration for this test.
  315. # Neither binary nor Python tests are specified so find all.
  316. self.AssertResultsEqual(
  317. self.test_runner.GetTestsToRun(
  318. [],
  319. '',
  320. False,
  321. available_configurations=self.fake_configurations),
  322. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py')],
  323. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]))
  324. # Specifying both binary and Python tests.
  325. self.AssertResultsEqual(
  326. self.test_runner.GetTestsToRun(
  327. ['gtest_unittest', 'gtest_color_test.py'],
  328. '',
  329. False,
  330. available_configurations=self.fake_configurations),
  331. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py')],
  332. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]))
  333. # Specifying binary tests suppresses Python tests.
  334. self.AssertResultsEqual(
  335. self.test_runner.GetTestsToRun(
  336. ['gtest_unittest'],
  337. '',
  338. False,
  339. available_configurations=self.fake_configurations),
  340. ([],
  341. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]))
  342. # Specifying Python tests suppresses binary tests.
  343. self.AssertResultsEqual(
  344. self.test_runner.GetTestsToRun(
  345. ['gtest_color_test.py'],
  346. '',
  347. False,
  348. available_configurations=self.fake_configurations),
  349. ([(GTEST_DBG_DIR, 'test/gtest_color_test.py')],
  350. []))
  351. def testIgnoresNonTestFiles(self):
  352. """Verifies that GetTestsToRun ignores non-test files in the filesystem."""
  353. self.fake_os = FakeOs(FakePath(
  354. current_dir=os.path.abspath(os.path.dirname(run_tests_util.__file__)),
  355. known_paths=[AddExeExtension(GTEST_DBG_DIR + '/gtest_nontest'),
  356. 'test/']))
  357. self.test_runner = run_tests_util.TestRunner(script_dir='.',
  358. injected_os=self.fake_os,
  359. injected_subprocess=None)
  360. self.AssertResultsEqual(
  361. self.test_runner.GetTestsToRun(
  362. [],
  363. '',
  364. True,
  365. available_configurations=self.fake_configurations),
  366. ([], []))
  367. def testWorksFromDifferentDir(self):
  368. """Exercises GetTestsToRun from a directory different from run_test.py's."""
  369. # Here we simulate an test script in directory /d/ called from the
  370. # directory /a/b/c/.
  371. self.fake_os = FakeOs(FakePath(
  372. current_dir=os.path.abspath('/a/b/c'),
  373. known_paths=[
  374. '/a/b/c/',
  375. AddExeExtension('/d/' + GTEST_DBG_DIR + '/gtest_unittest'),
  376. AddExeExtension('/d/' + GTEST_OPT_DIR + '/gtest_unittest'),
  377. '/d/test/gtest_color_test.py']))
  378. self.fake_configurations = ['dbg', 'opt']
  379. self.test_runner = run_tests_util.TestRunner(script_dir='/d/',
  380. injected_os=self.fake_os,
  381. injected_subprocess=None)
  382. # A binary test.
  383. self.AssertResultsEqual(
  384. self.test_runner.GetTestsToRun(
  385. ['gtest_unittest'],
  386. '',
  387. False,
  388. available_configurations=self.fake_configurations),
  389. ([],
  390. [('/d/' + GTEST_DBG_DIR, '/d/' + GTEST_DBG_DIR + '/gtest_unittest')]))
  391. # A Python test.
  392. self.AssertResultsEqual(
  393. self.test_runner.GetTestsToRun(
  394. ['gtest_color_test.py'],
  395. '',
  396. False,
  397. available_configurations=self.fake_configurations),
  398. ([('/d/' + GTEST_DBG_DIR, '/d/test/gtest_color_test.py')], []))
  399. def testNonTestBinary(self):
  400. """Exercises GetTestsToRun with a non-test parameter."""
  401. self.assert_(
  402. not self.test_runner.GetTestsToRun(
  403. ['gtest_unittest_not_really'],
  404. '',
  405. False,
  406. available_configurations=self.fake_configurations))
  407. def testNonExistingPythonTest(self):
  408. """Exercises GetTestsToRun with a non-existent Python test parameter."""
  409. self.assert_(
  410. not self.test_runner.GetTestsToRun(
  411. ['nonexistent_test.py'],
  412. '',
  413. False,
  414. available_configurations=self.fake_configurations))
  415. if run_tests_util.IS_WINDOWS or run_tests_util.IS_CYGWIN:
  416. def testDoesNotPickNonExeFilesOnWindows(self):
  417. """Verifies that GetTestsToRun does not find _test files on Windows."""
  418. self.fake_os = FakeOs(FakePath(
  419. current_dir=os.path.abspath(os.path.dirname(run_tests_util.__file__)),
  420. known_paths=['/d/' + GTEST_DBG_DIR + '/gtest_test', 'test/']))
  421. self.test_runner = run_tests_util.TestRunner(script_dir='.',
  422. injected_os=self.fake_os,
  423. injected_subprocess=None)
  424. self.AssertResultsEqual(
  425. self.test_runner.GetTestsToRun(
  426. [],
  427. '',
  428. True,
  429. available_configurations=self.fake_configurations),
  430. ([], []))
  431. class RunTestsTest(unittest.TestCase):
  432. """Exercises TestRunner.RunTests."""
  433. def SpawnSuccess(self, unused_executable, unused_argv):
  434. """Fakes test success by returning 0 as an exit code."""
  435. self.num_spawn_calls += 1
  436. return 0
  437. def SpawnFailure(self, unused_executable, unused_argv):
  438. """Fakes test success by returning 1 as an exit code."""
  439. self.num_spawn_calls += 1
  440. return 1
  441. def setUp(self):
  442. self.fake_os = FakeOs(FakePath(
  443. current_dir=os.path.abspath(os.path.dirname(run_tests_util.__file__)),
  444. known_paths=[
  445. AddExeExtension(GTEST_DBG_DIR + '/gtest_unittest'),
  446. AddExeExtension(GTEST_OPT_DIR + '/gtest_unittest'),
  447. 'test/gtest_color_test.py']))
  448. self.fake_configurations = ['dbg', 'opt']
  449. self.test_runner = run_tests_util.TestRunner(
  450. script_dir=os.path.dirname(__file__) or '.',
  451. injected_os=self.fake_os,
  452. injected_subprocess=None)
  453. self.num_spawn_calls = 0 # A number of calls to spawn.
  454. def testRunPythonTestSuccess(self):
  455. """Exercises RunTests to handle a Python test success."""
  456. self.fake_os.spawn_impl = self.SpawnSuccess
  457. self.assertEqual(
  458. self.test_runner.RunTests(
  459. [(GTEST_DBG_DIR, 'test/gtest_color_test.py')],
  460. []),
  461. 0)
  462. self.assertEqual(self.num_spawn_calls, 1)
  463. def testRunBinaryTestSuccess(self):
  464. """Exercises RunTests to handle a binary test success."""
  465. self.fake_os.spawn_impl = self.SpawnSuccess
  466. self.assertEqual(
  467. self.test_runner.RunTests(
  468. [],
  469. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]),
  470. 0)
  471. self.assertEqual(self.num_spawn_calls, 1)
  472. def testRunPythonTestFauilure(self):
  473. """Exercises RunTests to handle a Python test failure."""
  474. self.fake_os.spawn_impl = self.SpawnFailure
  475. self.assertEqual(
  476. self.test_runner.RunTests(
  477. [(GTEST_DBG_DIR, 'test/gtest_color_test.py')],
  478. []),
  479. 1)
  480. self.assertEqual(self.num_spawn_calls, 1)
  481. def testRunBinaryTestFailure(self):
  482. """Exercises RunTests to handle a binary test failure."""
  483. self.fake_os.spawn_impl = self.SpawnFailure
  484. self.assertEqual(
  485. self.test_runner.RunTests(
  486. [],
  487. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]),
  488. 1)
  489. self.assertEqual(self.num_spawn_calls, 1)
  490. def testCombinedTestSuccess(self):
  491. """Exercises RunTests to handle a success of both Python and binary test."""
  492. self.fake_os.spawn_impl = self.SpawnSuccess
  493. self.assertEqual(
  494. self.test_runner.RunTests(
  495. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')],
  496. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]),
  497. 0)
  498. self.assertEqual(self.num_spawn_calls, 2)
  499. def testCombinedTestSuccessAndFailure(self):
  500. """Exercises RunTests to handle a success of both Python and binary test."""
  501. def SpawnImpl(executable, argv):
  502. self.num_spawn_calls += 1
  503. # Simulates failure of a Python test and success of a binary test.
  504. if '.py' in executable or '.py' in argv[0]:
  505. return 1
  506. else:
  507. return 0
  508. self.fake_os.spawn_impl = SpawnImpl
  509. self.assertEqual(
  510. self.test_runner.RunTests(
  511. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')],
  512. [(GTEST_DBG_DIR, GTEST_DBG_DIR + '/gtest_unittest')]),
  513. 0)
  514. self.assertEqual(self.num_spawn_calls, 2)
  515. class ParseArgsTest(unittest.TestCase):
  516. """Exercises ParseArgs."""
  517. def testNoOptions(self):
  518. options, args = run_tests_util.ParseArgs('gtest', argv=['script.py'])
  519. self.assertEqual(args, ['script.py'])
  520. self.assert_(options.configurations is None)
  521. self.assertFalse(options.built_configurations)
  522. def testOptionC(self):
  523. options, args = run_tests_util.ParseArgs(
  524. 'gtest', argv=['script.py', '-c', 'dbg'])
  525. self.assertEqual(args, ['script.py'])
  526. self.assertEqual(options.configurations, 'dbg')
  527. self.assertFalse(options.built_configurations)
  528. def testOptionA(self):
  529. options, args = run_tests_util.ParseArgs('gtest', argv=['script.py', '-a'])
  530. self.assertEqual(args, ['script.py'])
  531. self.assertEqual(options.configurations, 'all')
  532. self.assertFalse(options.built_configurations)
  533. def testOptionB(self):
  534. options, args = run_tests_util.ParseArgs('gtest', argv=['script.py', '-b'])
  535. self.assertEqual(args, ['script.py'])
  536. self.assert_(options.configurations is None)
  537. self.assertTrue(options.built_configurations)
  538. def testOptionCAndOptionB(self):
  539. options, args = run_tests_util.ParseArgs(
  540. 'gtest', argv=['script.py', '-c', 'dbg', '-b'])
  541. self.assertEqual(args, ['script.py'])
  542. self.assertEqual(options.configurations, 'dbg')
  543. self.assertTrue(options.built_configurations)
  544. def testOptionH(self):
  545. help_called = [False]
  546. # Suppresses lint warning on unused arguments. These arguments are
  547. # required by optparse, even though they are unused.
  548. # pylint: disable-msg=W0613
  549. def VerifyHelp(option, opt, value, parser):
  550. help_called[0] = True
  551. # Verifies that -h causes the help callback to be called.
  552. help_called[0] = False
  553. _, args = run_tests_util.ParseArgs(
  554. 'gtest', argv=['script.py', '-h'], help_callback=VerifyHelp)
  555. self.assertEqual(args, ['script.py'])
  556. self.assertTrue(help_called[0])
  557. # Verifies that --help causes the help callback to be called.
  558. help_called[0] = False
  559. _, args = run_tests_util.ParseArgs(
  560. 'gtest', argv=['script.py', '--help'], help_callback=VerifyHelp)
  561. self.assertEqual(args, ['script.py'])
  562. self.assertTrue(help_called[0])
  563. if __name__ == '__main__':
  564. unittest.main()