Skip to content

Commit d3c3dbc

Browse files
committed
Ensure scrape output ends with newline
Normalize non-empty scrape output before it is written to stdout or an --output file, so generated text content behaves like a regular POSIX text stream. Constraint: Preserve empty output as empty and avoid duplicating an existing trailing newline. Rejected: Per-path stdout/file special cases because a single normalization point covers both sinks. Confidence: High. Scope-risk: Low; affects only scrape command output formatting plus test imports. Directive: PlatformNetwork/bounty-challenge#53315. Tested: cargo test -p cortex-cli. Tested: Local smoke verified stdout and --output bytes end in 0a. Not-tested: Full workspace test suite.
1 parent 7954d02 commit d3c3dbc

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/cortex-cli/src/agent_cmd/tests.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
#[cfg(test)]
44
mod tests {
55
use crate::agent_cmd::cli::{CopyArgs, ExportArgs};
6-
use crate::agent_cmd::loader::{
7-
load_builtin_agents, parse_frontmatter, read_file_with_encoding,
8-
};
6+
use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter};
97
use crate::agent_cmd::types::AgentMode;
8+
use crate::utils::file::read_file_with_encoding;
109

1110
#[test]
1211
fn test_read_file_with_utf8() {

src/cortex-cli/src/scrape_cmd/command.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ impl ScrapeCommand {
306306
// Parse and convert HTML
307307
self.process_html(&body, format)?
308308
};
309+
let output = ensure_trailing_newline(output);
309310

310311
// Write output
311312
match &self.output {
@@ -373,3 +374,10 @@ impl ScrapeCommand {
373374
Ok(output)
374375
}
375376
}
377+
378+
pub(super) fn ensure_trailing_newline(mut output: String) -> String {
379+
if !output.is_empty() && !output.ends_with('\n') {
380+
output.push('\n');
381+
}
382+
output
383+
}

src/cortex-cli/src/scrape_cmd/tests.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod tests {
66

77
use cortex_engine::create_client_builder;
88

9-
use crate::scrape_cmd::command::ScrapeCommand;
9+
use crate::scrape_cmd::command::{ScrapeCommand, ensure_trailing_newline};
1010
use crate::scrape_cmd::html::{html_to_markdown, html_to_text, normalize_whitespace};
1111
use crate::scrape_cmd::http::parse_headers;
1212
use crate::scrape_cmd::types::OutputFormat;
@@ -145,6 +145,19 @@ mod tests {
145145
assert!("invalid".parse::<OutputFormat>().is_err());
146146
}
147147

148+
#[test]
149+
fn test_ensure_trailing_newline() {
150+
assert_eq!(
151+
ensure_trailing_newline("scraped content".to_string()),
152+
"scraped content\n"
153+
);
154+
assert_eq!(
155+
ensure_trailing_newline("already terminated\n".to_string()),
156+
"already terminated\n"
157+
);
158+
assert_eq!(ensure_trailing_newline(String::new()), "");
159+
}
160+
148161
#[test]
149162
fn test_normalize_whitespace() {
150163
// normalize_whitespace collapses multiple whitespace into single space

0 commit comments

Comments
 (0)