googletest-fail-if-no-test-linked-test.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python3 # pylint: disable=g-interpreter-mismatch
  2. #
  3. # Copyright 2025, 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. """Tests for Google Test's --gtest_fail_if_no_test_linked flag."""
  32. import os
  33. from googletest.test import gtest_test_utils
  34. # The command line flag for enabling the fail-if-no-test-linked behavior.
  35. FAIL_IF_NO_TEST_LINKED_FLAG = "gtest_fail_if_no_test_linked"
  36. # The environment variable for the test output warnings file.
  37. TEST_WARNINGS_OUTPUT_FILE = "TEST_WARNINGS_OUTPUT_FILE"
  38. class GTestFailIfNoTestLinkedTest(gtest_test_utils.TestCase):
  39. """Tests the --gtest_fail_if_no_test_linked flag."""
  40. def Run(self, program_name, flag=None, env=None):
  41. """Run the given program with the given flag.
  42. Args:
  43. program_name: Name of the program to run.
  44. flag: The command line flag to pass to the program, or None.
  45. env: Dictionary with environment to pass to the subprocess.
  46. Returns:
  47. True if the program exits with code 0, false otherwise.
  48. """
  49. exe_path = gtest_test_utils.GetTestExecutablePath(program_name)
  50. args = [exe_path]
  51. if flag is not None:
  52. args += [flag]
  53. process = gtest_test_utils.Subprocess(args, capture_stderr=False, env=env)
  54. return process.exited and process.exit_code == 0
  55. def testSucceedsIfNoTestLinkedAndFlagNotSpecified(self):
  56. """Tests the behavior of no test linked and flag not specified."""
  57. self.assertTrue(
  58. self.Run("googletest-fail-if-no-test-linked-test-without-test_")
  59. )
  60. def testSucceedsIfNoTestLinkedAndFlagNotSpecifiedWithWarningFile(self):
  61. """Tests that no test linked results in warning file output."""
  62. warning_file = os.path.join(gtest_test_utils.GetTempDir(), "NO_TEST_LINKED")
  63. self.assertTrue(
  64. self.Run(
  65. "googletest-fail-if-no-test-linked-test-without-test_",
  66. env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
  67. )
  68. )
  69. warning_file_contents = open(warning_file, "r").read()
  70. self.assertEqual(
  71. warning_file_contents,
  72. "This test program does NOT link in any test case. Please make sure"
  73. " this is intended.\n",
  74. )
  75. def testFailsIfNoTestLinkedAndFlagSpecified(self):
  76. """Tests the behavior of no test linked and flag specified."""
  77. warning_file = os.path.join(
  78. gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
  79. )
  80. self.assertFalse(
  81. self.Run(
  82. "googletest-fail-if-no-test-linked-test-without-test_",
  83. f"--{FAIL_IF_NO_TEST_LINKED_FLAG}",
  84. env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
  85. )
  86. )
  87. with self.assertRaises(FileNotFoundError):
  88. open(warning_file, "r")
  89. def testSucceedsIfEnabledTestLinkedAndFlagNotSpecified(self):
  90. """Tests the behavior of enabled test linked and flag not specified."""
  91. warning_file = os.path.join(
  92. gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
  93. )
  94. self.assertTrue(
  95. self.Run(
  96. "googletest-fail-if-no-test-linked-test-with-enabled-test_",
  97. env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
  98. )
  99. )
  100. with self.assertRaises(FileNotFoundError):
  101. open(warning_file, "r")
  102. def testSucceedsIfEnabledTestLinkedAndFlagSpecified(self):
  103. """Tests the behavior of enabled test linked and flag specified."""
  104. warning_file = os.path.join(
  105. gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
  106. )
  107. self.assertTrue(
  108. self.Run(
  109. "googletest-fail-if-no-test-linked-test-with-enabled-test_",
  110. f"--{FAIL_IF_NO_TEST_LINKED_FLAG}",
  111. env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
  112. )
  113. )
  114. with self.assertRaises(FileNotFoundError):
  115. open(warning_file, "r")
  116. def testSucceedsIfDisabledTestLinkedAndFlagNotSpecified(self):
  117. """Tests the behavior of disabled test linked and flag not specified."""
  118. warning_file = os.path.join(
  119. gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
  120. )
  121. self.assertTrue(
  122. self.Run(
  123. "googletest-fail-if-no-test-linked-test-with-disabled-test_",
  124. env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
  125. )
  126. )
  127. with self.assertRaises(FileNotFoundError):
  128. open(warning_file, "r")
  129. def testSucceedsIfDisabledTestLinkedAndFlagSpecified(self):
  130. """Tests the behavior of disabled test linked and flag specified."""
  131. warning_file = os.path.join(
  132. gtest_test_utils.GetTempDir(), "SHOULD_NOT_EXIST"
  133. )
  134. self.assertTrue(
  135. self.Run(
  136. "googletest-fail-if-no-test-linked-test-with-disabled-test_",
  137. f"--{FAIL_IF_NO_TEST_LINKED_FLAG}",
  138. env={TEST_WARNINGS_OUTPUT_FILE: warning_file},
  139. )
  140. )
  141. with self.assertRaises(FileNotFoundError):
  142. open(warning_file, "r")
  143. if __name__ == "__main__":
  144. gtest_test_utils.Main()