Skip to content

Commit 337f88c

Browse files
committed
fix: Fix unit tests & code style
1 parent e07aa51 commit 337f88c

4 files changed

Lines changed: 129 additions & 67 deletions

File tree

src/functions/filesystem.rs

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use std::collections::HashMap;
1111
use std::fs;
1212
use std::path::Path;
13-
use tera::{to_value, Function, Result, Value};
13+
use tera::{Function, Result, Value, to_value};
1414

1515
/// Read file content function
1616
pub struct ReadFile {
@@ -25,12 +25,9 @@ impl ReadFile {
2525

2626
impl Function for ReadFile {
2727
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
28-
let path = args
29-
.get("path")
30-
.and_then(|v| v.as_str())
31-
.ok_or_else(|| {
32-
tera::Error::msg("read_file requires a 'path' argument (e.g., path=\"config.txt\")")
33-
})?;
28+
let path = args.get("path").and_then(|v| v.as_str()).ok_or_else(|| {
29+
tera::Error::msg("read_file requires a 'path' argument (e.g., path=\"config.txt\")")
30+
})?;
3431

3532
// Security: Prevent reading absolute paths or paths with parent directory traversal (unless trust mode is enabled)
3633
if !self.trust_mode && (path.starts_with('/') || path.contains("..")) {
@@ -40,11 +37,11 @@ impl Function for ReadFile {
4037
)));
4138
}
4239

43-
let content = fs::read_to_string(path).map_err(|e| {
44-
tera::Error::msg(format!("Failed to read file '{}': {}", path, e))
45-
})?;
40+
let content = fs::read_to_string(path)
41+
.map_err(|e| tera::Error::msg(format!("Failed to read file '{}': {}", path, e)))?;
4642

47-
to_value(&content).map_err(|e| tera::Error::msg(format!("Failed to convert content: {}", e)))
43+
to_value(&content)
44+
.map_err(|e| tera::Error::msg(format!("Failed to convert content: {}", e)))
4845
}
4946
}
5047

