Skip to content

Commit

Permalink
Fix warn later extra whitespace
Browse files Browse the repository at this point in the history
fmt::warning is whitespace agnostic. It's up to the BuildLog to enforce vertical whitespace. This matches warn_later output with existing warning output:

```
    fn warning(mut self: Box<Self>, s: &str) -> Box<dyn StartedAnnounceLogger> {
        writeln_now(&mut self.io, fmt::warning(s.trim()));
        writeln_now(&mut self.io, "");

        self
    }
```
  • Loading branch information
schneems committed Sep 5, 2023
1 parent 98c2964 commit a00a534
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
10 changes: 8 additions & 2 deletions commons/src/output/build_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ where
}

fn warn_later(self: Box<Self>, s: &str) -> Box<dyn SectionAnnounceLogger> {
match crate::output::warn_later::try_push(fmt::warning(s.trim())) {
let mut formatted = fmt::warning(s.trim());
formatted.push('\n');

match crate::output::warn_later::try_push(formatted) {
Ok(_) => self,
Err(error) => {
eprintln!("[Buildpack Warning]: Cannot use the delayed warning feature due to error: {error}");
Expand Down Expand Up @@ -273,7 +276,10 @@ where
}

fn warn_later(self: Box<Self>, s: &str) -> Box<dyn StartedAnnounceLogger> {
match crate::output::warn_later::try_push(fmt::warning(s.trim())) {
let mut formatted = fmt::warning(s.trim());
formatted.push('\n');

match crate::output::warn_later::try_push(formatted) {
Ok(_) => self,
Err(error) => {
eprintln!("[Buildpack Warning]: Cannot use the delayed warning feature due to error: {error}");
Expand Down
17 changes: 15 additions & 2 deletions commons/src/output/warn_later.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,9 @@ where
fn drop(&mut self) {
if let Some(warnings) = take() {
if !warnings.is_empty() {
writeln!(&mut self.io).expect("warn guard IO is writeable");
for warning in &warnings {
writeln!(&mut self.io, "{warning}").expect("warn guard IO is writeable");
writeln!(&mut self.io).expect("warn guard IO is writeable");
write!(&mut self.io, "{warning}").expect("warn guard IO is writeable");
}
}
}
Expand Down Expand Up @@ -336,4 +335,18 @@ mod test {

assert_contains!(reader.read_lossy().unwrap(), message);
}

#[test]
fn does_not_double_whitespace() {
let writer = ReadYourWrite::writer(Vec::new());
let reader = writer.reader();
let guard = WarnGuard::new(writer);

let message = "Caution: This test is hot\n";
try_push(message).unwrap();
drop(guard);

let expected = "\nCaution: This test is hot\n".to_string();
assert_eq!(expected, reader.read_lossy().unwrap());
}
}

0 comments on commit a00a534

Please sign in to comment.