5
5
# pylint: disable=redefined-outer-name
6
6
7
7
8
+ import os
8
9
from pathlib import Path
9
10
from unittest .mock import MagicMock
10
11
18
19
@pytest .fixture ()
19
20
def analyzer (tmp_path : Path ) -> TyposquattingPresenceAnalyzer :
20
21
"""Pytest fixture to create a TyposquattingPresenceAnalyzer instance with a dummy popular packages file."""
21
- # create a dummy popular packages file
22
- pkg_file = tmp_path / "popular.txt"
22
+ # Create a dummy popular packages file.
23
+ pkg_file = Path ( os . path . join ( tmp_path , "popular.txt" ))
23
24
popular_packages = ["requests" , "flask" , "pytest" ]
24
- pkg_file .write_text ("\n " .join (popular_packages ))
25
+ pkg_file .write_text ("\n " .join (popular_packages ), encoding = "utf-8" )
25
26
analyzer_instance = TyposquattingPresenceAnalyzer (str (pkg_file ))
26
27
return analyzer_instance
27
28
@@ -41,8 +42,8 @@ def test_analyze_similar_name_fail(analyzer: TyposquattingPresenceAnalyzer, pypi
41
42
assert result == HeuristicResult .FAIL
42
43
assert info ["package_name" ] == "reqursts"
43
44
assert info ["popular_package" ] == "requests"
44
- # ratio should match or exceed threshold 0.95
45
- assert isinstance (info ["similarity_ratio" ], ( int , float ) )
45
+ # The ratio should match or exceed threshold.
46
+ assert isinstance (info ["similarity_ratio" ], float )
46
47
assert info ["similarity_ratio" ] >= analyzer .distance_ratio_threshold
47
48
48
49
@@ -62,22 +63,24 @@ def test_analyze_nonexistent_file_skip() -> None:
62
63
63
64
64
65
@pytest .mark .parametrize (
65
- ("s1 " , "s2 " , "expected " ),
66
+ ("package1 " , "package2 " , "expected_ratio " ),
66
67
[
67
68
("requests" , "requests" , 1.0 ),
68
69
("reqursts" , "requests" , 11 / 12 ),
69
70
("abcd" , "wxyz" , 0.0 ),
70
71
],
71
72
)
72
- def test_jaro_distance (analyzer : TyposquattingPresenceAnalyzer , s1 : str , s2 : str , expected : float ) -> None :
73
+ def test_jaro_distance (
74
+ analyzer : TyposquattingPresenceAnalyzer , package1 : str , package2 : str , expected_ratio : float
75
+ ) -> None :
73
76
"""Test the Jaro distance calculation."""
74
- assert analyzer .jaro_distance (s1 , s2 ) == expected
77
+ assert analyzer .jaro_distance (package1 , package2 ) == expected_ratio
75
78
76
79
77
80
def test_empty_popular_packages_file (tmp_path : Path , pypi_package_json : MagicMock ) -> None :
78
81
"""Test the analyzer skips when the popular packages file is empty."""
79
- pkg_file = tmp_path / "empty_popular.txt"
80
- pkg_file .write_text ("" )
82
+ pkg_file = Path ( os . path . join ( tmp_path , "empty_popular.txt" ))
83
+ pkg_file .write_text ("" , encoding = "utf-8" )
81
84
analyzer_instance = TyposquattingPresenceAnalyzer (str (pkg_file ))
82
85
result , info = analyzer_instance .analyze (pypi_package_json )
83
86
assert result == HeuristicResult .SKIP
0 commit comments