Skip to content

Commit

Permalink
set for all tasks, better test
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit committed Oct 30, 2024
1 parent 7f4d229 commit cc43288
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 72 deletions.
2 changes: 2 additions & 0 deletions cli/npm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ impl NpmFetchResolver {
}
}

pub const NPM_CONFIG_USER_AGENT_ENV_VAR: &str = "npm_config_user_agent";

pub fn get_npm_config_user_agent() -> String {
format!(
"deno/{} npm/? deno/{} {} {}",
Expand Down
35 changes: 10 additions & 25 deletions cli/task_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ fn prepare_env_vars(
initial_cwd.to_string_lossy().to_string(),
);
}
if !env_vars.contains_key(crate::npm::NPM_CONFIG_USER_AGENT_ENV_VAR) {
env_vars.insert(
crate::npm::NPM_CONFIG_USER_AGENT_ENV_VAR.into(),
crate::npm::get_npm_config_user_agent(),
);
}
if let Some(node_modules_dir) = node_modules_dir {
prepend_to_path(
&mut env_vars,
Expand Down Expand Up @@ -215,10 +221,6 @@ impl ShellCommand for NpmCommand {
args.extend(context.args.iter().skip(1).cloned());

let mut state = context.state;
state.apply_env_var(
"npm_config_user_agent",
&crate::npm::get_npm_config_user_agent(),
);
state.apply_env_var(USE_PKG_JSON_HIDDEN_ENV_VAR_NAME, "1");
return ExecutableCommand::new(
"deno".to_string(),
Expand Down Expand Up @@ -250,12 +252,8 @@ pub struct NodeCommand;
impl ShellCommand for NodeCommand {
fn execute(
&self,
mut context: ShellCommandContext,
context: ShellCommandContext,
) -> LocalBoxFuture<'static, ExecuteResult> {
context.state.apply_env_var(
"npm_config_user_agent",
&crate::npm::get_npm_config_user_agent(),
);
// run with deno if it's a simple invocation, fall back to node
// if there are extra flags
let mut args = Vec::with_capacity(context.args.len());
Expand Down Expand Up @@ -296,13 +294,8 @@ pub struct NodeGypCommand;
impl ShellCommand for NodeGypCommand {
fn execute(
&self,
mut context: ShellCommandContext,
context: ShellCommandContext,
) -> LocalBoxFuture<'static, ExecuteResult> {
context.state.apply_env_var(
"npm_config_user_agent",
&crate::npm::get_npm_config_user_agent(),
);

// at the moment this shell command is just to give a warning if node-gyp is not found
// in the future, we could try to run/install node-gyp for the user with deno
if context.state.resolve_command_path("node-gyp").is_err() {
Expand All @@ -325,14 +318,10 @@ impl ShellCommand for NpxCommand {
) -> LocalBoxFuture<'static, ExecuteResult> {
if let Some(first_arg) = context.args.first().cloned() {
if let Some(command) = context.state.resolve_custom_command(&first_arg) {
let mut context = ShellCommandContext {
let context = ShellCommandContext {
args: context.args.iter().skip(1).cloned().collect::<Vec<_>>(),
..context
};
context.state.apply_env_var(
"npm_config_user_agent",
&crate::npm::get_npm_config_user_agent(),
);
command.execute(context)
} else {
// can't find the command, so fallback to running the real npx command
Expand Down Expand Up @@ -363,7 +352,7 @@ struct NpmPackageBinCommand {
impl ShellCommand for NpmPackageBinCommand {
fn execute(
&self,
mut context: ShellCommandContext,
context: ShellCommandContext,
) -> LocalBoxFuture<'static, ExecuteResult> {
let mut args = vec![
"run".to_string(),
Expand Down Expand Up @@ -407,10 +396,6 @@ impl ShellCommand for NodeModulesFileRunCommand {
"deno".to_string(),
std::env::current_exe().unwrap(),
);
context.state.apply_env_var(
"npm_config_user_agent",
&crate::npm::get_npm_config_user_agent(),
);
// set this environment variable so that the launched process knows the npm command name
context
.state
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn set_npm_user_agent() {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
std::env::set_var(
"npm_config_user_agent",
crate::npm::NPM_CONFIG_USER_AGENT_ENV_VAR,
crate::npm::get_npm_config_user_agent(),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"version": "1.0.0",
"bin": {
"print-npm-user-agent": "index.js"
},
"scripts": {
"postinstall": "echo postinstall && node index.js && exit 1"
}
}
33 changes: 14 additions & 19 deletions tests/specs/npm/user_agent_env_var/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"tempDir": true,
"tests": {
"run_npm_package": {
"set_for_npm_package": {
"steps": [
{
"args": "install",
Expand All @@ -13,37 +13,32 @@
}
]
},
"npx": {
"unset_for_local_file": {
"steps": [
{
"args": "install",
"output": "[WILDCARD]"
},
{
"if": "unix",
"args": "task run-with-npx",
"output": "npx.out"
"args": "run -A main.ts",
"output": "Download [WILDCARD]\nnpm_config_user_agent: undefined\n"
}
]
},
"bin_command": {
"set_for_tasks": {
"steps": [
{ "args": "install", "output": "[WILDCARD]" },
{
"args": "install",
"output": "[WILDCARD]"
},
{
"args": "task run-via-bin",
"output": "Task run-via-bin print-npm-user-agent\nnpm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD]\n"
"output": "bin_command.out"
}
]
},
"npm_run": {
"set_for_lifecycle_scripts": {
"steps": [
{
"args": "install",
"output": "[WILDCARD]"
},
{
"args": "task run-via-other-task",
"output": "npm_run.out"
"args": "install --allow-scripts",
"output": "postinstall.out",
"exitCode": 1
}
]
}
Expand Down
1 change: 1 addition & 0 deletions tests/specs/npm/user_agent_env_var/bin_command.out
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Task run-via-bin print-npm-user-agent
npm_config_user_agent: deno/[WILDCARD] npm/? deno/[WILDCARD] [WILDCARD] [WILDCARD]
2 changes: 1 addition & 1 deletion tests/specs/npm/user_agent_env_var/deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"nodeModulesDir": "auto"
}
}
18 changes: 0 additions & 18 deletions tests/specs/npm/user_agent_env_var/deno.lock

This file was deleted.

1 change: 1 addition & 0 deletions tests/specs/npm/user_agent_env_var/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(`npm_config_user_agent: ${Deno.env.get("npm_config_user_agent")}`);
3 changes: 0 additions & 3 deletions tests/specs/npm/user_agent_env_var/npm_run.out

This file was deleted.

2 changes: 0 additions & 2 deletions tests/specs/npm/user_agent_env_var/npx.out

This file was deleted.

4 changes: 1 addition & 3 deletions tests/specs/npm/user_agent_env_var/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"scripts": {
"run-with-npx": "npx print-npm-user-agent",
"run-via-other-task": "npm run run-via-bin",
"run-via-bin": "print-npm-user-agent"
},
"dependencies": {
"@denotest/print-npm-user-agent": "1.0.0"
}
}
}
10 changes: 10 additions & 0 deletions tests/specs/npm/user_agent_env_var/postinstall.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Download http://localhost:4260/@denotest%2fprint-npm-user-agent
Download http://localhost:4260/@denotest/print-npm-user-agent/1.0.0.tgz
Initialize @denotest/[email protected]
Initialize @denotest/[email protected]: running 'postinstall' script
error: script 'postinstall' in '@denotest/[email protected]' failed with exit code 1
stdout:
postinstall
npm_config_user_agent: deno/[WILDCARD] npm/? deno/2.0.3 macos aarch64

error: failed to run scripts for packages: @denotest/[email protected]

0 comments on commit cc43288

Please sign in to comment.