Skip to content

Commit ec7970c

Browse files
committed
[lp] POSIX tweaks
POSIX Compliance Fixes: 1. -m option - Now accepted as stub (was returning error) 2. -w option - Now accepted as stub (was returning error) Code Cleanup: 3. -c handling - Removed unused copy_mode parameter from read_input() function. Added comment explaining that copy mode is effectively always-on due to IPP requirements. Files Modified: - print/lp.rs - Main implementation - print/tests/lp/mod.rs - Updated tests
1 parent d455ab0 commit ec7970c

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

print/lp.rs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use num_traits::ToPrimitive;
2222
#[derive(Parser)]
2323
#[command(version, about = gettext("lp - send files to a printer"))]
2424
struct Args {
25-
/// Exit only after file access is no longer required (copy mode)
25+
/// Exit only after file access is no longer required (effectively always-on)
2626
#[arg(short = 'c')]
27-
copy: bool,
27+
_copy: bool,
2828

2929
/// Printer destination (IPP URI, e.g., ipp://host:631/ipp/print)
3030
#[arg(short = 'd')]
@@ -34,17 +34,17 @@ struct Args {
3434
#[arg(short = 'n', default_value = "1", value_parser = clap::value_parser!(u32).range(1..))]
3535
copies: u32,
3636

37-
/// Send mail after printing (not implemented)
37+
/// Send mail after printing (stub: accepted but not implemented)
3838
#[arg(short = 'm')]
39-
mail: bool,
39+
_mail: bool,
4040

4141
/// Suppress messages (no request ID output)
4242
#[arg(short = 's')]
4343
silent: bool,
4444

45-
/// Write to terminal after printing (not implemented)
45+
/// Write to terminal after printing (stub: accepted but not implemented)
4646
#[arg(short = 'w')]
47-
write: bool,
47+
_write: bool,
4848

4949
/// Printer-dependent options (can be specified multiple times)
5050
#[arg(short = 'o', action = clap::ArgAction::Append)]
@@ -92,24 +92,15 @@ fn validate_uri(dest: &str) -> Result<Uri, String> {
9292
.map_err(|_| gettext("invalid destination URI"))
9393
}
9494

95-
/// Read input data from a file or stdin
96-
fn read_input(path: &PathBuf, copy_mode: bool) -> Result<Vec<u8>, io::Error> {
95+
/// Read input data from a file or stdin.
96+
/// Note: Copy mode (-c) is effectively always-on since IPP requires full data upload.
97+
fn read_input(path: &PathBuf) -> Result<Vec<u8>, io::Error> {
9798
let path_str = path.to_string_lossy();
9899
if path_str == "-" {
99-
// Read from stdin
100100
let mut data = Vec::new();
101101
io::stdin().read_to_end(&mut data)?;
102102
Ok(data)
103-
} else if copy_mode {
104-
// Copy mode: read entire file into memory
105-
let mut file = File::open(path)?;
106-
let mut data = Vec::new();
107-
file.read_to_end(&mut data)?;
108-
Ok(data)
109103
} else {
110-
// Non-copy mode: still read the file, but we could potentially
111-
// stream it. For simplicity, we'll read it all since IPP needs
112-
// the data to be available for the request.
113104
let mut file = File::open(path)?;
114105
let mut data = Vec::new();
115106
file.read_to_end(&mut data)?;
@@ -209,14 +200,6 @@ fn send_print_job(
209200
}
210201

211202
fn do_lp(args: Args) -> Result<(), String> {
212-
// Check for unsupported options first
213-
if args.mail {
214-
return Err(gettext("-m option not supported"));
215-
}
216-
if args.write {
217-
return Err(gettext("-w option not supported"));
218-
}
219-
220203
// Get and validate destination
221204
let dest = get_destination(&args)?;
222205
let uri = validate_uri(&dest)?;
@@ -237,7 +220,7 @@ fn do_lp(args: Args) -> Result<(), String> {
237220
};
238221

239222
// Read input data
240-
let data = read_input(file, args.copy)
223+
let data = read_input(file)
241224
.map_err(|e| format!("{} '{}': {}", gettext("cannot open"), file.display(), e))?;
242225

243226
// Send print job

print/tests/lp/mod.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ fn lp_invalid_uri_error() {
5151
);
5252
}
5353

54-
/// Test that -m option returns error
54+
/// Test that -m option is accepted (stub implementation)
5555
#[test]
56-
fn lp_m_option_error() {
57-
run_test_with_env(
56+
fn lp_m_option_accepted() {
57+
run_test_with_checker_and_env(
5858
TestPlan {
5959
cmd: String::from("lp"),
6060
args: vec![
@@ -64,17 +64,31 @@ fn lp_m_option_error() {
6464
],
6565
stdin_data: String::from("test data"),
6666
expected_out: String::from(""),
67-
expected_err: String::from("lp: -m option not supported\n"),
67+
expected_err: String::from(""),
6868
expected_exit_code: 1,
6969
},
7070
&[("LPDEST", ""), ("PRINTER", "")],
71+
|_, output| {
72+
let stderr = String::from_utf8_lossy(&output.stderr);
73+
// -m should be accepted; any failure should be printer error, not option error
74+
assert!(
75+
!stderr.contains("-m option not supported"),
76+
"Expected -m to be accepted, but got: {}",
77+
stderr
78+
);
79+
assert!(
80+
stderr.contains("printer error"),
81+
"Expected printer error (no printer available), got: {}",
82+
stderr
83+
);
84+
},
7185
);
7286
}
7387

74-
/// Test that -w option returns error
88+
/// Test that -w option is accepted (stub implementation)
7589
#[test]
76-
fn lp_w_option_error() {
77-
run_test_with_env(
90+
fn lp_w_option_accepted() {
91+
run_test_with_checker_and_env(
7892
TestPlan {
7993
cmd: String::from("lp"),
8094
args: vec![
@@ -84,10 +98,24 @@ fn lp_w_option_error() {
8498
],
8599
stdin_data: String::from("test data"),
86100
expected_out: String::from(""),
87-
expected_err: String::from("lp: -w option not supported\n"),
101+
expected_err: String::from(""),
88102
expected_exit_code: 1,
89103
},
90104
&[("LPDEST", ""), ("PRINTER", "")],
105+
|_, output| {
106+
let stderr = String::from_utf8_lossy(&output.stderr);
107+
// -w should be accepted; any failure should be printer error, not option error
108+
assert!(
109+
!stderr.contains("-w option not supported"),
110+
"Expected -w to be accepted, but got: {}",
111+
stderr
112+
);
113+
assert!(
114+
stderr.contains("printer error"),
115+
"Expected printer error (no printer available), got: {}",
116+
stderr
117+
);
118+
},
91119
);
92120
}
93121

0 commit comments

Comments
 (0)