summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-09-20 19:49:47 +0200
committerLaurenz <laurmaedje@gmail.com>2022-09-20 19:49:47 +0200
commit3760748fddd3b793c79c370398a9d4a3fc5afc04 (patch)
treeb1a615e510aa231cfe9757a9c0a35a375e32e3ba /tests
parent757a701c1aa2a6fb80033c7e75666661818da6f9 (diff)
Refactor error handling
Diffstat (limited to 'tests')
-rw-r--r--tests/res/bad.svg5
-rw-r--r--tests/typ/code/import.typ2
-rw-r--r--tests/typ/graphics/image.typ6
-rw-r--r--tests/typ/utility/csv.typ2
-rw-r--r--tests/typeset.rs31
5 files changed, 27 insertions, 19 deletions
diff --git a/tests/res/bad.svg b/tests/res/bad.svg
new file mode 100644
index 00000000..b7828a61
--- /dev/null
+++ b/tests/res/bad.svg
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
+ <style>
+ </g>
+</svg>
diff --git a/tests/typ/code/import.typ b/tests/typ/code/import.typ
index 2c27f135..0c0b6c08 100644
--- a/tests/typ/code/import.typ
+++ b/tests/typ/code/import.typ
@@ -47,7 +47,7 @@
---
// Some non-text stuff.
-// Error: 16-37 failed to load source file (file is not valid utf-8)
+// Error: 16-37 file is not valid utf-8
#import * from "../../res/rhino.png"
---
diff --git a/tests/typ/graphics/image.typ b/tests/typ/graphics/image.typ
index e64b6c45..3bb5bdd1 100644
--- a/tests/typ/graphics/image.typ
+++ b/tests/typ/graphics/image.typ
@@ -55,5 +55,9 @@ A #image("/res/tiger.jpg", height: 1cm, width: 80%) B
#image("path/does/not/exist")
---
-// Error: 8-21 failed to load image (unknown image format)
+// Error: 8-21 unknown image format
#image("./image.typ")
+
+---
+// Error: 8-22 failed to parse svg: found closing tag 'g' instead of 'style' in line 4
+#image("/res/bad.svg")
diff --git a/tests/typ/utility/csv.typ b/tests/typ/utility/csv.typ
index ab955ab0..146cafae 100644
--- a/tests/typ/utility/csv.typ
+++ b/tests/typ/utility/csv.typ
@@ -11,5 +11,5 @@
#csv("nope.csv")
---
-// Error: 6-20 failed to load csv file (CSV error: record 2 (line: 3, byte: 8): found record with 3 fields, but the previous record has 2 fields)
+// Error: 6-20 failed to parse csv file: found 3 instead of 2 fields in line 3
#csv("/res/bad.csv")
diff --git a/tests/typeset.rs b/tests/typeset.rs
index 3ac353db..d3d365c3 100644
--- a/tests/typeset.rs
+++ b/tests/typeset.rs
@@ -4,7 +4,6 @@ use std::env;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::hash::Hash;
-use std::io;
use std::ops::Range;
use std::path::{Path, PathBuf};
@@ -15,6 +14,7 @@ use tiny_skia as sk;
use unscanny::Scanner;
use walkdir::WalkDir;
+use typst::diag::{FileError, FileResult};
use typst::eval::{Smart, Value};
use typst::font::{Font, FontBook};
use typst::frame::{Element, Frame};
@@ -238,17 +238,13 @@ impl World for TestWorld {
&self.config
}
- fn resolve(&self, path: &Path) -> io::Result<SourceId> {
+ fn resolve(&self, path: &Path) -> FileResult<SourceId> {
let hash = PathHash::new(path)?;
if let Some(&id) = self.nav.borrow().get(&hash) {
return Ok(id);
}
- let data = fs::read(path)?;
- let text = String::from_utf8(data).map_err(|_| {
- io::Error::new(io::ErrorKind::InvalidData, "file is not valid utf-8")
- })?;
-
+ let text = fs::read_to_string(path).map_err(|e| FileError::from_io(e, path))?;
let id = SourceId::from_raw(self.sources.len() as u16);
let source = Source::new(id, path, text);
self.sources.push(Box::new(source));
@@ -265,15 +261,17 @@ impl World for TestWorld {
&self.book
}
- fn font(&self, id: usize) -> io::Result<Font> {
- Ok(self.fonts[id].clone())
+ fn font(&self, id: usize) -> Option<Font> {
+ Some(self.fonts[id].clone())
}
- fn file(&self, path: &Path) -> io::Result<Buffer> {
+ fn file(&self, path: &Path) -> FileResult<Buffer> {
let hash = PathHash::new(path)?;
Ok(match self.files.borrow_mut().entry(hash) {
Entry::Occupied(entry) => entry.get().clone(),
- Entry::Vacant(entry) => entry.insert(fs::read(path)?.into()).clone(),
+ Entry::Vacant(entry) => entry
+ .insert(fs::read(path).map_err(|e| FileError::from_io(e, path))?.into())
+ .clone(),
})
}
}
@@ -283,15 +281,16 @@ impl World for TestWorld {
struct PathHash(u128);
impl PathHash {
- fn new(path: &Path) -> io::Result<Self> {
- let file = File::open(path)?;
- if file.metadata()?.is_file() {
- let handle = Handle::from_file(file)?;
+ fn new(path: &Path) -> FileResult<Self> {
+ let f = |e| FileError::from_io(e, path);
+ let file = File::open(path).map_err(f)?;
+ if file.metadata().map_err(f)?.is_file() {
+ let handle = Handle::from_file(file).map_err(f)?;
let mut state = SipHasher::new();
handle.hash(&mut state);
Ok(Self(state.finish128().as_u128()))
} else {
- Err(io::ErrorKind::NotFound.into())
+ Err(FileError::NotFound(path.into()))
}
}
}