1212# *******************************************************************************
1313from collections import defaultdict
1414from collections .abc import Callable
15+ from gettext import find
1516from pathlib import Path
1617
1718import pytest
19+ import logging
1820from pytest import TempPathFactory
1921from src .extensions .score_source_code_linker .parse_source_files import (
20- GITHUB_BASE_URL ,
22+ get_github_base_url ,
23+ find_git_root ,
24+ get_github_repo_info ,
2125 extract_requirements ,
2226 get_git_hash ,
27+ parse_git_output ,
28+ logger as scl_logger ,
2329)
2430
2531
@@ -74,39 +80,42 @@ def dummy_git_hash_func(input: str) -> Callable[[str], str]:
7480
7581def test_extract_requirements (create_tmp_files : Path ):
7682 root_dir = create_tmp_files
77-
83+ github_base_url = get_github_base_url ()
7884 results_dict1 = extract_requirements (
79- str (root_dir / "testfile.txt" ), dummy_git_hash_func ("no-hash" )
85+ str (root_dir / "testfile.txt" ), github_base_url , dummy_git_hash_func ("no-hash" )
8086 )
8187 expected_dict1 : dict [str , list [str ]] = defaultdict (list )
8288 expected_dict1 ["TEST_REQ__LINKED_ID" ].append (
83- f"{ GITHUB_BASE_URL } no-hash/{ root_dir } /testfile.txt#L7"
89+ f"{ github_base_url } /blob/ no-hash/{ root_dir } /testfile.txt#L7"
8490 )
8591 expected_dict1 ["TEST_REQ__LINKED_TRACE" ].append (
86- f"{ GITHUB_BASE_URL } no-hash/{ root_dir } /testfile.txt#L11"
92+ f"{ github_base_url } /blob/ no-hash/{ root_dir } /testfile.txt#L11"
8793 )
8894
8995 # Assumed random hash here to test if passed correctly
9096 results_dict2 = extract_requirements (
9197 str (root_dir / "testfile2.txt" ),
98+ github_base_url ,
9299 dummy_git_hash_func ("aacce4887ceea1f884135242a8c182db1447050" ),
93100 )
94101 expected_dict2 : dict [str , list [str ]] = defaultdict (list )
95102 expected_dict2 ["TEST_REQ__LINKED_DIFFERENT_FILE" ].append (
96- f"{ GITHUB_BASE_URL } aacce4887ceea1f884135242a8c182db1447050/{ root_dir } /testfile2.txt#L3"
103+ f"{ github_base_url } /blob/ aacce4887ceea1f884135242a8c182db1447050/{ root_dir } /testfile2.txt#L3"
97104 )
98105
99- results_dict3 = extract_requirements (str (root_dir / "testfile3.txt" ))
106+ results_dict3 = extract_requirements (
107+ str (root_dir / "testfile3.txt" ), github_base_url
108+ )
100109 expected_dict3 : dict [str , list [str ]] = defaultdict (list )
101110
102111 # if there is no git-hash returned from command.
103112 # This happens if the file is new and not committed yet.
104113 results_dict4 = extract_requirements (
105- str (root_dir / "testfile2.txt" ), dummy_git_hash_func ("" )
114+ str (root_dir / "testfile2.txt" ), github_base_url , dummy_git_hash_func ("" )
106115 )
107116 expected_dict4 : dict [str , list [str ]] = defaultdict (list )
108117 expected_dict4 ["TEST_REQ__LINKED_DIFFERENT_FILE" ].append (
109- f"{ GITHUB_BASE_URL } /{ root_dir } /testfile2.txt#L3"
118+ f"{ github_base_url } /blob/ /{ root_dir } /testfile2.txt#L3"
110119 )
111120
112121 assert results_dict1 == expected_dict1
@@ -118,3 +127,62 @@ def test_extract_requirements(create_tmp_files: Path):
118127def test_get_git_hash ():
119128 assert get_git_hash ("testfile.x" ) == "file_not_found"
120129 assert get_git_hash ("" ) == "file_not_found"
130+
131+
132+ # These tests aren't great / exhaustive, but an okay first step into the right direction.
133+
134+
135+ def test_get_github_repo_info ():
136+ # I'd argue the happy path is tested with the other ones?
137+ with pytest .raises (AssertionError ):
138+ get_github_repo_info (Path ("." ))
139+
140+
141+ git_test_data_ok = [
142+ (
143+ "origin https://github.com/eclipse-score/test-repo.git (fetch)" ,
144+ "eclipse-score/test-repo" ,
145+ ),
146+ (
147+ "origin git@github.com:eclipse-score/test-repo.git (fetch)" ,
148+ "eclipse-score/test-repo" ,
149+ ),
150+ ("origin git@github.com:eclipse-score/test-repo.git" , "eclipse-score/test-repo" ),
151+ ("upstream git@github.com:upstream/repo.git (fetch)" , "upstream/repo" ),
152+ ]
153+
154+
155+ @pytest .mark .parametrize ("input,output" , git_test_data_ok )
156+ def test_parse_git_output_ok (input , output ):
157+ assert output == parse_git_output (input )
158+
159+
160+ git_test_data_bad = [
161+ ("origin " , "" ),
162+ (
163+ " " ,
164+ "" ,
165+ ),
166+ ]
167+
168+
169+ @pytest .mark .parametrize ("input,output" , git_test_data_bad )
170+ def test_parse_git_output_bad (caplog , input , output ):
171+ with caplog .at_level (logging .WARNING , logger = scl_logger .name ):
172+ result = parse_git_output (input )
173+ assert len (caplog .messages ) == 1
174+ assert caplog .records [0 ].levelname == "WARNING"
175+ assert (
176+ f"Got wrong input line from 'get_github_repo_info'. Input: { input } . Expected example: 'origin git@github.com:user/repo.git'"
177+ in caplog .records [0 ].message
178+ )
179+ assert output == result
180+
181+
182+ def test_get_github_base_url ():
183+ # Not really a great test imo.
184+ git_root = find_git_root ()
185+ repo = get_github_repo_info (git_root )
186+ expected = f"https://github.com/{ repo } "
187+ actual = get_github_base_url ()
188+ assert expected == actual
0 commit comments