Debian Codegraph @

testsuite/pdfgrep.tests/cache.exp

raw
1 setenv XDG_CACHE_HOME "$pdfdir"
2
3 set cachedir "$pdfdir/pdfgrep/"
4
5 proc count_cache_files num {
6 global cachedir test
7 set len [llength [glob $cachedir/*]]
8 if {$len == $num} {
9 ppass $test
10 } else {
11 pfail "$test -- file count: $len"
12 }
13 }
14
15 ######################################################################
16
17 set test "cache creates file"
18
19 clear_pdfdir
20 set pdf [mkpdf pdf {
21 this is a test.
22 }]
23
24 pdfgrep --cache test $pdf
25 expect eof
26 count_cache_files 1
27
28 ######################################################################
29
30 set test "cache creates another file"
31
32 set pdf2 [mkpdf pdf2 {
33 this is another test.
34 }]
35
36 pdfgrep --cache test $pdf2
37 expect eof
38 count_cache_files 2
39
40 ######################################################################
41
42 set test "cache doesn't create new file for same content"
43
44 file copy $pdf2 $pdfdir/pdf3.pdf
45
46 pdfgrep --cache test $pdfdir/pdf3.pdf
47 expect eof
48 count_cache_files 2
49
50 ######################################################################
51
52 set test "cached call returns the same as original"
53
54 clear_pdfdir
55 set pdf [mkpdf pdf {
56 this is a test
57 }]
58
59 pdfgrep --cache test $pdf
60 expect eof
61
62 pdfgrep_expect --cache test $pdf "this is a test"
63
64 ######################################################################
65
66 set test "cached limit works"
67
68 clear_pdfdir
69 file mkdir $cachedir
70 for {set i 0} {$i < 10} {incr i} {
71 exec touch -t 199001010101 "$cachedir/foo$i"
72 }
73
74 set pdf [mkpdf pdf {
75 this is a test.
76 }]
77
78 setenv PDFGREP_CACHE_LIMIT 5
79
80 pdfgrep --cache test $pdf
81 expect eof
82
83 # 5 old ones and the one from the pdf
84 count_cache_files 6
85
86 ######################################################################
87
88 set test "cache works when XDG_CACHE_HOME is not set"
89
90 unsetenv XDG_CACHE_HOME
91 set homedir $env(HOME)
92 setenv HOME "$pdfdir"
93 set cachedir "$pdfdir/.cache/pdfgrep"
94
95
96 clear_pdfdir
97 set pdf [mkpdf pdf {
98 this is a test.
99 }]
100
101 pdfgrep --cache test $pdf
102 expect eof
103 count_cache_files 1
104
105 setenv HOME $homedir
106
107 ######################################################################
108
109 set test "warning if XDG_CACHE_HOME cannot be created"
110
111 setenv XDG_CACHE_HOME "$pdfdir/testdir/cache"
112
113 clear_pdfdir
114
115 set pdf [mkpdf pdf {
116 this is a test
117 }]
118
119 pdfgrep_expect_with_err --cache test $pdf \
120 ".*pdfgrep: warning: .*
121 this is a test"
122
123 ######################################################################
124
125 set test "warning if XDG_CACHE_HOME/pdfgrep cannot be created"
126
127 setenv XDG_CACHE_HOME "$pdfdir/cache"
128
129 # Create /cache as file instead of directory to make mkdir return EEXIST but
130 # prevent the creation of subdirs. Creating a readonly directory does not work
131 # in case tests are executed as root (relevant in containers).
132 exec touch "$pdfdir/cache"
133
134 pdfgrep_expect_with_err --cache test $pdf \
135 ".*pdfgrep: warning: .*
136 this is a test"
137
138 # TODO Find a way to test cache size limit without creating too many files
139
140 ######################################################################
141
142 set test "cache works for recursive search in subdirs"
143
144 # See https://gitlab.com/pdfgrep/pdfgrep/issues/7
145
146 clear_pdfdir
147
148 file mkdir $pdfdir/subdir
149 mkpdf pdf "foobar"
150 file rename $pdfdir/pdf.pdf $pdfdir/subdir/pdf.pdf
151
152 pdfgrep_expect --cache -r "foobar" $pdfdir \
153 "$pdfdir/subdir/pdf.pdf:foobar"
154
155 ######################################################################
156
157 set test "cache works with --warn-empty"
158
159 # See https://gitlab.com/pdfgrep/pdfgrep/issues/35
160
161 clear_pdfdir
162 set pdf [mkpdf pdf {
163 this is a test
164 }]
165
166 pdfgrep --cache --warn-empty test $pdf
167 expect eof
168
169 pdfgrep_expect --cache --warn-empty test $pdf "this is a test"
170
171 ######################################################################
172
173 set test "cache saves and restores page labels"
174
175 clear_pdfdir
176 set pdf [mkpdf foo {
177 \pagenumbering{roman}
178 first page
179 \newpage
180 second page
181 \newpage
182 third page
183 }]
184
185 pdfgrep_expect --cache --page-number=label page $pdf \
186 "i:first page
187 ii:second page
188 iii:third page"
189
190 expect_exit_status 0