test_filereporter.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 for FileReporters"""
  4. import os
  5. import sys
  6. from coverage.plugin import FileReporter
  7. from coverage.python import PythonFileReporter
  8. from tests.coveragetest import CoverageTest
  9. # pylint: disable=import-error
  10. # Unable to import 'aa' (No module named aa)
  11. def native(filename):
  12. """Make `filename` into a native form."""
  13. return filename.replace("/", os.sep)
  14. class FileReporterTest(CoverageTest):
  15. """Tests for FileReporter classes."""
  16. run_in_temp_dir = False
  17. def setUp(self):
  18. super(FileReporterTest, self).setUp()
  19. # Parent class saves and restores sys.path, we can just modify it.
  20. testmods = self.nice_file(os.path.dirname(__file__), 'modules')
  21. sys.path.append(testmods)
  22. def test_filenames(self):
  23. acu = PythonFileReporter("aa/afile.py")
  24. bcu = PythonFileReporter("aa/bb/bfile.py")
  25. ccu = PythonFileReporter("aa/bb/cc/cfile.py")
  26. self.assertEqual(acu.relative_filename(), "aa/afile.py")
  27. self.assertEqual(bcu.relative_filename(), "aa/bb/bfile.py")
  28. self.assertEqual(ccu.relative_filename(), "aa/bb/cc/cfile.py")
  29. self.assertEqual(acu.source(), "# afile.py\n")
  30. self.assertEqual(bcu.source(), "# bfile.py\n")
  31. self.assertEqual(ccu.source(), "# cfile.py\n")
  32. def test_odd_filenames(self):
  33. acu = PythonFileReporter("aa/afile.odd.py")
  34. bcu = PythonFileReporter("aa/bb/bfile.odd.py")
  35. b2cu = PythonFileReporter("aa/bb.odd/bfile.py")
  36. self.assertEqual(acu.relative_filename(), "aa/afile.odd.py")
  37. self.assertEqual(bcu.relative_filename(), "aa/bb/bfile.odd.py")
  38. self.assertEqual(b2cu.relative_filename(), "aa/bb.odd/bfile.py")
  39. self.assertEqual(acu.source(), "# afile.odd.py\n")
  40. self.assertEqual(bcu.source(), "# bfile.odd.py\n")
  41. self.assertEqual(b2cu.source(), "# bfile.py\n")
  42. def test_modules(self):
  43. import aa
  44. import aa.bb
  45. import aa.bb.cc
  46. acu = PythonFileReporter(aa)
  47. bcu = PythonFileReporter(aa.bb)
  48. ccu = PythonFileReporter(aa.bb.cc)
  49. self.assertEqual(acu.relative_filename(), native("aa.py"))
  50. self.assertEqual(bcu.relative_filename(), native("aa/bb.py"))
  51. self.assertEqual(ccu.relative_filename(), native("aa/bb/cc.py"))
  52. self.assertEqual(acu.source(), "# aa\n")
  53. self.assertEqual(bcu.source(), "# bb\n")
  54. self.assertEqual(ccu.source(), "") # yes, empty
  55. def test_module_files(self):
  56. import aa.afile
  57. import aa.bb.bfile
  58. import aa.bb.cc.cfile
  59. acu = PythonFileReporter(aa.afile)
  60. bcu = PythonFileReporter(aa.bb.bfile)
  61. ccu = PythonFileReporter(aa.bb.cc.cfile)
  62. self.assertEqual(acu.relative_filename(), native("aa/afile.py"))
  63. self.assertEqual(bcu.relative_filename(), native("aa/bb/bfile.py"))
  64. self.assertEqual(ccu.relative_filename(), native("aa/bb/cc/cfile.py"))
  65. self.assertEqual(acu.source(), "# afile.py\n")
  66. self.assertEqual(bcu.source(), "# bfile.py\n")
  67. self.assertEqual(ccu.source(), "# cfile.py\n")
  68. def test_comparison(self):
  69. acu = FileReporter("aa/afile.py")
  70. acu2 = FileReporter("aa/afile.py")
  71. zcu = FileReporter("aa/zfile.py")
  72. bcu = FileReporter("aa/bb/bfile.py")
  73. assert acu == acu2 and acu <= acu2 and acu >= acu2
  74. assert acu < zcu and acu <= zcu and acu != zcu
  75. assert zcu > acu and zcu >= acu and zcu != acu
  76. assert acu < bcu and acu <= bcu and acu != bcu
  77. assert bcu > acu and bcu >= acu and bcu != acu
  78. def test_egg(self):
  79. # Test that we can get files out of eggs, and read their source files.
  80. # The egg1 module is installed by an action in igor.py.
  81. import egg1
  82. import egg1.egg1
  83. # Verify that we really imported from an egg. If we did, then the
  84. # __file__ won't be an actual file, because one of the "directories"
  85. # in the path is actually the .egg zip file.
  86. self.assert_doesnt_exist(egg1.__file__)
  87. ecu = PythonFileReporter(egg1)
  88. eecu = PythonFileReporter(egg1.egg1)
  89. self.assertEqual(ecu.source(), u"")
  90. self.assertIn(u"# My egg file!", eecu.source().splitlines())