Skip to content

Commit 796c780

Browse files
committed
rust: shorten test code
1 parent 5640c0d commit 796c780

File tree

12 files changed

+196
-214
lines changed

12 files changed

+196
-214
lines changed

rust/bear/src/args.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ pub fn cli() -> Command {
197197
#[cfg(test)]
198198
mod test {
199199
use super::*;
200-
use crate::vec_of_strings;
201200

202201
#[test]
203202
fn test_intercept_call() {
@@ -219,13 +218,13 @@ mod test {
219218
assert_eq!(
220219
arguments,
221220
Arguments {
222-
config: Some("~/bear.yaml".to_string()),
221+
config: Some("~/bear.yaml".into()),
223222
mode: Mode::Intercept {
224223
input: BuildCommand {
225-
arguments: vec_of_strings!["make", "all"]
224+
arguments: vec!["make", "all"].into_iter().map(String::from).collect()
226225
},
227226
output: BuildEvents {
228-
file_name: "custom.json".to_string()
227+
file_name: "custom.json".into()
229228
},
230229
},
231230
}
@@ -245,10 +244,10 @@ mod test {
245244
config: None,
246245
mode: Mode::Intercept {
247246
input: BuildCommand {
248-
arguments: vec_of_strings!["make", "all"]
247+
arguments: vec!["make", "all"].into_iter().map(String::from).collect()
249248
},
250249
output: BuildEvents {
251-
file_name: "events.json".to_string()
250+
file_name: "events.json".into()
252251
},
253252
},
254253
}
@@ -275,13 +274,13 @@ mod test {
275274
assert_eq!(
276275
arguments,
277276
Arguments {
278-
config: Some("~/bear.yaml".to_string()),
277+
config: Some("~/bear.yaml".into()),
279278
mode: Mode::Semantic {
280279
input: BuildEvents {
281-
file_name: "custom.json".to_string()
280+
file_name: "custom.json".into()
282281
},
283282
output: BuildSemantic {
284-
file_name: "result.json".to_string(),
283+
file_name: "result.json".into(),
285284
append: true
286285
},
287286
},
@@ -302,10 +301,10 @@ mod test {
302301
config: None,
303302
mode: Mode::Semantic {
304303
input: BuildEvents {
305-
file_name: "events.json".to_string()
304+
file_name: "events.json".into()
306305
},
307306
output: BuildSemantic {
308-
file_name: "compile_commands.json".to_string(),
307+
file_name: "compile_commands.json".into(),
309308
append: false
310309
},
311310
},
@@ -336,10 +335,10 @@ mod test {
336335
config: Some("~/bear.yaml".to_string()),
337336
mode: Mode::Combined {
338337
input: BuildCommand {
339-
arguments: vec_of_strings!["make", "all"]
338+
arguments: vec!["make", "all"].into_iter().map(String::from).collect()
340339
},
341340
output: BuildSemantic {
342-
file_name: "result.json".to_string(),
341+
file_name: "result.json".into(),
343342
append: true
344343
},
345344
},
@@ -360,10 +359,10 @@ mod test {
360359
config: None,
361360
mode: Mode::Combined {
362361
input: BuildCommand {
363-
arguments: vec_of_strings!["make", "all"]
362+
arguments: vec!["make", "all"].into_iter().map(String::from).collect(),
364363
},
365364
output: BuildSemantic {
366-
file_name: "compile_commands.json".to_string(),
365+
file_name: "compile_commands.json".into(),
367366
append: false
368367
},
369368
},

rust/bear/src/config.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ pub mod loader {
491491
mod test {
492492
use super::super::*;
493493
use super::*;
494-
use crate::{vec_of_pathbuf, vec_of_strings};
495494

496495
#[test]
497496
fn test_wrapper_config() {
@@ -553,12 +552,15 @@ pub mod loader {
553552
intercept: Intercept::Wrapper {
554553
path: default_wrapper_executable(),
555554
directory: PathBuf::from("/tmp"),
556-
executables: vec_of_pathbuf![
555+
executables: vec![
557556
"/usr/bin/cc",
558557
"/usr/bin/c++",
559558
"/usr/bin/clang",
560-
"/usr/bin/clang++"
561-
],
559+
"/usr/bin/clang++",
560+
]
561+
.into_iter()
562+
.map(PathBuf::from)
563+
.collect(),
562564
},
563565
output: Output::Clang {
564566
compilers: vec![
@@ -576,24 +578,24 @@ pub mod loader {
576578
path: PathBuf::from("/usr/bin/c++"),
577579
ignore: IgnoreOrConsider::Conditional,
578580
arguments: Arguments {
579-
match_: vec_of_strings!["-###"],
581+
match_: vec!["-###".into()],
580582
..Default::default()
581583
},
582584
},
583585
Compiler {
584586
path: PathBuf::from("/usr/bin/clang"),
585587
ignore: IgnoreOrConsider::Never,
586588
arguments: Arguments {
587-
add: vec_of_strings!["-DDEBUG"],
588-
remove: vec_of_strings!["-Wall"],
589+
add: vec!["-DDEBUG".into()],
590+
remove: vec!["-Wall".into()],
589591
..Default::default()
590592
},
591593
},
592594
Compiler {
593595
path: PathBuf::from("/usr/bin/clang++"),
594596
ignore: IgnoreOrConsider::Never,
595597
arguments: Arguments {
596-
remove: vec_of_strings!["-Wall"],
598+
remove: vec!["-Wall".into()],
597599
..Default::default()
598600
},
599601
},
@@ -654,7 +656,10 @@ pub mod loader {
654656
intercept: Intercept::Wrapper {
655657
path: default_wrapper_executable(),
656658
directory: default_wrapper_directory(),
657-
executables: vec_of_pathbuf!["/usr/bin/cc", "/usr/bin/c++"],
659+
executables: vec!["/usr/bin/cc", "/usr/bin/c++"]
660+
.into_iter()
661+
.map(PathBuf::from)
662+
.collect(),
658663
},
659664
output: Output::Clang {
660665
compilers: vec![],

rust/bear/src/fixtures.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

rust/bear/src/intercept/mod.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ pub trait Collector {
5757
fn stop(&self) -> Result<(), anyhow::Error>;
5858
}
5959

60+
/// Process id is an OS identifier for a process.
61+
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
62+
pub struct ProcessId(pub u32);
63+
6064
/// Represent a relevant life cycle event of a process.
6165
///
6266
/// In the current implementation, we only have one event, the `Started` event.
@@ -98,9 +102,37 @@ impl fmt::Display for Execution {
98102
}
99103
}
100104

101-
/// Process id is an OS identifier for a process.
102-
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
103-
pub struct ProcessId(pub u32);
105+
#[cfg(test)]
106+
pub fn execution(
107+
executable: &str,
108+
arguments: Vec<&str>,
109+
working_dir: &str,
110+
environment: HashMap<&str, &str>,
111+
) -> Execution {
112+
Execution {
113+
executable: PathBuf::from(executable),
114+
arguments: arguments.iter().map(|s| s.to_string()).collect(),
115+
working_dir: PathBuf::from(working_dir),
116+
environment: environment
117+
.iter()
118+
.map(|(k, v)| (k.to_string(), v.to_string()))
119+
.collect(),
120+
}
121+
}
122+
123+
#[cfg(test)]
124+
pub fn event(
125+
pid: u32,
126+
executable: &str,
127+
arguments: Vec<&str>,
128+
working_dir: &str,
129+
environment: HashMap<&str, &str>,
130+
) -> Event {
131+
Event {
132+
pid: ProcessId(pid),
133+
execution: execution(executable, arguments, working_dir, environment),
134+
}
135+
}
104136

105137
/// The service is responsible for collecting the events from the supervised processes.
106138
///

rust/bear/src/intercept/tcp.rs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -226,54 +226,37 @@ mod tests {
226226
}
227227

228228
mod fixtures {
229+
use super::super::super::event;
229230
use super::*;
230-
use crate::intercept::{Execution, ProcessId};
231-
use crate::{map_of_strings, vec_of_strings};
232231
use std::collections::HashMap;
233-
use std::path::PathBuf;
234232

235233
pub(super) static EVENTS: std::sync::LazyLock<Vec<Event>> =
236234
std::sync::LazyLock::new(|| {
237235
vec![
238-
Event {
239-
pid: ProcessId(3425),
240-
execution: Execution {
241-
executable: PathBuf::from("/usr/bin/ls"),
242-
arguments: vec_of_strings!["ls", "-l"],
243-
working_dir: PathBuf::from("/tmp"),
244-
environment: HashMap::new(),
245-
},
246-
},
247-
Event {
248-
pid: ProcessId(3492),
249-
execution: Execution {
250-
executable: PathBuf::from("/usr/bin/cc"),
251-
arguments: vec_of_strings![
252-
"cc",
253-
"-c",
254-
"./file_a.c",
255-
"-o",
256-
"./file_a.o"
257-
],
258-
working_dir: PathBuf::from("/home/user"),
259-
environment: map_of_strings! {
260-
"PATH" => "/usr/bin:/bin",
261-
"HOME" => "/home/user",
262-
},
263-
},
264-
},
265-
Event {
266-
pid: ProcessId(3522),
267-
execution: Execution {
268-
executable: PathBuf::from("/usr/bin/ld"),
269-
arguments: vec_of_strings!["ld", "-o", "./file_a", "./file_a.o"],
270-
working_dir: PathBuf::from("/opt/project"),
271-
environment: map_of_strings! {
272-
"PATH" => "/usr/bin:/bin",
273-
"LD_LIBRARY_PATH" => "/usr/lib:/lib",
274-
},
275-
},
276-
},
236+
event(
237+
3425,
238+
"/usr/bin/ls",
239+
vec!["ls", "-l"],
240+
"/tmp",
241+
HashMap::new(),
242+
),
243+
event(
244+
3492,
245+
"/usr/bin/cc",
246+
vec!["cc", "-c", "./file_a.c", "-o", "./file_a.o"],
247+
"/home/user",
248+
HashMap::from([("PATH", "/usr/bin:/bin"), ("HOME", "/home/user")]),
249+
),
250+
event(
251+
3522,
252+
"/usr/bin/ld",
253+
vec!["ld", "-o", "./file_a", "./file_a.o"],
254+
"/opt/project",
255+
HashMap::from([
256+
("PATH", "/usr/bin:/bin"),
257+
("LD_LIBRARY_PATH", "/usr/lib:/lib"),
258+
]),
259+
),
277260
]
278261
});
279262
}

rust/bear/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
pub mod args;
44
pub mod config;
5-
mod fixtures;
65
pub mod intercept;
76
pub mod modes;
87
pub mod output;

0 commit comments

Comments
 (0)