Skip to content

Commit 947466c

Browse files
committed
fix integration tests for windows (#fix 214)
1 parent b06cfdf commit 947466c

File tree

1 file changed

+47
-62
lines changed

1 file changed

+47
-62
lines changed

pdf/tests/integration.rs

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::str;
2+
use std::path::{Path, PathBuf};
23
use pdf::file::FileOptions;
34
use pdf::object::*;
45
use pdf::parser::{parse, ParseFlags};
56
use glob::glob;
67

7-
macro_rules! file_path {
8-
( $subdir:expr ) => { concat!("../files/", $subdir) }
9-
}
108
macro_rules! run {
119
($e:expr) => (
1210
match $e {
@@ -18,9 +16,22 @@ macro_rules! run {
1816
)
1917
}
2018

19+
fn files() -> PathBuf {
20+
Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().join("files")
21+
}
22+
fn file_path(s: &str) -> PathBuf {
23+
files().join(s)
24+
}
25+
fn dir_pdfs(path: PathBuf) -> impl Iterator<Item=PathBuf> {
26+
path.read_dir().unwrap()
27+
.filter_map(|r| r.ok())
28+
.map(|e| e.path())
29+
.filter(|p| p.extension().map(|e| e == "pdf").unwrap_or(false))
30+
}
31+
2132
#[test]
2233
fn open_file() {
23-
let _ = run!(FileOptions::uncached().open(file_path!("example.pdf")));
34+
let _ = run!(FileOptions::uncached().open(file_path("example.pdf")));
2435
#[cfg(all(feature = "mmap", feature = "cache"))]
2536
let _ = run!({
2637
use memmap2::Mmap;
@@ -33,61 +44,42 @@ fn open_file() {
3344
#[cfg(feature="cache")]
3445
#[test]
3546
fn read_pages() {
36-
for entry in glob(file_path!("*.pdf")).expect("Failed to read glob pattern") {
37-
match entry {
38-
Ok(path) => {
39-
println!("\n == Now testing `{}` ==", path.to_str().unwrap());
47+
for path in dir_pdfs(files()) {
48+
println!("\n == Now testing `{}` ==", path.to_str().unwrap());
4049

41-
let path = path.to_str().unwrap();
42-
let file = run!(FileOptions::cached().open(path));
43-
for i in 0 .. file.num_pages() {
44-
println!("Read page {}", i);
45-
let _ = file.get_page(i);
46-
}
47-
}
48-
Err(e) => println!("{:?}", e)
50+
let path = path.to_str().unwrap();
51+
let file = run!(FileOptions::cached().open(path));
52+
for i in 0 .. file.num_pages() {
53+
println!("Read page {}", i);
54+
let _ = file.get_page(i);
4955
}
5056
}
5157
}
5258

5359
#[test]
5460
fn user_password() {
55-
for entry in glob(file_path!("password_protected/*.pdf"))
56-
.expect("Failed to read glob pattern")
57-
{
58-
match entry {
59-
Ok(path) => {
60-
println!("\n\n == Now testing `{}` ==\n", path.to_str().unwrap());
61+
for path in dir_pdfs(file_path("password_protected")) {
62+
println!("\n\n == Now testing `{}` ==\n", path.to_str().unwrap());
6163

62-
let path = path.to_str().unwrap();
63-
let file = run!(FileOptions::uncached().password(b"userpassword").open(path));
64-
for i in 0 .. file.num_pages() {
65-
println!("\nRead page {}", i);
66-
let _ = file.get_page(i);
67-
}
68-
}
69-
Err(e) => println!("{:?}", e)
64+
let path = path.to_str().unwrap();
65+
let file = run!(FileOptions::uncached().password(b"userpassword").open(path));
66+
for i in 0 .. file.num_pages() {
67+
println!("\nRead page {}", i);
68+
let _ = file.get_page(i);
7069
}
7170
}
7271
}
7372

7473
#[test]
7574
fn owner_password() {
76-
for entry in glob(file_path!("password_protected/*.pdf"))
77-
.expect("Failed to read glob pattern")
78-
{
79-
match entry {
80-
Ok(path) => {
81-
println!("\n\n == Now testing `{}` ==\n", path.to_str().unwrap());
75+
for path in dir_pdfs(file_path("password_protected")) {
76+
println!("\n\n == Now testing `{}` ==\n", path.to_str().unwrap());
8277

83-
let path = path.to_str().unwrap();
84-
let file = run!(FileOptions::uncached().password(b"ownerpassword").open(path));
85-
for i in 0 .. file.num_pages() {
86-
println!("\nRead page {}", i);
87-
let _ = file.get_page(i);
88-
}
89-
}
90-
Err(e) => println!("{:?}", e)
78+
let path = path.to_str().unwrap();
79+
let file = run!(FileOptions::uncached().password(b"ownerpassword").open(path));
80+
for i in 0 .. file.num_pages() {
81+
println!("\nRead page {}", i);
82+
let _ = file.get_page(i);
9183
}
9284
}
9385
}
@@ -97,26 +89,19 @@ fn owner_password() {
9789
#[cfg(feature="cache")]
9890
#[test]
9991
fn invalid_pdfs() {
100-
for entry in glob(file_path!("invalid/*.pdf"))
101-
.expect("Failed to read glob pattern")
102-
{
103-
match entry {
104-
Ok(path) => {
105-
let path = path.to_str().unwrap();
106-
println!("\n\n == Now testing `{}` ==\n", path);
92+
for path in dir_pdfs(file_path("invalid")) {
93+
let path = path.to_str().unwrap();
94+
println!("\n\n == Now testing `{}` ==\n", path);
10795

108-
match FileOptions::cached().open(path) {
109-
Ok(file) => {
110-
for i in 0 .. file.num_pages() {
111-
let _ = file.get_page(i);
112-
}
113-
}
114-
Err(_) => {
115-
continue;
116-
}
96+
match FileOptions::cached().open(path) {
97+
Ok(file) => {
98+
for i in 0 .. file.num_pages() {
99+
let _ = file.get_page(i);
117100
}
118101
}
119-
Err(e) => panic!("error when reading glob patterns: {:?}", e),
102+
Err(_) => {
103+
continue;
104+
}
120105
}
121106
}
122107
}
@@ -125,7 +110,7 @@ fn invalid_pdfs() {
125110
#[test]
126111
fn parse_objects_from_stream() {
127112
use pdf::object::NoResolve;
128-
let file = run!(FileOptions::cached().open(file_path!("xelatex.pdf")));
113+
let file = run!(FileOptions::cached().open(file_path("xelatex.pdf")));
129114
let resolver = file.resolver();
130115

131116
// .. we know that object 13 of that file is an ObjectStream

0 commit comments

Comments
 (0)