Skip to content

convert dirs from list of strings to list of pathbufs #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,8 @@ pub struct FileSystemLoader {
}

impl FileSystemLoader {
pub fn new(dirs: Vec<String>, encoding: &'static Encoding) -> Self {
Self {
dirs: dirs.iter().map(PathBuf::from).collect(),
encoding,
}
pub fn new(dirs: Vec<PathBuf>, encoding: &'static Encoding) -> Self {
Self { dirs, encoding }
}

pub fn from_pathbuf(dirs: Vec<PathBuf>, encoding: &'static Encoding) -> Self {
Expand Down Expand Up @@ -286,7 +283,7 @@ mod tests {
Python::with_gil(|py| {
let engine = EngineData::empty();
let loader =
FileSystemLoader::new(vec!["tests/templates".to_string()], encoding_rs::UTF_8);
FileSystemLoader::new(vec![PathBuf::from("tests/templates")], encoding_rs::UTF_8);
let template = loader
.get_template(py, "basic.txt", &engine)
.unwrap()
Expand All @@ -305,7 +302,7 @@ mod tests {
Python::with_gil(|py| {
let engine = EngineData::empty();
let loader =
FileSystemLoader::new(vec!["tests/templates".to_string()], encoding_rs::UTF_8);
FileSystemLoader::new(vec![PathBuf::from("tests/templates")], encoding_rs::UTF_8);
let error = loader.get_template(py, "missing.txt", &engine).unwrap_err();

let mut expected = std::env::current_dir().unwrap();
Expand All @@ -329,7 +326,7 @@ mod tests {
Python::with_gil(|py| {
let engine = EngineData::empty();
let loader =
FileSystemLoader::new(vec!["tests/templates".to_string()], encoding_rs::UTF_8);
FileSystemLoader::new(vec![PathBuf::from("tests/templates")], encoding_rs::UTF_8);
let error = loader
.get_template(py, "invalid.txt", &engine)
.unwrap()
Expand Down Expand Up @@ -364,7 +361,7 @@ mod tests {

// Create a FileSystemLoader for the CachedLoader
let filesystem_loader =
FileSystemLoader::new(vec!["tests/templates".to_string()], encoding_rs::UTF_8);
FileSystemLoader::new(vec![PathBuf::from("tests/templates")], encoding_rs::UTF_8);

// Wrap the FileSystemLoader in a CachedLoader
let mut cached_loader = CachedLoader::new(vec![Loader::FileSystem(filesystem_loader)]);
Expand Down Expand Up @@ -407,7 +404,7 @@ mod tests {
Python::with_gil(|py| {
let engine = EngineData::empty();
let filesystem_loader =
FileSystemLoader::new(vec!["tests/templates".to_string()], encoding_rs::UTF_8);
FileSystemLoader::new(vec![PathBuf::from("tests/templates")], encoding_rs::UTF_8);

let mut cached_loader = CachedLoader::new(vec![Loader::FileSystem(filesystem_loader)]);
let error = cached_loader
Expand Down Expand Up @@ -444,7 +441,7 @@ mod tests {
Python::with_gil(|py| {
let engine = EngineData::empty();
let filesystem_loader =
FileSystemLoader::new(vec!["tests/templates".to_string()], encoding_rs::UTF_8);
FileSystemLoader::new(vec![PathBuf::from("tests/templates")], encoding_rs::UTF_8);

let mut cached_loader = CachedLoader::new(vec![Loader::FileSystem(filesystem_loader)]);
let error = cached_loader
Expand Down
2 changes: 1 addition & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub mod django_rusty_templates {

#[pyclass]
pub struct Engine {
dirs: Vec<String>,
dirs: Vec<PathBuf>,
app_dirs: bool,
context_processors: Vec<String>,
debug: bool,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pathlib import Path

import pytest
from django.conf import settings
from django.template.engine import Engine
from django.template.library import InvalidTemplateLibrary

Expand Down Expand Up @@ -38,3 +41,15 @@ def test_import_libraries_no_register():
)

assert str(exc_info.value) == expected


def test_pathlib_dirs():
engine = RustyTemplates({
"NAME": "rust",
"OPTIONS": {},
"DIRS": [Path(settings.BASE_DIR) / "templates"],
"APP_DIRS": False,
})

template = engine.get_template("basic.txt")
assert template.render({"user": "Lily"}) == "Hello Lily!\n"