Skip to content

Commit

Permalink
internal: Improve testing API further. (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Jul 12, 2023
1 parent 7e78315 commit e7222b1
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 90 deletions.
19 changes: 19 additions & 0 deletions .github/actions/delete-target-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require("fs");
const path = require("path");

for (let file of [
"plugins/target/debug",
"plugins/target/wasm32-wasi/debug/.fingerprint",
"plugins/target/wasm32-wasi/debug/build",
"plugins/target/wasm32-wasi/debug/deps",
"plugins/target/wasm32-wasi/debug/incremental",
]) {
try {
fs.rmSync(path.join(process.env.GITHUB_WORKSPACE || process.cwd(), file), {
recursive: true,
force: true,
});
} catch (e) {
console.error(e);
}
}
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ jobs:
- uses: moonrepo/setup-rust@v0
with:
bins: cargo-wasi, cargo-nextest
cache: false
- run: cd plugins && cargo wasi build -p proto_wasm_test
if: ${{ runner.os != 'Windows' }}
# Windows runs out of disk space
- run: |
cd plugins;
cargo wasi build -p proto_wasm_test;
node ../.github/actions/delete-target-files.js;
if: ${{ runner.os == 'Windows' }}
- run: cargo nextest run --workspace --exclude proto_pdk
run:
name: Run
Expand Down
20 changes: 17 additions & 3 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ pub use verifier::*;

use std::path::{Path, PathBuf};

#[derive(Clone, Debug)]
pub struct Proto {
pub bin_dir: PathBuf,
pub plugins_dir: PathBuf,
pub temp_dir: PathBuf,
pub tools_dir: PathBuf,
pub home_dir: PathBuf,
pub home: PathBuf,
pub root: PathBuf,
}

Expand All @@ -53,18 +54,31 @@ impl Proto {
plugins_dir: root.join("plugins"),
temp_dir: root.join("temp"),
tools_dir: root.join("tools"),
home_dir: get_home_dir()?,
home: get_home_dir()?,
root,
})
}

pub fn new_testing(sandbox: &Path) -> Self {
let root = sandbox.join(".proto");

Proto {
bin_dir: root.join("bin"),
plugins_dir: root.join("plugins"),
temp_dir: root.join("temp"),
tools_dir: root.join("tools"),
home: sandbox.join(".home"),
root: root.to_owned(),
}
}

pub fn from(root: &Path) -> Self {
Proto {
bin_dir: root.join("bin"),
plugins_dir: root.join("plugins"),
temp_dir: root.join("temp"),
tools_dir: root.join("tools"),
home_dir: get_home_dir().expect("Missing home directory."),
home: get_home_dir().expect("Missing home directory."),
root: root.to_owned(),
}
}
Expand Down
10 changes: 8 additions & 2 deletions crates/core/src/shimmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct ShimContext<'tool> {
pub after_args: Option<&'tool str>,

// TOOL INFO
/// Path to the `~/.proto/bin` directory.
pub globals_bin_dir: Option<&'tool Path>,
/// Path to the proto tool installation directory.
pub tool_dir: Option<&'tool Path>,
pub tool_version: Option<&'tool str>,
Expand Down Expand Up @@ -169,7 +171,11 @@ pub fn create_global_shim_with_name<'tool, C: AsRef<ShimContext<'tool>>>(
find_only: bool,
) -> Result<PathBuf, ProtoError> {
let context = context.as_ref();
let shim_path = get_bin_dir()?.join(get_shim_file_name(name, true));
let globals_dir = get_bin_dir()?;
let shim_path = context
.globals_bin_dir
.unwrap_or(globals_dir.as_path())
.join(get_shim_file_name(name, true));

if !find_only {
debug!(tool = context.bin, file = ?shim_path, "Creating global shim");
Expand All @@ -187,7 +193,7 @@ pub fn create_local_shim<'tool, C: AsRef<ShimContext<'tool>>>(
let shim_path = context
.tool_dir
.as_ref()
.unwrap()
.expect("Missing tool dir for shims.")
.join("shims")
.join(get_shim_file_name(context.bin, false));

Expand Down
15 changes: 10 additions & 5 deletions crates/pdk-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ pub use proto_wasm_plugin::WasmPlugin;
pub use wrapper::WasmTestWrapper;

use proto_core::Proto;
use std::env;
use std::path::{Path, PathBuf};
use std::{env, fs};

static mut LOGGING: bool = false;

pub fn create_plugin(name: &str, id: &str, sandbox: &Path) -> WasmTestWrapper {
pub fn create_plugin(id: &str, sandbox: &Path) -> WasmTestWrapper {
let mut wasm_target_dir =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR!"));
let wasm_file_name = env::var("CARGO_PKG_NAME").expect("Missing CARGO_PKG_NAME!");

loop {
let next_target = wasm_target_dir.join("target/wasm32-wasi/debug");
Expand All @@ -33,11 +34,11 @@ pub fn create_plugin(name: &str, id: &str, sandbox: &Path) -> WasmTestWrapper {
if !LOGGING {
LOGGING = true;

extism::set_log_file(wasm_target_dir.join(format!("{name}.log")), None);
extism::set_log_file(wasm_target_dir.join(format!("{wasm_file_name}.log")), None);
}
};

let wasm_file = wasm_target_dir.join(format!("{name}.wasm"));
let wasm_file = wasm_target_dir.join(format!("{wasm_file_name}.wasm"));

if !wasm_file.exists() {
panic!(
Expand All @@ -46,7 +47,11 @@ pub fn create_plugin(name: &str, id: &str, sandbox: &Path) -> WasmTestWrapper {
);
}

// Folders must exists for WASM to compile correctly!
fs::create_dir_all(sandbox.join(".home")).unwrap();
fs::create_dir_all(sandbox.join(".proto")).unwrap();

WasmTestWrapper {
tool: WasmPlugin::new(Proto::from(sandbox), id.into(), wasm_file).unwrap(),
tool: WasmPlugin::new(Proto::new_testing(sandbox), id.into(), wasm_file).unwrap(),
}
}
Loading

0 comments on commit e7222b1

Please sign in to comment.