[WIP] Integration of TestEnvironment

This commit is contained in:
Leonard Steppy 2025-02-08 22:49:11 +01:00
parent b6a6e63358
commit d87cd468cc
2 changed files with 56 additions and 11 deletions

View File

@ -9,9 +9,18 @@ use std::{env, io};
pub trait Environment {
fn args_os(&self) -> Vec<OsString>;
fn var<K>(&self, key: K) -> Result<String, VarError>
fn var_os<K>(&self, key: K) -> Option<OsString>
where
K: AsRef<OsStr>;
fn var<K>(&self, key: K) -> Result<String, VarError>
where
K: AsRef<OsStr>,
{
self
.var_os(key)
.ok_or(VarError::NotPresent)
.and_then(|s| s.into_string().map_err(VarError::NotUnicode))
}
fn set_var<K, V>(&mut self, key: K, value: V)
where
K: AsRef<OsStr>,
@ -28,6 +37,13 @@ impl Environment for Prod {
env::args_os().collect()
}
fn var_os<K>(&self, key: K) -> Option<OsString>
where
K: AsRef<OsStr>
{
env::var_os(key)
}
fn var<K>(&self, key: K) -> Result<String, VarError>
where
K: AsRef<OsStr>,

View File

@ -2,23 +2,52 @@ use crate::environment::Environment;
use crate::shell_interface::{
CommandOutput, CommandResult, ExitStatus, ShellCommand, ShellInterface, StartError,
};
use std::env::VarError;
use std::collections::{HashMap, VecDeque};
use std::ffi::{OsStr, OsString};
use std::io::Error;
use std::path::PathBuf;
struct TestEnvironment {}
#[derive(Debug)]
pub struct TestEnvironment {
/// passed command line arguments
args_os: Vec<OsString>,
/// set environment variables - we assume a pure environment by default
env_vars: HashMap<OsString, OsString>,
/// home directory, relative to the target/test folder
home_dir: PathBuf,
/// pending lines of std input
stdin: VecDeque<String>,
/// whether an ssh agent has been started successfully
ssh_agent_started: bool,
// TODO ssh servers and local server
}
#[derive(Debug)]
pub struct FsEntry {
pub name: OsString,
pub kind: FsEntryKind,
}
#[derive(Debug)]
pub enum FsEntryKind {
Directory {
entries: Vec<FsEntry>,
},
File {
contents: String,
}
}
impl Environment for TestEnvironment {
fn args_os(&self) -> Vec<OsString> {
todo!()
self.args_os.clone()
}
fn var<K>(&self, key: K) -> Result<String, VarError>
where
K: AsRef<OsStr>,
fn var_os<K>(&self, key: K) -> Option<OsString>
where
K: AsRef<OsStr>
{
todo!()
self.env_vars.get(key.as_ref()).map(|s| s.into())
}
fn set_var<K, V>(&mut self, key: K, value: V)
@ -26,15 +55,15 @@ impl Environment for TestEnvironment {
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
todo!()
self.env_vars.insert(key.as_ref().into(), value.as_ref().into());
}
fn get_home_directory(&self) -> Option<PathBuf> {
todo!()
PathBuf::from("target/integration_test").join(&self.home_dir).into()
}
fn read_line(&mut self) -> Result<String, Error> {
todo!()
self.stdin.pop_front().ok_or_else(|| Error::other("Unexpected call to read_line: No input prepared"))
}
}