Skip to content
This repository was archived by the owner on Jan 26, 2025. It is now read-only.

Commit fbe72a9

Browse files
tingerrrtingerrr
authored andcommitted
chore: allow checking for file existance only in TempEnv
This patch checking for the existance of file without comparing their content in TempEnv, this is particularly useful for blobs which like PNGs which may be compressed on disk, but no in the tests or library output.
1 parent 15cd11b commit fbe72a9

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

crates/typst-test-lib/src/_dev/fs.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::stdx;
99
pub struct TempEnv {
1010
root: TempDir,
1111
found: BTreeMap<PathBuf, Option<Vec<u8>>>,
12-
expected: BTreeMap<PathBuf, Option<Vec<u8>>>,
12+
expected: BTreeMap<PathBuf, Option<Option<Vec<u8>>>>,
1313
}
1414

1515
/// Set up the project structure.
@@ -55,10 +55,19 @@ impl Expect {
5555
self
5656
}
5757

58-
pub fn expect_file<P: AsRef<Path>>(&mut self, path: P, content: impl AsRef<[u8]>) -> &mut Self {
58+
pub fn expect_file<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
59+
self.0.add_expected(path.as_ref().to_path_buf(), Some(None));
60+
self
61+
}
62+
63+
pub fn expect_file_content<P: AsRef<Path>>(
64+
&mut self,
65+
path: P,
66+
content: impl AsRef<[u8]>,
67+
) -> &mut Self {
5968
let content = content.as_ref();
6069
self.0
61-
.add_expected(path.as_ref().to_path_buf(), Some(content.to_owned()));
70+
.add_expected(path.as_ref().to_path_buf(), Some(Some(content.to_owned())));
6271
self
6372
}
6473

@@ -110,7 +119,7 @@ impl TempEnv {
110119
}
111120

112121
impl TempEnv {
113-
fn add_expected(&mut self, expected: PathBuf, content: Option<Vec<u8>>) {
122+
fn add_expected(&mut self, expected: PathBuf, content: Option<Option<Vec<u8>>>) {
114123
for ancestor in expected.ancestors() {
115124
self.expected.insert(ancestor.to_path_buf(), None);
116125
}
@@ -154,8 +163,10 @@ impl TempEnv {
154163
if let Some(found) = self.found.remove(&expected_path) {
155164
let expected = expected_value.unwrap_or_default();
156165
let found = found.unwrap_or_default();
157-
if expected != found {
158-
not_matched.insert(expected_path, (found, expected));
166+
if let Some(expected) = expected {
167+
if expected != found {
168+
not_matched.insert(expected_path, (found, expected));
169+
}
159170
}
160171
} else {
161172
not_found.insert(expected_path);

crates/typst-test-lib/src/doc/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ mod tests {
274274
doc.save(root).unwrap();
275275
},
276276
|root| {
277-
root.expect_file("1.png", doc.buffers[0].encode_png().unwrap())
278-
.expect_file("2.png", doc.buffers[1].encode_png().unwrap())
279-
.expect_file("3.png", doc.buffers[2].encode_png().unwrap())
277+
root.expect_file_content("1.png", doc.buffers[0].encode_png().unwrap())
278+
.expect_file_content("2.png", doc.buffers[1].encode_png().unwrap())
279+
.expect_file_content("3.png", doc.buffers[2].encode_png().unwrap())
280280
},
281281
);
282282
}

crates/typst-test-lib/src/project/vcs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ mod tests {
141141
},
142142
|root| {
143143
root.expect_dir("tests/fancy/out")
144-
.expect_file("tests/fancy/out/.gitignore", GITIGNORE_CONTENT)
144+
.expect_file_content("tests/fancy/out/.gitignore", GITIGNORE_CONTENT)
145145
},
146146
);
147147
}
@@ -161,7 +161,7 @@ mod tests {
161161
},
162162
|root| {
163163
root.expect_dir("tests/fancy/out")
164-
.expect_file("tests/fancy/out/.gitignore", GITIGNORE_CONTENT)
164+
.expect_file_content("tests/fancy/out/.gitignore", GITIGNORE_CONTENT)
165165
},
166166
);
167167
}

crates/typst-test-lib/src/test/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,10 @@ mod tests {
527527
.unwrap();
528528
},
529529
|root| {
530-
root.expect_file("tests/compile-only/test.typ", "Hello World")
531-
.expect_file("tests/ephemeral/test.typ", "Hello World")
532-
.expect_file("tests/ephemeral/ref.typ", "Hello\nWorld")
533-
.expect_file("tests/persistent/test.typ", "Hello World")
530+
root.expect_file_content("tests/compile-only/test.typ", "Hello World")
531+
.expect_file_content("tests/ephemeral/test.typ", "Hello World")
532+
.expect_file_content("tests/ephemeral/ref.typ", "Hello\nWorld")
533+
.expect_file_content("tests/persistent/test.typ", "Hello World")
534534
.expect_dir("tests/persistent/ref")
535535
},
536536
);
@@ -547,12 +547,12 @@ mod tests {
547547
test("persistent").make_ephemeral(&paths, None).unwrap();
548548
},
549549
|root| {
550-
root.expect_file("tests/compile-only/test.typ", "Hello World")
551-
.expect_file("tests/compile-only/ref.typ", "Hello World")
552-
.expect_file("tests/ephemeral/test.typ", "Hello World")
553-
.expect_file("tests/ephemeral/ref.typ", "Hello World")
554-
.expect_file("tests/persistent/test.typ", "Hello World")
555-
.expect_file("tests/persistent/ref.typ", "Hello World")
550+
root.expect_file_content("tests/compile-only/test.typ", "Hello World")
551+
.expect_file_content("tests/compile-only/ref.typ", "Hello World")
552+
.expect_file_content("tests/ephemeral/test.typ", "Hello World")
553+
.expect_file_content("tests/ephemeral/ref.typ", "Hello World")
554+
.expect_file_content("tests/persistent/test.typ", "Hello World")
555+
.expect_file_content("tests/persistent/ref.typ", "Hello World")
556556
},
557557
);
558558
}
@@ -576,11 +576,11 @@ mod tests {
576576
.unwrap();
577577
},
578578
|root| {
579-
root.expect_file("tests/compile-only/test.typ", "Hello World")
579+
root.expect_file_content("tests/compile-only/test.typ", "Hello World")
580580
.expect_dir("tests/compile-only/ref")
581-
.expect_file("tests/ephemeral/test.typ", "Hello World")
581+
.expect_file_content("tests/ephemeral/test.typ", "Hello World")
582582
.expect_dir("tests/ephemeral/ref")
583-
.expect_file("tests/persistent/test.typ", "Hello World")
583+
.expect_file_content("tests/persistent/test.typ", "Hello World")
584584
.expect_dir("tests/persistent/ref")
585585
},
586586
);
@@ -599,9 +599,9 @@ mod tests {
599599
test("persistent").make_compile_only(&paths, None).unwrap();
600600
},
601601
|root| {
602-
root.expect_file("tests/compile-only/test.typ", "Hello World")
603-
.expect_file("tests/ephemeral/test.typ", "Hello World")
604-
.expect_file("tests/persistent/test.typ", "Hello World")
602+
root.expect_file_content("tests/compile-only/test.typ", "Hello World")
603+
.expect_file_content("tests/ephemeral/test.typ", "Hello World")
604+
.expect_file_content("tests/persistent/test.typ", "Hello World")
605605
},
606606
);
607607
}

0 commit comments

Comments
 (0)