blob: 88d615ab7c3e97596fa2f8cf132dc494db15500d [file] [log] [blame] [edit]
import contextlib
import pathlib
import tempfile
import unittest
from cq.scripts.helpers import coverage_utils
class CoverageUtilsTest(unittest.TestCase):
def setUp(self):
super().setUp()
with contextlib.ExitStack() as stack:
self._temp_dir_path = pathlib.Path(
stack.enter_context(tempfile.TemporaryDirectory()))
self.addCleanup(stack.pop_all().close)
self._workspace_path = self._temp_dir_path / 'workspace'
self._workspace_path.mkdir()
def test_rewrite_file_paths_in_lcov_with_rbe_prefix(self):
real_file = self._workspace_path / 'foo'
real_file.touch()
input_file = self._temp_dir_path / 'input.lcov'
input_file.write_text('\n'.join(_lcov_record('/b/f/w/foo')))
output_file = self._temp_dir_path / 'output.lcov'
coverage_utils.rewrite_file_paths_in_lcov(
input_file, output_file, self._workspace_path)
expected_contents = '\n'.join(_lcov_record('foo'))
self.assertEqual(output_file.read_text(), expected_contents)
def test_rewrite_file_paths_in_lcov_with_symlink(self):
real_file = self._workspace_path / 'real'
real_file.touch()
symlink = self._workspace_path / 'symlink'
symlink.symlink_to(real_file)
input_file = self._temp_dir_path / 'input.lcov'
input_file.write_text('\n'.join(_lcov_record(symlink)))
output_file = self._temp_dir_path / 'output.lcov'
coverage_utils.rewrite_file_paths_in_lcov(
input_file, output_file, self._workspace_path)
expected_contents = '\n'.join(_lcov_record('real'))
self.assertEqual(output_file.read_text(), expected_contents)
def test_rewrite_file_paths_in_lcov_with_rbe_symlink(self):
real_file = self._workspace_path / 'real'
real_file.touch()
symlink = self._workspace_path / 'symlink'
symlink.symlink_to(real_file)
input_file = self._temp_dir_path / 'input.lcov'
input_file.write_text('\n'.join(_lcov_record('/b/f/w/symlink')))
output_file = self._temp_dir_path / 'output.lcov'
coverage_utils.rewrite_file_paths_in_lcov(
input_file, output_file, self._workspace_path)
expected_contents = '\n'.join(_lcov_record('real'))
self.assertEqual(output_file.read_text(), expected_contents)
def test_rewrite_file_paths_in_lcov_with_absolute_path(self):
real_file = self._workspace_path / 'real'
real_file.touch()
input_file = self._temp_dir_path / 'input.lcov'
input_file.write_text('\n'.join(_lcov_record(real_file)))
output_file = self._temp_dir_path / 'output.lcov'
coverage_utils.rewrite_file_paths_in_lcov(
input_file, output_file, self._workspace_path)
expected_contents = '\n'.join(_lcov_record('real'))
self.assertEqual(output_file.read_text(), expected_contents)
def test_rewrite_file_paths_in_lcov_with_absolute_missing_path(self):
real_file = self._workspace_path / 'real'
input_file = self._temp_dir_path / 'input.lcov'
input_file.write_text('\n'.join(_lcov_record(real_file)))
output_file = self._temp_dir_path / 'output.lcov'
coverage_utils.rewrite_file_paths_in_lcov(
input_file, output_file, self._workspace_path)
expected_contents = '\n'.join(_lcov_record('real'))
self.assertEqual(output_file.read_text(), expected_contents)
def test_rewrite_file_paths_in_lcov_with_relative_path(self):
real_file = self._workspace_path / 'real'
real_file.touch()
input_file = self._temp_dir_path / 'input.lcov'
input_file.write_text('\n'.join(_lcov_record('real')))
output_file = self._temp_dir_path / 'output.lcov'
with self.assertRaises(ValueError):
coverage_utils.rewrite_file_paths_in_lcov(
input_file, output_file, self._workspace_path)
def _lcov_record(path):
return [
f'SF:{path}',
'LH:10',
'LC:22',
'end_of_record',
'',
]
if __name__ == '__main__':
unittest.main()