Fix coloring in the error string

This commit is contained in:
Fabrice Reix 2024-04-09 12:45:44 +02:00 committed by hurl-bot
parent a870767e99
commit a28fe82622
No known key found for this signature in database
GPG Key ID: 1283A2B4A0DCAF8D
6 changed files with 35 additions and 17 deletions

View File

@ -0,0 +1,8 @@
error: Assert status code
--> tests_failed/color.hurl:2:6
 |
 | GET http://localhost:8000/undefined
 2 | HTTP 200
 | ^^^ actual value is <404>
 |

View File

@ -0,0 +1 @@
4

View File

@ -0,0 +1,4 @@
GET http://localhost:8000/undefined
HTTP 200

View File

@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_failed/color.hurl --color

View File

@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_failed/color.hurl --color

View File

@ -448,13 +448,21 @@ pub(crate) fn error_string<E: Error>(
// The number of digits of the lines count.
let loc_max_width = max(lines.len().to_string().len(), 2);
let separator = "|";
let separator = if colored {
separator.blue().bold().to_string()
} else {
separator.to_string()
};
let spaces = " ".repeat(loc_max_width);
let prefix = format!("{spaces} {separator}");
let prefix = if colored {
prefix.blue().bold().to_string()
} else {
prefix.to_string()
};
let prefix_with_number = format!("{error_line:>loc_max_width$} {separator}");
let prefix_with_number = if colored {
prefix_with_number.blue().bold().to_string()
} else {
prefix_with_number.to_string()
};
// 1. First line is the description, ex. `Assert status code`.
let description = if colored {
@ -510,18 +518,9 @@ pub(crate) fn error_string<E: Error>(
// with the line number '|' prefix
let message = get_message(error, &lines, colored);
for (i, line) in split_lines(&message).iter().enumerate() {
if i == 0 {
let loc_max_width = max(lines.len().to_string().len(), 2);
let mut s = format!("{error_line:>loc_max_width$}");
if colored {
s = s.blue().bold().to_string();
}
text.push_str(format!("\n{s} |{line}").as_str());
} else {
text.push_str(format!("\n{prefix}{line}").as_str());
}
text.push('\n');
text.push_str(if i == 0 { &prefix_with_number } else { &prefix });
text.push_str(line);
}
// 6. Appends additional empty line