pdftract/crates/pdftract-cli/tests/test_hash_exit_codes.rs
jedarden 2af3b0aeea fix(pdftract-3954u): make map_error_to_exit_code public in hash module
- Made map_error_to_exit_code() function public in hash.rs so it can be
  called from main.rs
- Added test file test_hash_exit_codes.rs to verify exit code behavior
- Updated verification note with current implementation status

The hash subcommand was already implemented but map_error_to_exit_code
was private, causing a compilation error. This fix resolves the issue.

Related: pdftract-3954u
2026-05-28 04:44:45 -04:00

78 lines
2.2 KiB
Rust

//! Tests for hash subcommand exit codes.
use std::process::Command;
#[test]
fn test_hash_nonexistent_file() {
let output = Command::new("cargo")
.args(["run", "--bin", "pdftract", "--", "hash", "/nonexistent/file.pdf"])
.output()
.expect("Failed to run pdftract hash");
// Exit code 4 for file not found
assert_eq!(output.status.code(), Some(4));
// stderr should contain error message
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("not found") || stderr.contains("No such file"));
}
#[test]
fn test_hash_help() {
let output = Command::new("cargo")
.args(["run", "--bin", "pdftract", "--", "hash", "--help"])
.output()
.expect("Failed to run pdftract hash --help");
// Help should exit with 0
assert_eq!(output.status.code(), Some(0));
// Should show help text
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("Compute the PDF structural fingerprint"));
assert!(stdout.contains("--password"));
}
#[test]
fn test_hash_url_flag() {
let output = Command::new("cargo")
.args(["run", "--bin", "pdftract", "--", "hash", "--help"])
.output()
.expect("Failed to run pdftract hash --help");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("header"));
}
#[cfg(feature = "remote")]
#[test]
fn test_hash_url_not_found() {
let output = Command::new("cargo")
.args([
"run",
"--bin",
"pdftract",
"--features",
"remote",
"--",
"hash",
"https://nonexistent.invalid/test.pdf",
])
.output()
.expect("Failed to run pdftract hash");
// Exit code 4 for URL not found/DNS failure
let code = output.status.code();
assert!(code == Some(4) || code == Some(5), "Expected exit code 4 or 5, got {:?}", code);
}
#[test]
fn test_hash_basic_invocation() {
// Test that the hash subcommand is recognized
let output = Command::new("cargo")
.args(["run", "--bin", "pdftract", "--", "hash", "--help"])
.output()
.expect("Failed to run pdftract hash --help");
assert!(output.status.success());
}