Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

tests: Add unit tests. #1

Merged
merged 10 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .prototools
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[plugins]
schema-test = "source:target/wasm32-wasi/debug/schema_plugin.wasm"
moon-test = "source:https://raw.githubusercontent.com/moonrepo/moon/master/proto-plugin.toml"
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ crate-type = ['cdylib']

[dependencies]
extism-pdk = "0.3.3"
proto_pdk = "0.4.2"
proto_schema_plugin = { version = "0.8.1", path = "../../proto/crates/schema-plugin" }
proto_pdk = { version = "0.4.3" }
proto_schema_plugin = { version = "0.9.1" }
regex = "1.9.3"
serde = "1.0.183"
serde_json = "1.0.104"
Expand All @@ -20,6 +20,6 @@ starbase_utils = { version = "0.2.17", default-features = false, features = [
] }

[dev-dependencies]
proto_pdk_test_utils = "0.3.3"
proto_pdk_test_utils = { version = "0.3.5" }
starbase_sandbox = "0.1.8"
tokio = "1.30.0"
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ Build the plugin:
cargo build --target wasm32-wasi
```

Test the plugin by running `proto` commands. Requires proto >= v0.12.
Test the plugin by running `proto` commands. Requires proto >= v0.14.

```shell
proto install schema-test
proto list-remote schema-test
proto install moon-test
proto list-remote moon-test
```

> Since this plugin requires an external schema file, its testing uses moon: https://moonrepo.dev/docs/install#proto
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// WASM cannot be executed through the test runner and we need to avoid building
// WASM code for non-WASM targets. We can solve both of these with a cfg flag.

#[cfg(not(test))]
mod proto;

#[cfg(not(test))]
pub use proto::*;
28 changes: 15 additions & 13 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,10 @@ pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMeta
}

fn is_musl() -> bool {
if cfg!(macos) {
false
} else {
unsafe {
match exec_command(Json(ExecCommandInput::pipe("ldd", ["--version"]))) {
Ok(res) => res.0.stdout.contains("musl"),
Err(_) => false,
}
unsafe {
match exec_command(Json(ExecCommandInput::pipe("ldd", ["--version"]))) {
Ok(res) => res.0.stdout.contains("musl"),
Err(_) => false,
}
}
}
Expand All @@ -84,7 +80,14 @@ fn interpolate_tokens(value: &str, schema: &Schema, env: &Environment) -> String

// Avoid detecting musl unless requested
if value.contains("{libc}") {
value = value.replace("{libc}", if is_musl() { "musl" } else { "gnu" });
value = value.replace(
"{libc}",
if env.os != HostOS::MacOS && env.os != HostOS::Windows && is_musl() {
"musl"
} else {
"gnu"
},
);
}

value
Expand All @@ -110,7 +113,7 @@ pub fn download_prebuilt(
&input.env,
);
let checksum_url = schema.install.checksum_url.as_ref().map(|url| {
interpolate_tokens(&url, &schema, &input.env).replace("{checksum_file}", &checksum_file)
interpolate_tokens(url, &schema, &input.env).replace("{checksum_file}", &checksum_file)
});

Ok(Json(DownloadPrebuiltOutput {
Expand All @@ -132,7 +135,6 @@ pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBi
fallback_last_globals_dir: true,
globals_lookup_dirs: schema.globals.lookup_dirs,
globals_prefix: schema.globals.package_prefix,
..LocateBinsOutput::default()
}))
}

Expand All @@ -151,7 +153,7 @@ pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVers
if let Some(repository) = schema.resolve.git_url {
let pattern = regex::Regex::new(&schema.resolve.git_tag_pattern)?;

let tags = load_git_tags(&repository)?
let tags = load_git_tags(repository)?
.into_iter()
.filter_map(|t| {
pattern
Expand All @@ -175,7 +177,7 @@ pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVers
}
JsonValue::Object(o) => {
if let Some(JsonValue::String(v)) = o.get(version_key) {
versions.push(remove_v_prefix(&v).to_string());
versions.push(remove_v_prefix(v).to_string());
}
}
_ => {}
Expand Down
19 changes: 19 additions & 0 deletions tests/__fixtures__/schemas/base.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
bin = "moon-test"
name = "moon-test"
type = "cli"

[platform.linux]
download-file = "moon-{arch}-unknown-linux-{libc}"

[platform.macos]
download-file = "moon-{arch}-apple-darwin"

[platform.windows]
download-file = "moon-{arch}-pc-windows-msvc.exe"

[install]
download-url = "https://github.com/moonrepo/moon/releases/download/v{version}/{download_file}"
unpack = false

[resolve]
git-url = "https://github.com/moonrepo/moon"
23 changes: 23 additions & 0 deletions tests/__fixtures__/schemas/bins.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
bin = "moon-test"
name = "moon-test"
type = "cli"

[platform.linux]
archive-prefix = "moon-linux"
bin-path = "lin/moon"
download-file = "moon-{arch}-unknown-linux-gnu"

[platform.macos]
bin-path = "mac/moon"
checksum-file = "SHASUM256.txt"
download-file = "moon-{arch}-apple-darwin"

[platform.windows]
bin-path = "win/moon.exe"
download-file = "moon-{arch}-pc-windows-msvc.exe"

[install]
download-url = "https://github.com/moonrepo/moon/releases/download/v{version}/{download_file}"

[resolve]
git-url = "https://github.com/moonrepo/moon"
16 changes: 16 additions & 0 deletions tests/__fixtures__/schemas/shim-local.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
bin = "moon-test"
name = "moon-test"
type = "cli"

[platform.linux]
download-file = "moon-{arch}-unknown-linux-{libc}"

[platform.macos]
download-file = "moon-{arch}-apple-darwin"

[platform.windows]
download-file = "moon-{arch}-pc-windows-msvc.exe"

[shim]
local = true
parent_bin = "prnt"
15 changes: 15 additions & 0 deletions tests/__fixtures__/schemas/shim-no-global.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bin = "moon-test"
name = "moon-test"
type = "cli"

[platform.linux]
download-file = "moon-{arch}-unknown-linux-{libc}"

[platform.macos]
download-file = "moon-{arch}-apple-darwin"

[platform.windows]
download-file = "moon-{arch}-pc-windows-msvc.exe"

[shim]
global = false
Loading