Skip to content

Commit a029c7a

Browse files
committed
add argv to start event, +tests
1 parent 6e26543 commit a029c7a

File tree

27 files changed

+332
-61
lines changed

27 files changed

+332
-61
lines changed

crates/volta-core/src/event.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
use std::env;
44
use std::time::{SystemTime, UNIX_EPOCH};
55

6-
use serde::Serialize;
6+
use serde::{Deserialize, Serialize};
77

88
use crate::error::{ExitCode, VoltaError};
99
use crate::hook::Publish;
1010
use crate::monitor::Monitor;
1111
use crate::session::ActivityKind;
1212

1313
// the Event data that is serialized to JSON and sent the plugin
14-
#[derive(Serialize)]
14+
#[derive(Deserialize, Serialize)]
1515
pub struct Event {
1616
timestamp: u64,
17-
name: String,
18-
event: EventKind,
17+
pub name: String,
18+
pub event: EventKind,
1919
}
2020

21-
#[derive(Serialize)]
21+
#[derive(Deserialize, Serialize, PartialEq, Debug)]
2222
pub struct ErrorEnv {
2323
argv: String,
2424
exec_path: String,
@@ -27,10 +27,12 @@ pub struct ErrorEnv {
2727
platform_version: String,
2828
}
2929

30-
#[derive(Serialize)]
30+
#[derive(Deserialize, Serialize, PartialEq, Debug)]
3131
#[serde(rename_all = "lowercase")]
32-
enum EventKind {
33-
Start,
32+
pub enum EventKind {
33+
Start {
34+
argv: String,
35+
},
3436
End {
3537
exit_code: i32,
3638
},
@@ -98,8 +100,8 @@ impl EventLog {
98100
EventLog { events: Vec::new() }
99101
}
100102

101-
pub fn add_event_start(&mut self, activity_kind: ActivityKind) {
102-
self.add_event(EventKind::Start, activity_kind)
103+
pub fn add_event_start(&mut self, activity_kind: ActivityKind, argv: String) {
104+
self.add_event(EventKind::Start { argv }, activity_kind)
103105
}
104106
pub fn add_event_end(&mut self, activity_kind: ActivityKind, exit_code: ExitCode) {
105107
self.add_event(
@@ -144,7 +146,7 @@ impl EventLog {
144146
#[cfg(test)]
145147
pub mod tests {
146148

147-
use super::EventLog;
149+
use super::{EventKind, EventLog};
148150
use crate::error::{ErrorKind, ExitCode};
149151
use crate::session::ActivityKind;
150152

@@ -153,21 +155,33 @@ pub mod tests {
153155
let mut event_log = EventLog::init();
154156
assert_eq!(event_log.events.len(), 0);
155157

156-
event_log.add_event_start(ActivityKind::Current);
158+
event_log.add_event_start(ActivityKind::Current, String::from("foo"));
157159
assert_eq!(event_log.events.len(), 1);
158160
assert_eq!(event_log.events[0].name, "current");
161+
assert_eq!(
162+
event_log.events[0].event,
163+
EventKind::Start {
164+
argv: String::from("foo")
165+
}
166+
);
159167

160168
event_log.add_event_end(ActivityKind::Pin, ExitCode::NetworkError);
161169
assert_eq!(event_log.events.len(), 2);
162170
assert_eq!(event_log.events[1].name, "pin");
171+
assert_eq!(event_log.events[1].event, EventKind::End { exit_code: 5 });
163172

164173
event_log.add_event_tool_end(ActivityKind::Version, 12);
165174
assert_eq!(event_log.events.len(), 3);
166175
assert_eq!(event_log.events[2].name, "version");
176+
assert_eq!(
177+
event_log.events[2].event,
178+
EventKind::ToolEnd { exit_code: 12 }
179+
);
167180

168181
let error = ErrorKind::BinaryExecError.into();
169182
event_log.add_event_error(ActivityKind::Install, &error);
170183
assert_eq!(event_log.events.len(), 4);
171184
assert_eq!(event_log.events[3].name, "install");
185+
// not checking the error because it has too much machine-specific info
172186
}
173187
}

crates/volta-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod command;
44
pub mod error;
5-
mod event;
5+
pub mod event;
66
pub mod fs;
77
mod hook;
88
pub mod inventory;

crates/volta-core/src/monitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Monitor {
2828
match json {
2929
Ok(data) => {
3030
// FIXME: tighten up this error message
31-
write!(p_stdin, "{}", data).expect("Writing data to plugin failed!");
31+
write!(p_stdin, "{}\n", data).expect("Writing data to plugin failed!");
3232
}
3333
Err(error) => {
3434
// FIXME: tighten up this error message

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ 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-
session.add_event_start(ActivityKind::Node);
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);
1318
// Don't re-evaluate the platform if this is a recursive call
1419
let platform = match env::var_os(RECURSION_ENV_VAR) {
1520
Some(_) => None,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ 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-
session.add_event_start(ActivityKind::Npm);
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);
2429
// Don't re-evaluate the context or global install interception if this is a recursive call
2530
let platform = match env::var_os(RECURSION_ENV_VAR) {
2631
Some(_) => None,

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

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

1717
/// Build a `ToolCommand` for npx
1818
pub(super) fn command(args: &[OsString], session: &mut Session) -> Fallible<Executor> {
19-
session.add_event_start(ActivityKind::Npx);
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);
2025
// Don't re-evaluate the context if this is a recursive call
2126
let platform = match env::var_os(RECURSION_ENV_VAR) {
2227
Some(_) => None,

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ 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-
session.add_event_start(ActivityKind::Yarn);
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);
2126
// Don't re-evaluate the context or global install interception if this is a recursive call
2227
let platform = match env::var_os(RECURSION_ENV_VAR) {
2328
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) {
135-
self.event_log.add_event_start(activity_kind)
134+
pub fn add_event_start(&mut self, activity_kind: ActivityKind, argv: String) {
135+
self.event_log.add_event_start(activity_kind, argv)
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) -> Fallible<ExitCode> {
48+
pub(crate) fn run(self, session: &mut Session, argv: String) -> 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)
58+
command.run(session, argv)
5959
} else {
60-
Volta::from_iter(["volta", "help"].iter()).run(session)
60+
Volta::from_iter(["volta", "help"].iter()).run(session, argv)
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) -> Fallible<ExitCode> {
133+
pub(crate) fn run(self, session: &mut Session, argv: String) -> Fallible<ExitCode> {
134134
match self {
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),
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),
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) -> Fallible<ExitCode> {
36-
session.add_event_start(ActivityKind::Completions);
35+
fn run(self, session: &mut Session, argv: String) -> Fallible<ExitCode> {
36+
session.add_event_start(ActivityKind::Completions, argv);
3737

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

0 commit comments

Comments
 (0)