From 0213419f3cfd40bf6a30e4347469458965eb262d Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Mon, 20 Nov 2023 16:42:00 -0800 Subject: [PATCH 1/7] Fix incorrect windows path. --- CHANGELOG.md | 6 ++++++ crates/node/src/proto.rs | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b18d86..4456393 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +#### 🐞 Fixes + +- Fixed an incorrect globals directory on Windows. + ## 0.5.2 #### 🚀 Updates diff --git a/crates/node/src/proto.rs b/crates/node/src/proto.rs index e4008e6..df2af63 100644 --- a/crates/node/src/proto.rs +++ b/crates/node/src/proto.rs @@ -199,6 +199,20 @@ pub fn download_prebuilt( })) } +pub fn get_globals_dirs(env: &HostEnvironment) -> Vec { + let mut dirs = vec![]; + + // Windows for some reason removes the /bin suffix when installing into it, + // so we also need to account for the path without /bin. But keep the /bin path + // as the final path and for the install to trigger correctly. + if env.os == HostOS::Windows { + dirs.push("$PROTO_HOME/tools/node/globals".into()); + } + + dirs.push("$PROTO_HOME/tools/node/globals/bin".into()); + dirs +} + #[plugin_fn] pub fn locate_executables( Json(_): Json, @@ -206,7 +220,7 @@ pub fn locate_executables( let env = get_proto_environment()?; Ok(Json(LocateExecutablesOutput { - globals_lookup_dirs: vec!["$PROTO_HOME/tools/node/globals/bin".into()], + globals_lookup_dirs: get_globals_dirs(&env), primary: Some(ExecutableConfig::new(if env.os == HostOS::Windows { format!("{}.exe", BIN) } else { @@ -288,7 +302,7 @@ pub fn locate_bins(Json(_): Json) -> FnResult Date: Mon, 20 Nov 2023 16:45:18 -0800 Subject: [PATCH 2/7] Update depman. --- crates/common/src/lib.rs | 16 ++++++++++++++++ crates/node-depman/src/proto.rs | 7 ++++--- crates/node/src/proto.rs | 16 +--------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index 2391104..8627a99 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -4,3 +4,19 @@ mod package_json; pub use node_dist::*; pub use package_json::*; + +use proto_pdk_api::HostEnvironment; + +pub fn get_globals_dirs(env: &HostEnvironment) -> Vec { + let mut dirs = vec![]; + + // Windows for some reason removes the /bin suffix when installing into it, + // so we also need to account for the path without /bin. But keep the /bin path + // as the final path and for the install to trigger correctly. + if env.os.is_windows() { + dirs.push("$PROTO_HOME/tools/node/globals".into()); + } + + dirs.push("$PROTO_HOME/tools/node/globals/bin".into()); + dirs +} diff --git a/crates/node-depman/src/proto.rs b/crates/node-depman/src/proto.rs index 94c8709..751db2b 100644 --- a/crates/node-depman/src/proto.rs +++ b/crates/node-depman/src/proto.rs @@ -1,7 +1,7 @@ use crate::npm_registry::parse_registry_response; use crate::package_manager::PackageManager; use extism_pdk::*; -use node_common::{commands, BinField, NodeDistVersion, PackageJson}; +use node_common::{commands, get_globals_dirs, BinField, NodeDistVersion, PackageJson}; use proto_pdk::*; use std::collections::HashMap; use std::fs; @@ -310,7 +310,7 @@ pub fn locate_executables( }; Ok(Json(LocateExecutablesOutput { - globals_lookup_dirs: vec!["$PROTO_HOME/tools/node/globals/bin".into()], + globals_lookup_dirs: get_globals_dirs(&env), primary: Some(primary), secondary, ..LocateExecutablesOutput::default() @@ -393,6 +393,7 @@ pub fn pre_run(Json(input): Json) -> FnResult<()> { #[plugin_fn] pub fn locate_bins(Json(input): Json) -> FnResult> { + let env = get_proto_environment()?; let mut bin_path = None; let package_path = input.context.tool_dir.join("package.json"); let manager = PackageManager::detect(); @@ -436,7 +437,7 @@ pub fn locate_bins(Json(input): Json) -> FnResult Vec { - let mut dirs = vec![]; - - // Windows for some reason removes the /bin suffix when installing into it, - // so we also need to account for the path without /bin. But keep the /bin path - // as the final path and for the install to trigger correctly. - if env.os == HostOS::Windows { - dirs.push("$PROTO_HOME/tools/node/globals".into()); - } - - dirs.push("$PROTO_HOME/tools/node/globals/bin".into()); - dirs -} - #[plugin_fn] pub fn locate_executables( Json(_): Json, From ea6e90da520a3420f0cab0cb9d1b6fc0197dd9d6 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 22 Nov 2023 13:55:36 -0800 Subject: [PATCH 3/7] Update prefix usage. --- CHANGELOG.md | 7 ++++- Cargo.lock | 4 +-- crates/common/src/commands.rs | 47 +++++++++++++++++++++------------ crates/common/src/lib.rs | 16 ----------- crates/node-depman/Cargo.toml | 2 +- crates/node-depman/src/proto.rs | 18 ++++++++----- crates/node/Cargo.toml | 2 +- crates/node/src/proto.rs | 17 ++++++++---- 8 files changed, 64 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4456393..f8e4106 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,16 @@ # Changelog -## Unreleased +## 0.5.3 #### 🐞 Fixes - Fixed an incorrect globals directory on Windows. +#### ⚙️ Internal + +- Updated dependencies. +- Updated globals install to use a `--prefix` arg instead of `PREFIX` env var. + ## 0.5.2 #### 🚀 Updates diff --git a/Cargo.lock b/Cargo.lock index b7fd0fe..6965592 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1850,7 +1850,7 @@ dependencies = [ [[package]] name = "node_depman_plugin" -version = "0.5.2" +version = "0.5.3" dependencies = [ "extism-pdk", "node_common", @@ -1865,7 +1865,7 @@ dependencies = [ [[package]] name = "node_plugin" -version = "0.5.2" +version = "0.5.3" dependencies = [ "extism-pdk", "node_common", diff --git a/crates/common/src/commands.rs b/crates/common/src/commands.rs index a21e673..a69aee0 100644 --- a/crates/common/src/commands.rs +++ b/crates/common/src/commands.rs @@ -1,46 +1,59 @@ -use proto_pdk_api::ExecCommandInput; +use proto_pdk_api::{ExecCommandInput, HostEnvironment}; use std::path::Path; -pub fn install_global(dependency: &str, globals_dir: &Path) -> ExecCommandInput { +pub fn get_global_prefix>(env: &HostEnvironment, globals_dir: T) -> String { + let prefix = globals_dir.as_ref().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. + if env.os.is_windows() { + return prefix; + } + + // On Unix, globals are nested within a /bin directory, and since our + // fixed globals dir ends in /bin, we must remove it and set the prefix + // to the parent directory. This way everything resolves correctly. + prefix.replace("/bin", "") +} + +pub fn install_global(dependency: &str, globals_prefix: String) -> ExecCommandInput { let mut cmd = ExecCommandInput::inherit( "npm", [ "install", + dependency, "--global", "--loglevel", "warn", "--no-audit", "--no-update-notifier", - dependency, + "--prefix", + &globals_prefix, ], ); cmd.env_vars .insert("PROTO_INSTALL_GLOBAL".into(), "true".into()); - // Remove the /bin component - cmd.env_vars.insert( - "PREFIX".into(), - globals_dir.parent().unwrap().to_string_lossy().to_string(), - ); - cmd } -pub fn uninstall_global(dependency: &str, globals_dir: &Path) -> ExecCommandInput { +pub fn uninstall_global(dependency: &str, globals_prefix: String) -> ExecCommandInput { let mut cmd = ExecCommandInput::inherit( "npm", - ["uninstall", "--global", "--loglevel", "warn", dependency], + [ + "uninstall", + dependency, + "--global", + "--loglevel", + "warn", + "--prefix", + &globals_prefix, + ], ); cmd.env_vars .insert("PROTO_INSTALL_GLOBAL".into(), "true".into()); - // Remove the /bin component - cmd.env_vars.insert( - "PREFIX".into(), - globals_dir.parent().unwrap().to_string_lossy().to_string(), - ); - cmd } diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs index 8627a99..2391104 100644 --- a/crates/common/src/lib.rs +++ b/crates/common/src/lib.rs @@ -4,19 +4,3 @@ mod package_json; pub use node_dist::*; pub use package_json::*; - -use proto_pdk_api::HostEnvironment; - -pub fn get_globals_dirs(env: &HostEnvironment) -> Vec { - let mut dirs = vec![]; - - // Windows for some reason removes the /bin suffix when installing into it, - // so we also need to account for the path without /bin. But keep the /bin path - // as the final path and for the install to trigger correctly. - if env.os.is_windows() { - dirs.push("$PROTO_HOME/tools/node/globals".into()); - } - - dirs.push("$PROTO_HOME/tools/node/globals/bin".into()); - dirs -} diff --git a/crates/node-depman/Cargo.toml b/crates/node-depman/Cargo.toml index e8d50b8..e573bc5 100644 --- a/crates/node-depman/Cargo.toml +++ b/crates/node-depman/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "node_depman_plugin" -version = "0.5.2" +version = "0.5.3" edition = "2021" license = "MIT" publish = false diff --git a/crates/node-depman/src/proto.rs b/crates/node-depman/src/proto.rs index 751db2b..14fdb93 100644 --- a/crates/node-depman/src/proto.rs +++ b/crates/node-depman/src/proto.rs @@ -1,7 +1,10 @@ use crate::npm_registry::parse_registry_response; use crate::package_manager::PackageManager; use extism_pdk::*; -use node_common::{commands, get_globals_dirs, BinField, NodeDistVersion, PackageJson}; +use node_common::{ + commands::{self, get_global_prefix}, + BinField, NodeDistVersion, PackageJson, +}; use proto_pdk::*; use std::collections::HashMap; use std::fs; @@ -310,7 +313,7 @@ pub fn locate_executables( }; Ok(Json(LocateExecutablesOutput { - globals_lookup_dirs: get_globals_dirs(&env), + globals_lookup_dirs: vec!["$PROTO_HOME/tools/node/globals/bin".into()], primary: Some(primary), secondary, ..LocateExecutablesOutput::default() @@ -321,9 +324,11 @@ pub fn locate_executables( pub fn install_global( Json(input): Json, ) -> FnResult> { + let env = get_proto_environment()?; + let result = exec_command!(commands::install_global( &input.dependency, - &input.globals_dir.real_path(), + get_global_prefix(&env, input.globals_dir.real_path()), )); Ok(Json(InstallGlobalOutput::from_exec_command(result))) @@ -333,9 +338,11 @@ pub fn install_global( pub fn uninstall_global( Json(input): Json, ) -> FnResult> { + let env = get_proto_environment()?; + let result = exec_command!(commands::uninstall_global( &input.dependency, - &input.globals_dir.real_path(), + get_global_prefix(&env, input.globals_dir.real_path()), )); Ok(Json(UninstallGlobalOutput::from_exec_command(result))) @@ -393,7 +400,6 @@ pub fn pre_run(Json(input): Json) -> FnResult<()> { #[plugin_fn] pub fn locate_bins(Json(input): Json) -> FnResult> { - let env = get_proto_environment()?; let mut bin_path = None; let package_path = input.context.tool_dir.join("package.json"); let manager = PackageManager::detect(); @@ -437,7 +443,7 @@ pub fn locate_bins(Json(input): Json) -> FnResult, ) -> FnResult> { + let env = get_proto_environment()?; + let result = exec_command!(commands::install_global( &input.dependency, - &input.globals_dir.real_path(), + get_global_prefix(&env, input.globals_dir.real_path()), )); Ok(Json(InstallGlobalOutput::from_exec_command(result))) @@ -232,9 +237,11 @@ pub fn install_global( pub fn uninstall_global( Json(input): Json, ) -> FnResult> { + let env = get_proto_environment()?; + let result = exec_command!(commands::uninstall_global( &input.dependency, - &input.globals_dir.real_path(), + get_global_prefix(&env, input.globals_dir.real_path()), )); Ok(Json(UninstallGlobalOutput::from_exec_command(result))) @@ -288,7 +295,7 @@ pub fn locate_bins(Json(_): Json) -> FnResult Date: Wed, 22 Nov 2023 14:00:36 -0800 Subject: [PATCH 4/7] Update deps. --- Cargo.lock | 44 ++++++++++++++++++++++++++------------------ Cargo.toml | 4 ++-- rust-toolchain.toml | 2 +- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6965592..d52d88d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2104,9 +2104,9 @@ dependencies = [ [[package]] name = "proto_core" -version = "0.23.0" +version = "0.23.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46d2573a386c1851ae91f412dc9495d54b13759d307588c148bef6a98b487591" +checksum = "37b4467e6e2819e74fc2247ff2d69486713414386fcc6549d172748bbcfa2a57" dependencies = [ "cached", "extism", @@ -2125,6 +2125,7 @@ dependencies = [ "starbase_events", "starbase_styles", "starbase_utils", + "system_env", "thiserror", "tinytemplate", "tracing", @@ -2163,9 +2164,9 @@ dependencies = [ [[package]] name = "proto_pdk_test_utils" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e903c50774be861c179391305e68f529d758bb8255eb91c1c688c28c7d770bf" +checksum = "2a2d0ec151cb028a169a2a87f184f54e3c6bda24c62a1cc22154df26be485343" dependencies = [ "extism", "proto_core", @@ -2582,9 +2583,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.192" +version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001" +checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89" dependencies = [ "serde_derive", ] @@ -2601,9 +2602,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.192" +version = "1.0.193" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1" +checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3" dependencies = [ "proc-macro2", "quote", @@ -2690,6 +2691,12 @@ dependencies = [ "digest", ] +[[package]] +name = "shell-words" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" + [[package]] name = "shellexpand" version = "2.1.2" @@ -2845,9 +2852,9 @@ dependencies = [ [[package]] name = "starbase_utils" -version = "0.3.7" +version = "0.3.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d948dcd63f1dd11f2b7a9ab15c53e7866faa11ccd31377dca1625c106d40fcee" +checksum = "9af464d976956393070880234f8d9f3509f04555266174abfd9b11386cdce4bc" dependencies = [ "dirs 5.0.1", "fs4", @@ -2861,7 +2868,7 @@ dependencies = [ "starbase_styles", "thiserror", "tokio", - "toml 0.8.6", + "toml 0.8.8", "tracing", "wax", ] @@ -2943,12 +2950,13 @@ dependencies = [ [[package]] name = "system_env" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5430c2b1d8315d22edaebbd40617f9110ccfd95e95684802d0d17907f44966" +checksum = "63476c953552c18056a153eaabc04d2987bc64f07633acc80d6a65a8574f1a3b" dependencies = [ "serde", "serde_json", + "shell-words", "thiserror", ] @@ -3167,14 +3175,14 @@ dependencies = [ [[package]] name = "toml" -version = "0.8.6" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ff9e3abce27ee2c9a37f9ad37238c1bdd4e789c84ba37df76aa4d528f5072cc" +checksum = "a1a195ec8c9da26928f773888e0742ca3ca1040c6cd859c919c9f59c1954ab35" dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.20.7", + "toml_edit 0.21.0", ] [[package]] @@ -3201,9 +3209,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.20.7" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70f427fce4d84c72b5b732388bf4a9f4531b53f74e2887e3ecb2481f68f66d81" +checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" dependencies = [ "indexmap 2.0.0", "serde", diff --git a/Cargo.toml b/Cargo.toml index 5d14155..1fb2733 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,12 +6,12 @@ members = ["crates/*"] extism-pdk = "0.3.4" proto_pdk = { version = "0.10.3" } # , path = "../../proto/crates/pdk" } proto_pdk_api = { version = "0.10.4" } # , path = "../../proto/crates/pdk-api" } -proto_pdk_test_utils = { version = "0.11.0" } # , path = "../../proto/crates/pdk-test-utils" } +proto_pdk_test_utils = { version = "0.11.1" } # , path = "../../proto/crates/pdk-test-utils" } regex = { version = "1.10.2", default-features = false, features = [ "std", "unicode", ] } -serde = "1.0.192" +serde = "1.0.193" serde_json = "1.0.108" starbase_sandbox = "0.1.12" tokio = { version = "1.34.0", features = ["full"] } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index f172dde..89705a9 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] profile = "default" -channel = "1.73.0" +channel = "1.74.0" From 4632ee4c63362e33dd5316877ab20465525fba77 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 22 Nov 2023 14:11:42 -0800 Subject: [PATCH 5/7] 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))] From f6678a85aa705b8cd080a0ab1e4e2ad3ebac10bb Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 22 Nov 2023 14:14:52 -0800 Subject: [PATCH 6/7] Fix imports. --- crates/node-depman/tests/hooks_test.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/node-depman/tests/hooks_test.rs b/crates/node-depman/tests/hooks_test.rs index 5657f5f..4d97134 100644 --- a/crates/node-depman/tests/hooks_test.rs +++ b/crates/node-depman/tests/hooks_test.rs @@ -1,11 +1,13 @@ +// Importing proto_pdk crashes Windows because it contains WASM code #[cfg(not(windows))] mod hooks { + use proto_pdk::{RunHook, ToolContext, UserConfigSettings}; + use proto_pdk_test_utils::create_plugin; + use starbase_sandbox::create_empty_sandbox; + use std::env; + 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() { From 8f32f3300f5821d2cbbf92cda2417285c19048bc Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Wed, 22 Nov 2023 15:00:17 -0800 Subject: [PATCH 7/7] Fix test --- crates/node/tests/versions_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/node/tests/versions_test.rs b/crates/node/tests/versions_test.rs index 02392a5..5da45c6 100644 --- a/crates/node/tests/versions_test.rs +++ b/crates/node/tests/versions_test.rs @@ -6,7 +6,7 @@ generate_resolve_versions_tests!("node-test", { "10.1" => "10.1.0", "lts-gallium" => "16.20.2", "lts/fermium" => "14.21.3", - "stable" => "20.9.0", + "stable" => "20.10.0", "node" => "21.2.0", });