|
| 1 | +//! Shared test helpers for the oak_ide integration suite. |
| 2 | +
|
| 3 | +use aether_path::FilePath; |
| 4 | +use biome_rowan::TextRange; |
| 5 | +use biome_rowan::TextSize; |
| 6 | +use oak_db::DbInputs; |
| 7 | +use oak_db::File; |
| 8 | +use oak_db::OakDatabase; |
| 9 | +use oak_db::Package; |
| 10 | +use oak_db::Root; |
| 11 | +use oak_db::RootKind; |
| 12 | +use oak_ide::FileRange; |
| 13 | +use oak_package_metadata::namespace::Namespace; |
| 14 | +use oak_scan::DbScan; |
| 15 | +use salsa::Setter; |
| 16 | +use stdext::SortedVec; |
| 17 | +use url::Url; |
| 18 | + |
| 19 | +pub fn file_url(name: &str) -> Url { |
| 20 | + // `Url::to_file_path` on Windows requires a drive-letter prefix, so |
| 21 | + // synthesize one for tests. Linux is happy with rootless paths. |
| 22 | + if cfg!(windows) { |
| 23 | + Url::parse(&format!("file:///C:/project/R/{name}")).unwrap() |
| 24 | + } else { |
| 25 | + Url::parse(&format!("file:///project/R/{name}")).unwrap() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +pub fn lib_url(name: &str) -> Url { |
| 30 | + Url::parse(&format!("file:///library/{name}")).unwrap() |
| 31 | +} |
| 32 | + |
| 33 | +pub fn workspace_url(name: &str) -> Url { |
| 34 | + Url::parse(&format!("file:///workspace/{name}")).unwrap() |
| 35 | +} |
| 36 | + |
| 37 | +pub fn upsert(db: &mut OakDatabase, name: &str, contents: &str) -> File { |
| 38 | + db.upsert_editor(FilePath::from_url(&file_url(name)), contents.to_string()) |
| 39 | +} |
| 40 | + |
| 41 | +pub fn offset(n: u32) -> TextSize { |
| 42 | + TextSize::from(n) |
| 43 | +} |
| 44 | + |
| 45 | +pub fn range(start: u32, end: u32) -> TextRange { |
| 46 | + TextRange::new(TextSize::from(start), TextSize::from(end)) |
| 47 | +} |
| 48 | + |
| 49 | +/// Project results to in-file ranges (single-file tests). |
| 50 | +pub fn ranges(refs: &[FileRange]) -> Vec<TextRange> { |
| 51 | + refs.iter().map(|r| r.range).collect() |
| 52 | +} |
| 53 | + |
| 54 | +/// Project results to `(file, range)` pairs (cross-file tests). |
| 55 | +pub fn pairs(refs: &[FileRange]) -> Vec<(File, TextRange)> { |
| 56 | + refs.iter().map(|r| (r.file, r.range)).collect() |
| 57 | +} |
| 58 | + |
| 59 | +/// Install `name` as a library package exporting `exports`, with one file at |
| 60 | +/// `R/{file_name}`. Returns the package file. |
| 61 | +pub fn install_library_package( |
| 62 | + db: &mut OakDatabase, |
| 63 | + name: &str, |
| 64 | + exports: &[&str], |
| 65 | + file_name: &str, |
| 66 | + contents: &str, |
| 67 | +) -> File { |
| 68 | + install_pkg(db, RootKind::Library, name, exports, file_name, contents) |
| 69 | +} |
| 70 | + |
| 71 | +/// Install `name` as a workspace package exporting `exports`, with one file at |
| 72 | +/// `R/{file_name}`. Returns the package file. |
| 73 | +pub fn install_workspace_package( |
| 74 | + db: &mut OakDatabase, |
| 75 | + name: &str, |
| 76 | + exports: &[&str], |
| 77 | + file_name: &str, |
| 78 | + contents: &str, |
| 79 | +) -> File { |
| 80 | + install_pkg(db, RootKind::Workspace, name, exports, file_name, contents) |
| 81 | +} |
| 82 | + |
| 83 | +fn install_pkg( |
| 84 | + db: &mut OakDatabase, |
| 85 | + kind: RootKind, |
| 86 | + name: &str, |
| 87 | + exports: &[&str], |
| 88 | + file_name: &str, |
| 89 | + contents: &str, |
| 90 | +) -> File { |
| 91 | + let (pkg_url, file_url, root_url, version) = match kind { |
| 92 | + RootKind::Library => ( |
| 93 | + lib_url(&format!("{name}/DESCRIPTION")), |
| 94 | + lib_url(&format!("{name}/R/{file_name}")), |
| 95 | + lib_url(name), |
| 96 | + Some("1.0.0".to_string()), |
| 97 | + ), |
| 98 | + RootKind::Workspace => ( |
| 99 | + workspace_url(&format!("{name}/DESCRIPTION")), |
| 100 | + workspace_url(&format!("{name}/R/{file_name}")), |
| 101 | + workspace_url(name), |
| 102 | + None, |
| 103 | + ), |
| 104 | + }; |
| 105 | + let namespace = Namespace { |
| 106 | + exports: SortedVec::from_vec(exports.iter().map(|s| s.to_string()).collect()), |
| 107 | + ..Default::default() |
| 108 | + }; |
| 109 | + let pkg = Package::new( |
| 110 | + db, |
| 111 | + FilePath::from_url(&pkg_url), |
| 112 | + name.to_string(), |
| 113 | + version, |
| 114 | + namespace, |
| 115 | + Vec::new(), |
| 116 | + Vec::new(), |
| 117 | + None, |
| 118 | + ); |
| 119 | + let file = File::new( |
| 120 | + db, |
| 121 | + FilePath::from_url(&file_url), |
| 122 | + oak_db::FileRevision::zero(), |
| 123 | + Some(contents.to_string()), |
| 124 | + Some(pkg), |
| 125 | + ); |
| 126 | + pkg.set_files(db).to(vec![file]); |
| 127 | + let root = Root::new(db, FilePath::from_url(&root_url), kind, Vec::new(), vec![ |
| 128 | + pkg, |
| 129 | + ]); |
| 130 | + match kind { |
| 131 | + RootKind::Library => db.library_roots().set_roots(db).to(vec![root]), |
| 132 | + RootKind::Workspace => db.workspace_roots().set_roots(db).to(vec![root]), |
| 133 | + }; |
| 134 | + file |
| 135 | +} |
0 commit comments