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

Commit

Permalink
tests: Add unit tests. (#1)
Browse files Browse the repository at this point in the history
* Add initial files.

* Add shim tests.

* Add download tests.

* Update deps.

* Update readme.

* Fix format.

* Fix lib.

* Fix test.

* Fix paths.

* Skip windows.
milesj authored Aug 11, 2023
1 parent 0e6851a commit 0fdb360
Showing 15 changed files with 495 additions and 20 deletions.
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
@@ -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"
@@ -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
@@ -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
@@ -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,
}
}
}
@@ -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
@@ -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 {
@@ -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()
}))
}

@@ -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
@@ -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());
}
}
_ => {}
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
255 changes: 255 additions & 0 deletions tests/download_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
use proto_pdk_test_utils::*;
use starbase_sandbox::{create_empty_sandbox, locate_fixture};
use std::path::PathBuf;

generate_download_install_tests!(
"schema-test",
"1.10.0",
Some(locate_fixture("schemas").join("base.toml"))
);

#[test]
fn supports_linux_arm64() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::Arm64,
os: HostOS::Linux,
version: "20.0.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: Some("moon-linux".into()),
checksum_name: Some("CHECKSUM.txt".into()),
checksum_url: None,
download_name: Some("moon-aarch64-unknown-linux-gnu".into()),
download_url: "https://github.com/moonrepo/moon/releases/download/v20.0.0/moon-aarch64-unknown-linux-gnu".into()
}
);
}

#[test]
fn supports_linux_x64() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::X64,
os: HostOS::Linux,
version: "20.0.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: Some("moon-linux".into()),
checksum_name: Some("CHECKSUM.txt".into()),
checksum_url: None,
download_name: Some("moon-x86_64-unknown-linux-gnu".into()),
download_url: "https://github.com/moonrepo/moon/releases/download/v20.0.0/moon-x86_64-unknown-linux-gnu".into()
}
);
}

#[test]
fn supports_macos_arm64() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::Arm64,
os: HostOS::MacOS,
version: "20.0.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: None,
checksum_name: Some("SHASUM256.txt".into()),
checksum_url: None,
download_name: Some("moon-aarch64-apple-darwin".into()),
download_url: "https://github.com/moonrepo/moon/releases/download/v20.0.0/moon-aarch64-apple-darwin".into()
}
);
}

#[test]
fn supports_macos_x64() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::X64,
os: HostOS::MacOS,
version: "20.0.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: None,
checksum_name: Some("SHASUM256.txt".into()),
checksum_url: None,
download_name: Some("moon-x86_64-apple-darwin".into()),
download_url: "https://github.com/moonrepo/moon/releases/download/v20.0.0/moon-x86_64-apple-darwin".into()
}
);
}

#[test]
fn supports_windows_arm64() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::Arm64,
os: HostOS::Windows,
version: "20.0.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: None,
checksum_name: Some("CHECKSUM.txt".into()),
checksum_url: None,
download_name: Some("moon-aarch64-pc-windows-msvc.exe".into()),
download_url: "https://github.com/moonrepo/moon/releases/download/v20.0.0/moon-aarch64-pc-windows-msvc.exe".into()
}
);
}

#[test]
fn supports_windows_x86() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::X86,
os: HostOS::Windows,
version: "20.0.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: None,
checksum_name: Some("CHECKSUM.txt".into()),
checksum_url: None,
download_name: Some("moon-x86-pc-windows-msvc.exe".into()),
download_url: "https://github.com/moonrepo/moon/releases/download/v20.0.0/moon-x86-pc-windows-msvc.exe".into()
}
);
}

#[test]
fn locates_linux_bin() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin
.locate_bins(LocateBinsInput {
env: Environment {
arch: HostArch::Arm64,
os: HostOS::Linux,
version: "20.0.0".into(),
..Default::default()
},
home_dir: PathBuf::new(),
tool_dir: PathBuf::new(),
})
.bin_path,
Some("lin/moon".into())
);
}

#[test]
fn locates_macos_bin() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin
.locate_bins(LocateBinsInput {
env: Environment {
arch: HostArch::X64,
os: HostOS::MacOS,
version: "20.0.0".into(),
..Default::default()
},
home_dir: PathBuf::new(),
tool_dir: PathBuf::new(),
})
.bin_path,
Some("mac/moon".into())
);
}

