compiletest/runtest/
debugger.rs1use std::fmt::Write;
2use std::fs::File;
3use std::io::{BufRead, BufReader};
4
5use camino::{Utf8Path, Utf8PathBuf};
6
7use crate::directives::{LineNumber, line_directive};
8use crate::runtest::ProcRes;
9
10pub(crate) struct DebuggerCommands {
12 pub(crate) commands: Vec<String>,
14 pub(crate) breakpoint_lines: Vec<LineNumber>,
16 check_lines: Vec<(LineNumber, String)>,
18 file: Utf8PathBuf,
20 revision: Option<String>,
22}
23
24impl DebuggerCommands {
25 pub(crate) fn parse_from(
26 file: &Utf8Path,
27 debugger_prefix: &str,
28 test_revision: Option<&str>,
29 ) -> Result<Self, String> {
30 let mut breakpoint_lines = vec![];
31 let mut commands = vec![];
32 let mut check_lines = vec![];
33 let reader = BufReader::new(File::open(file.as_std_path()).unwrap());
34 for (line_number, line) in LineNumber::enumerate().zip(reader.lines()) {
35 let line = line.map_err(|e| format!("Error while parsing debugger commands: {}", e))?;
36
37 if line.contains("#break") {
39 breakpoint_lines.push(line_number);
40 continue;
41 }
42
43 let Some(directive) = line_directive(file, line_number, &line) else { continue };
44 let Ok(directive) = directive.map_err(|e| e.raise_fatal());
45
46 if !directive.applies_to_test_revision(test_revision) {
47 continue;
48 }
49
50 let Some(directive_kind) = directive.name.strip_prefix(debugger_prefix) else {
51 continue;
52 };
53
54 match (directive_kind, directive.value_after_colon()) {
55 ("-command", Some(command)) => commands.push(command.to_string()),
56 ("-check", Some(pattern)) => check_lines.push((line_number, pattern.to_string())),
57 ("-repr", Some(var_name)) => {
58 commands.push(format!("repr {}", var_name.trim()));
61 check_lines.push((line_number, format!("{var_name}: Ok")));
64 }
65 _ => continue,
66 }
67 }
68
69 Ok(Self {
70 commands,
71 breakpoint_lines,
72 check_lines,
73 file: file.to_path_buf(),
74 revision: test_revision.map(str::to_owned),
75 })
76 }
77
78 pub(crate) fn check_output(&self, debugger_run_result: &ProcRes) -> Result<(), String> {
82 let mut found = vec![];
84 let mut missing = vec![];
86 let mut last_idx = 0;
88 let dbg_lines: Vec<&str> = debugger_run_result.stdout.lines().collect();
89
90 for (src_lineno, ck_line) in &self.check_lines {
91 if let Some(offset) = dbg_lines
92 .iter()
93 .skip(last_idx)
94 .position(|out_line| check_single_line(out_line, &ck_line))
95 {
96 last_idx += offset;
97 found.push((src_lineno, dbg_lines[last_idx]));
98 } else {
99 missing.push((src_lineno, ck_line));
100 }
101 }
102
103 if missing.is_empty() {
104 Ok(())
105 } else {
106 let fname = self.file.file_name().unwrap();
107 let revision_suffix =
108 self.revision.as_ref().map_or(String::new(), |r| format!("#{}", r));
109 let mut msg = format!(
110 "check directive(s) from `{}{}` not found in debugger output. errors:",
111 self.file, revision_suffix
112 );
113
114 for (src_lineno, err_line) in missing {
115 write!(msg, "\n ({fname}:{src_lineno}) `{err_line}`").unwrap();
116 }
117
118 if !found.is_empty() {
119 let init = "\nthe following subset of check directive(s) was found successfully:";
120 msg.push_str(init);
121 for (src_lineno, found_line) in found {
122 write!(msg, "\n ({fname}:{src_lineno}) `{found_line}`").unwrap();
123 }
124 }
125
126 Err(msg)
127 }
128 }
129}
130
131fn check_single_line(line: &str, check_line: &str) -> bool {
133 let line = line.trim();
136 let check_line = check_line.trim();
137 let can_start_anywhere = check_line.starts_with("[...]");
138 let can_end_anywhere = check_line.ends_with("[...]");
139
140 let check_fragments: Vec<&str> =
141 check_line.split("[...]").filter(|frag| !frag.is_empty()).collect();
142 if check_fragments.is_empty() {
143 return true;
144 }
145
146 let (mut rest, first_fragment) = if can_start_anywhere {
147 let Some(pos) = line.find(check_fragments[0]) else {
148 return false;
149 };
150 (&line[pos + check_fragments[0].len()..], 1)
151 } else {
152 (line, 0)
153 };
154
155 for current_fragment in &check_fragments[first_fragment..] {
156 let Some(pos) = rest.find(current_fragment) else {
157 return false;
158 };
159 rest = &rest[pos + current_fragment.len()..];
160 }
161
162 can_end_anywhere || rest.is_empty()
163}