Skip to content

Commit 4de72bb

Browse files
refactor: add redact for line numbers
1 parent 214d9e8 commit 4de72bb

File tree

7 files changed

+147
-110
lines changed

7 files changed

+147
-110
lines changed

Cargo.lock

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ ariadne = { version = "0.5.1", features = ["auto-color"] }
105105
clap_complete = { version = "4.5.4" }
106106
schemars = "0.8"
107107
nucleo-matcher = "0.3.1"
108-
regex = { version = "1.10.3", default-features = false, features = ["std"] }
108+
regex = { version = "1.11.0", default-features = false, features = ["std"] }
109109
rand = "0.8.5"
110110
base64 = "0.22.1"
111111
derive_builder = "0.20.2"

crates/moon-test-util/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ rust-version.workspace = true
2626
anyhow.workspace = true
2727
dunce.workspace = true
2828
ignore.workspace = true
29-
snapbox.workspace = true
29+
snapbox = { workspace = true, features = ["regex"] }
3030
tempfile.workspace = true
3131
which.workspace = true
3232
home.workspace = true
33+
regex.workspace = true

crates/moon-test-util/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
// For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn.
1818

1919
pub mod cmdtest;
20+
pub mod stack_trace;
2021
pub mod test_dir;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// moon: The build system and package manager for MoonBit.
2+
// Copyright (C) 2024 International Digital Economy Academy
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
//
17+
// For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn.
18+
19+
use std::path::Path;
20+
21+
fn stack_trace_line_number_regex() -> regex::Regex {
22+
regex::Regex::new(r"(?<redacted>:[0-9]+)(?:[ \t]+(?:at|by)|\n|$)")
23+
.expect("valid stack trace line number regex")
24+
}
25+
26+
pub fn redaction_regex(pattern: &str) -> regex::Regex {
27+
regex::Regex::new(pattern).expect("valid redaction regex")
28+
}
29+
30+
pub fn stack_trace_redactions(_src_dir: &Path) -> snapbox::Redactions {
31+
let mut redactions = snapbox::Redactions::new();
32+
redactions
33+
.insert("[LINE_NUMBER]", stack_trace_line_number_regex())
34+
.expect("valid stack trace line number redaction");
35+
redactions
36+
.insert(
37+
"[CORE_PATH]",
38+
regex::Regex::new(
39+
r"(?<redacted>(?:\$MOON_HOME|(?:[A-Za-z]:)?/[^ \t\r\n]*\.moon)/lib/core)",
40+
)
41+
.expect("valid moon core path regex"),
42+
)
43+
.expect("valid moon core path redaction");
44+
redactions
45+
}

crates/moon/tests/test_cases/native_abort_trace/mod.rs