#[test]
fn locates_windows_bin() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("bins.toml"),
);

assert_eq!(
plugin
.locate_bins(LocateBinsInput {
env: Environment {
arch: HostArch::X64,
os: HostOS::Windows,
version: "20.0.0".into(),
..Default::default()
},
home_dir: PathBuf::new(),
tool_dir: PathBuf::new(),
})
.bin_path,
Some("win/moon.exe".into())
);
}
21 changes: 21 additions & 0 deletions tests/metadata_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use proto_pdk_test_utils::*;
use starbase_sandbox::{create_empty_sandbox, locate_fixture};

#[test]
fn registers_metadata() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("base.toml"),
);

assert_eq!(
plugin.register_tool(ToolMetadataInput::default()),
ToolMetadataOutput {
name: "moon-test".into(),
type_of: PluginType::CLI,
..ToolMetadataOutput::default()
}
);
}
44 changes: 44 additions & 0 deletions tests/shims_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use proto_pdk_test_utils::*;
use starbase_sandbox::{assert_snapshot, create_empty_sandbox, locate_fixture};

#[cfg(not(windows))]
generate_global_shims_test!(
"schema-test",
[],
Some(locate_fixture("schemas").join("base.toml"))
);

#[tokio::test]
async fn doesnt_create_global_shim() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas/shim-no-global.toml"),
);

plugin.tool.create_shims(false).await.unwrap();

assert!(!sandbox.path().join(".proto/bin/schema-test").exists());
}

#[cfg(not(windows))]
#[tokio::test]
async fn can_create_local_shim() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas/shim-local.toml"),
);

plugin.tool.create_shims(false).await.unwrap();

assert_snapshot!(std::fs::read_to_string(
sandbox
.path()
.join(".proto/tools/schema-test/latest/shims/schema-test")
)
.unwrap()
.replace(sandbox.path().to_str().unwrap(), "/workspace"));
}
20 changes: 20 additions & 0 deletions tests/snapshots/shims_test__can_create_local_shim.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: tests/shims_test.rs
expression: "std::fs::read_to_string(sandbox.path().join(\".proto/tools/schema-test/latest/shims/schema-test\")).unwrap().replace(sandbox.path().to_str().unwrap(),\n \"/workspace\")"
---
#!/usr/bin/env bash
set -e
[ -n "$PROTO_DEBUG" ] && set -x



export PROTO_SCHEMA_TEST_DIR="/workspace/.proto/tools/schema-test/latest"






exec "/workspace/.proto/tools/schema-test/latest/schema-test" "$@"


13 changes: 13 additions & 0 deletions tests/snapshots/shims_test__creates_global_shims.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: tests/shims_test.rs
expression: "std::fs::read_to_string(sandbox.path().join(\".proto/bin\").join(if cfg!(windows)\n {\n format!(\"{}.cmd\", \"schema-test\")\n } else { \"schema-test\".to_string() })).unwrap()"
---
#!/usr/bin/env bash
set -e
[ -n "$PROTO_DEBUG" ] && set -x



exec proto run schema-test -- "$@"


43 changes: 43 additions & 0 deletions tests/versions_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use proto_pdk_test_utils::*;
use starbase_sandbox::{create_empty_sandbox, locate_fixture};

generate_resolve_versions_tests!(
"schema-test",
{
"1.0" => "1.0.3",
"1.4" => "1.4.0",
"1.5" => "1.5.1",
"1" => "1.11.1",
},
Some(locate_fixture("schemas").join("base.toml"))
);

#[test]
fn loads_versions_from_git_tags() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("base.toml"),
);

let output = plugin.load_versions(LoadVersionsInput::default());

assert!(!output.versions.is_empty());
}

#[test]
fn sets_latest_alias() {
let sandbox = create_empty_sandbox();
let plugin = create_schema_plugin(
"schema-test",
sandbox.path(),
locate_fixture("schemas").join("base.toml"),
);

let output = plugin.load_versions(LoadVersionsInput::default());

assert!(output.latest.is_some());
assert!(output.aliases.contains_key("latest"));
assert_eq!(output.aliases.get("latest"), output.latest.as_ref());
}

0 comments on commit 0fdb360

Please sign in to comment.