gmock_test_utils.py 3.6 KB

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