use std::path::{Path, PathBuf};
use crate::errors::BntoError;
pub trait ProcessContext: Send + Sync {
fn run_command(&self, cmd: &str, args: &[&str]) -> Result<Vec<u8>, BntoError>;
fn temp_file(&self, suffix: &str) -> Result<PathBuf, BntoError>;
fn env_var(&self, key: &str) -> Option<String>;
fn work_dir(&self) -> Result<&Path, BntoError>;
}
pub struct NoopContext;
impl ProcessContext for NoopContext {
fn run_command(&self, _cmd: &str, _args: &[&str]) -> Result<Vec<u8>, BntoError> {
Err(BntoError::ProcessingFailed(
"System commands not available in browser".to_string(),
))
}
fn temp_file(&self, _suffix: &str) -> Result<PathBuf, BntoError> {
Err(BntoError::ProcessingFailed(
"Temp files not available in browser".to_string(),
))
}
fn env_var(&self, _key: &str) -> Option<String> {
None
}
fn work_dir(&self) -> Result<&Path, BntoError> {
Err(BntoError::ProcessingFailed(
"Working directory not available in browser".to_string(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_noop_context_run_command_returns_err() {
let ctx = NoopContext;
let result = ctx.run_command("ls", &[]);
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("not available in browser")
);
}
#[test]
fn test_noop_context_temp_file_returns_err() {
let ctx = NoopContext;
let result = ctx.temp_file(".txt");
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("not available in browser")
);
}
#[test]
fn test_noop_context_env_var_returns_none() {
let ctx = NoopContext;
assert_eq!(ctx.env_var("PATH"), None);
}
#[test]
fn test_noop_context_work_dir_returns_err() {
let ctx = NoopContext;
let result = ctx.work_dir();
assert!(result.is_err());
assert!(
result
.unwrap_err()
.to_string()
.contains("not available in browser")
);
}
}