You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.5 KiB

  1. # Copyright 2006, Google Inc.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. """Unit test utilities for Google C++ Mocking Framework."""
  30. import os
  31. import sys
  32. # Determines path to gtest_test_utils and imports it.
  33. SCRIPT_DIR = os.path.dirname(__file__) or '.'
  34. # isdir resolves symbolic links.
  35. gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../../googletest/test')
  36. if os.path.isdir(gtest_tests_util_dir):
  37. GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir
  38. else:
  39. GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../googletest/test')
  40. sys.path.append(GTEST_TESTS_UTIL_DIR)
  41. # pylint: disable=C6204
  42. import gtest_test_utils
  43. def GetSourceDir():
  44. """Returns the absolute path of the directory where the .py files are."""
  45. return gtest_test_utils.GetSourceDir()
  46. def GetTestExecutablePath(executable_name):
  47. """Returns the absolute path of the test binary given its name.
  48. The function will print a message and abort the program if the resulting file
  49. doesn't exist.
  50. Args:
  51. executable_name: name of the test binary that the test script runs.
  52. Returns:
  53. The absolute path of the test binary.
  54. """
  55. return gtest_test_utils.GetTestExecutablePath(executable_name)
  56. def GetExitStatus(exit_code):
  57. """Returns the argument to exit(), or -1 if exit() wasn't called.
  58. Args:
  59. exit_code: the result value of os.system(command).
  60. """
  61. if os.name == 'nt':
  62. # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
  63. # the argument to exit() directly.
  64. return exit_code
  65. else:
  66. # On Unix, os.WEXITSTATUS() must be used to extract the exit status
  67. # from the result of os.system().
  68. if os.WIFEXITED(exit_code):
  69. return os.WEXITSTATUS(exit_code)
  70. else:
  71. return -1
  72. # Suppresses the "Invalid const name" lint complaint
  73. # pylint: disable-msg=C6409
  74. # Exposes utilities from gtest_test_utils.
  75. Subprocess = gtest_test_utils.Subprocess
  76. TestCase = gtest_test_utils.TestCase
  77. environ = gtest_test_utils.environ
  78. SetEnvVar = gtest_test_utils.SetEnvVar
  79. PREMATURE_EXIT_FILE_ENV_VAR = gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR
  80. # pylint: enable-msg=C6409
  81. def Main():
  82. """Runs the unit test."""
  83. gtest_test_utils.Main()