Skip to content

Commit 4db5b3e

Browse files
committed
begin to implement detecting and migrating conditions to ui_test
1 parent fe1e433 commit 4db5b3e

File tree

4 files changed

+567
-94
lines changed

4 files changed

+567
-94
lines changed

src/tools/compiletest/src/header/cfg.rs

+30-52
Original file line numberDiff line numberDiff line change
@@ -42,56 +42,38 @@ pub(super) fn handle_only(config: &Config, comment: TestComment<'_>) -> IgnoreDe
4242

4343
/// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
4444
/// or `normalize-stderr-32bit`.
45-
pub(super) fn parse_cfg_name_directive<'line>(
45+
pub(super) fn parse_cfg_name_directive(
4646
config: &Config,
47-
comment: &'line TestComment<'line>,
47+
comment: &TestComment<'_>,
4848
prefix: &str,
49-
) -> MatchOutcome<'line> {
50-
match comment.comment() {
51-
CommentKind::Compiletest(line) => {
52-
parse_cfg_name_directive_compiletest(config, line, prefix)
53-
}
54-
CommentKind::UiTest(line) => parse_cfg_name_directive_ui_test(config, line, prefix),
55-
}
56-
}
57-
58-
fn directive_name_for_line<'line, 'p>(
59-
line: &'line str,
60-
prefix: &'p str,
61-
) -> Option<(&'line str, Option<&'line str>)> {
62-
// Directives start with a specified prefix, and are immediately followed by a '-'.
63-
let expected_start = format!("{}-", prefix);
64-
let after_prefix = if line.starts_with(expected_start.as_str()) {
65-
&line[expected_start.len()..]
66-
} else {
67-
return None;
49+
) -> MatchOutcome {
50+
let comment_kind = comment.comment();
51+
let Some((name, comment)) = comment_kind.parse_name_comment().and_then(|(name, comment)| {
52+
name.strip_prefix(format!("{}-", prefix).as_str()).map(|stripped| (stripped, comment))
53+
}) else {
54+
return MatchOutcome::NotADirective;
6855
};
56+
let comment = comment.map(|c| c.trim().trim_start_matches('-').trim().to_owned());
6957

70-
// If there is a ':' or a ' ' (space), split the name off, and consider the rest of the line to
71-
// be a "comment" that is ignored.
72-
let (name, comment) = after_prefix
73-
.split_once(&[':', ' '])
74-
.map(|(l, c)| (l.trim(), Some(c)))
75-
.unwrap_or((after_prefix, None));
76-
77-
// Some of the matchers might be "" depending on what the target information is. To avoid
78-
// problems we outright reject empty directives.
79-
if name == "" { None } else { Some((name, comment)) }
58+
match comment_kind {
59+
CommentKind::Compiletest(_) => {
60+
parse_cfg_name_directive_compiletest(config, name, comment, prefix)
61+
}
62+
CommentKind::UiTest(_) => parse_cfg_name_directive_ui_test(config, name, comment),
63+
}
8064
}
8165

82-
fn parse_cfg_name_directive_ui_test<'line>(
66+
fn parse_cfg_name_directive_ui_test(
8367
config: &Config,
84-
line: &'line str,
85-
prefix: &str,
86-
) -> MatchOutcome<'line> {
87-
let Some((name, comment)) = directive_name_for_line(line, prefix) else {
88-
return MatchOutcome::NotADirective;
89-
};
90-
let comment = comment.map(|c| c.trim().trim_start_matches('-').trim());
91-
68+
name: &str,
69+
comment: Option<String>,
70+
) -> MatchOutcome {
9271
let target_cfg = config.target_cfg();
9372

94-
if name == "on-host" {
73+
// Parsing copied from ui_test: https://github.com/oli-obk/ui_test/blob/a18ef37bf3dcccf5a1a631eddd55759fe0b89617/src/parser.rs#L187
74+
if name == "test" {
75+
MatchOutcome::Match { message: String::from("always"), comment }
76+
} else if name == "on-host" {
9577
unimplemented!("idk what to do about this yet")
9678
} else if let Some(bits) = name.strip_suffix("bit") {
9779
let Ok(bits) = bits.parse::<u32>() else {
@@ -126,16 +108,12 @@ fn parse_cfg_name_directive_ui_test<'line>(
126108
}
127109
}
128110

129-
fn parse_cfg_name_directive_compiletest<'a>(
111+
fn parse_cfg_name_directive_compiletest(
130112
config: &Config,
131-
line: &'a str,
113+
name: &str,
114+
comment: Option<String>,
132115
prefix: &str,
133-
) -> MatchOutcome<'a> {
134-
let Some((name, comment)) = directive_name_for_line(line, prefix) else {
135-
return MatchOutcome::NotADirective;
136-
};
137-
let comment = comment.map(|c| c.trim().trim_start_matches('-').trim());
138-
116+
) -> MatchOutcome {
139117
macro_rules! condition {
140118
(
141119
name: $name:expr,
@@ -315,11 +293,11 @@ fn parse_cfg_name_directive_compiletest<'a>(
315293
}
316294

317295
#[derive(Clone, PartialEq, Debug)]
318-
pub(super) enum MatchOutcome<'a> {
296+
pub(super) enum MatchOutcome {
319297
/// No match.
320-
NoMatch { message: String, comment: Option<&'a str> },
298+
NoMatch { message: String, comment: Option<String> },
321299
/// Match.
322-
Match { message: String, comment: Option<&'a str> },
300+
Match { message: String, comment: Option<String> },
323301
/// The directive was invalid.
324302
Invalid,
325303
/// The directive is handled by other parts of our tooling.

src/tools/test_common/src/lib.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct TestComment<'line> {
1313
}
1414

1515
impl<'line> TestComment<'line> {
16+
#[must_use]
1617
pub const fn new(
1718
line: &'line str,
1819
revision: Option<&'line str>,
@@ -22,23 +23,28 @@ impl<'line> TestComment<'line> {
2223
Self { revision, comment, line_num, full_line: line }
2324
}
2425

26+
#[must_use]
2527
pub const fn revision(&self) -> Option<&str> {
2628
self.revision
2729
}
2830

29-
pub const fn comment(&self) -> CommentKind<'_> {
31+
#[must_use]
32+
pub const fn comment(&'line self) -> CommentKind<'line> {
3033
self.comment
3134
}
3235

36+
#[must_use]
3337
pub const fn line_num(&self) -> usize {
3438
self.line_num
3539
}
3640

41+
#[must_use]
3742
pub const fn comment_str(&self) -> &str {
3843
self.comment.line()
3944
}
4045

4146
/// The full line that contains the comment. You almost never want this.
47+
#[must_use]
4248
pub const fn full_line(&self) -> &str {
4349
self.full_line
4450
}
@@ -54,12 +60,27 @@ pub enum CommentKind<'line> {
5460
UiTest(&'line str),
5561
}
5662

57-
impl CommentKind<'_> {
58-
pub const fn line(&self) -> &str {
59-
match self {
63+
impl<'line> CommentKind<'line> {
64+
#[must_use]
65+
pub const fn line(&'line self) -> &'line str {
66+
match *self {
6067
CommentKind::Compiletest(line) | CommentKind::UiTest(line) => line,
6168
}
6269
}
70+
71+
/// If the comment is of the form `name:extra` or `name extra`, returns the name and returns
72+
/// the rest of the line as a "comment", if it exists.
73+
#[must_use]
74+
pub fn parse_name_comment(&'line self) -> Option<(&'line str, Option<&'line str>)> {
75+
let line = self.line();
76+
// If there is a ':' or a ' ' (space), split the name off, and consider the rest of the
77+
// line to be a "comment" that is ignored.
78+
let (name, comment) =
79+
line.split_once(&[':', ' ']).map(|(l, c)| (l.trim(), Some(c))).unwrap_or((line, None));
80+
81+
// Reject empty names
82+
if name == "" { None } else { Some((name, comment)) }
83+
}
6384
}
6485

6586
/// Iterates over the test header for the given `testfile`, and calls a closure

src/tools/tidy/src/ui_tests.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ pub fn check(path: &Path, bad: &mut bool) {
137137
}
138138
"rs" => {
139139
// FIXME (ui_test): make this configurable somehow
140-
// let mode = HeaderCheckMode::Error;
141-
let mode = HeaderCheckMode::Fix;
140+
let mode = HeaderCheckMode::Error;
141+
// let mode = HeaderCheckMode::Fix;
142142
check_ui_test_headers(bad, file_path, mode);
143143
}
144144
_ => {}
@@ -224,23 +224,25 @@ fn fix_header_errors(file_path: &Path, bad_lines: Vec<HeaderAction>) -> io::Resu
224224
let mut new_line =
225225
replace_compiletest_comment(header_action.line()).unwrap();
226226
// This is always a directive that contains the compiletest name.
227-
let name_start = new_line.find(compiletest_name).unwrap();
227+
let name_start = new_line.find(compiletest_name.as_str()).unwrap();
228228
new_line.replace_range(
229229
name_start..(name_start + compiletest_name.len()),
230-
ui_test_name,
230+
ui_test_name.as_str(),
231231
);
232232
new_line
233233
}
234234
LineAction::UseUITestName { compiletest_name, ui_test_name } => {
235235
// This is always a directive that contains the compiletest name.
236-
let name_start = header_action.line().find(compiletest_name).unwrap();
236+
let name_start =
237+
header_action.line().find(compiletest_name.as_str()).unwrap();
237238
let mut new_line = header_action.line().to_string();
238239
new_line.replace_range(
239240
name_start..(name_start + compiletest_name.len()),
240-
ui_test_name,
241+
ui_test_name.as_str(),
241242
);
242243
new_line
243244
}
245+
LineAction::Error { message } => todo!(),
244246
},
245247
)
246248
})

0 commit comments

Comments
 (0)