From 0fdb3601fcbb49009ad45207dca2b653e2dd4d49 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Fri, 11 Aug 2023 10:21:18 -0700 Subject: [PATCH] tests: Add unit tests. (#1) * Add initial files. * Add shim tests. * Add download tests. * Update deps. * Update readme. * Fix format. * Fix lib. * Fix test. * Fix paths. * Skip windows. --- .prototools | 2 +- Cargo.toml | 6 +- README.md | 8 +- src/lib.rs | 2 + src/proto.rs | 28 +- tests/__fixtures__/schemas/base.toml | 19 ++ tests/__fixtures__/schemas/bins.toml | 23 ++ tests/__fixtures__/schemas/shim-local.toml | 16 ++ .../__fixtures__/schemas/shim-no-global.toml | 15 ++ tests/download_test.rs | 255 ++++++++++++++++++ tests/metadata_test.rs | 21 ++ tests/shims_test.rs | 44 +++ .../shims_test__can_create_local_shim.snap | 20 ++ .../shims_test__creates_global_shims.snap | 13 + tests/versions_test.rs | 43 +++ 15 files changed, 495 insertions(+), 20 deletions(-) create mode 100644 tests/__fixtures__/schemas/base.toml create mode 100644 tests/__fixtures__/schemas/bins.toml create mode 100644 tests/__fixtures__/schemas/shim-local.toml create mode 100644 tests/__fixtures__/schemas/shim-no-global.toml create mode 100644 tests/download_test.rs create mode 100644 tests/metadata_test.rs create mode 100644 tests/shims_test.rs create mode 100644 tests/snapshots/shims_test__can_create_local_shim.snap create mode 100644 tests/snapshots/shims_test__creates_global_shims.snap create mode 100644 tests/versions_test.rs diff --git a/.prototools b/.prototools index dd100e0..e007b67 100644 --- a/.prototools +++ b/.prototools @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 3b7fd34..8fbc9d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/README.md b/README.md index 206e8f3..260a4e5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index b8010b4..4311d51 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::*; diff --git a/src/proto.rs b/src/proto.rs index 1fe99ce..f6332af 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -61,14 +61,10 @@ pub fn register_tool(Json(_): Json) -> FnResult 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) -> FnResult) -> FnResult) -> FnResult { 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()); } } _ => {} diff --git a/tests/__fixtures__/schemas/base.toml b/tests/__fixtures__/schemas/base.toml new file mode 100644 index 0000000..0812bcc --- /dev/null +++ b/tests/__fixtures__/schemas/base.toml @@ -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" diff --git a/tests/__fixtures__/schemas/bins.toml b/tests/__fixtures__/schemas/bins.toml new file mode 100644 index 0000000..f62827e --- /dev/null +++ b/tests/__fixtures__/schemas/bins.toml @@ -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" diff --git a/tests/__fixtures__/schemas/shim-local.toml b/tests/__fixtures__/schemas/shim-local.toml new file mode 100644 index 0000000..086e554 --- /dev/null +++ b/tests/__fixtures__/schemas/shim-local.toml @@ -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" diff --git a/tests/__fixtures__/schemas/shim-no-global.toml b/tests/__fixtures__/schemas/shim-no-global.toml new file mode 100644 index 0000000..9b506ca --- /dev/null +++ b/tests/__fixtures__/schemas/shim-no-global.toml @@ -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 diff --git a/tests/download_test.rs b/tests/download_test.rs new file mode 100644 index 0000000..cb1591d --- /dev/null +++ b/tests/download_test.rs @@ -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()) + ); +} diff --git a/tests/metadata_test.rs b/tests/metadata_test.rs new file mode 100644 index 0000000..1a60451 --- /dev/null +++ b/tests/metadata_test.rs @@ -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() + } + ); +} diff --git a/tests/shims_test.rs b/tests/shims_test.rs new file mode 100644 index 0000000..d089c39 --- /dev/null +++ b/tests/shims_test.rs @@ -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")); +} diff --git a/tests/snapshots/shims_test__can_create_local_shim.snap b/tests/snapshots/shims_test__can_create_local_shim.snap new file mode 100644 index 0000000..0d0277d --- /dev/null +++ b/tests/snapshots/shims_test__can_create_local_shim.snap @@ -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" "$@" + + diff --git a/tests/snapshots/shims_test__creates_global_shims.snap b/tests/snapshots/shims_test__creates_global_shims.snap new file mode 100644 index 0000000..7cb414e --- /dev/null +++ b/tests/snapshots/shims_test__creates_global_shims.snap @@ -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 -- "$@" + + diff --git a/tests/versions_test.rs b/tests/versions_test.rs new file mode 100644 index 0000000..4ae0c34 --- /dev/null +++ b/tests/versions_test.rs @@ -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()); +}