Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2599,17 +2599,21 @@ fn resolve_message_inbox(root: &Path, id: &str, host: &str) -> Result<PathBuf> {

/// Body from `-m`, else stdin (so `st2 message send x < file` works).
fn body_or_stdin(body: Option<String>) -> Result<String> {
match body {
Some(b) => Ok(b),
let body = match body {
Some(body) => body,
None => {
use std::io::Read as _;
let mut s = String::new();
let mut body = String::new();
std::io::stdin()
.read_to_string(&mut s)
.read_to_string(&mut body)
.context("reading message body from stdin")?;
Ok(s)
body
}
};
if body.is_empty() {
anyhow::bail!("message body must not be empty");
}
Ok(body)
}

/// `[identity] <filename>` positionals: if `second` is present, `first` is the identity; otherwise
Expand Down
29 changes: 29 additions & 0 deletions tests/message_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ fn sent(root: &Path, identity: &str, extra: &[&str]) -> std::process::Output {
.unwrap()
}

#[test]
fn send_rejects_empty_explicit_and_stdin_bodies_without_persisting_a_message() {
for body_arg in [Some(""), None] {
let tmp = tempfile::tempdir().unwrap();
write_agent(tmp.path(), "sender");
write_agent(tmp.path(), "recipient");

let mut command = Command::new(env!("CARGO_BIN_EXE_st2"));
command
.args(["message", "send", "recipient", "--root"])
.arg(tmp.path())
.args(["--host", "h", "--as", "sender"])
.stdin(Stdio::null());
if let Some(body) = body_arg {
command.args(["-m", body]);
}

let output = command.output().unwrap();
assert!(!output.status.success());
assert!(
String::from_utf8_lossy(&output.stderr).contains("message body must not be empty"),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(!tmp.path().join("h/recipient/resources").exists());
assert!(!tmp.path().join("h/sender/resources").exists());
}
}

#[test]
fn event_metadata_is_exposed_by_list_and_read_json() {
let tmp = tempfile::tempdir().unwrap();
Expand Down
Loading