Skip to content

Commit 2a8f01f

Browse files
committed
Fix failing integration test
There was a very subtle issue with assert_cmd where you can unwrap the output twice, causing `.failure()` to always panic
1 parent f266068 commit 2a8f01f

File tree

7 files changed

+56
-31
lines changed

7 files changed

+56
-31
lines changed

Cargo.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ nom = "7.1.3"
3939
assert-json-diff = "2.0.2"
4040
assert_cmd = "2.0.16"
4141
wiremock = "0.6.2"
42+
predicates = "3.1.2"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Usage: maconomy <COMMAND>
3535
Commands:
3636
get Get the time sheet for the current week
3737
set Set number of hours on day(s) for a given job and task
38-
clear Remove hours hours on day(s) for a given job and task
38+
clear Remove hours on day(s) for a given job and task
3939
submit Submit time sheet for week
4040
logout Log out
4141
line Operate on entire lines in the time sheet

src/cli/arguments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub enum Command {
8989
year: Option<i32>,
9090
},
9191

92-
/// Remove hours hours on day(s) for a given job and task
92+
/// Remove hours on day(s) for a given job and task
9393
Clear {
9494
/// Name of the job
9595
#[arg(long, short)]

src/cli/commands.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use tokio::sync::Mutex;
1717

1818
macro_rules! exit_with_error {
1919
($($arg:tt)*) => {{
20+
log::warn!("Exiting with error");
2021
eprintln!($($arg)*);
2122
std::process::exit(1);
2223
}};

src/infrastructure/repositories/time_sheet_repository.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl TimeSheetRepository<'_> {
123123

124124
time_sheet.find_line_nr(job, task).with_context(|| {
125125
format!(
126-
"format not find job '{job}' and task '{task}', even after creating a new \
126+
"did not find job '{job}' and task '{task}', even after creating a new \
127127
line for it"
128128
)
129129
})?

tests/integration/cli.rs

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@ use crate::helpers::{
55
mock_set_hours, mock_tasks_search,
66
},
77
};
8-
use assert_cmd::{assert::OutputAssertExt, Command};
8+
use assert_cmd::Command;
99
use std::{env, ffi};
1010
use wiremock::MockServer;
1111

1212
fn run_json(
1313
args: impl IntoIterator<Item = impl AsRef<ffi::OsStr>>,
1414
server_url: &str,
1515
) -> serde_json::Value {
16-
let output = run(args, server_url);
16+
let output = run(args, server_url).unwrap();
1717
serde_json::from_slice(&output.stdout).unwrap()
1818
}
1919

2020
fn run(
2121
args: impl IntoIterator<Item = impl AsRef<ffi::OsStr>>,
2222
server_url: &str,
23-
) -> std::process::Output {
23+
) -> assert_cmd::Command {
2424
env::set_var("MACONOMY__MACONOMY_URL", server_url);
25-
Command::cargo_bin("maconomy").unwrap().args(args).unwrap()
26-
// output.stdout.into_output().to_string()
25+
let mut cmd = Command::cargo_bin("maconomy").unwrap();
26+
cmd.args(args);
27+
cmd
2728
}
2829

2930
#[tokio::main]
@@ -91,27 +92,24 @@ async fn test_set_hours() {
9192
create_test_config();
9293

9394
// When
94-
let output = run(
95-
[
96-
"set",
97-
"8",
98-
"--job",
99-
"job one",
100-
"--task",
101-
"some task one",
102-
"--day",
103-
"monday",
104-
],
105-
&mock_server.uri(),
106-
);
95+
let cmd = [
96+
"set",
97+
"8",
98+
"--job",
99+
"job one",
100+
"--task",
101+
"some task one",
102+
"--day",
103+
"monday",
104+
];
105+
let mut output = run(cmd, &mock_server.uri());
107106

108107
// Then
109-
assert!(output.status.success());
108+
output.assert().success();
110109
}
111110

112111
#[tokio::main]
113112
#[test]
114-
#[ignore]
115113
async fn test_set_hours_err() {
116114
// Given
117115

@@ -125,13 +123,19 @@ async fn test_set_hours_err() {
125123
create_test_config();
126124

127125
// When
128-
let output = run(
129-
["set", "--job", "doesn't exist", "--task", "some task", "8"],
130-
&mock_server.uri(),
131-
);
126+
let cmd = [
127+
"set",
128+
"--job",
129+
"doesn't exist",
130+
"--task",
131+
"some task one",
132+
"8",
133+
];
134+
let mut output = run(cmd, &mock_server.uri());
132135

133-
// Then
134-
// TODO: `output.assert().failure()` doesn't seem to work. Is it because my program panics?
135-
// dbg!(&output.stdout.into_output().to_string());
136-
output.assert().failure();
136+
let expected_stoud_prefix = "Something went wrong when adding a new line to the time sheet";
137+
output
138+
.assert()
139+
.stderr(predicates::str::starts_with(expected_stoud_prefix))
140+
.failure();
137141
}

0 commit comments

Comments
 (0)