Skip to content

Commit 07315fe

Browse files
authored
Rollup merge of #100206 - RalfJung:miri-terminfo, r=thomcc
test: skip terminfo parsing in Miri Terminfo parsing takes a significant amount of time in Miri, making libtest startup very slow. To work around that Miri in fact unsets the `TERM` variable. However, this means we don't get colors in `cargo miri test`. So I propose we add some logic in libtest that skips parsing terminfo files under Miri, and just uses the regular basic coloring commands (taken from the `colored` crate). As far as I can see, these two commands are all that libtest ever needs from terminfo, so Miri doesn't even lose any functionality through this. If you want I can entirely remove the terminfo parsing code and just use these commands instead. Cc rust-lang/miri#2292 `@saethlin`
2 parents 790377a + 5076c90 commit 07315fe

File tree

1 file changed

+20
-0
lines changed
  • library/test/src/term/terminfo

1 file changed

+20
-0
lines changed

library/test/src/term/terminfo/mod.rs

+20
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ impl TermInfo {
8080

8181
/// Creates a TermInfo for the named terminal.
8282
pub(crate) fn from_name(name: &str) -> Result<TermInfo, Error> {
83+
if cfg!(miri) {
84+
// Avoid all the work of parsing the terminfo (it's pretty slow under Miri), and just
85+
// assume that the standard color codes work (like e.g. the 'colored' crate).
86+
return Ok(TermInfo {
87+
names: Default::default(),
88+
bools: Default::default(),
89+
numbers: Default::default(),
90+
strings: Default::default(),
91+
});
92+
}
93+
8394
get_dbpath_for_term(name)
8495
.ok_or_else(|| {
8596
Error::IoError(io::Error::new(io::ErrorKind::NotFound, "terminfo file not found"))
@@ -119,13 +130,22 @@ pub(crate) struct TerminfoTerminal<T> {
119130
impl<T: Write + Send> Terminal for TerminfoTerminal<T> {
120131
fn fg(&mut self, color: color::Color) -> io::Result<bool> {
121132
let color = self.dim_if_necessary(color);
133+
if cfg!(miri) && color < 8 {
134+
// The Miri logic for this only works for the most basic 8 colors, which we just assume
135+
// the terminal will support. (`num_colors` is always 0 in Miri, so higher colors will
136+
// just fail. But libtest doesn't use any higher colors anyway.)
137+
return write!(self.out, "\x1B[3{color}m").and(Ok(true));
138+
}
122139
if self.num_colors > color {
123140
return self.apply_cap("setaf", &[Param::Number(color as i32)]);
124141
}
125142
Ok(false)
126143
}
127144

128145
fn reset(&mut self) -> io::Result<bool> {
146+
if cfg!(miri) {
147+
return write!(self.out, "\x1B[0m").and(Ok(true));
148+
}
129149
// are there any terminals that have color/attrs and not sgr0?
130150
// Try falling back to sgr, then op
131151
let cmd = match ["sgr0", "sgr", "op"].iter().find_map(|cap| self.ti.strings.get(*cap)) {

0 commit comments

Comments
 (0)