From 63b3f25b330ea318cb2bb6c2ce7237a89ba2c028 Mon Sep 17 00:00:00 2001 From: cclecle Date: Sun, 19 Mar 2023 10:03:50 +0000 Subject: [PATCH] fix quality warning + commit messages unittest --- src/pygitversionhelper/gitversionhelper.py | 28 +++++++++++----------- test/test_gitversionhelper.py | 11 ++++++--- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/pygitversionhelper/gitversionhelper.py b/src/pygitversionhelper/gitversionhelper.py index 9f70d58..9bbcef3 100644 --- a/src/pygitversionhelper/gitversionhelper.py +++ b/src/pygitversionhelper/gitversionhelper.py @@ -53,7 +53,7 @@ def _exec(cmd: str, root: str | os.PathLike | None = None, raw:bool = False) -> raise gitversionhelper.unknownGITFatalError(p.stderr) if int(p.returncode) < 0: #pragma: nocover raise gitversionhelper.unknownGITError(p.stderr) - + if raw: return p.stdout lines = p.stdout.splitlines() @@ -126,7 +126,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods """ tag not found exception """ - + @classmethod def getMessagesSinceTag(cls,tag:str,**kwargs) -> str: """ @@ -140,38 +140,38 @@ class gitversionhelper: # pylint: disable=too-few-public-methods """ current_commit_id=cls.getLast(**kwargs) tag_commit_id=cls.getFromTag(tag,**kwargs) - - if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)): + + if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)): commits = _exec(f"git rev-list --first-parent --ancestry-path {tag_commit_id}..{current_commit_id}") else: commits = _exec(f"git rev-list --ancestry-path {tag_commit_id}..{current_commit_id}") result=[] for commit in commits: result.append(cls.getMessage(commit,**kwargs)) - - if ((cls.__OptDict["merged_output"] in kwargs) and (kwargs[cls.__OptDict["merged_output"]] is True)): + + if ((cls.__OptDict["merged_output"] in kwargs) and (kwargs[cls.__OptDict["merged_output"]] is True)): print("JOIN") return os.linesep.join(result) return result - + @classmethod - def getMessage(cls, id:str, **kwargs) -> str: + def getMessage(cls, hash:str) -> str: """ retrieve a commit message from repository Args: - id: id of the commit + hash: id of the commit Returns: the commit message """ try: - res=_exec(f"git log -z --pretty=tformat:%B%-C() -n 1 {id}",None,True).rstrip('\x00') + res=_exec(f"git log -z --pretty=tformat:%B%-C() -n 1 {hash}",None,True).rstrip('\x00') except gitversionhelper.unknownGITFatalError as _e: raise cls.commitNotFound("no commit found in commit history") from _e return res.replace('\r\n','\n').replace('\n','\r\n') - + @classmethod - def getFromTag(cls,tag:str,**kwargs) -> str: + def getFromTag(cls,tag:str) -> str: """ retrieve a commit from repository associated to a tag Args: @@ -196,14 +196,14 @@ class gitversionhelper: # pylint: disable=too-few-public-methods Returns: the commit Id """ - if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)): + if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)): try: res = _exec("git rev-parse HEAD") except gitversionhelper.unknownGITFatalError as _e: raise cls.commitNotFound("no commit found in commit history") from _e else: res = _exec("git for-each-ref --sort=-committerdate refs/heads/ --count 1 --format=%(objectname)") - + if len(res)==0: raise cls.commitNotFound("no commit found in commit history") return res[0] diff --git a/test/test_gitversionhelper.py b/test/test_gitversionhelper.py index 0d7db2b..ed0af02 100644 --- a/test/test_gitversionhelper.py +++ b/test/test_gitversionhelper.py @@ -1235,7 +1235,7 @@ class Test_gitversionhelper(unittest.TestCase): pygitversionhelper.gitversionhelper.commit.getFromTag("TAG") def test_nominal__commit_getMessage(self): - commit_message="AAAABBB CCCCDDDD" + commit_message="AAAABBB CCCCDDDD".replace('\r\n','\n').replace('\n','\r\n') with open("demofile.txt", "w+t") as tmpFile: tmpFile.write("testvalue") os.system("git add .") @@ -1247,7 +1247,8 @@ class Test_gitversionhelper(unittest.TestCase): def test_nominal__commit_getMessage2(self): commit_message="""AAAABBB CCCCDDDD - -f dfsds dfsdfs $""" + -f dfsds dfsdfs $""".replace('\r\n','\n').replace('\n','\r\n') + with open("demofile.txt", "w+t") as tmpFile: tmpFile.write("testvalue") os.system("git add .") @@ -1258,13 +1259,14 @@ class Test_gitversionhelper(unittest.TestCase): commit = pygitversionhelper.gitversionhelper.commit.getLast() message = pygitversionhelper.gitversionhelper.commit.getMessage(commit) - print(message) + self.assertEqual(message,commit_message) def test_nominal__commit_getMessagesSinceLastTag(self): commit_message1="1.1 update this"+ os.linesep+\ "1.1 fix that" + os.linesep+\ "1.1 test" + commit_message1=commit_message1.replace('\r\n','\n').replace('\n','\r\n') with open("demofile.txt", "w+t") as tmpFile: tmpFile.write("testvalue") os.system("git add .") @@ -1277,6 +1279,7 @@ class Test_gitversionhelper(unittest.TestCase): commit_message2="2.1 update this"+ os.linesep+\ "2.1 fix that" + os.linesep+\ "2.1 test" + commit_message2=commit_message2.replace('\r\n','\n').replace('\n','\r\n') with open("demofile.txt", "w+t") as tmpFile: tmpFile.write("testvalue2") os.system("git add .") @@ -1288,6 +1291,7 @@ class Test_gitversionhelper(unittest.TestCase): commit_message3="3.1 update this"+ os.linesep+\ "3.1 fix that" + os.linesep+\ "3.1 test" + commit_message3=commit_message3.replace('\r\n','\n').replace('\n','\r\n') with open("demofile.txt", "w+t") as tmpFile: tmpFile.write("testvalue3") os.system("git add .") @@ -1299,6 +1303,7 @@ class Test_gitversionhelper(unittest.TestCase): commit_message4="4.1 update this" + os.linesep+\ "4.1 fix that" + os.linesep+\ "4.1 test" + commit_message4=commit_message4.replace('\r\n','\n').replace('\n','\r\n') with open("demofile.txt", "w+t") as tmpFile: tmpFile.write("testvalue4") os.system("git add .")