From 45802120fb8b1200114b787b57dc829eed102d4f Mon Sep 17 00:00:00 2001 From: "Teppei.F" <37261985+T3pp31@users.noreply.github.com> Date: Sat, 30 May 2026 00:13:23 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20stdin=E5=85=A5=E5=8A=9B=E3=81=A7?= =?UTF-8?q?=E5=89=8D=E5=BE=8C=E3=82=B9=E3=83=9A=E3=83=BC=E3=82=B9=E3=81=8C?= =?UTF-8?q?=E6=B6=88=E3=81=88=E3=82=8B=E4=B8=8D=E5=85=B7=E5=90=88=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit get_input_text と対話モードの prompt_for_text で全面 trim() していたため、 --text 引数と stdin 入力で暗号化結果が一致しない問題を解消する。 末尾の改行のみ trim_trailing_newline で除去し、スペースは保持する。 回帰防止の CLI 統合テストを追加。 Co-authored-by: Cursor --- src/cli.rs | 8 +++---- tests/cli_output_tests.rs | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 809e367..d15968f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -178,10 +178,10 @@ fn get_input_text( ) .into()); } - Ok(input.trim().to_string()) + Ok(trim_trailing_newline(&input).to_string()) } -#[cfg(test)] +/// Strips only trailing line endings from stdin/file-style input, preserving spaces. fn trim_trailing_newline(input: &str) -> &str { input.trim_end_matches(['\n', '\r']) } @@ -227,13 +227,13 @@ fn output_result( /// /// # Returns /// -/// The trimmed input string +/// Input with trailing line endings removed (leading/trailing spaces preserved). fn prompt_for_text(prompt: &str) -> io::Result { print!("{}", prompt); io::stdout().flush()?; let mut input = String::new(); io::stdin().read_line(&mut input)?; - Ok(input.trim().to_string()) + Ok(trim_trailing_newline(&input).to_string()) } /// Validates shift input string and returns parsed value with optional warning diff --git a/tests/cli_output_tests.rs b/tests/cli_output_tests.rs index e4134f3..2a782da 100644 --- a/tests/cli_output_tests.rs +++ b/tests/cli_output_tests.rs @@ -51,6 +51,55 @@ fn test_cli_encrypt_with_text_arg() { ); } +#[test] +fn test_cli_encrypt_stdin_matches_text_arg_for_surrounding_spaces() { + use std::io::Write; + use std::process::{Command, Stdio}; + + // Given: --text with surrounding spaces + let text_arg_output = Command::new("cargo") + .args(["run", "--", "encrypt", "--text", " Hello ", "--shift", "3"]) + .output() + .expect("Failed to execute CLI with --text"); + assert!(text_arg_output.status.success()); + let text_arg_line = String::from_utf8_lossy(&text_arg_output.stdout) + .lines() + .last() + .unwrap_or("") + .to_string(); + + // When: the same text is provided via stdin + let mut stdin_child = Command::new("cargo") + .args(["run", "--", "encrypt", "--shift", "3"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("Failed to spawn CLI for stdin"); + stdin_child + .stdin + .as_mut() + .unwrap() + .write_all(b" Hello \n") + .unwrap(); + let stdin_output = stdin_child.wait_with_output().unwrap(); + assert!(stdin_output.status.success()); + let stdin_stdout = String::from_utf8_lossy(&stdin_output.stdout); + let stdin_line = stdin_stdout + .strip_prefix("Enter text: ") + .unwrap_or(&stdin_stdout) + .trim_end_matches(['\n', '\r']) + .to_string(); + + // Then: ciphertext matches and surrounding spaces are preserved + assert_eq!( + text_arg_line, stdin_line, + "--text line: {:?}, stdin ciphertext: {:?}, full stdin stdout: {:?}", + text_arg_line, stdin_line, stdin_stdout + ); + assert_eq!(stdin_line, " Khoor "); +} + #[test] fn test_cli_decrypt_with_text_arg() { // Given: CLI with decrypt command and text argument From 743e5665c4645bb4223952415bfeaa72b1b2fc68 Mon Sep 17 00:00:00 2001 From: "Teppei.F" <37261985+T3pp31@users.noreply.github.com> Date: Sat, 30 May 2026 00:14:39 +0900 Subject: [PATCH 2/2] =?UTF-8?q?style:=20cargo=20fmt=20=E3=81=AB=E5=90=88?= =?UTF-8?q?=E3=82=8F=E3=81=9B=E3=81=A6=20CLI=20=E7=B5=B1=E5=90=88=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=92=E6=95=B4=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- tests/cli_output_tests.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/cli_output_tests.rs b/tests/cli_output_tests.rs index 2a782da..ae12562 100644 --- a/tests/cli_output_tests.rs +++ b/tests/cli_output_tests.rs @@ -58,7 +58,15 @@ fn test_cli_encrypt_stdin_matches_text_arg_for_surrounding_spaces() { // Given: --text with surrounding spaces let text_arg_output = Command::new("cargo") - .args(["run", "--", "encrypt", "--text", " Hello ", "--shift", "3"]) + .args([ + "run", + "--", + "encrypt", + "--text", + " Hello ", + "--shift", + "3", + ]) .output() .expect("Failed to execute CLI with --text"); assert!(text_arg_output.status.success());