Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up execution_spec #1727

Merged
merged 8 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
83 changes: 83 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ convert_case = "0.6.0"
rand = "0.8.5"
tailcall-macros = { path = "tailcall-macros" }
tonic-types = "0.11.0"
datatest-stable = "0.2.7"
tokio-test = "0.4.4"


[dev-dependencies]
Expand Down Expand Up @@ -244,3 +246,8 @@ harness = false
[[bench]]
name = "protobuf_convert_output"
harness = false

[[test]]
name = "execution_spec"
harness = false

23 changes: 23 additions & 0 deletions tests/core/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extern crate core;

use std::borrow::Cow;
use std::collections::HashMap;

use tailcall::EnvIO;

#[derive(Clone)]
pub struct TestEnvIO {
vars: HashMap<String, String>,
}

impl EnvIO for TestEnvIO {
fn get(&self, key: &str) -> Option<Cow<'_, str>> {
self.vars.get(key).map(Cow::from)
}
}

impl TestEnvIO {
pub fn init(vars: Option<HashMap<String, String>>) -> Self {
Self { vars: vars.unwrap_or_default() }
}
}
67 changes: 67 additions & 0 deletions tests/core/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
extern crate core;

use std::path::PathBuf;

use anyhow::{anyhow, Context};
use tailcall::FileIO;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use super::runtime::ExecutionSpec;
pub struct MockFileSystem {
spec: ExecutionSpec,
}

impl MockFileSystem {
pub fn new(spec: ExecutionSpec) -> MockFileSystem {
MockFileSystem { spec }
}
}

#[async_trait::async_trait]
impl FileIO for MockFileSystem {
async fn write<'a>(&'a self, _path: &'a str, _content: &'a [u8]) -> anyhow::Result<()> {
Err(anyhow!("Cannot write to a file in an execution spec"))
}

async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
let base = PathBuf::from(path);
let path = base
.file_name()
.context("Invalid file path")?
.to_str()
.context("Invalid OsString")?;
match self.spec.files.get(path) {
Some(x) => Ok(x.to_owned()),
None => Err(anyhow!("No such file or directory (os error 2)")),
}
}
}

#[derive(Clone)]
pub struct TestFileIO {}

impl TestFileIO {
pub fn init() -> Self {
TestFileIO {}
}
}

#[async_trait::async_trait]
impl FileIO for TestFileIO {
async fn write<'a>(&'a self, path: &'a str, content: &'a [u8]) -> anyhow::Result<()> {
let mut file = tokio::fs::File::create(path).await?;
file.write_all(content)
.await
.map_err(|e| anyhow!("{}", e))?;
Ok(())
}

async fn read<'a>(&'a self, path: &'a str) -> anyhow::Result<String> {
let mut file = tokio::fs::File::open(path).await?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)
.await
.map_err(|e| anyhow!("{}", e))?;
Ok(String::from_utf8(buffer)?)
}
}
Loading
Loading