def _get_file_reporter(self, morf): """Get a FileReporter for a module or filename.""" plugin = None if isinstance(morf, string_class): abs_morf = abs_file(morf) plugin_name = self.data.plugin_data().get(abs_morf) if plugin_name: plugin = self.plugins.get(plugin_name) if plugin: file_reporter = plugin.file_reporter(abs_morf) if file_reporter is None: raise CoverageException( "Plugin %r did not provide a file reporter for %r." % ( plugin._coverage_plugin_name, morf ) ) else: file_reporter = PythonFileReporter(morf, self) # The FileReporter can have a name attribute, but if it doesn't, we'll # supply it as the relative path to self.filename. if not hasattr(file_reporter, "name"): file_reporter.name = self.file_locator.relative_filename( file_reporter.filename ) return file_reporter
def _canonical_path(self, morf, directory=False): """Return the canonical path of the module or file `morf`. If the module is a package, then return its directory. If it is a module, then return its file, unless `directory` is True, in which case return its enclosing directory. """ morf_path = PythonFileReporter(morf, self).filename if morf_path.endswith("__init__.py") or directory: morf_path = os.path.split(morf_path)[0] return morf_path
def test_filenames(self): acu = PythonFileReporter("aa/afile.py") bcu = PythonFileReporter("aa/bb/bfile.py") ccu = PythonFileReporter("aa/bb/cc/cfile.py") self.assertEqual(acu.relative_filename(), "aa/afile.py") self.assertEqual(bcu.relative_filename(), "aa/bb/bfile.py") self.assertEqual(ccu.relative_filename(), "aa/bb/cc/cfile.py") self.assertEqual(acu.source(), "# afile.py\n") self.assertEqual(bcu.source(), "# bfile.py\n") self.assertEqual(ccu.source(), "# cfile.py\n")
def test_odd_filenames(self): acu = PythonFileReporter("aa/afile.odd.py") bcu = PythonFileReporter("aa/bb/bfile.odd.py") b2cu = PythonFileReporter("aa/bb.odd/bfile.py") self.assertEqual(acu.relative_filename(), "aa/afile.odd.py") self.assertEqual(bcu.relative_filename(), "aa/bb/bfile.odd.py") self.assertEqual(b2cu.relative_filename(), "aa/bb.odd/bfile.py") self.assertEqual(acu.source(), "# afile.odd.py\n") self.assertEqual(bcu.source(), "# bfile.odd.py\n") self.assertEqual(b2cu.source(), "# bfile.py\n")
def test_egg(self): # Test that we can get files out of eggs, and read their source files. # The egg1 module is installed by an action in igor.py. import egg1 import egg1.egg1 # Verify that we really imported from an egg. If we did, then the # __file__ won't be an actual file, because one of the "directories" # in the path is actually the .egg zip file. self.assert_doesnt_exist(egg1.__file__) ecu = PythonFileReporter(egg1) eecu = PythonFileReporter(egg1.egg1) self.assertEqual(ecu.source(), u"") self.assertEqual(eecu.source().split("\n")[0], u"# My egg file!")
def test_odd_filenames(self): acu = PythonFileReporter("aa/afile.odd.py") bcu = PythonFileReporter("aa/bb/bfile.odd.py") b2cu = PythonFileReporter("aa/bb.odd/bfile.py") self.assertEqual(acu.name, "aa/afile.odd.py") self.assertEqual(bcu.name, "aa/bb/bfile.odd.py") self.assertEqual(b2cu.name, "aa/bb.odd/bfile.py") self.assertEqual(acu.flat_rootname(), "aa_afile_odd_py") self.assertEqual(bcu.flat_rootname(), "aa_bb_bfile_odd_py") self.assertEqual(b2cu.flat_rootname(), "aa_bb_odd_bfile_py") self.assertEqual(acu.source(), "# afile.odd.py\n") self.assertEqual(bcu.source(), "# bfile.odd.py\n") self.assertEqual(b2cu.source(), "# bfile.py\n")
def test_filenames(self): acu = PythonFileReporter("aa/afile.py") bcu = PythonFileReporter("aa/bb/bfile.py") ccu = PythonFileReporter("aa/bb/cc/cfile.py") self.assertEqual(acu.name, "aa/afile.py") self.assertEqual(bcu.name, "aa/bb/bfile.py") self.assertEqual(ccu.name, "aa/bb/cc/cfile.py") self.assertEqual(acu.flat_rootname(), "aa_afile_py") self.assertEqual(bcu.flat_rootname(), "aa_bb_bfile_py") self.assertEqual(ccu.flat_rootname(), "aa_bb_cc_cfile_py") self.assertEqual(acu.source(), "# afile.py\n") self.assertEqual(bcu.source(), "# bfile.py\n") self.assertEqual(ccu.source(), "# cfile.py\n")
def test_modules(self): import aa import aa.bb import aa.bb.cc acu = PythonFileReporter(aa) bcu = PythonFileReporter(aa.bb) ccu = PythonFileReporter(aa.bb.cc) self.assertEqual(acu.relative_filename(), native("aa.py")) self.assertEqual(bcu.relative_filename(), native("aa/bb.py")) self.assertEqual(ccu.relative_filename(), native("aa/bb/cc.py")) self.assertEqual(acu.source(), "# aa\n") self.assertEqual(bcu.source(), "# bb\n") self.assertEqual(ccu.source(), "") # yes, empty
def test_module_files(self): import aa.afile import aa.bb.bfile import aa.bb.cc.cfile acu = PythonFileReporter(aa.afile) bcu = PythonFileReporter(aa.bb.bfile) ccu = PythonFileReporter(aa.bb.cc.cfile) self.assertEqual(acu.relative_filename(), native("aa/afile.py")) self.assertEqual(bcu.relative_filename(), native("aa/bb/bfile.py")) self.assertEqual(ccu.relative_filename(), native("aa/bb/cc/cfile.py")) self.assertEqual(acu.source(), "# afile.py\n") self.assertEqual(bcu.source(), "# bfile.py\n") self.assertEqual(ccu.source(), "# cfile.py\n")
def _canonical_dir(self, morf): """Return the canonical directory of the module or file `morf`.""" morf_filename = PythonFileReporter(morf, self).filename return os.path.split(morf_filename)[0]
def test_modules(self): import aa import aa.bb import aa.bb.cc acu = PythonFileReporter(aa) bcu = PythonFileReporter(aa.bb) ccu = PythonFileReporter(aa.bb.cc) self.assertEqual(acu.relative_filename(), native("aa/__init__.py")) self.assertEqual(bcu.relative_filename(), native("aa/bb/__init__.py")) self.assertEqual(ccu.relative_filename(), native("aa/bb/cc/__init__.py")) self.assertEqual(acu.source(), "# aa\n") self.assertEqual(bcu.source(), "# bb\n") self.assertEqual(ccu.source(), "") # yes, empty
def test_module_files(self): import aa.afile import aa.bb.bfile import aa.bb.cc.cfile acu = PythonFileReporter(aa.afile) bcu = PythonFileReporter(aa.bb.bfile) ccu = PythonFileReporter(aa.bb.cc.cfile) assert acu.relative_filename() == native("aa/afile.py") assert bcu.relative_filename() == native("aa/bb/bfile.py") assert ccu.relative_filename() == native("aa/bb/cc/cfile.py") assert acu.source() == "# afile.py\n" assert bcu.source() == "# bfile.py\n" assert ccu.source() == "# cfile.py\n"
def test_modules(self): import aa import aa.bb import aa.bb.cc acu = PythonFileReporter(aa) bcu = PythonFileReporter(aa.bb) ccu = PythonFileReporter(aa.bb.cc) assert acu.relative_filename() == native("aa/__init__.py") assert bcu.relative_filename() == native("aa/bb/__init__.py") assert ccu.relative_filename() == native("aa/bb/cc/__init__.py") assert acu.source() == "# aa\n" assert bcu.source() == "# bb\n" assert ccu.source() == "" # yes, empty
def test_odd_filenames(self): acu = PythonFileReporter("aa/afile.odd.py") bcu = PythonFileReporter("aa/bb/bfile.odd.py") b2cu = PythonFileReporter("aa/bb.odd/bfile.py") assert acu.relative_filename() == "aa/afile.odd.py" assert bcu.relative_filename() == "aa/bb/bfile.odd.py" assert b2cu.relative_filename() == "aa/bb.odd/bfile.py" assert acu.source() == "# afile.odd.py\n" assert bcu.source() == "# bfile.odd.py\n" assert b2cu.source() == "# bfile.py\n"
def test_filenames(self): acu = PythonFileReporter("aa/afile.py") bcu = PythonFileReporter("aa/bb/bfile.py") ccu = PythonFileReporter("aa/bb/cc/cfile.py") assert acu.relative_filename() == "aa/afile.py" assert bcu.relative_filename() == "aa/bb/bfile.py" assert ccu.relative_filename() == "aa/bb/cc/cfile.py" assert acu.source() == "# afile.py\n" assert bcu.source() == "# bfile.py\n" assert ccu.source() == "# cfile.py\n"