test_misc.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Tests of miscellaneous stuff."""
  4. import sys
  5. import coverage
  6. from coverage.version import _make_url, _make_version
  7. from coverage.misc import Hasher, file_be_gone
  8. from tests.coveragetest import CoverageTest
  9. class HasherTest(CoverageTest):
  10. """Test our wrapper of md5 hashing."""
  11. run_in_temp_dir = False
  12. def test_string_hashing(self):
  13. h1 = Hasher()
  14. h1.update("Hello, world!")
  15. h2 = Hasher()
  16. h2.update("Goodbye!")
  17. h3 = Hasher()
  18. h3.update("Hello, world!")
  19. self.assertNotEqual(h1.hexdigest(), h2.hexdigest())
  20. self.assertEqual(h1.hexdigest(), h3.hexdigest())
  21. def test_bytes_hashing(self):
  22. h1 = Hasher()
  23. h1.update(b"Hello, world!")
  24. h2 = Hasher()
  25. h2.update(b"Goodbye!")
  26. self.assertNotEqual(h1.hexdigest(), h2.hexdigest())
  27. def test_dict_hashing(self):
  28. h1 = Hasher()
  29. h1.update({'a': 17, 'b': 23})
  30. h2 = Hasher()
  31. h2.update({'b': 23, 'a': 17})
  32. self.assertEqual(h1.hexdigest(), h2.hexdigest())
  33. class RemoveFileTest(CoverageTest):
  34. """Tests of misc.file_be_gone."""
  35. def test_remove_nonexistent_file(self):
  36. # It's OK to try to remove a file that doesn't exist.
  37. file_be_gone("not_here.txt")
  38. def test_remove_actual_file(self):
  39. # It really does remove a file that does exist.
  40. self.make_file("here.txt", "We are here, we are here, we are here!")
  41. file_be_gone("here.txt")
  42. self.assert_doesnt_exist("here.txt")
  43. def test_actual_errors(self):
  44. # Errors can still happen.
  45. # ". is a directory" on Unix, or "Access denied" on Windows
  46. with self.assertRaises(OSError):
  47. file_be_gone(".")
  48. class VersionTest(CoverageTest):
  49. """Tests of version.py"""
  50. run_in_temp_dir = False
  51. def test_version_info(self):
  52. # Make sure we didn't screw up the version_info tuple.
  53. self.assertIsInstance(coverage.version_info, tuple)
  54. self.assertEqual([type(d) for d in coverage.version_info], [int, int, int, str, int])
  55. self.assertIn(coverage.version_info[3], ['alpha', 'beta', 'candidate', 'final'])
  56. def test_make_version(self):
  57. self.assertEqual(_make_version(4, 0, 0, 'alpha', 0), "4.0a0")
  58. self.assertEqual(_make_version(4, 0, 0, 'alpha', 1), "4.0a1")
  59. self.assertEqual(_make_version(4, 0, 0, 'final', 0), "4.0")
  60. self.assertEqual(_make_version(4, 1, 2, 'beta', 3), "4.1.2b3")
  61. self.assertEqual(_make_version(4, 1, 2, 'final', 0), "4.1.2")
  62. self.assertEqual(_make_version(5, 10, 2, 'candidate', 7), "5.10.2rc7")
  63. def test_make_url(self):
  64. self.assertEqual(
  65. _make_url(4, 0, 0, 'final', 0),
  66. "https://coverage.readthedocs.io"
  67. )
  68. self.assertEqual(
  69. _make_url(4, 1, 2, 'beta', 3),
  70. "https://coverage.readthedocs.io/en/coverage-4.1.2b3"
  71. )
  72. class SetupPyTest(CoverageTest):
  73. """Tests of setup.py"""
  74. run_in_temp_dir = False
  75. def test_metadata(self):
  76. status, output = self.run_command_status(
  77. "python setup.py --description --version --url --author"
  78. )
  79. self.assertEqual(status, 0)
  80. out = output.splitlines()
  81. self.assertIn("measurement", out[0])
  82. self.assertEqual(out[1], coverage.__version__)
  83. self.assertEqual(out[2], coverage.__url__)
  84. self.assertIn("Ned Batchelder", out[3])
  85. def test_more_metadata(self):
  86. # Let's be sure we pick up our own setup.py
  87. # CoverageTest restores the original sys.path for us.
  88. sys.path.insert(0, '')
  89. from setup import setup_args
  90. classifiers = setup_args['classifiers']
  91. self.assertGreater(len(classifiers), 7)
  92. self.assert_starts_with(classifiers[-1], "Development Status ::")
  93. long_description = setup_args['long_description'].splitlines()
  94. self.assertGreater(len(long_description), 7)
  95. self.assertNotEqual(long_description[0].strip(), "")
  96. self.assertNotEqual(long_description[-1].strip(), "")