Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_diff__format_html__no_source_on_disk():
from pycobertura.cli import diff
from pycobertura.filesystem import FileSystem
runner = CliRunner()
pytest.raises(FileSystem.FileNotFound, runner.invoke, diff, [
'--format', 'html',
'tests/dummy.with-dummy2-better-cov.xml',
'tests/dummy.with-dummy2-better-and-worse.xml',
], catch_exceptions=False)
# FIXME: make this O(1)
return self.real_filename(filename) in self.zipfile.namelist()
@contextmanager
def open(self, filename):
filename = self.real_filename(filename)
try:
f = self.zipfile.open(filename)
yield f
f.close()
except KeyError:
raise self.FileNotFound(filename)
class GitFileSystem(FileSystem):
def __init__(self, repo_folder, ref):
self.repository = repo_folder
self.ref = ref
self.repository_root = self._get_root_path(repo_folder)
# the report may have been collected in a subfolder of the repository
# root. Each file path shall thus be completed by the prefix.
self.prefix = self.repository.replace(self.repository_root, "").lstrip("/")
def real_filename(self, filename):
prefix = "{}/".format(self.prefix) if self.prefix else ""
return "{ref}:{prefix}{filename}".format(
prefix=prefix, ref=self.ref, filename=filename
)
def has_file(self, filename):
command = "git --no-pager show {}".format(self.real_filename(filename))
def open(self, filename):
"""
Yield a file-like object for file `filename`.
This function is a context manager.
"""
filename = self.real_filename(filename)
if not os.path.exists(filename):
raise self.FileNotFound(filename)
with codecs.open(filename, encoding="utf-8") as f:
yield f
class ZipFileSystem(FileSystem):
def __init__(self, zip_file, source_prefix=None):
self.zipfile = zipfile.ZipFile(zip_file)
self.source_prefix = source_prefix
def real_filename(self, filename):
if self.source_prefix is not None:
filename = os.path.join(self.source_prefix, filename)
return filename
def has_file(self, filename):
# FIXME: make this O(1)
return self.real_filename(filename) in self.zipfile.namelist()
@contextmanager
def open(self, filename):
import io
import zipfile
import subprocess
import shlex
from contextlib import contextmanager
class FileSystem(object):
class FileNotFound(Exception):
def __init__(self, path):
super(self.__class__, self).__init__(path)
self.path = path
class DirectoryFileSystem(FileSystem):
def __init__(self, source_dir, source_prefix=None):
self.source_dir = source_dir
self.source_prefix = source_prefix
def real_filename(self, filename):
if self.source_prefix is not None:
filename = os.path.join(self.source_prefix, filename)
return os.path.join(self.source_dir, filename)
def has_file(self, filename):
# FIXME: make this O(1)
filename = self.real_filename(filename)
return os.path.isfile(filename)
@contextmanager
def open(self, filename):