1
+ import hashlib
1
2
import sys
2
3
3
4
from path import Path
4
5
5
6
sys .path .insert (0 , "." )
6
7
7
-
8
8
from pdf_white_cut .logger import logger
9
9
from pdf_white_cut .worker import cut_pdf
10
10
@@ -21,15 +21,21 @@ def __init__(self, input: str, ignore: int = 0) -> None:
21
21
self .ignore = ignore
22
22
23
23
@property
24
- def input_path (self ):
24
+ def input_path (self ) -> Path :
25
25
return Path ("./{}" .format (self .input ))
26
26
27
27
@property
28
- def output_path (self ):
28
+ def output_path (self ) -> Path :
29
29
output = Path ("./output/{}" .format (self .input ))
30
30
output .dirname ().makedirs_p ()
31
31
return output
32
32
33
+ @property
34
+ def wanted_path (self ) -> Path :
35
+ d , base = self .input_path .dirname (), self .input_path .basename ()
36
+ wanted = d / "wanted" / base
37
+ return wanted
38
+
33
39
34
40
def test_regression () -> None :
35
41
cases = [
@@ -52,9 +58,26 @@ def test_bugfix_cut_again() -> None:
52
58
cut_pdf (case .input_path , case .output_path , case .ignore )
53
59
54
60
61
+ def check_case (case : Case ):
62
+ if case .wanted_path .exists ():
63
+ logger .info ("assert for case {} {}" , case .output_path , case .wanted_path )
64
+ # TODO: hash compare is not a good solution
65
+ return file_hash (case .output_path ) == file_hash (case .wanted_path )
66
+ return True
67
+
68
+
69
+ def file_hash (filepath , hash_func = hashlib .md5 ):
70
+ hasher = hash_func ()
71
+ with open (filepath , 'rb' ) as f :
72
+ for chunk in iter (lambda : f .read (4096 ), b"" ): # 逐块读取
73
+ hasher .update (chunk )
74
+ return hasher .hexdigest ()
75
+
76
+
55
77
def test_bugfix_rotate () -> None :
56
78
base = Path ("cases/bugfix/rotate" )
57
79
cases = [Case (i ) for i in base .files ("*.pdf" )]
58
80
59
81
for case in cases :
60
82
cut_pdf (case .input_path , case .output_path , case .ignore )
83
+ check_case (case )
0 commit comments