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

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 2, 2023
1 parent a1810bc commit d4b32ae
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
37 changes: 37 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use extism_pdk::*;
use proto_pdk::*;
use std::path::PathBuf;

#[host_fn]
extern "ExtismHost" {
fn get_env_var(name: &str) -> String;
}

pub fn get_rustup_home(env: &HostEnvironment) -> Result<PathBuf, Error> {
// Variable returns a real path
Ok(host_env!("RUSTUP_HOME")
.map(PathBuf::from)
// So we need our fallback to also be a real path
.unwrap_or_else(|| env.home_dir.real_path().join(".rustup")))
}

pub fn get_channel_from_version(spec: &VersionSpec) -> String {
if spec.is_canary() {
"nightly".to_owned()
} else {
spec.to_string()
}
}

pub fn is_non_version_channel(spec: &UnresolvedVersionSpec) -> bool {
match spec {
UnresolvedVersionSpec::Canary => true,
UnresolvedVersionSpec::Alias(value) => {
value == "stable"
|| value == "beta"
|| value == "nightly"
|| value.starts_with("nightly")
}
_ => false,
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod toolchain_toml;
// 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 helpers;

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

Expand Down
31 changes: 1 addition & 30 deletions src/proto.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::helpers::*;
use crate::toolchain_toml::ToolchainToml;
use extism_pdk::*;
use proto_pdk::*;
Expand All @@ -7,20 +8,11 @@ use std::path::PathBuf;
#[host_fn]
extern "ExtismHost" {
fn exec_command(input: Json<ExecCommandInput>) -> Json<ExecCommandOutput>;
fn get_env_var(name: &str) -> String;
fn host_log(input: Json<HostLogInput>);
}

static NAME: &str = "Rust";

fn get_rustup_home(env: &HostEnvironment) -> Result<PathBuf, Error> {
// Variable returns a real path
Ok(host_env!("RUSTUP_HOME")
.map(PathBuf::from)
// So we need our fallback to also be a real path
.unwrap_or_else(|| env.home_dir.real_path().join(".rustup")))
}

#[plugin_fn]
pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMetadataOutput>> {
let env = get_proto_environment()?;
Expand All @@ -39,27 +31,6 @@ pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMeta
}))
}

fn get_channel_from_version(spec: &VersionSpec) -> String {
if spec.is_canary() {
"nightly".to_owned()
} else {
spec.to_string()
}
}

fn is_non_version_channel(spec: &UnresolvedVersionSpec) -> bool {
match spec {
UnresolvedVersionSpec::Canary => true,
UnresolvedVersionSpec::Alias(value) => {
value == "stable"
|| value == "beta"
|| value == "nightly"
|| value.starts_with("nightly")
}
_ => false,
}
}

#[plugin_fn]
pub fn native_install(
Json(input): Json<NativeInstallInput>,
Expand Down
6 changes: 3 additions & 3 deletions tests/download_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn locates_linux_bin() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "1.69.0".into(),
version: VersionSpec::parse("1.69.0").unwrap(),
..Default::default()
},
})
Expand All @@ -43,7 +43,7 @@ fn locates_macos_bin() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "1.69.0".into(),
version: VersionSpec::parse("1.69.0").unwrap(),
..Default::default()
},
})
Expand All @@ -67,7 +67,7 @@ fn locates_windows_bin() {
plugin
.locate_bins(LocateBinsInput {
context: ToolContext {
version: "1.69.0".into(),
version: VersionSpec::parse("1.69.0").unwrap(),
..Default::default()
},
})
Expand Down
5 changes: 4 additions & 1 deletion tests/metadata_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ fn registers_metadata() {
});

assert_eq!(metadata.name, "Rust");
assert_eq!(metadata.default_version, Some("stable".to_owned()));
assert_eq!(
metadata.default_version,
Some(UnresolvedVersionSpec::Alias("stable".to_owned()))
);
assert!(metadata.inventory.disable_progress_bars);
assert!(metadata.inventory.override_dir.is_some());
assert!(metadata.inventory.version_suffix.is_some());
Expand Down
4 changes: 2 additions & 2 deletions tests/versions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn parses_rust_toolchain() {
file: "rust-toolchain".into(),
}),
ParseVersionFileOutput {
version: Some("1.60.0".into()),
version: Some(UnresolvedVersionSpec::parse("1.60.0").unwrap()),
}
);
}
Expand Down Expand Up @@ -72,7 +72,7 @@ fn parses_rust_toolchain_toml() {
file: "rust-toolchain.toml".into(),
}),
ParseVersionFileOutput {
version: Some("1.70.0".into()),
version: Some(UnresolvedVersionSpec::parse("1.70.0").unwrap()),
}
);
}
Expand Down

0 comments on commit d4b32ae

Please sign in to comment.