Lines changed: 35 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,45 @@ use super::*;
33
#[cfg(unix)]
44
#[test]
55
fn test_native_abort_trace() {
6+
const ANSI_LINE_NUMBER_REGEX: &str = r"(?<redacted>:[0-9]+)(?:(?:\x1b\[[0-9;]*m)|(?:\[ANSI_[A-Z]+\]))*(?:[ \t]+(?:(?:\x1b\[[0-9;]*m)|(?:\[ANSI_[A-Z]+\]))*(?:at|by)|\n|$)";
67
let dir = TestDir::new("native_abort_trace/native_abort_trace.in");
7-
8-
fn strip_ansi(input: &str) -> String {
9-
let mut out = String::with_capacity(input.len());
10-
let mut chars = input.chars().peekable();
11-
while let Some(c) = chars.next() {
12-
if c == '\x1b' {
13-
if chars.peek() == Some(&'[') {
14-
chars.next();
15-
for c2 in chars.by_ref() {
16-
if c2 == 'm' {
17-
break;
18-
}
19-
}
20-
}
21-
continue;
22-
}
23-
out.push(c);
24-
}
25-
out
26-
}
27-
28-
fn normalize_line_numbers(input: &str) -> String {
29-
let mut out = String::with_capacity(input.len());
30-
for line in input.lines() {
31-
let Some(pos) = line.find("main.c:") else {
32-
out.push_str(line);
33-
out.push('\n');
34-
continue;
35-
};
36-
let prefix = &line[..pos];
37-
let rest = &line[pos + "main.c:".len()..];
38-
let mut digits_len = 0;
39-
for ch in rest.chars() {
40-
if ch.is_ascii_digit() {
41-
digits_len += ch.len_utf8();
42-
} else {
43-
break;
44-
}
45-
}
46-
if digits_len == 0 {
47-
out.push_str(line);
48-
out.push('\n');
49-
continue;
50-
}
51-
let suffix = &rest[digits_len..];
52-
out.push_str(prefix);
53-
out.push_str("main.c:<line>");
54-
out.push_str(suffix);
55-
out.push('\n');
56-
}
57-
out
58-
}
59-
60-
let output = snapbox::cmd::Command::new(moon_bin())
8+
let mut redactions = moon_test_util::stack_trace::stack_trace_redactions(dir.as_ref());
9+
redactions
10+
.insert("[ANSI_RED]", "\u{1b}[31m")
11+
.expect("valid ANSI red redaction");
12+
redactions
13+
.insert("[ANSI_GRAY]", "\u{1b}[90m")
14+
.expect("valid ANSI gray redaction");
15+
redactions
16+
.insert("[ANSI_CYAN]", "\u{1b}[36m")
17+
.expect("valid ANSI cyan redaction");
18+
redactions
19+
.insert("[ANSI_BOLD]", "\u{1b}[1m")
20+
.expect("valid ANSI bold redaction");
21+
redactions
22+
.insert("[ANSI_RESET]", "\u{1b}[0m")
23+
.expect("valid ANSI reset redaction");
24+
redactions
25+
.insert(
26+
"[LINE_NUMBER]",
27+
moon_test_util::stack_trace::redaction_regex(ANSI_LINE_NUMBER_REGEX),
28+
)
29+
.expect("valid ANSI stack trace line number redaction");
30+
snapbox::cmd::Command::new(moon_bin())
31+
.with_assert(snapbox::Assert::new().redact_with(redactions))
6132
.current_dir(&dir)
33+
.env("NO_COLOR", "1")
34+
.env("CLICOLOR", "0")
6235
.args(["run", "--target", "native", "cmd/main"])
6336
.assert()
6437
.success()
65-
.get_output()
66-
.to_owned();
67-
68-
let mut out = String::from_utf8_lossy(&output.stdout).to_string();
69-
out.push_str(&String::from_utf8_lossy(&output.stderr));
70-
let out = replace_dir(&normalize_line_numbers(&strip_ansi(&out)), &dir);
38+
.stdout_eq("Hello\n")
39+
.stderr_eq(snapbox::str![[r#"
40+
[ANSI_RED]RUNTIME ERROR: abort() called[ANSI_RESET]
41+
[ANSI_GRAY][CORE_PATH]/builtin/option.mbt[LINE_NUMBER][ANSI_RESET] [ANSI_CYAN]at[ANSI_RESET] [ANSI_BOLD]@moonbitlang/core/option.Option::unwrap[Int][ANSI_RESET]
42+
[ANSI_GRAY][..]/cmd/main/main.mbt[LINE_NUMBER][ANSI_RESET] [ANSI_CYAN]by[ANSI_RESET] [ANSI_BOLD]@username/scratch/cmd/main.g[ANSI_RESET]
43+
[ANSI_GRAY][..]/cmd/main/main.mbt[LINE_NUMBER][ANSI_RESET] [ANSI_CYAN]by[ANSI_RESET] [ANSI_BOLD]@username/scratch/cmd/main.f[ANSI_RESET]
44+
[ANSI_GRAY][..]/cmd/main/main.mbt[LINE_NUMBER][ANSI_RESET] [ANSI_CYAN]by[ANSI_RESET] [ANSI_BOLD]main[ANSI_RESET]
7145
72-
check(
73-
&out,
74-
expect![[r#"
75-
Hello
76-
RUNTIME ERROR: abort() called
77-
$MOON_HOME/lib/core/builtin/option.mbt:39 at @moonbitlang/core/option.Option::unwrap[Int]
78-
$ROOT/cmd/main/main.mbt:14 by @username/scratch/cmd/main.g
79-
$ROOT/cmd/main/main.mbt:9 by @username/scratch/cmd/main.f
80-
$ROOT/cmd/main/main.mbt:4 by main
81-
"#]],
82-
);
46+
"#]]);
8347
}

0 commit comments

Comments
 (0)