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 download tests.

* Finish tests.

* Actually run in CI.

* Fix windows.
  • Loading branch information
milesj authored Jul 12, 2023
1 parent 6060785 commit ae660cd
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ jobs:
with:
bins: cargo-wasi, cargo-nextest
- run: cargo wasi build -p bun_plugin
- run: cargo nextest run
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ crate-type = ['cdylib']

[dependencies]
extism-pdk = "0.3.3"
proto_pdk = "0.2.1"
proto_pdk = "0.2.2"
serde = "1.0.167"

[dev-dependencies]
proto_core = "0.12.2"
proto_pdk_test_utils = "0.1.2"
starbase_sandbox = "0.1.5"
tokio = "1.29.1"
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +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::*;
181 changes: 181 additions & 0 deletions tests/download_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
use proto_pdk::*;
use proto_pdk_test_utils::{create_plugin, generate_download_install_tests};
use starbase_sandbox::create_empty_sandbox;
use std::path::PathBuf;

#[cfg(not(windows))]
generate_download_install_tests!("bun-test", "0.6.0");

#[test]
fn supports_linux_arm64() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::Arm64,
os: HostOS::Linux,
version: "1.2.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: Some("bun-linux-aarch64".into()),
bin_path: None,
checksum_name: None,
checksum_url: Some(
"https://github.com/oven-sh/bun/releases/download/bun-v1.2.0/SHASUMS256.txt".into()
),
download_name: Some("bun-linux-aarch64.zip".into()),
download_url:
"https://github.com/oven-sh/bun/releases/download/bun-v1.2.0/bun-linux-aarch64.zip"
.into()
}
);
}

#[test]
fn supports_linux_x64() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::X64,
os: HostOS::Linux,
version: "1.2.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: Some("bun-linux-x64".into()),
bin_path: None,
checksum_name: None,
checksum_url: Some(
"https://github.com/oven-sh/bun/releases/download/bun-v1.2.0/SHASUMS256.txt".into()
),
download_name: Some("bun-linux-x64.zip".into()),
download_url:
"https://github.com/oven-sh/bun/releases/download/bun-v1.2.0/bun-linux-x64.zip"
.into()
}
);
}

#[test]
fn supports_macos_arm64() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::Arm64,
os: HostOS::MacOS,
version: "1.2.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: Some("bun-darwin-aarch64".into()),
bin_path: None,
checksum_name: None,
checksum_url: Some(
"https://github.com/oven-sh/bun/releases/download/bun-v1.2.0/SHASUMS256.txt".into()
),
download_name: Some("bun-darwin-aarch64.zip".into()),
download_url:
"https://github.com/oven-sh/bun/releases/download/bun-v1.2.0/bun-darwin-aarch64.zip"
.into()
}
);
}

#[test]
fn supports_macos_x64() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

assert_eq!(
plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::X64,
os: HostOS::MacOS,
version: "1.2.0".into(),
..Default::default()
}
}),
DownloadPrebuiltOutput {
archive_prefix: Some("bun-darwin-x64".into()),
bin_path: None,
checksum_name: None,
checksum_url: Some(
"https://github.com/oven-sh/bun/releases/download/bun-v1.2.0/SHASUMS256.txt".into()
),
download_name: Some("bun-darwin-x64.zip".into()),
download_url:
"https://github.com/oven-sh/bun/releases/download/bun-v1.2.0/bun-darwin-x64.zip"
.into()
}
);
}

#[test]
#[should_panic(expected = "Unable to install Bun, unsupported platform windows.")]
fn doesnt_support_windows() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

plugin.download_prebuilt(DownloadPrebuiltInput {
env: Environment {
arch: HostArch::X64,
os: HostOS::Windows,
version: "1.2.0".into(),
..Default::default()
},
});
}

#[test]
fn locates_unix_bin() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

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

#[test]
fn locates_windows_bin() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

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

#[test]
fn registers_metadata() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

assert_eq!(
plugin.register_tool(ToolMetadataInput::default()),
ToolMetadataOutput {
name: "Bun".into(),
..ToolMetadataOutput::default()
}
);
}
5 changes: 5 additions & 0 deletions tests/shims_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use proto_pdk_test_utils::{create_plugin, generate_global_shims_test};
use starbase_sandbox::{assert_snapshot, create_empty_sandbox};

#[cfg(not(windows))]
generate_global_shims_test!("bun-test", ["bunx"]);
13 changes: 13 additions & 0 deletions tests/snapshots/shims_test__creates_global_shims-2.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(\"bunx\")).unwrap()"
---
#!/usr/bin/env bash
set -e
[ -n "$PROTO_DEBUG" ] && set -x



exec proto run bun-test -- x "$@"


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(\"bun-test\")).unwrap()"
---
#!/usr/bin/env bash
set -e
[ -n "$PROTO_DEBUG" ] && set -x



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


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

generate_resolve_versions_tests!("bun-test", {
"0.4" => "0.4.0",
"0.5.1" => "0.5.1",
});

#[test]
fn loads_versions_from_git() {
let sandbox = create_empty_sandbox();
let plugin = create_plugin("bun-test", sandbox.path());

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_plugin("bun-test", sandbox.path());

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 ae660cd

Please sign in to comment.