setenv XDG_CACHE_HOME "$pdfdir" set cachedir "$pdfdir/pdfgrep/" proc count_cache_files num { global cachedir test set len [llength [glob $cachedir/*]] if {$len == $num} { ppass $test } else { pfail "$test -- file count: $len" } } ###################################################################### set test "cache creates file" clear_pdfdir set pdf [mkpdf pdf { this is a test. }] pdfgrep --cache test $pdf expect eof count_cache_files 1 ###################################################################### set test "cache creates another file" set pdf2 [mkpdf pdf2 { this is another test. }] pdfgrep --cache test $pdf2 expect eof count_cache_files 2 ###################################################################### set test "cache doesn't create new file for same content" file copy $pdf2 $pdfdir/pdf3.pdf pdfgrep --cache test $pdfdir/pdf3.pdf expect eof count_cache_files 2 ###################################################################### set test "cached call returns the same as original" clear_pdfdir set pdf [mkpdf pdf { this is a test }] pdfgrep --cache test $pdf expect eof pdfgrep_expect --cache test $pdf "this is a test" ###################################################################### set test "cached limit works" clear_pdfdir file mkdir $cachedir for {set i 0} {$i < 10} {incr i} { exec touch -t 199001010101 "$cachedir/foo$i" } set pdf [mkpdf pdf { this is a test. }] setenv PDFGREP_CACHE_LIMIT 5 pdfgrep --cache test $pdf expect eof # 5 old ones and the one from the pdf count_cache_files 6 ###################################################################### set test "cache works when XDG_CACHE_HOME is not set" unsetenv XDG_CACHE_HOME set homedir $env(HOME) setenv HOME "$pdfdir" set cachedir "$pdfdir/.cache/pdfgrep" clear_pdfdir set pdf [mkpdf pdf { this is a test. }] pdfgrep --cache test $pdf expect eof count_cache_files 1 setenv HOME $homedir ###################################################################### set test "warning if XDG_CACHE_HOME cannot be created" setenv XDG_CACHE_HOME "$pdfdir/testdir/cache" clear_pdfdir set pdf [mkpdf pdf { this is a test }] pdfgrep_expect_with_err --cache test $pdf \ ".*pdfgrep: warning: .* this is a test" ###################################################################### set test "warning if XDG_CACHE_HOME/pdfgrep cannot be created" setenv XDG_CACHE_HOME "$pdfdir/cache" # Create /cache as file instead of directory to make mkdir return EEXIST but # prevent the creation of subdirs. Creating a readonly directory does not work # in case tests are executed as root (relevant in containers). exec touch "$pdfdir/cache" pdfgrep_expect_with_err --cache test $pdf \ ".*pdfgrep: warning: .* this is a test" # TODO Find a way to test cache size limit without creating too many files ###################################################################### set test "cache works for recursive search in subdirs" # See https://gitlab.com/pdfgrep/pdfgrep/issues/7 clear_pdfdir file mkdir $pdfdir/subdir mkpdf pdf "foobar" file rename $pdfdir/pdf.pdf $pdfdir/subdir/pdf.pdf pdfgrep_expect --cache -r "foobar" $pdfdir \ "$pdfdir/subdir/pdf.pdf:foobar" ###################################################################### set test "cache works with --warn-empty" # See https://gitlab.com/pdfgrep/pdfgrep/issues/35 clear_pdfdir set pdf [mkpdf pdf { this is a test }] pdfgrep --cache --warn-empty test $pdf expect eof pdfgrep_expect --cache --warn-empty test $pdf "this is a test" ###################################################################### set test "cache saves and restores page labels" clear_pdfdir set pdf [mkpdf foo { \pagenumbering{roman} first page \newpage second page \newpage third page }] pdfgrep_expect --cache --page-number=label page $pdf \ "i:first page ii:second page iii:third page" expect_exit_status 0