@@ -61,12 +58,9 @@ impl FileExists {
6158

6259
impl Function for FileExists {
6360
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
64-
let path = args
65-
.get("path")
66-
.and_then(|v| v.as_str())
67-
.ok_or_else(|| {
68-
tera::Error::msg("file_exists requires a 'path' argument (e.g., path=\"file.txt\")")
69-
})?;
61+
let path = args.get("path").and_then(|v| v.as_str()).ok_or_else(|| {
62+
tera::Error::msg("file_exists requires a 'path' argument (e.g., path=\"file.txt\")")
63+
})?;
7064

7165
// Security: Prevent checking absolute paths or paths with parent directory traversal (unless trust mode is enabled)
7266
if !self.trust_mode && (path.starts_with('/') || path.contains("..")) {
@@ -78,7 +72,7 @@ impl Function for FileExists {
7872

7973
let exists = Path::new(path).exists();
8074

81-
to_value(&exists).map_err(|e| tera::Error::msg(format!("Failed to convert result: {}", e)))
75+
to_value(exists).map_err(|e| tera::Error::msg(format!("Failed to convert result: {}", e)))
8276
}
8377
}
8478

@@ -95,12 +89,9 @@ impl ListDir {
9589

9690
impl Function for ListDir {
9791
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
98-
let path = args
99-
.get("path")
100-
.and_then(|v| v.as_str())
101-
.ok_or_else(|| {
102-
tera::Error::msg("list_dir requires a 'path' argument (e.g., path=\"./data\")")
103-
})?;
92+
let path = args.get("path").and_then(|v| v.as_str()).ok_or_else(|| {
93+
tera::Error::msg("list_dir requires a 'path' argument (e.g., path=\"./data\")")
94+
})?;
10495

10596
// Security: Prevent listing absolute paths or paths with parent directory traversal (unless trust mode is enabled)
10697
if !self.trust_mode && (path.starts_with('/') || path.contains("..")) {
@@ -115,9 +106,8 @@ impl Function for ListDir {
115106

116107
let mut files: Vec<String> = Vec::new();
117108
for entry in entries {
118-
let entry = entry.map_err(|e| {
119-
tera::Error::msg(format!("Failed to read directory entry: {}", e))
120-
})?;
109+
let entry = entry
110+
.map_err(|e| tera::Error::msg(format!("Failed to read directory entry: {}", e)))?;
121111
let file_name = entry
122112
.file_name()
123113
.into_string()
@@ -197,12 +187,9 @@ impl FileSize {
197187

198188
impl Function for FileSize {
199189
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
200-
let path = args
201-
.get("path")
202-
.and_then(|v| v.as_str())
203-
.ok_or_else(|| {
204-
tera::Error::msg("file_size requires a 'path' argument (e.g., path=\"data.bin\")")
205-
})?;
190+
let path = args.get("path").and_then(|v| v.as_str()).ok_or_else(|| {
191+
tera::Error::msg("file_size requires a 'path' argument (e.g., path=\"data.bin\")")
192+
})?;
206193

207194
// Security: Prevent accessing absolute paths or paths with parent directory traversal (unless trust mode is enabled)
208195
if !self.trust_mode && (path.starts_with('/') || path.contains("..")) {
@@ -212,12 +199,13 @@ impl Function for FileSize {
212199
)));
213200
}
214201

215-
let metadata = fs::metadata(path)
216-
.map_err(|e| tera::Error::msg(format!("Failed to get file metadata for '{}': {}", path, e)))?;
202+
let metadata = fs::metadata(path).map_err(|e| {
203+
tera::Error::msg(format!("Failed to get file metadata for '{}': {}", path, e))
204+
})?;
217205

218206
let size = metadata.len();
219207

220-
to_value(&size).map_err(|e| tera::Error::msg(format!("Failed to convert result: {}", e)))
208+
to_value(size).map_err(|e| tera::Error::msg(format!("Failed to convert result: {}", e)))
221209
}
222210
}
223211

@@ -234,12 +222,9 @@ impl FileModified {
234222

235223
impl Function for FileModified {
236224
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
237-
let path = args
238-
.get("path")
239-
.and_then(|v| v.as_str())
240-
.ok_or_else(|| {
241-
tera::Error::msg("file_modified requires a 'path' argument (e.g., path=\"file.txt\")")
242-
})?;
225+
let path = args.get("path").and_then(|v| v.as_str()).ok_or_else(|| {
226+
tera::Error::msg("file_modified requires a 'path' argument (e.g., path=\"file.txt\")")
227+
})?;
243228

244229
// Security: Prevent accessing absolute paths or paths with parent directory traversal (unless trust mode is enabled)
245230
if !self.trust_mode && (path.starts_with('/') || path.contains("..")) {
@@ -249,8 +234,9 @@ impl Function for FileModified {
249234
)));
250235
}
251236

252-
let metadata = fs::metadata(path)
253-
.map_err(|e| tera::Error::msg(format!("Failed to get file metadata for '{}': {}", path, e)))?;
237+
let metadata = fs::metadata(path).map_err(|e| {
238+
tera::Error::msg(format!("Failed to get file metadata for '{}': {}", path, e))
239+
})?;
254240

255241
let modified = metadata
256242
.modified()
@@ -263,6 +249,7 @@ impl Function for FileModified {
263249

264250
let timestamp = duration.as_secs();
265251

266-
to_value(&timestamp).map_err(|e| tera::Error::msg(format!("Failed to convert result: {}", e)))
252+
to_value(timestamp)
253+
.map_err(|e| tera::Error::msg(format!("Failed to convert result: {}", e)))
267254
}
268255
}

src/functions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
//! }
5151
//! ```
5252
53-
pub mod filter_env;
5453
pub mod filesystem;
54+
pub mod filter_env;
5555
pub mod hash;
5656
pub mod random_string;
5757
pub mod uuid_gen;

src/renderer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ fn read_template(template_source: Option<&str>) -> Result<String, Box<dyn std::e
6363
}
6464

6565
/// Renders the template with the given context
66-
fn render(template_content: &str, context: &Context, trust_mode: bool) -> Result<String, Box<dyn std::error::Error>> {
66+
fn render(
67+
template_content: &str,
68+
context: &Context,
69+
trust_mode: bool,
70+
) -> Result<String, Box<dyn std::error::Error>> {
6771
let mut tera = Tera::default();
6872

6973
// Register all custom functions

0 commit comments

Comments
 (0)