oakum 0.12.1

A polyglot release tool that derives dependent version bumps from the dependency graph
Documentation
//! Load a [`oakum::template::TemplateSource`] through the repository Dir.
//!
//! File paths go through [`super::fs::resolve_capability_path`] and nowhere
//! else, so a `{ file = ... }` value cannot escape the checkout (ADR-0006).

use std::io::Read;
use std::path::Path;

use cap_std::fs::Dir;
use oakum::template::TemplateSource;

use super::fs::{open_read_only, resolve_capability_path};
use super::CliError;

pub(super) fn load_template_body(
    dir: &Dir,
    repo_path: &Path,
    source: &TemplateSource,
) -> Result<String, Box<dyn std::error::Error>> {
    match source {
        TemplateSource::Inline(body) => Ok(body.clone()),
        TemplateSource::File(relative) => load_contained_file(dir, repo_path, relative, "template"),
    }
}

/// `noun` is the user-facing name in errors (`template`, `--notes-file`).
pub(super) fn load_contained_file(
    dir: &Dir,
    repo_path: &Path,
    relative: &str,
    noun: &str,
) -> Result<String, Box<dyn std::error::Error>> {
    let resolved = resolve_capability_path(dir, repo_path, Path::new(relative))
        .map_err(|err| CliError::new(format!("failed to resolve {noun} `{relative}`: {err}")))?;
    let mut file = open_read_only(dir, &resolved)
        .map_err(|err| CliError::new(format!("failed to open {noun} `{relative}`: {err}")))?;
    let metadata = file
        .metadata()
        .map_err(|err| CliError::new(format!("failed to inspect {noun} `{relative}`: {err}")))?;
    if !metadata.is_file() {
        return Err(Box::new(CliError::new(format!(
            "{noun} `{relative}` is not a regular file"
        ))));
    }
    let mut body = String::new();
    file.read_to_string(&mut body)
        .map_err(|err| CliError::new(format!("failed to read {noun} `{relative}`: {err}")))?;
    Ok(body)
}

#[cfg(test)]
mod tests {
    use super::{load_contained_file, load_template_body};

    use crate::test_fixture::Fixture;
    use cap_std::ambient_authority;
    use cap_std::fs::Dir;
    use oakum::template::TemplateSource;
    use std::fs;

    #[test]
    fn parent_escape_is_refused() {
        let root = Fixture::new("tpl", "escape");
        let dir = Dir::open_ambient_dir(&root, ambient_authority()).expect("dir");
        let err = load_template_body(
            &dir,
            &root,
            &TemplateSource::File(String::from("../secret.md")),
        )
        .expect_err("escape");
        assert!(err.to_string().contains("outside the repository"), "{err}");
        assert!(!err.to_string().contains("No such file"), "{err}");
    }

    #[test]
    fn missing_file_is_not_an_escape() {
        let root = Fixture::new("tpl", "missing");
        let dir = Dir::open_ambient_dir(&root, ambient_authority()).expect("dir");
        let err = load_template_body(
            &dir,
            &root,
            &TemplateSource::File(String::from("missing.md")),
        )
        .expect_err("missing");
        assert!(
            err.to_string().contains("failed to resolve template"),
            "{err}"
        );
        assert!(!err.to_string().contains("outside the repository"), "{err}");
    }

    #[test]
    fn contained_file_keeps_template_in_the_path() {
        let root = Fixture::new("tpl", "path");
        let dir = Dir::open_ambient_dir(&root, ambient_authority()).expect("dir");
        let err = load_contained_file(&dir, &root, "my template notes.md", "--notes-file")
            .expect_err("missing");
        assert!(err.to_string().contains("my template notes.md"), "{err}");
        assert!(
            !err.to_string().contains("my --notes-file notes.md"),
            "{err}"
        );
    }

    #[test]
    fn file_inside_the_repo_is_read() {
        let root = Fixture::new("tpl", "read");
        fs::write(root.join("notes.md"), "hello {{ n }}\n").expect("notes");
        let dir = Dir::open_ambient_dir(&root, ambient_authority()).expect("dir");
        let body = load_template_body(&dir, &root, &TemplateSource::File(String::from("notes.md")))
            .expect("read");
        assert_eq!(body, "hello {{ n }}\n");
    }

    #[test]
    fn inline_body_is_returned() {
        let root = Fixture::new("tpl", "inline");
        let dir = Dir::open_ambient_dir(&root, ambient_authority()).expect("dir");
        let body = load_template_body(
            &dir,
            &root,
            &TemplateSource::Inline(String::from("hello {{ n }}")),
        )
        .expect("inline");
        assert_eq!(body, "hello {{ n }}");
    }
}