Skip to content

Commit

Permalink
Fix failing integration test
Browse files Browse the repository at this point in the history
There was a very subtle issue with assert_cmd where you can unwrap the
output twice, causing `.failure()` to always panic
  • Loading branch information
mawkler committed Nov 14, 2024
1 parent f266068 commit 2a8f01f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 31 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ nom = "7.1.3"
assert-json-diff = "2.0.2"
assert_cmd = "2.0.16"
wiremock = "0.6.2"
predicates = "3.1.2"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Usage: maconomy <COMMAND>
Commands:
get Get the time sheet for the current week
set Set number of hours on day(s) for a given job and task
clear Remove hours hours on day(s) for a given job and task
clear Remove hours on day(s) for a given job and task
submit Submit time sheet for week
logout Log out
line Operate on entire lines in the time sheet
Expand Down
2 changes: 1 addition & 1 deletion src/cli/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub enum Command {
year: Option<i32>,
},

/// Remove hours hours on day(s) for a given job and task
/// Remove hours on day(s) for a given job and task
Clear {
/// Name of the job
#[arg(long, short)]
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tokio::sync::Mutex;

macro_rules! exit_with_error {
($($arg:tt)*) => {{
log::warn!("Exiting with error");
eprintln!($($arg)*);
std::process::exit(1);
}};
Expand Down
2 changes: 1 addition & 1 deletion src/infrastructure/repositories/time_sheet_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl TimeSheetRepository<'_> {

time_sheet.find_line_nr(job, task).with_context(|| {
format!(
"format not find job '{job}' and task '{task}', even after creating a new \
"did not find job '{job}' and task '{task}', even after creating a new \
line for it"
)
})?
Expand Down
60 changes: 32 additions & 28 deletions tests/integration/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ use crate::helpers::{
mock_set_hours, mock_tasks_search,
},
};
use assert_cmd::{assert::OutputAssertExt, Command};
use assert_cmd::Command;
use std::{env, ffi};
use wiremock::MockServer;

fn run_json(
args: impl IntoIterator<Item = impl AsRef<ffi::OsStr>>,
server_url: &str,
) -> serde_json::Value {
let output = run(args, server_url);
let output = run(args, server_url).unwrap();
serde_json::from_slice(&output.stdout).unwrap()
}

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

#[tokio::main]
Expand Down Expand Up @@ -91,27 +92,24 @@ async fn test_set_hours() {
create_test_config();

// When
let output = run(
[
"set",
"8",
"--job",
"job one",
"--task",
"some task one",
"--day",
"monday",
],
&mock_server.uri(),
);
let cmd = [
"set",
"8",
"--job",
"job one",
"--task",
"some task one",
"--day",
"monday",
];
let mut output = run(cmd, &mock_server.uri());

// Then
assert!(output.status.success());
output.assert().success();
}

#[tokio::main]
#[test]
#[ignore]
async fn test_set_hours_err() {
// Given

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

// When
let output = run(
["set", "--job", "doesn't exist", "--task", "some task", "8"],
&mock_server.uri(),
);
let cmd = [
"set",
"--job",
"doesn't exist",
"--task",
"some task one",
"8",
];
let mut output = run(cmd, &mock_server.uri());

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

0 comments on commit 2a8f01f

Please sign in to comment.