tytanic_core/test/
template.rs

1use std::fs;
2use std::io;
3
4use typst::syntax::FileId;
5use typst::syntax::Source;
6use typst::syntax::VirtualPath;
7
8use super::Id;
9use crate::project::Project;
10
11/// A compile-only template test.
12#[derive(Debug, Clone, PartialEq, Eq, Hash)]
13pub struct Test {
14    id: Id,
15}
16
17impl Test {
18    pub fn load(project: &Project) -> Option<Self> {
19        if project.template_entrypoint().is_some() {
20            return Some(Self { id: Id::template() });
21        }
22
23        None
24    }
25}
26
27impl Test {
28    pub fn id(&self) -> &Id {
29        &self.id
30    }
31}
32
33impl Test {
34    /// Loads the test script source of this test.
35    #[tracing::instrument(skip(project))]
36    pub fn load_source(&self, project: &Project) -> io::Result<Source> {
37        let test_script = project
38            .template_entrypoint()
39            .expect("Existence of template test ensures existence of entrypoint");
40
41        Ok(Source::new(
42            FileId::new(
43                None,
44                VirtualPath::new(
45                    test_script
46                        .strip_prefix(project.root())
47                        .unwrap_or(&test_script),
48                ),
49            ),
50            fs::read_to_string(test_script)?,
51        ))
52    }
53}