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 22, 2023
1 parent 726cab1 commit 4632ee4
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 166 deletions.
7 changes: 3 additions & 4 deletions crates/common/src/commands.rs
Original file line number Diff line number Diff line change
@@ -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<T: AsRef<Path>>(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.
Expand Down
6 changes: 3 additions & 3 deletions crates/node-depman/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
Expand Down Expand Up @@ -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)))
Expand All @@ -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)))
Expand Down
11 changes: 5 additions & 6 deletions crates/node-depman/tests/globals_test.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
250 changes: 126 additions & 124 deletions crates/node-depman/tests/hooks_test.rs
Original file line number Diff line number Diff line change
@@ -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()
});
}
}
}
8 changes: 3 additions & 5 deletions crates/node-depman/tests/shims_test.rs
Original file line number Diff line number Diff line change
@@ -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"]);
}
4 changes: 2 additions & 2 deletions crates/node/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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)))
Expand Down
5 changes: 2 additions & 3 deletions crates/node/tests/globals_test.rs
Original file line number Diff line number Diff line change
@@ -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");
Loading

0 comments on commit 4632ee4

Please sign in to comment.