From d87cd468cc4a34afbd86559792445fd00f5626a2 Mon Sep 17 00:00:00 2001 From: Steppy Date: Sat, 8 Feb 2025 22:49:11 +0100 Subject: [PATCH] [WIP] Integration of TestEnvironment --- src/environment.rs | 18 ++++++++++++++- src/integration_test.rs | 49 ++++++++++++++++++++++++++++++++--------- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/environment.rs b/src/environment.rs index 6955767..6328bfd 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -9,9 +9,18 @@ use std::{env, io}; pub trait Environment { fn args_os(&self) -> Vec; - fn var(&self, key: K) -> Result + fn var_os(&self, key: K) -> Option where K: AsRef; + fn var(&self, key: K) -> Result + where + K: AsRef, + { + self + .var_os(key) + .ok_or(VarError::NotPresent) + .and_then(|s| s.into_string().map_err(VarError::NotUnicode)) + } fn set_var(&mut self, key: K, value: V) where K: AsRef, @@ -28,6 +37,13 @@ impl Environment for Prod { env::args_os().collect() } + fn var_os(&self, key: K) -> Option + where + K: AsRef + { + env::var_os(key) + } + fn var(&self, key: K) -> Result where K: AsRef, diff --git a/src/integration_test.rs b/src/integration_test.rs index 160349e..e177d30 100644 --- a/src/integration_test.rs +++ b/src/integration_test.rs @@ -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, + /// set environment variables - we assume a pure environment by default + env_vars: HashMap, + /// home directory, relative to the target/test folder + home_dir: PathBuf, + /// pending lines of std input + stdin: VecDeque, + /// 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, + }, + File { + contents: String, + } +} impl Environment for TestEnvironment { fn args_os(&self) -> Vec { - todo!() + self.args_os.clone() } - fn var(&self, key: K) -> Result - where - K: AsRef, + fn var_os(&self, key: K) -> Option + where + K: AsRef { - todo!() + self.env_vars.get(key.as_ref()).map(|s| s.into()) } fn set_var(&mut self, key: K, value: V) @@ -26,15 +55,15 @@ impl Environment for TestEnvironment { K: AsRef, V: AsRef, { - todo!() + self.env_vars.insert(key.as_ref().into(), value.as_ref().into()); } fn get_home_directory(&self) -> Option { - todo!() + PathBuf::from("target/integration_test").join(&self.home_dir).into() } fn read_line(&mut self) -> Result { - todo!() + self.stdin.pop_front().ok_or_else(|| Error::other("Unexpected call to read_line: No input prepared")) } }