Skip to content

Commit bc6e592

Browse files
authored
Merge pull request #20639 from ChayimFriedman2/update-test-abs
fix: Resolve paths to snapshot test libraries absolutely
2 parents 412932e + f91b99b commit bc6e592

File tree

2 files changed

+85
-23
lines changed

2 files changed

+85
-23
lines changed

crates/ide/src/hover/tests.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10569,6 +10569,77 @@ macro_rules! str {
1056910569
);
1057010570
}
1057110571

10572+
#[test]
10573+
fn test_runnables_with_snapshot_tests_indirect_dep() {
10574+
check_actions(
10575+
r#"
10576+
//- /lib.rs crate:foo deps:utils
10577+
use utils::expect_test::expect;
10578+
10579+
#[test]
10580+
fn test$0() {
10581+
let actual = "new25";
10582+
expect!["new25"].assert_eq(&actual);
10583+
}
10584+
10585+
//- /expect-test/lib.rs crate:expect_test
10586+
struct Expect;
10587+
10588+
impl Expect {
10589+
fn assert_eq(&self, actual: &str) {}
10590+
}
10591+
10592+
#[macro_export]
10593+
macro_rules! expect {
10594+
($e:expr) => Expect; // dummy
10595+
}
10596+
10597+
//- /utils/lib.rs crate:utils deps:expect_test
10598+
pub use expect_test;
10599+
"#,
10600+
expect![[r#"
10601+
[
10602+
Reference(
10603+
FilePositionWrapper {
10604+
file_id: FileId(
10605+
0,
10606+
),
10607+
offset: 44,
10608+
},
10609+
),
10610+
Runnable(
10611+
Runnable {
10612+
use_name_in_title: false,
10613+
nav: NavigationTarget {
10614+
file_id: FileId(
10615+
0,
10616+
),
10617+
full_range: 33..121,
10618+
focus_range: 44..48,
10619+
name: "test",
10620+
kind: Function,
10621+
},
10622+
kind: Test {
10623+
test_id: Path(
10624+
"test",
10625+
),
10626+
attr: TestAttr {
10627+
ignore: false,
10628+
},
10629+
},
10630+
cfg: None,
10631+
update_test: UpdateTest {
10632+
expect_test: true,
10633+
insta: false,
10634+
snapbox: false,
10635+
},
10636+
},
10637+
),
10638+
]
10639+
"#]],
10640+
);
10641+
}
10642+
1057210643
#[test]
1057310644
fn drop_glue() {
1057410645
check(

crates/ide/src/runnables.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use arrayvec::ArrayVec;
44
use ast::HasName;
55
use cfg::{CfgAtom, CfgExpr};
66
use hir::{
7-
AsAssocItem, AttrsWithOwner, HasAttrs, HasCrate, HasSource, ModPath, Name, PathKind, Semantics,
8-
Symbol, db::HirDatabase, sym,
7+
AsAssocItem, AttrsWithOwner, HasAttrs, HasCrate, HasSource, Semantics, Symbol, db::HirDatabase,
8+
sym,
99
};
1010
use ide_assists::utils::{has_test_related_attribute, test_related_attribute_syn};
1111
use ide_db::{
@@ -352,8 +352,7 @@ pub(crate) fn runnable_fn(
352352
.call_site();
353353

354354
let file_range = fn_source.syntax().original_file_range_with_macro_call_input(sema.db);
355-
let update_test =
356-
UpdateTest::find_snapshot_macro(sema, &fn_source.file_syntax(sema.db), file_range);
355+
let update_test = UpdateTest::find_snapshot_macro(sema, file_range);
357356

358357
let cfg = def.attrs(sema.db).cfg();
359358
Some(Runnable { use_name_in_title: false, nav, kind, cfg, update_test })
@@ -388,7 +387,7 @@ pub(crate) fn runnable_mod(
388387
file_id: module_source.file_id.original_file(sema.db),
389388
range: module_syntax.text_range(),
390389
};
391-
let update_test = UpdateTest::find_snapshot_macro(sema, &module_syntax, file_range);
390+
let update_test = UpdateTest::find_snapshot_macro(sema, file_range);
392391

393392
Some(Runnable {
394393
use_name_in_title: false,
@@ -428,8 +427,7 @@ pub(crate) fn runnable_impl(
428427
let impl_source = sema.source(*def)?;
429428
let impl_syntax = impl_source.syntax();
430429
let file_range = impl_syntax.original_file_range_with_macro_call_input(sema.db);
431-
let update_test =
432-
UpdateTest::find_snapshot_macro(sema, &impl_syntax.file_syntax(sema.db), file_range);
430+
let update_test = UpdateTest::find_snapshot_macro(sema, file_range);
433431

434432
Some(Runnable {
435433
use_name_in_title: false,
@@ -475,7 +473,7 @@ fn runnable_mod_outline_definition(
475473
file_id: mod_source.file_id.original_file(sema.db),
476474
range: mod_syntax.text_range(),
477475
};
478-
let update_test = UpdateTest::find_snapshot_macro(sema, &mod_syntax, file_range);
476+
let update_test = UpdateTest::find_snapshot_macro(sema, file_range);
479477

480478
Some(Runnable {
481479
use_name_in_title: false,
@@ -641,7 +639,7 @@ pub struct UpdateTest {
641639
pub snapbox: bool,
642640
}
643641

644-
static SNAPSHOT_TEST_MACROS: OnceLock<FxHashMap<&str, Vec<ModPath>>> = OnceLock::new();
642+
static SNAPSHOT_TEST_MACROS: OnceLock<FxHashMap<&str, Vec<[Symbol; 2]>>> = OnceLock::new();
645643

646644
impl UpdateTest {
647645
const EXPECT_CRATE: &str = "expect_test";
@@ -665,22 +663,17 @@ impl UpdateTest {
665663
const SNAPBOX_CRATE: &str = "snapbox";
666664
const SNAPBOX_MACROS: &[&str] = &["assert_data_eq", "file", "str"];
667665

668-
fn find_snapshot_macro(
669-
sema: &Semantics<'_, RootDatabase>,
670-
scope: &SyntaxNode,
671-
file_range: hir::FileRange,
672-
) -> Self {
666+
fn find_snapshot_macro(sema: &Semantics<'_, RootDatabase>, file_range: hir::FileRange) -> Self {
673667
fn init<'a>(
674668
krate_name: &'a str,
675669
paths: &[&str],
676-
map: &mut FxHashMap<&'a str, Vec<ModPath>>,
670+
map: &mut FxHashMap<&'a str, Vec<[Symbol; 2]>>,
677671
) {
678672
let mut res = Vec::with_capacity(paths.len());
679-
let krate = Name::new_symbol_root(Symbol::intern(krate_name));
673+
let krate = Symbol::intern(krate_name);
680674
for path in paths {
681-
let segments = [krate.clone(), Name::new_symbol_root(Symbol::intern(path))];
682-
let mod_path = ModPath::from_segments(PathKind::Abs, segments);
683-
res.push(mod_path);
675+
let segments = [krate.clone(), Symbol::intern(path)];
676+
res.push(segments);
684677
}
685678
map.insert(krate_name, res);
686679
}
@@ -694,11 +687,9 @@ impl UpdateTest {
694687
});
695688

696689
let search_scope = SearchScope::file_range(file_range);
697-
let find_macro = |paths: &[ModPath]| {
690+
let find_macro = |paths: &[[Symbol; 2]]| {
698691
for path in paths {
699-
let Some(items) = sema.resolve_mod_path(scope, path) else {
700-
continue;
701-
};
692+
let items = hir::resolve_absolute_path(sema.db, path.iter().cloned());
702693
for item in items {
703694
if let hir::ItemInNs::Macros(makro) = item
704695
&& Definition::Macro(makro)

0 commit comments

Comments
 (0)