Skip to content

Commit

Permalink
Added unit tests for processPrefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Philippe Lapointe (Riedel) authored and RPGillespie6 committed Jan 30, 2024
1 parent a002170 commit c80ad2b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 4 additions & 1 deletion fastcov.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ def processPrefix(path, prefix, prefix_strip):
p = p.joinpath(s)

if len(prefix) > 0:
p = Path(prefix).joinpath(p)
if p.is_absolute():
p = Path(prefix).joinpath(p.relative_to('/'))
else:
p = Path(prefix).joinpath(p)

return str(p)

Expand Down
38 changes: 38 additions & 0 deletions test/unit/test_gcov_prefix_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""
Author: Jean-Philippe Lapointe
Make sure fastcov's gcov prefix options are processing the file paths as expected
"""

import pytest
import fastcov

from dataclasses import dataclass
import os

@dataclass
class TestSet:
__test__ = False
path: str
prefix: str
prefix_strip: int
expected_result: str


def test_simpleStripping():
testPatterns = [
# Making the path relative to the root of the git repo
TestSet('/home/user1/work/git/repo/subdir/to/some/file.cpp', '', 5, 'subdir/to/some/file.cpp'),
# Essentially changing user directory from user1 to user2
TestSet('/home/user1/work/git/repo/subdir/to/some/file.cpp', '/home/user2', 2, '/home/user2/work/git/repo/subdir/to/some/file.cpp'),
# Relative path, shouldn't get modified.
TestSet('subdir/to/some/file.cpp', '/home/user2', 2, 'subdir/to/some/file.cpp'),
# Current file should exist, it won't get messed with
TestSet(os.path.abspath(__file__), '/home/user2', 1, os.path.abspath(__file__)),
# Just prefixing an already absolute path
TestSet('/usr/include/someUnknownHeaderFile.h', '/home/user2/work/git/repo', 0, '/home/user2/work/git/repo/usr/include/someUnknownHeaderFile.h')
]

for elem in testPatterns:
assert(fastcov.processPrefix(elem.path, elem.prefix, elem.prefix_strip) == elem.expected_result)

0 comments on commit c80ad2b

Please sign in to comment.