Skip to content

Commit 5c86a7f

Browse files
authored
Merge pull request EricCrosson#84 from andrew-scott-fischer/add-tests
Add tests
2 parents 87c8b3e + ab7f512 commit 5c86a7f

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

src/cli.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn duration_from_str(
1212
}
1313
}
1414

15-
#[derive(Clone, Copy, Debug)]
15+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1616
pub(crate) enum Retry {
1717
ForDuration(Duration),
1818
NumberOfTimes(u64),
@@ -98,3 +98,60 @@ pub(crate) struct Cli {
9898
#[arg(help = "The command to run")]
9999
pub command: Vec<String>,
100100
}
101+
102+
#[cfg(test)]
103+
mod tests {
104+
use super::*;
105+
106+
#[test]
107+
fn test_retry_from_str_number_of_times() {
108+
let input = "3x";
109+
let expected = Retry::NumberOfTimes(3);
110+
let result = Retry::from_str(input).unwrap();
111+
assert_eq!(result, expected);
112+
}
113+
114+
#[test]
115+
fn test_retry_from_str_for_duration() {
116+
let input = "10s";
117+
let expected = Retry::ForDuration(Duration::from_secs(10));
118+
let result = Retry::from_str(input).unwrap();
119+
assert_eq!(result, expected);
120+
}
121+
122+
#[test]
123+
fn test_retry_from_str_invalid_input() {
124+
let input = "abc";
125+
let result = Retry::from_str(input);
126+
assert!(result.is_err());
127+
}
128+
129+
#[test]
130+
fn test_cli_up_to_retry_from_str() {
131+
let input = "3x";
132+
let expected = Retry::NumberOfTimes(3);
133+
let cli = Cli::parse_from(&["", "--up-to", input]);
134+
assert_eq!(cli.up_to, expected);
135+
}
136+
137+
#[test]
138+
fn test_cli_task_timeout_duration_from_str() {
139+
let input = "5m";
140+
let expected = Some(Duration::from_secs(300));
141+
let cli = Cli::parse_from(&["", "--task-timeout", input, "--up-to", "1x"]);
142+
assert_eq!(cli.task_timeout, expected);
143+
}
144+
145+
#[test]
146+
fn test_cli_command() {
147+
let input = vec!["ping", "-c", "1", "google.com"];
148+
let expected = input.iter().map(|s| s.to_owned()).collect::<Vec<_>>();
149+
let cli = Cli::parse_from(
150+
&["", "--up-to", "1x", "--"]
151+
.into_iter()
152+
.chain(input.into_iter())
153+
.collect::<Vec<_>>(),
154+
);
155+
assert_eq!(cli.command, expected);
156+
}
157+
}

src/main.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,78 @@ async fn main() -> Result<()> {
103103
let args: Cli = Cli::parse();
104104
run_tasks(args.command, args.up_to, args.task_timeout).await
105105
}
106+
#[cfg(test)]
107+
mod tests {
108+
use super::*;
109+
110+
#[tokio::test]
111+
async fn test_eval_success() {
112+
let command = vec!["true".to_owned()];
113+
let exit_status = eval(&command).await.unwrap();
114+
assert_eq!(exit_status.success(), true);
115+
}
116+
117+
#[tokio::test]
118+
async fn test_eval_failure() {
119+
let command = vec!["false".to_owned()];
120+
let exit_status = eval(&command).await.unwrap();
121+
assert_eq!(exit_status.success(), false);
122+
}
123+
124+
#[tokio::test]
125+
async fn test_run_task_success() {
126+
let command = vec!["true".to_owned()];
127+
let task_outcome = run_task(&command, None).await.unwrap();
128+
assert_eq!(task_outcome, TaskOutcome::Success);
129+
}
130+
131+
#[tokio::test]
132+
async fn test_run_task_failure() {
133+
let command = vec!["false".to_owned()];
134+
let task_outcome = run_task(&command, None).await.unwrap();
135+
assert_eq!(task_outcome, TaskOutcome::Timeout);
136+
}
137+
138+
#[tokio::test]
139+
async fn test_run_task_timeout() {
140+
let command = vec!["sleep".to_owned(), "10".to_owned()];
141+
let task_timeout = Some(Duration::from_secs(1));
142+
let task_outcome = run_task(&command, task_timeout).await.unwrap();
143+
assert_eq!(task_outcome, TaskOutcome::Timeout);
144+
}
145+
146+
#[tokio::test]
147+
async fn test_loop_task_success() {
148+
let command = vec!["true".to_owned()];
149+
let task_timeout = Some(Duration::from_secs(5));
150+
let task_outcome = loop_task(&command, task_timeout).await.unwrap();
151+
assert_eq!(task_outcome, TaskOutcome::Success);
152+
}
153+
154+
#[tokio::test]
155+
async fn test_run_tasks_success() {
156+
let command = vec!["true".to_owned()];
157+
let up_to = Retry::NumberOfTimes(3);
158+
let task_timeout = Some(Duration::from_secs(5));
159+
let result = run_tasks(command, up_to, task_timeout).await;
160+
assert_eq!(result.is_ok(), true);
161+
}
162+
163+
#[tokio::test]
164+
async fn test_run_tasks_failure() {
165+
let command = vec!["false".to_owned()];
166+
let up_to = Retry::NumberOfTimes(3);
167+
let task_timeout = Some(Duration::from_secs(5));
168+
let result = run_tasks(command, up_to, task_timeout).await;
169+
assert_eq!(result.is_err(), true);
170+
}
171+
172+
#[tokio::test]
173+
async fn test_run_tasks_timeout() {
174+
let command = vec!["sleep".to_owned(), "10".to_owned()];
175+
let up_to = Retry::ForDuration(Duration::from_secs(5));
176+
let task_timeout = Some(Duration::from_secs(1));
177+
let result = run_tasks(command, up_to, task_timeout).await;
178+
assert_eq!(result.is_err(), true);
179+
}
180+
}

0 commit comments

Comments
 (0)