HTML report generation fails on too long path · Issue #580 · nedbat/coveragepy (original) (raw)

Originally reported by István Bede (Bitbucket: ibede, GitHub: ibede)


Hi!

I'm trying to generate HTML report on my coverage. Unfortunately I have very deep folder structure with some long directory and file names. The HTML generation tries to create an .html filename containing all this path, but on Windows 7 it fails if the file name is longer than 260 (NTFS). It reports an error like:

#!sh

---------- coverage: platform win32, python 2.7.13-final-0 -----------
Internal Error: runTests aborted: [Errno 2] No such file or directory: u'logs/coverage/_jenkins_work_XXXX_nightly_main_build_workspace_YYYYYYYYYYYYYYYYY-master_AAAAAAAAAAAAAAAAAAAAAAA_src_BBBBBBBBBBBBBBBBBBBB_CCCCCCCCCCCCCCCCCCCCCCC_py.html'

I made a small workaround/patch to make it work:
Added directory_depth config value in coverage.cfg:

#!ini

[html]
directory = logs/coverage/
directory_depth = 4

and patched html generation the following way in html.py:

#!python
    def html_file(self, fr, analysis):
        """Generate an HTML file for one source file."""
        relname = fr.relative_filename()
        if self.config.dir_depth > 0: # initialized with -1
            import re
            relname = relname[[m.start() for m in re.finditer('\\\\', relname)][relname.count('\\') - self.config.dir_depth - 1] + 1:]
        rootname = flat_rootname(relname)
        html_filename = rootname + ".html"
        html_path = os.path.join(self.directory, html_filename)

and of course I added dir_depth to CoverageConfig.

As a result, only the needed directory depth is included in the filename. I don't know if other OS or file system is problematic or not.

Note: maybe the annotate module is also affected as it uses the same mechanism for file name generation.

Thanks for the good work!