From c80ad2b9506044e5c908bf811382f00ccef8235e Mon Sep 17 00:00:00 2001 From: "Jean-Philippe Lapointe (Riedel)" Date: Tue, 30 Jan 2024 11:11:24 -0500 Subject: [PATCH] Added unit tests for processPrefix. --- fastcov.py | 5 +++- test/unit/test_gcov_prefix_options.py | 38 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/unit/test_gcov_prefix_options.py diff --git a/fastcov.py b/fastcov.py index e2ac46c..9b8e0e0 100755 --- a/fastcov.py +++ b/fastcov.py @@ -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) diff --git a/test/unit/test_gcov_prefix_options.py b/test/unit/test_gcov_prefix_options.py new file mode 100644 index 0000000..45d0aaf --- /dev/null +++ b/test/unit/test_gcov_prefix_options.py @@ -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)