From 7d35aec080191cc63f1a4be097960d01070b686c Mon Sep 17 00:00:00 2001 From: Meir Elbaz Date: Mon, 28 Apr 2025 15:38:28 +0300 Subject: [PATCH 1/2] convert dirs from string to pathbuf --- src/loaders.rs | 19 ++++++++----------- src/template.rs | 2 +- tests/settings.py | 3 ++- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/loaders.rs b/src/loaders.rs index e5d14b26..b8517b08 100644 --- a/src/loaders.rs +++ b/src/loaders.rs @@ -70,11 +70,8 @@ pub struct FileSystemLoader { } impl FileSystemLoader { - pub fn new(dirs: Vec, encoding: &'static Encoding) -> Self { - Self { - dirs: dirs.iter().map(PathBuf::from).collect(), - encoding, - } + pub fn new(dirs: Vec, encoding: &'static Encoding) -> Self { + Self { dirs, encoding } } pub fn from_pathbuf(dirs: Vec, encoding: &'static Encoding) -> Self { @@ -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() @@ -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(); @@ -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() @@ -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)]); @@ -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 @@ -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 diff --git a/src/template.rs b/src/template.rs index 19e423a6..817bab10 100644 --- a/src/template.rs +++ b/src/template.rs @@ -100,7 +100,7 @@ pub mod django_rusty_templates { #[pyclass] pub struct Engine { - dirs: Vec, + dirs: Vec, app_dirs: bool, context_processors: Vec, debug: bool, diff --git a/tests/settings.py b/tests/settings.py index ea6f82f3..af9099af 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,4 +1,5 @@ import os +from pathlib import Path BASE_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -21,7 +22,7 @@ }, { "BACKEND": "django_rusty_templates.RustyTemplates", - "DIRS": ["tests/templates"], + "DIRS": ["tests/templates", Path(BASE_DIR) / "templates"], "NAME": "rusty", "OPTIONS": { "libraries": { From f265fff3cbfbae774185ed5b8b1eb7972417798b Mon Sep 17 00:00:00 2001 From: Meir Elbaz Date: Mon, 28 Apr 2025 18:58:11 +0300 Subject: [PATCH 2/2] move the test to another file --- tests/settings.py | 3 +-- tests/test_engine.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/settings.py b/tests/settings.py index af9099af..ea6f82f3 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,5 +1,4 @@ import os -from pathlib import Path BASE_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -22,7 +21,7 @@ }, { "BACKEND": "django_rusty_templates.RustyTemplates", - "DIRS": ["tests/templates", Path(BASE_DIR) / "templates"], + "DIRS": ["tests/templates"], "NAME": "rusty", "OPTIONS": { "libraries": { diff --git a/tests/test_engine.py b/tests/test_engine.py index ae21c3f2..6eced50b 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -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 @@ -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"