From 4632ee4c63362e33dd5316877ab20465525fba77 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 22 Nov 2023 14:11:42 -0800 Subject: [PATCH] Update tests. --- crates/common/src/commands.rs | 7 +- crates/node-depman/src/proto.rs | 6 +- crates/node-depman/tests/globals_test.rs | 11 +- crates/node-depman/tests/hooks_test.rs | 250 ++++++++++++----------- crates/node-depman/tests/shims_test.rs | 8 +- crates/node/src/proto.rs | 4 +- crates/node/tests/globals_test.rs | 5 +- crates/node/tests/hooks_test.rs | 40 ++-- crates/node/tests/shims_test.rs | 1 + 9 files changed, 166 insertions(+), 166 deletions(-) diff --git a/crates/common/src/commands.rs b/crates/common/src/commands.rs index a69aee0..e80290e 100644 --- a/crates/common/src/commands.rs +++ b/crates/common/src/commands.rs @@ -1,8 +1,7 @@ -use proto_pdk_api::{ExecCommandInput, HostEnvironment}; -use std::path::Path; +use proto_pdk_api::{ExecCommandInput, HostEnvironment, VirtualPath}; -pub fn get_global_prefix>(env: &HostEnvironment, globals_dir: T) -> String { - let prefix = globals_dir.as_ref().to_string_lossy().to_string(); +pub fn get_global_prefix(env: &HostEnvironment, globals_dir: &VirtualPath) -> String { + let prefix = globals_dir.real_path().to_string_lossy().to_string(); // On Windows, globals will be installed into the prefix as-is, // so binaries will exist in the root of the prefix. diff --git a/crates/node-depman/src/proto.rs b/crates/node-depman/src/proto.rs index 14fdb93..385c3a1 100644 --- a/crates/node-depman/src/proto.rs +++ b/crates/node-depman/src/proto.rs @@ -250,7 +250,7 @@ pub fn download_prebuilt( }; Ok(Json(DownloadPrebuiltOutput { - archive_prefix: Some(get_archive_prefix(&manager, &version)), + archive_prefix: Some(get_archive_prefix(&manager, version)), download_url: format!( "https://registry.npmjs.org/{package_name}/-/{package_without_scope}-{version}.tgz", ), @@ -328,7 +328,7 @@ pub fn install_global( let result = exec_command!(commands::install_global( &input.dependency, - get_global_prefix(&env, input.globals_dir.real_path()), + get_global_prefix(&env, &input.globals_dir), )); Ok(Json(InstallGlobalOutput::from_exec_command(result))) @@ -342,7 +342,7 @@ pub fn uninstall_global( let result = exec_command!(commands::uninstall_global( &input.dependency, - get_global_prefix(&env, input.globals_dir.real_path()), + get_global_prefix(&env, &input.globals_dir), )); Ok(Json(UninstallGlobalOutput::from_exec_command(result))) diff --git a/crates/node-depman/tests/globals_test.rs b/crates/node-depman/tests/globals_test.rs index 36ee784..61b9924 100644 --- a/crates/node-depman/tests/globals_test.rs +++ b/crates/node-depman/tests/globals_test.rs @@ -1,13 +1,12 @@ // These work locally but fail in CI... hard to debug! -// use proto_pdk_test_utils::*; -// use starbase_sandbox::create_empty_sandbox; +use proto_pdk_test_utils::*; -// mod npm { -// use super::*; +mod npm { + use super::*; -// generate_globals_test!("npm-test", "prettier"); -// } + generate_globals_test!("npm-test", "prettier"); +} // mod pnpm { // use super::*; diff --git a/crates/node-depman/tests/hooks_test.rs b/crates/node-depman/tests/hooks_test.rs index 758092b..5657f5f 100644 --- a/crates/node-depman/tests/hooks_test.rs +++ b/crates/node-depman/tests/hooks_test.rs @@ -1,130 +1,132 @@ -use proto_pdk::{RunHook, ToolContext, UserConfigSettings}; -use proto_pdk_test_utils::create_plugin; -use starbase_sandbox::create_empty_sandbox; -use std::env; - -mod npm_hooks { - use super::*; - - #[test] - fn does_nothing_if_no_args() { - let sandbox = create_empty_sandbox(); - let plugin = create_plugin("npm-test", sandbox.path()); - - plugin.pre_run(RunHook::default()); - } - - #[test] - fn skips_when_env_var_set() { - let sandbox = create_empty_sandbox(); - let plugin = create_plugin("npm-test", sandbox.path()); - - env::set_var("PROTO_INSTALL_GLOBAL", "1"); - - plugin.pre_run(RunHook { - passthrough_args: vec!["install".into(), "-g".into(), "typescript".into()], - context: ToolContext::default(), - }); - - env::remove_var("PROTO_INSTALL_GLOBAL"); - } - - #[test] - fn can_bypass_with_user_config() { - let sandbox = create_empty_sandbox(); - let mut plugin = create_plugin("npm-test", sandbox.path()); - - plugin.tool.plugin.manifest.config.insert( - "proto_user_config".into(), - serde_json::to_string(&UserConfigSettings { - node_intercept_globals: false, - ..UserConfigSettings::default() - }) - .unwrap(), - ); - - plugin.tool.plugin.reload_config().unwrap(); - - plugin.pre_run(RunHook { - passthrough_args: vec!["install".into(), "-g".into(), "typescript".into()], - ..RunHook::default() - }); - } - - #[test] - #[should_panic(expected = "Global binaries must be installed")] - fn errors_if_installing_global() { - let sandbox = create_empty_sandbox(); - let plugin = create_plugin("npm-test", sandbox.path()); - - plugin.pre_run(RunHook { - passthrough_args: vec!["install".into(), "-g".into(), "typescript".into()], - ..RunHook::default() - }); - } - - #[test] - fn doesnt_error_for_other_commands() { - let sandbox = create_empty_sandbox(); - let plugin = create_plugin("npm-test", sandbox.path()); - - plugin.pre_run(RunHook { - passthrough_args: vec!["info".into(), "--json".into(), "typescript".into()], - ..RunHook::default() - }); - } -} - -mod pnpm_hooks { - use super::*; - - #[test] - #[should_panic(expected = "Global binaries must be installed")] - fn errors_if_installing_global() { - let sandbox = create_empty_sandbox(); - let plugin = create_plugin("pnpm-test", sandbox.path()); - - plugin.pre_run(RunHook { - passthrough_args: vec!["add".into(), "--global".into(), "typescript".into()], - ..RunHook::default() - }); +#[cfg(not(windows))] +mod hooks { + mod npm { + use super::*; + use proto_pdk::{RunHook, ToolContext, UserConfigSettings}; + use proto_pdk_test_utils::create_plugin; + use starbase_sandbox::create_empty_sandbox; + use std::env; + + #[test] + fn does_nothing_if_no_args() { + let sandbox = create_empty_sandbox(); + let plugin = create_plugin("npm-test", sandbox.path()); + + plugin.pre_run(RunHook::default()); + } + + #[test] + fn skips_when_env_var_set() { + let sandbox = create_empty_sandbox(); + let plugin = create_plugin("npm-test", sandbox.path()); + + env::set_var("PROTO_INSTALL_GLOBAL", "1"); + + plugin.pre_run(RunHook { + passthrough_args: vec!["install".into(), "-g".into(), "typescript".into()], + context: ToolContext::default(), + }); + + env::remove_var("PROTO_INSTALL_GLOBAL"); + } + + #[test] + fn can_bypass_with_user_config() { + let sandbox = create_empty_sandbox(); + let mut plugin = create_plugin("npm-test", sandbox.path()); + + plugin.tool.plugin.manifest.config.insert( + "proto_user_config".into(), + serde_json::to_string(&UserConfigSettings { + node_intercept_globals: false, + ..UserConfigSettings::default() + }) + .unwrap(), + ); + + plugin.tool.plugin.reload_config().unwrap(); + + plugin.pre_run(RunHook { + passthrough_args: vec!["install".into(), "-g".into(), "typescript".into()], + ..RunHook::default() + }); + } + + #[test] + #[should_panic(expected = "Global binaries must be installed")] + fn errors_if_installing_global() { + let sandbox = create_empty_sandbox(); + let plugin = create_plugin("npm-test", sandbox.path()); + + plugin.pre_run(RunHook { + passthrough_args: vec!["install".into(), "-g".into(), "typescript".into()], + ..RunHook::default() + }); + } + + #[test] + fn doesnt_error_for_other_commands() { + let sandbox = create_empty_sandbox(); + let plugin = create_plugin("npm-test", sandbox.path()); + + plugin.pre_run(RunHook { + passthrough_args: vec!["info".into(), "--json".into(), "typescript".into()], + ..RunHook::default() + }); + } } - #[test] - fn doesnt_error_for_other_commands() { - let sandbox = create_empty_sandbox(); - let plugin = create_plugin("pnpm-test", sandbox.path()); - - plugin.pre_run(RunHook { - passthrough_args: vec!["info".into(), "--json".into(), "typescript".into()], - ..RunHook::default() - }); + mod pnpm { + use super::*; + + #[test] + #[should_panic(expected = "Global binaries must be installed")] + fn errors_if_installing_global() { + let sandbox = create_empty_sandbox(); + let plugin = create_plugin("pnpm-test", sandbox.path()); + + plugin.pre_run(RunHook { + passthrough_args: vec!["add".into(), "--global".into(), "typescript".into()], + ..RunHook::default() + }); + } + + #[test] + fn doesnt_error_for_other_commands() { + let sandbox = create_empty_sandbox(); + let plugin = create_plugin("pnpm-test", sandbox.path()); + + plugin.pre_run(RunHook { + passthrough_args: vec!["info".into(), "--json".into(), "typescript".into()], + ..RunHook::default() + }); + } } -} - -mod yarn_hooks { - use super::*; - - #[test] - #[should_panic(expected = "Global binaries must be installed")] - fn errors_if_installing_global() { - let sandbox = create_empty_sandbox(); - let plugin = create_plugin("yarn-test", sandbox.path()); - - plugin.pre_run(RunHook { - passthrough_args: vec!["global".into(), "add".into(), "typescript".into()], - ..RunHook::default() - }); - } - - #[test] - fn doesnt_error_for_other_commands() { - let sandbox = create_empty_sandbox(); - let plugin = create_plugin("yarn-test", sandbox.path()); - plugin.pre_run(RunHook { - passthrough_args: vec!["info".into(), "--json".into(), "typescript".into()], - ..RunHook::default() - }); + mod yarn { + use super::*; + + #[test] + #[should_panic(expected = "Global binaries must be installed")] + fn errors_if_installing_global() { + let sandbox = create_empty_sandbox(); + let plugin = create_plugin("yarn-test", sandbox.path()); + + plugin.pre_run(RunHook { + passthrough_args: vec!["global".into(), "add".into(), "typescript".into()], + ..RunHook::default() + }); + } + + #[test] + fn doesnt_error_for_other_commands() { + let sandbox = create_empty_sandbox(); + let plugin = create_plugin("yarn-test", sandbox.path()); + + plugin.pre_run(RunHook { + passthrough_args: vec!["info".into(), "--json".into(), "typescript".into()], + ..RunHook::default() + }); + } } } diff --git a/crates/node-depman/tests/shims_test.rs b/crates/node-depman/tests/shims_test.rs index 06f2ea1..7afb0f1 100644 --- a/crates/node-depman/tests/shims_test.rs +++ b/crates/node-depman/tests/shims_test.rs @@ -1,22 +1,20 @@ -use proto_pdk_test_utils::*; - #[cfg(not(windows))] mod npm { - use super::*; + use proto_pdk_test_utils::*; generate_shims_test!("npm-test", ["npx", "node-gyp"]); } #[cfg(not(windows))] mod pnpm { - use super::*; + use proto_pdk_test_utils::*; generate_shims_test!("pnpm-test", ["pnpx"]); } #[cfg(not(windows))] mod yarn { - use super::*; + use proto_pdk_test_utils::*; generate_shims_test!("yarn-test", ["yarnpkg"]); } diff --git a/crates/node/src/proto.rs b/crates/node/src/proto.rs index d7250c6..5a0e90d 100644 --- a/crates/node/src/proto.rs +++ b/crates/node/src/proto.rs @@ -227,7 +227,7 @@ pub fn install_global( let result = exec_command!(commands::install_global( &input.dependency, - get_global_prefix(&env, input.globals_dir.real_path()), + get_global_prefix(&env, &input.globals_dir), )); Ok(Json(InstallGlobalOutput::from_exec_command(result))) @@ -241,7 +241,7 @@ pub fn uninstall_global( let result = exec_command!(commands::uninstall_global( &input.dependency, - get_global_prefix(&env, input.globals_dir.real_path()), + get_global_prefix(&env, &input.globals_dir), )); Ok(Json(UninstallGlobalOutput::from_exec_command(result))) diff --git a/crates/node/tests/globals_test.rs b/crates/node/tests/globals_test.rs index 436c278..88898a0 100644 --- a/crates/node/tests/globals_test.rs +++ b/crates/node/tests/globals_test.rs @@ -1,4 +1,3 @@ -// use proto_pdk_test_utils::*; -// use starbase_sandbox::create_empty_sandbox; +use proto_pdk_test_utils::*; -// generate_globals_test!("node-test", "prettier"); +generate_globals_test!("node-test", "prettier"); diff --git a/crates/node/tests/hooks_test.rs b/crates/node/tests/hooks_test.rs index a84561b..5771ab3 100644 --- a/crates/node/tests/hooks_test.rs +++ b/crates/node/tests/hooks_test.rs @@ -1,23 +1,25 @@ -use proto_pdk::InstallHook; -use proto_pdk_test_utils::{core::VersionSpec, create_plugin, ToolManifest, UnresolvedVersionSpec}; -use serial_test::serial; -use starbase_sandbox::create_empty_sandbox; -use std::collections::HashSet; -use std::env; -use std::path::PathBuf; - -fn set_vars(path: PathBuf) { - env::set_var("PROTO_HOME", path.to_string_lossy().to_string()); - env::set_var("PROTO_NODE_VERSION", "18.0.0"); -} - -fn reset_vars() { - env::remove_var("PROTO_HOME"); - env::remove_var("PROTO_NODE_VERSION"); -} - +// Importing proto_pdk crashes Windows because it contains WASM code +#[cfg(not(windows))] mod node_hooks { - use super::*; + use proto_pdk::InstallHook; + use proto_pdk_test_utils::{ + core::VersionSpec, create_plugin, ToolManifest, UnresolvedVersionSpec, + }; + use serial_test::serial; + use starbase_sandbox::create_empty_sandbox; + use std::collections::HashSet; + use std::env; + use std::path::PathBuf; + + fn set_vars(path: PathBuf) { + env::set_var("PROTO_HOME", path.to_string_lossy().to_string()); + env::set_var("PROTO_NODE_VERSION", "18.0.0"); + } + + fn reset_vars() { + env::remove_var("PROTO_HOME"); + env::remove_var("PROTO_NODE_VERSION"); + } #[test] #[serial] diff --git a/crates/node/tests/shims_test.rs b/crates/node/tests/shims_test.rs index 87bbfd1..e0fe05c 100644 --- a/crates/node/tests/shims_test.rs +++ b/crates/node/tests/shims_test.rs @@ -1,3 +1,4 @@ +#[cfg(not(windows))] use proto_pdk_test_utils::*; #[cfg(not(windows))]