| 1 |
# This file contains functions that can be used by all test scripts in |
| 2 |
# pdfgrep.tests. |
| 3 |
# |
| 4 |
# It is only loaded once for all tests. |
| 5 |
|
| 6 |
######################################## |
| 7 |
### Running pdfgrep #################### |
| 8 |
######################################## |
| 9 |
|
| 10 |
# Don't remove null bytes by default. pdfgrep should not have them in the output |
| 11 |
# unless explicitly told so. And then we want to be able to test for them. |
| 12 |
remove_nulls -d 0 |
| 13 |
|
| 14 |
set pdfgrep_path [file normalize ../src/pdfgrep] |
| 15 |
|
| 16 |
# Set LC_ALL to "C" to avoid locale settings influencing the test results |
| 17 |
set env(LC_ALL) "C" |
| 18 |
|
| 19 |
# Just spawns pdfgrep with the given arguments |
| 20 |
proc pdfgrep args { |
| 21 |
global spawn_id pdfgrep_path |
| 22 |
spawn $pdfgrep_path {*}$args |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
# Spawns pdfgrep and fail if pdfgrep prints something on stderr. |
| 27 |
proc pdfgrep_noerr args { |
| 28 |
global spawn_id test |
| 29 |
pdfgrep {*}$args |
| 30 |
expect_before { |
| 31 |
-re "^pdfgrep:" {pfail "$test -- error from pdfgrep"; expect eof} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
# The version of poppler that this pdfgrep is built against. |
| 36 |
# Format [Major Minor Patchlevel] |
| 37 |
# |
| 38 |
# You can disable all poppler version checks (for example, if you have a patched |
| 39 |
# poppler installed) by setting the variable $disable_poppler_version_check to |
| 40 |
# true. |
| 41 |
set poppler_version {0 0 0} |
| 42 |
|
| 43 |
# Is pdfgrep compiled with pcre support? |
| 44 |
set have_pcre false |
| 45 |
|
| 46 |
# Is pdfgrep compiled with libunac support? |
| 47 |
set have_unac false |
| 48 |
|
| 49 |
# Parse the output of pdfgrep --version, to get the configuration parameters. |
| 50 |
log_user 0 |
| 51 |
pdfgrep --version |
| 52 |
while {1} { |
| 53 |
expect { |
| 54 |
-re "Using poppler version (\[^\r\]+)\r\n" { |
| 55 |
set poppler_version [split $expect_out(1,string) "."] |
| 56 |
} |
| 57 |
-re "Using libunac version \[^\r\]*\r\n" { |
| 58 |
set have_unac true |
| 59 |
} |
| 60 |
-re "Using libpcre2 version \[^\r\]*\r\n" { |
| 61 |
set have_pcre true |
| 62 |
} |
| 63 |
eof break |
| 64 |
} |
| 65 |
} |
| 66 |
log_user 1 |
| 67 |
|
| 68 |
# This can be set in test scripts to generate UNSUPPORTED test results, if the |
| 69 |
# actual poppler version is less than the required one. |
| 70 |
set required_poppler_version {0 0 0} |
| 71 |
|
| 72 |
# This can be set in test scripts to generate UNSUPPORTED test results, if |
| 73 |
# pdfgrep is not build with libpcre support. |
| 74 |
set requires_pcre_support false |
| 75 |
|
| 76 |
# This can be set in test scripts to generate UNSUPPORTED test results, if |
| 77 |
# pdfgrep is not build with libunac support. |
| 78 |
set requires_unac_support false |
| 79 |
|
| 80 |
# Syntax: pdfgrep_expect args pattern |
| 81 |
# |
| 82 |
# Spawns pdfgrep with args, fail on stderr and compares the output with pattern. |
| 83 |
# |
| 84 |
# pattern is a regular expression that _completely_ matches the output. I.e it |
| 85 |
# pdfgrep_expect encloses it int ^$. |
| 86 |
proc pdfgrep_expect args { |
| 87 |
pdfgrep_expect_internal 0 0 {*}$args |
| 88 |
} |
| 89 |
|
| 90 |
# Same as above, but expect the test to fail |
| 91 |
proc pdfgrep_expect_x args { |
| 92 |
pdfgrep_expect_internal 1 0 {*}$args |
| 93 |
} |
| 94 |
|
| 95 |
# Same as pdfgrep_expect, but don't immediately fail if pdfgrep prints to |
| 96 |
# stderr. |
| 97 |
proc pdfgrep_expect_with_err args { |
| 98 |
pdfgrep_expect_internal 0 1 {*}$args |
| 99 |
} |
| 100 |
|
| 101 |
# Same as before, but expect the test to fail |
| 102 |
proc pdfgrep_expect_with_err_x args { |
| 103 |
pdfgrep_expect_internal 1 1 {*}$args |
| 104 |
} |
| 105 |
|
| 106 |
proc pdfgrep_expect_internal {expected_failure allow_errors args} { |
| 107 |
global spawn_id test required_poppler_version |
| 108 |
|
| 109 |
set output [lindex $args end] |
| 110 |
set args [lreplace $args end end] |
| 111 |
|
| 112 |
# expect configures the terminal to use \r\n for newlines, but since we |
| 113 |
# don't want to sprinkle our source code with \r, we convert newlines. |
| 114 |
set output [string map {\n \r\n} $output] |
| 115 |
|
| 116 |
# --color=never is necessary because we dont want to have escape sequences |
| 117 |
# in the output. |
| 118 |
if {$allow_errors} { |
| 119 |
pdfgrep --color=never {*}$args |
| 120 |
} else { |
| 121 |
pdfgrep_noerr --color=never {*}$args |
| 122 |
} |
| 123 |
expect { |
| 124 |
# The final newline is only optionally matched, because some client code |
| 125 |
# may include it |
| 126 |
-re "^[set output](\r\n)?\$" { |
| 127 |
expect eof { |
| 128 |
if {$expected_failure} { |
| 129 |
pxpass $test |
| 130 |
} else { |
| 131 |
ppass $test |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
default { |
| 136 |
if {$expected_failure} { |
| 137 |
pxfail $test |
| 138 |
} else { |
| 139 |
pfail $test |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
# reset, so the next test must set it again |
| 145 |
reset_configuration |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
# Expect pdfgrep to print an error message |
| 150 |
proc pdfgrep_expect_error args { |
| 151 |
global test required_poppler_version |
| 152 |
|
| 153 |
pdfgrep {*}$args |
| 154 |
expect { |
| 155 |
-re "pdfgrep: " { ppass $test; expect eof } |
| 156 |
default { pfail $test } |
| 157 |
} |
| 158 |
|
| 159 |
# reset, so the next test must set it again |
| 160 |
reset_configuration |
| 161 |
} |
| 162 |
|
| 163 |
# Compare the exit status of the last spawned program to $expected |
| 164 |
proc expect_exit_status {expected} { |
| 165 |
global test required_poppler_version |
| 166 |
|
| 167 |
set wait_status [wait] |
| 168 |
|
| 169 |
if {[lindex $wait_status 3] != $expected} { |
| 170 |
send_log "Exit status is [lindex $wait_status 3], but expected $expected\n" |
| 171 |
pfail "$test \[exit status\]" |
| 172 |
} else { |
| 173 |
ppass "$test \[exit status\]" |
| 174 |
} |
| 175 |
|
| 176 |
# reset, so the next test must set it again |
| 177 |
reset_configuration |
| 178 |
} |
| 179 |
|
| 180 |
# Test if the required version is greater than the detected poppler version |
| 181 |
# |
| 182 |
# @return A boolean depending on the poppler version |
| 183 |
proc poppler_greater {} { |
| 184 |
global poppler_version required_poppler_version disable_poppler_version_check |
| 185 |
lassign $required_poppler_version rmaj rmin rpatch |
| 186 |
lassign $poppler_version maj min patch |
| 187 |
|
| 188 |
|
| 189 |
# Always return true if the check is disabled |
| 190 |
if {[info exists disable_poppler_version_check] |
| 191 |
&& $disable_poppler_version_check} { |
| 192 |
|
| 193 |
return true |
| 194 |
} |
| 195 |
|
| 196 |
if {$maj > $rmaj} { return true } |
| 197 |
if {$maj == $rmaj && $min > $rmin } {return true} |
| 198 |
if {$maj == $rmaj && $min == $rmin && $patch >= $rpatch} {return true} |
| 199 |
return false |
| 200 |
} |
| 201 |
|
| 202 |
# The following four procedures are like pass, fail, xpass and xfail but call |
| 203 |
# unsupported, if the poppler version is not the requested one. |
| 204 |
proc ppass arg { |
| 205 |
when_supported pass $arg |
| 206 |
} |
| 207 |
proc pfail arg { |
| 208 |
when_supported fail $arg |
| 209 |
} |
| 210 |
proc pxpass arg { |
| 211 |
when_supported xpass $arg |
| 212 |
} |
| 213 |
proc pxfail arg { |
| 214 |
when_supported xfail $arg |
| 215 |
} |
| 216 |
|
| 217 |
proc when_supported {action arg} { |
| 218 |
global poppler_version required_poppler_version |
| 219 |
global requires_pcre_support have_pcre |
| 220 |
global requires_unac_support have_unac |
| 221 |
if {[poppler_greater] && \ |
| 222 |
(!$requires_pcre_support || $have_pcre) && \ |
| 223 |
(!$requires_unac_support || $have_unac)} { |
| 224 |
$action $arg |
| 225 |
} else { |
| 226 |
unsupported "$arg -- required configuration was not found" |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
proc reset_configuration {} { |
| 231 |
global required_poppler_version requires_pcre_support requires_unac_support |
| 232 |
set required_poppler_version {0 0 0} |
| 233 |
set requires_pcre_support false |
| 234 |
set requires_unac_support false |
| 235 |
} |
| 236 |
|
| 237 |
######################################## |
| 238 |
### PDF generation #################### |
| 239 |
######################################## |
| 240 |
|
| 241 |
# The following code supports on the fly generation of PDFs. This way, |
| 242 |
# individual tests can use highly customized PDFs and we don't have to check |
| 243 |
# those into version control. |
| 244 |
|
| 245 |
|
| 246 |
# The directory where the PDFs will be generated. |
| 247 |
# NOTE This will frequently be # removed, so don't put important data there |
| 248 |
# TODO On Linux, the -t option for mktemp is marked as deprecated and the |
| 249 |
# --tmpdir option should be used. Unfortunately, this option is not available on |
| 250 |
# the BSDs. |
| 251 |
set pdfdir [exec mktemp -t -d pdfgrep_tests.XXXXXXXXXX] |
| 252 |
|
| 253 |
|
| 254 |
# Delete $pdfdir recursively and create it anew |
| 255 |
proc clear_pdfdir {} { |
| 256 |
global pdfdir |
| 257 |
file delete -force -- $pdfdir |
| 258 |
file mkdir $pdfdir |
| 259 |
} |
| 260 |
|
| 261 |
# The latex template |
| 262 |
set latex_pre {\documentclass{article} |
| 263 |
\usepackage[utf8]{inputenc} |
| 264 |
\usepackage[parfill]{parskip} |
| 265 |
\usepackage{hyperref} |
| 266 |
\pagestyle{empty} |
| 267 |
\begin{document}} |
| 268 |
set latex_post {\end{document}} |
| 269 |
|
| 270 |
|
| 271 |
# Create a pdf called $name.pdf in $pdfdir containing $content. |
| 272 |
# |
| 273 |
# This is done by first creating a .tex file with $content between |
| 274 |
# \begin{document} and \end{document} and then calling pdflatex on it. |
| 275 |
# |
| 276 |
# Returns the name of the newly created PDF file |
| 277 |
proc mkpdf {name content} { |
| 278 |
global pdfdir latex_pre latex_post |
| 279 |
set fp [open "$pdfdir/$name.tex" w] |
| 280 |
puts $fp $latex_pre |
| 281 |
puts $fp $content |
| 282 |
puts $fp $latex_post |
| 283 |
close $fp |
| 284 |
|
| 285 |
set saved_dir [pwd] |
| 286 |
cd $pdfdir |
| 287 |
if {[catch {exec pdflatex -interaction=batchmode "$name.tex"}]} { |
| 288 |
unsupported "could not execute pdflatex" |
| 289 |
cd $saved_dir |
| 290 |
error "pdflatex" |
| 291 |
} |
| 292 |
cd $saved_dir |
| 293 |
|
| 294 |
return "$pdfdir/$name.pdf" |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
######################################## |
| 299 |
### Infrastructure ##################### |
| 300 |
######################################## |
| 301 |
|
| 302 |
# The following functions are called by dejagnu |
| 303 |
|
| 304 |
proc pdfgrep_version {} { |
| 305 |
global pdfgrep_path |
| 306 |
puts "" |
| 307 |
note [exec $pdfgrep_path --version] |
| 308 |
puts "" |
| 309 |
} |