Skip to content

Commit

Permalink
rust-lang#107307 Adding additional information to "cargo test --list"…
Browse files Browse the repository at this point in the history
… output

earlier:
  p2dchecks::fibonacci_test::case_1: test

now:
  // test test_type | ignored_or_not | location_info
  p2dchecks::fibonacci_test::case_1: test | false | src\lib.rs:57:8: 57:19
  • Loading branch information
parthopdas committed Feb 8, 2023
1 parent 3f059f6 commit dda61d8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Session.vim
.project
.favorites.json
.settings/
.vs/

## Tool
.valgrindrc
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ pub fn expand_test_or_bench(
cx.expr_none(sp)
},
),
// location_info: <relative_path_of_source>:<start_line>:<start_col>: <end_line>:<end_col>
field(
"location_info",
cx.expr_str(sp, item_location_info(cx, &item)),
),
// compile_fail: true | false
field("compile_fail", cx.expr_bool(sp, false)),
// no_run: true | false
Expand Down Expand Up @@ -396,6 +401,12 @@ fn should_ignore_message(cx: &ExtCtxt<'_>, i: &ast::Item) -> Option<Symbol> {
}
}

fn item_location_info(cx: &ExtCtxt<'_>, item: &ast::Item) -> Symbol {
let ident_sp = cx.with_def_site_ctxt(item.ident.span);
let li = cx.sess.source_map().span_to_location_info_string(ident_sp);
Symbol::intern(&li)
}

fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
match cx.sess.find_by_name(&i.attrs, sym::should_panic) {
Some(attr) => {
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ impl SourceMap {
self.span_to_string(sp, FileNameDisplayPreference::Remapped)
}

/// Format the span's location information into string
pub fn span_to_location_info_string(&self, sp: Span) -> String {
self.span_to_string(sp, FileNameDisplayPreference::Local)
}

/// Format the span location suitable for pretty printing anotations with relative line numbers
pub fn span_to_relative_line_string(&self, sp: Span, relative_to: Span) -> String {
if self.files.borrow().source_files.is_empty() || sp.is_dummy() || relative_to.is_dummy() {
Expand Down
6 changes: 3 additions & 3 deletions library/test/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
for test in filter_tests(opts, tests).into_iter() {
use crate::TestFn::*;

let TestDescAndFn { desc: TestDesc { name, .. }, testfn } = test;
let TestDescAndFn { desc: TestDesc { name, ignore, location_info, .. }, testfn } = test;

let fntype = match testfn {
StaticTestFn(..) | DynTestFn(..) => {
Expand All @@ -165,8 +165,8 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
}
};

writeln!(output, "{name}: {fntype}")?;
st.write_log(|| format!("{fntype} {name}\n"))?;
writeln!(output, "{name}: {fntype} | {ignore} | {location_info}")?;
st.write_log(|| format!("{fntype} {name} {ignore} {location_info}\n"))?;
}

fn plural(count: u32, s: &str) -> String {
Expand Down
1 change: 1 addition & 0 deletions library/test/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub struct TestDesc {
pub name: TestName,
pub ignore: bool,
pub ignore_message: Option<&'static str>,
pub location_info: &'static str,
pub should_panic: options::ShouldPanic,
pub compile_fail: bool,
pub no_run: bool,
Expand Down

0 comments on commit dda61d8

Please sign in to comment.