Skip to content

Commit b367410

Browse files
committed
don't add the args until the end, as a new event
1 parent fa4186c commit b367410

File tree

5 files changed

+101
-88
lines changed

5 files changed

+101
-88
lines changed

crates/volta-core/src/event.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ pub struct ErrorEnv {
3030
#[derive(Deserialize, Serialize, PartialEq, Debug)]
3131
#[serde(rename_all = "lowercase")]
3232
pub enum EventKind {
33-
Start {
34-
argv: String,
35-
},
33+
Start,
3634
End {
3735
exit_code: i32,
3836
},
@@ -44,6 +42,9 @@ pub enum EventKind {
4442
ToolEnd {
4543
exit_code: i32,
4644
},
45+
Args {
46+
argv: String,
47+
},
4748
}
4849

4950
impl EventKind {
@@ -101,16 +102,7 @@ impl EventLog {
101102
}
102103

103104
pub fn add_event_start(&mut self, activity_kind: ActivityKind) {
104-
let argv = env::args_os()
105-
.enumerate()
106-
.fold(String::new(), |mut result, (i, arg)| {
107-
if i > 0 {
108-
result.push(' ');
109-
}
110-
result.push_str(&arg.to_string_lossy());
111-
result
112-
});
113-
self.add_event(EventKind::Start { argv }, activity_kind)
105+
self.add_event(EventKind::Start, activity_kind)
114106
}
115107
pub fn add_event_end(&mut self, activity_kind: ActivityKind, exit_code: ExitCode) {
116108
self.add_event(
@@ -133,6 +125,18 @@ impl EventLog {
133125
activity_kind,
134126
)
135127
}
128+
pub fn add_event_args(&mut self) {
129+
let argv = env::args_os()
130+
.enumerate()
131+
.fold(String::new(), |mut result, (i, arg)| {
132+
if i > 0 {
133+
result.push(' ');
134+
}
135+
result.push_str(&arg.to_string_lossy());
136+
result
137+
});
138+
self.add_event(EventKind::Args { argv }, ActivityKind::Args)
139+
}
136140

137141
fn add_event(&mut self, event_kind: EventKind, activity_kind: ActivityKind) {
138142
let event = event_kind.into_event(activity_kind);
@@ -167,18 +171,7 @@ pub mod tests {
167171
event_log.add_event_start(ActivityKind::Current);
168172
assert_eq!(event_log.events.len(), 1);
169173
assert_eq!(event_log.events[0].name, "current");
170-
match event_log.events[0].event {
171-
EventKind::Start { ref argv } => {
172-
let re = Regex::new("volta_core").unwrap();
173-
assert!(re.is_match(argv));
174-
}
175-
_ => {
176-
panic!(
177-
"Expected EventKind::Start {{ argv }}, Got: {:?}",
178-
event_log.events[0].event
179-
);
180-
}
181-
}
174+
assert_eq!(event_log.events[0].event, EventKind::Start);
182175

183176
event_log.add_event_end(ActivityKind::Pin, ExitCode::NetworkError);
184177
assert_eq!(event_log.events.len(), 2);
@@ -198,5 +191,21 @@ pub mod tests {
198191
assert_eq!(event_log.events.len(), 4);
199192
assert_eq!(event_log.events[3].name, "install");
200193
// not checking the error because it has too much machine-specific info
194+
195+
event_log.add_event_args();
196+
assert_eq!(event_log.events.len(), 5);
197+
assert_eq!(event_log.events[4].name, "args");
198+
match event_log.events[4].event {
199+
EventKind::Args { ref argv } => {
200+
let re = Regex::new("volta_core").unwrap();
201+
assert!(re.is_match(argv));
202+
}
203+
_ => {
204+
panic!(
205+
"Expected EventKind::Args {{ argv }}, Got: {:?}",
206+
event_log.events[4].event
207+
);
208+
}
209+
}
201210
}
202211
}

crates/volta-core/src/session.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub enum ActivityKind {
3636
Which,
3737
Setup,
3838
Run,
39+
Args,
3940
}
4041

4142
impl Display for ActivityKind {
@@ -62,6 +63,7 @@ impl Display for ActivityKind {
6263
ActivityKind::Completions => "completions",
6364
ActivityKind::Which => "which",
6465
ActivityKind::Run => "run",
66+
ActivityKind::Args => "args",
6567
};
6668
f.write_str(s)
6769
}
@@ -158,12 +160,14 @@ impl Session {
158160
}
159161
}
160162

161-
pub fn exit(self, code: ExitCode) -> ! {
163+
pub fn exit(mut self, code: ExitCode) -> ! {
164+
self.event_log.add_event_args();
162165
self.publish_to_event_log();
163166
code.exit();
164167
}
165168

166-
pub fn exit_tool(self, code: i32) -> ! {
169+
pub fn exit_tool(mut self, code: i32) -> ! {
170+
self.event_log.add_event_args();
167171
self.publish_to_event_log();
168172
exit(code);
169173
}

tests/acceptance/hooks.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::path::PathBuf;
22
use std::{thread, time};
33

4-
use crate::support::events_helpers::{assert_events, match_end, match_error, match_start};
4+
use crate::support::events_helpers::{
5+
assert_events, match_args, match_end, match_error, match_start,
6+
};
57
use crate::support::sandbox::sandbox;
68
use hamcrest2::assert_that;
79
use hamcrest2::prelude::*;
@@ -155,16 +157,14 @@ fn redirects_download() {
155157
assert_events(
156158
&s,
157159
vec![
158-
(
159-
"volta",
160-
match_start(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
161-
),
162-
(
163-
"install",
164-
match_start(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
165-
),
160+
("volta", match_start()),
161+
("install", match_start()),
166162
("volta", match_error(5, "Could not download node")),
167163
("volta", match_end(5)),
164+
(
165+
"args",
166+
match_args(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
167+
),
168168
],
169169
);
170170
}
@@ -192,16 +192,14 @@ fn merges_project_and_default_hooks() {
192192
assert_events(
193193
&s,
194194
vec![
195-
(
196-
"volta",
197-
match_start(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
198-
),
199-
(
200-
"install",
201-
match_start(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
202-
),
195+
("volta", match_start()),
196+
("install", match_start()),
203197
("volta", match_error(5, "Could not download yarn")),
204198
("volta", match_end(5)),
199+
(
200+
"args",
201+
match_args(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
202+
),
205203
],
206204
);
207205

@@ -217,16 +215,14 @@ fn merges_project_and_default_hooks() {
217215
assert_events(
218216
&s,
219217
vec![
220-
(
221-
"volta",
222-
match_start(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
223-
),
224-
(
225-
"install",
226-
match_start(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
227-
),
218+
("volta", match_start()),
219+
("install", match_start()),
228220
("volta", match_error(5, "Could not download node")),
229221
("volta", match_end(5)),
222+
(
223+
"args",
224+
match_args(format!("{} install [email protected]", VOLTA_BINARY).as_str()),
225+
),
230226
],
231227
);
232228
}
@@ -261,16 +257,14 @@ fn merges_workspace_hooks() {
261257
assert_events(
262258
&s,
263259
vec![
264-
(
265-
"volta",
266-
match_start(format!("{} pin [email protected]", VOLTA_BINARY).as_str()),
267-
),
268-
(
269-
"pin",
270-
match_start(format!("{} pin [email protected]", VOLTA_BINARY).as_str()),
271-
),
260+
("volta", match_start()),
261+
("pin", match_start()),
272262
("volta", match_error(5, "Could not download yarn")),
273263
("volta", match_end(5)),
264+
(
265+
"args",
266+
match_args(format!("{} pin [email protected]", VOLTA_BINARY).as_str()),
267+
),
274268
],
275269
);
276270

@@ -286,16 +280,14 @@ fn merges_workspace_hooks() {
286280
assert_events(
287281
&s,
288282
vec![
289-
(
290-
"volta",
291-
match_start(format!("{} pin [email protected]", VOLTA_BINARY).as_str()),
292-
),
293-
(
294-
"pin",
295-
match_start(format!("{} pin [email protected]", VOLTA_BINARY).as_str()),
296-
),
283+
("volta", match_start()),
284+
("pin", match_start()),
297285
("volta", match_error(5, "Could not download npm")),
298286
("volta", match_end(5)),
287+
(
288+
"args",
289+
match_args(format!("{} pin [email protected]", VOLTA_BINARY).as_str()),
290+
),
299291
],
300292
);
301293

tests/acceptance/merged_platform.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{thread, time};
22

3-
use crate::support::events_helpers::{assert_events, match_start, match_tool_end};
3+
use crate::support::events_helpers::{assert_events, match_args, match_start, match_tool_end};
44
use crate::support::sandbox::{sandbox, DistroMetadata, NodeFixture, NpmFixture, YarnFixture};
55
use hamcrest2::assert_that;
66
use hamcrest2::prelude::*;
@@ -262,12 +262,13 @@ fn uses_project_yarn_if_available() {
262262
assert_events(
263263
&s,
264264
vec![
265+
("tool", match_start()),
266+
("yarn", match_start()),
267+
("tool", match_tool_end(0)),
265268
(
266-
"tool",
267-
match_start(format!("{} --version", YARN_SHIM).as_str()),
269+
"args",
270+
match_args(format!("{} --version", YARN_SHIM).as_str()),
268271
),
269-
("yarn", match_start("--version")),
270-
("tool", match_tool_end(0)),
271272
],
272273
);
273274
}

tests/acceptance/support/events_helpers.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ use hamcrest2::prelude::*;
77
use volta_core::event::{Event, EventKind};
88

99
pub enum EventKindMatcher<'a> {
10-
Start { argv: &'a str },
10+
Start,
1111
End { exit_code: i32 },
1212
Error { exit_code: i32, error: &'a str },
1313
ToolEnd { exit_code: i32 },
14+
Args { argv: &'a str },
1415
}
1516

16-
pub fn match_start(argv: &str) -> EventKindMatcher {
17-
EventKindMatcher::Start { argv }
17+
pub fn match_start() -> EventKindMatcher<'static> {
18+
EventKindMatcher::Start
1819
}
1920

2021
pub fn match_error(exit_code: i32, error: &str) -> EventKindMatcher {
@@ -29,6 +30,10 @@ pub fn match_tool_end(exit_code: i32) -> EventKindMatcher<'static> {
2930
EventKindMatcher::ToolEnd { exit_code }
3031
}
3132

33+
pub fn match_args(argv: &str) -> EventKindMatcher {
34+
EventKindMatcher::Args { argv }
35+
}
36+
3237
pub fn assert_events(sandbox: &Sandbox, matchers: Vec<(&str, EventKindMatcher)>) {
3338
let events_path = sandbox.root().join("events.json");
3439
assert_that!(&events_path, file_exists());
@@ -39,20 +44,10 @@ pub fn assert_events(sandbox: &Sandbox, matchers: Vec<(&str, EventKindMatcher)>)
3944
assert_that!(events.len(), eq(matchers.len()));
4045

4146
for (i, matcher) in matchers.iter().enumerate() {
42-
//println!("Element at position {}: {:?}", i, matcher);
4347
assert_that!(&events[i].name, eq(matcher.0));
4448
match matcher.1 {
45-
EventKindMatcher::Start {
46-
argv: expected_argv,
47-
} => {
48-
if let EventKind::Start { argv } = &events[i].event {
49-
assert_that!(argv.clone(), matches_regex(expected_argv));
50-
} else {
51-
panic!(
52-
"Expected: Start {{ argv: {} }}, Got: {:?}",
53-
expected_argv, events[i].event
54-
);
55-
}
49+
EventKindMatcher::Start => {
50+
assert_that!(&events[i].event, eq(&EventKind::Start));
5651
}
5752
EventKindMatcher::End {
5853
exit_code: expected_exit_code,
@@ -95,6 +90,18 @@ pub fn assert_events(sandbox: &Sandbox, matchers: Vec<(&str, EventKindMatcher)>)
9590
);
9691
}
9792
}
93+
EventKindMatcher::Args {
94+
argv: expected_argv,
95+
} => {
96+
if let EventKind::Args { argv } = &events[i].event {
97+
assert_that!(argv.clone(), matches_regex(expected_argv));
98+
} else {
99+
panic!(
100+
"Expected: Args {{ argv: {} }}, Got: {:?}",
101+
expected_argv, events[i].event
102+
);
103+
}
104+
}
98105
}
99106
}
100107
}

0 commit comments

Comments
 (0)