Skip to content

Commit 520d92d

Browse files
committed
don't pass argv around, just generate it when needed
1 parent df64aa1 commit 520d92d

File tree

20 files changed

+62
-76
lines changed

20 files changed

+62
-76
lines changed

crates/volta-core/src/event.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ impl EventLog {
100100
EventLog { events: Vec::new() }
101101
}
102102

103-
pub fn add_event_start(&mut self, activity_kind: ActivityKind, argv: String) {
103+
pub fn add_event_start(&mut self, activity_kind: ActivityKind) {
104+
let argv = env::args_os()
105+
.map(|arg| arg.to_string_lossy().to_string())
106+
.collect::<Vec<String>>()
107+
.join(" ");
104108
self.add_event(EventKind::Start { argv }, activity_kind)
105109
}
106110
pub fn add_event_end(&mut self, activity_kind: ActivityKind, exit_code: ExitCode) {
@@ -148,21 +152,28 @@ pub mod tests {
148152
use super::{EventKind, EventLog};
149153
use crate::error::{ErrorKind, ExitCode};
150154
use crate::session::ActivityKind;
155+
use regex::Regex;
151156

152157
#[test]
153158
fn test_adding_events() {
154159
let mut event_log = EventLog::init();
155160
assert_eq!(event_log.events.len(), 0);
156161

157-
event_log.add_event_start(ActivityKind::Current, String::from("foo"));
162+
event_log.add_event_start(ActivityKind::Current);
158163
assert_eq!(event_log.events.len(), 1);
159164
assert_eq!(event_log.events[0].name, "current");
160-
assert_eq!(
161-
event_log.events[0].event,
162-
EventKind::Start {
163-
argv: String::from("foo")
165+
match event_log.events[0].event {
166+
EventKind::Start { ref argv } => {
167+
let re = Regex::new("volta_core").unwrap();
168+
assert!(re.is_match(argv));
164169
}
165-
);
170+
_ => {
171+
panic!(
172+
"Expected EventKind::Start {{ argv }}, Got: {:?}",
173+
event_log.events[0].event
174+
);
175+
}
176+
}
166177

167178
event_log.add_event_end(ActivityKind::Pin, ExitCode::NetworkError);
168179
assert_eq!(event_log.events.len(), 2);

crates/volta-core/src/run/node.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ use crate::session::{ActivityKind, Session};
99

1010
/// Build a `ToolCommand` for Node
1111
pub(super) fn command(args: &[OsString], session: &mut Session) -> Fallible<Executor> {
12-
let node_argv = args
13-
.iter()
14-
.map(|s| s.to_string_lossy().to_string())
15-
.collect::<Vec<String>>()
16-
.join(" ");
17-
session.add_event_start(ActivityKind::Node, node_argv);
12+
session.add_event_start(ActivityKind::Node);
1813
// Don't re-evaluate the platform if this is a recursive call
1914
let platform = match env::var_os(RECURSION_ENV_VAR) {
2015
Some(_) => None,

crates/volta-core/src/run/npm.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ use crate::version::VersionSpec;
2020
/// If the command is _not_ a global install / uninstall or we don't have a default platform, then
2121
/// we will allow npm to execute the command as usual.
2222
pub(super) fn command(args: &[OsString], session: &mut Session) -> Fallible<Executor> {
23-
let npm_argv = args
24-
.iter()
25-
.map(|s| s.to_string_lossy().to_string())
26-
.collect::<Vec<String>>()
27-
.join(" ");
28-
session.add_event_start(ActivityKind::Npm, npm_argv);
23+
session.add_event_start(ActivityKind::Npm);
2924
// Don't re-evaluate the context or global install interception if this is a recursive call
3025
let platform = match env::var_os(RECURSION_ENV_VAR) {
3126
Some(_) => None,

crates/volta-core/src/run/npx.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ lazy_static! {
1616

1717
/// Build a `ToolCommand` for npx
1818
pub(super) fn command(args: &[OsString], session: &mut Session) -> Fallible<Executor> {
19-
let npx_argv = args
20-
.iter()
21-
.map(|s| s.to_string_lossy().to_string())
22-
.collect::<Vec<String>>()
23-
.join(" ");
24-
session.add_event_start(ActivityKind::Npx, npx_argv);
19+
session.add_event_start(ActivityKind::Npx);
2520
// Don't re-evaluate the context if this is a recursive call
2621
let platform = match env::var_os(RECURSION_ENV_VAR) {
2722
Some(_) => None,

crates/volta-core/src/run/yarn.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ use crate::session::{ActivityKind, Session};
1717
/// If the command is _not_ a global add / remove or we don't have a default platform, then
1818
/// we will allow Yarn to execute the command as usual.
1919
pub(super) fn command(args: &[OsString], session: &mut Session) -> Fallible<Executor> {
20-
let yarn_argv = args
21-
.iter()
22-
.map(|s| s.to_string_lossy().to_string())
23-
.collect::<Vec<String>>()
24-
.join(" ");
25-
session.add_event_start(ActivityKind::Yarn, yarn_argv);
20+
session.add_event_start(ActivityKind::Yarn);
2621
// Don't re-evaluate the context or global install interception if this is a recursive call
2722
let platform = match env::var_os(RECURSION_ENV_VAR) {
2823
Some(_) => None,

crates/volta-core/src/session.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ impl Session {
131131
self.hooks.get(self.project()?)
132132
}
133133

134-
pub fn add_event_start(&mut self, activity_kind: ActivityKind, argv: String) {
135-
self.event_log.add_event_start(activity_kind, argv)
134+
pub fn add_event_start(&mut self, activity_kind: ActivityKind) {
135+
self.event_log.add_event_start(activity_kind)
136136
}
137137
pub fn add_event_end(&mut self, activity_kind: ActivityKind, exit_code: ExitCode) {
138138
self.event_log.add_event_end(activity_kind, exit_code)

src/cli.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub(crate) struct Volta {
4545
}
4646

4747
impl Volta {
48-
pub(crate) fn run(self, session: &mut Session, argv: String) -> Fallible<ExitCode> {
48+
pub(crate) fn run(self, session: &mut Session) -> Fallible<ExitCode> {
4949
if self.version {
5050
// suffix indicator for dev build
5151
if cfg!(debug_assertions) {
@@ -55,9 +55,9 @@ impl Volta {
5555
}
5656
Ok(ExitCode::Success)
5757
} else if let Some(command) = self.command {
58-
command.run(session, argv)
58+
command.run(session)
5959
} else {
60-
Volta::from_iter(["volta", "help"].iter()).run(session, argv)
60+
Volta::from_iter(["volta", "help"].iter()).run(session)
6161
}
6262
}
6363
}
@@ -130,18 +130,18 @@ otherwise, they will be written to `stdout`.
130130
}
131131

132132
impl Subcommand {
133-
pub(crate) fn run(self, session: &mut Session, argv: String) -> Fallible<ExitCode> {
133+
pub(crate) fn run(self, session: &mut Session) -> Fallible<ExitCode> {
134134
match self {
135-
Subcommand::Fetch(fetch) => fetch.run(session, argv),
136-
Subcommand::Install(install) => install.run(session, argv),
137-
Subcommand::Uninstall(uninstall) => uninstall.run(session, argv),
138-
Subcommand::Pin(pin) => pin.run(session, argv),
139-
Subcommand::List(list) => list.run(session, argv),
140-
Subcommand::Completions(completions) => completions.run(session, argv),
141-
Subcommand::Which(which) => which.run(session, argv),
142-
Subcommand::Use(r#use) => r#use.run(session, argv),
143-
Subcommand::Setup(setup) => setup.run(session, argv),
144-
Subcommand::Run(run) => run.run(session, argv),
135+
Subcommand::Fetch(fetch) => fetch.run(session),
136+
Subcommand::Install(install) => install.run(session),
137+
Subcommand::Uninstall(uninstall) => uninstall.run(session),
138+
Subcommand::Pin(pin) => pin.run(session),
139+
Subcommand::List(list) => list.run(session),
140+
Subcommand::Completions(completions) => completions.run(session),
141+
Subcommand::Which(which) => which.run(session),
142+
Subcommand::Use(r#use) => r#use.run(session),
143+
Subcommand::Setup(setup) => setup.run(session),
144+
Subcommand::Run(run) => run.run(session),
145145
}
146146
}
147147
}

src/command/completions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub(crate) struct Completions {
3232
}
3333

3434
impl Command for Completions {
35-
fn run(self, session: &mut Session, argv: String) -> Fallible<ExitCode> {
36-
session.add_event_start(ActivityKind::Completions, argv);
35+
fn run(self, session: &mut Session) -> Fallible<ExitCode> {
36+
session.add_event_start(ActivityKind::Completions);
3737

3838
let mut app = crate::cli::Volta::clap();
3939
match self.out_file {

src/command/fetch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub(crate) struct Fetch {
1414
}
1515

1616
impl Command for Fetch {
17-
fn run(self, session: &mut Session, argv: String) -> Fallible<ExitCode> {
18-
session.add_event_start(ActivityKind::Fetch, argv);
17+
fn run(self, session: &mut Session) -> Fallible<ExitCode> {
18+
session.add_event_start(ActivityKind::Fetch);
1919

2020
for tool in tool::Spec::from_strings(&self.tools, "fetch")? {
2121
tool.resolve(session)?.fetch(session)?;

src/command/install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub(crate) struct Install {
1414
}
1515

1616
impl Command for Install {
17-
fn run(self, session: &mut Session, argv: String) -> Fallible<ExitCode> {
18-
session.add_event_start(ActivityKind::Install, argv);
17+
fn run(self, session: &mut Session) -> Fallible<ExitCode> {
18+
session.add_event_start(ActivityKind::Install);
1919

2020
for tool in Spec::from_strings(&self.tools, "install")? {
2121
tool.resolve(session)?.install(session)?;

0 commit comments

Comments
 (0)