Remove load_cargo_with_fake_sysroot

This commit is contained in:
lucasholten 2025-01-01 16:24:21 +01:00
parent 947dfdce0c
commit e12fe30351

View file

@ -18,18 +18,25 @@ use crate::{
}; };
fn load_cargo(file: &str) -> (CrateGraph, ProcMacroPaths) { fn load_cargo(file: &str) -> (CrateGraph, ProcMacroPaths) {
load_cargo_with_overrides(file, CfgOverrides::default()) let project_workspace = load_workspace_from_metadata(file);
to_crate_graph(project_workspace, &mut Default::default())
} }
fn load_cargo_with_overrides( fn load_cargo_with_overrides(
file: &str, file: &str,
cfg_overrides: CfgOverrides, cfg_overrides: CfgOverrides,
) -> (CrateGraph, ProcMacroPaths) { ) -> (CrateGraph, ProcMacroPaths) {
let project_workspace =
ProjectWorkspace { cfg_overrides, ..load_workspace_from_metadata(file) };
to_crate_graph(project_workspace, &mut Default::default())
}
fn load_workspace_from_metadata(file: &str) -> ProjectWorkspace {
let meta: Metadata = get_test_json_file(file); let meta: Metadata = get_test_json_file(file);
let manifest_path = let manifest_path =
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap(); ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
let cargo_workspace = CargoWorkspace::new(meta, manifest_path, Default::default()); let cargo_workspace = CargoWorkspace::new(meta, manifest_path, Default::default());
let project_workspace = ProjectWorkspace { ProjectWorkspace {
kind: ProjectWorkspaceKind::Cargo { kind: ProjectWorkspaceKind::Cargo {
cargo: cargo_workspace, cargo: cargo_workspace,
build_scripts: WorkspaceBuildScripts::default(), build_scripts: WorkspaceBuildScripts::default(),
@ -37,46 +44,12 @@ fn load_cargo_with_overrides(
error: None, error: None,
set_test: true, set_test: true,
}, },
cfg_overrides, cfg_overrides: Default::default(),
sysroot: Sysroot::empty(), sysroot: Sysroot::empty(),
rustc_cfg: Vec::new(), rustc_cfg: Vec::new(),
toolchain: None, toolchain: None,
target_layout: Err("target_data_layout not loaded".into()), target_layout: Err("target_data_layout not loaded".into()),
};
to_crate_graph(project_workspace)
}
fn load_cargo_with_fake_sysroot(
file_map: &mut FxHashMap<AbsPathBuf, FileId>,
file: &str,
) -> (CrateGraph, ProcMacroPaths) {
let meta: Metadata = get_test_json_file(file);
let manifest_path =
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
let cargo_workspace = CargoWorkspace::new(meta, manifest_path, Default::default());
let project_workspace = ProjectWorkspace {
kind: ProjectWorkspaceKind::Cargo {
cargo: cargo_workspace,
build_scripts: WorkspaceBuildScripts::default(),
rustc: Err(None),
error: None,
set_test: true,
},
sysroot: get_fake_sysroot(),
rustc_cfg: Vec::new(),
cfg_overrides: Default::default(),
toolchain: None,
target_layout: Err("target_data_layout not loaded".into()),
};
project_workspace.to_crate_graph(
&mut {
|path| {
let len = file_map.len();
Some(*file_map.entry(path.to_path_buf()).or_insert(FileId::from_raw(len as u32)))
} }
},
&Default::default(),
)
} }
fn load_rust_project(file: &str) -> (CrateGraph, ProcMacroPaths) { fn load_rust_project(file: &str) -> (CrateGraph, ProcMacroPaths) {
@ -91,7 +64,7 @@ fn load_rust_project(file: &str) -> (CrateGraph, ProcMacroPaths) {
target_layout: Err(Arc::from("test has no data layout")), target_layout: Err(Arc::from("test has no data layout")),
cfg_overrides: Default::default(), cfg_overrides: Default::default(),
}; };
to_crate_graph(project_workspace) to_crate_graph(project_workspace, &mut Default::default())
} }
fn get_test_json_file<T: DeserializeOwned>(file: &str) -> T { fn get_test_json_file<T: DeserializeOwned>(file: &str) -> T {
@ -160,13 +133,15 @@ fn rooted_project_json(data: ProjectJsonData) -> ProjectJson {
ProjectJson::new(None, base, data) ProjectJson::new(None, base, data)
} }
fn to_crate_graph(project_workspace: ProjectWorkspace) -> (CrateGraph, ProcMacroPaths) { fn to_crate_graph(
project_workspace: ProjectWorkspace,
file_map: &mut FxHashMap<AbsPathBuf, FileId>,
) -> (CrateGraph, ProcMacroPaths) {
project_workspace.to_crate_graph( project_workspace.to_crate_graph(
&mut { &mut {
let mut counter = 0; |path| {
move |_path| { let len = file_map.len();
counter += 1; Some(*file_map.entry(path.to_path_buf()).or_insert(FileId::from_raw(len as u32)))
Some(FileId::from_raw(counter))
} }
}, },
&Default::default(), &Default::default(),
@ -256,8 +231,7 @@ fn rust_project_is_proc_macro_has_proc_macro_dep() {
#[test] #[test]
fn crate_graph_dedup_identical() { fn crate_graph_dedup_identical() {
let (mut crate_graph, proc_macros) = let (mut crate_graph, proc_macros) = load_cargo("regex-metadata.json");
load_cargo_with_fake_sysroot(&mut Default::default(), "regex-metadata.json");
crate_graph.sort_deps(); crate_graph.sort_deps();
let (d_crate_graph, mut d_proc_macros) = (crate_graph.clone(), proc_macros.clone()); let (d_crate_graph, mut d_proc_macros) = (crate_graph.clone(), proc_macros.clone());
@ -269,17 +243,19 @@ fn crate_graph_dedup_identical() {
#[test] #[test]
fn crate_graph_dedup() { fn crate_graph_dedup() {
let path_map = &mut Default::default(); let mut file_map = Default::default();
let (mut crate_graph, _proc_macros) =
load_cargo_with_fake_sysroot(path_map, "ripgrep-metadata.json");
assert_eq!(crate_graph.iter().count(), 81);
crate_graph.sort_deps();
let (regex_crate_graph, mut regex_proc_macros) =
load_cargo_with_fake_sysroot(path_map, "regex-metadata.json");
assert_eq!(regex_crate_graph.iter().count(), 60);
let ripgrep_workspace = load_workspace_from_metadata("ripgrep-metadata.json");
let (mut crate_graph, _proc_macros) = to_crate_graph(ripgrep_workspace, &mut file_map);
assert_eq!(crate_graph.iter().count(), 71);
let regex_workspace = load_workspace_from_metadata("regex-metadata.json");
let (regex_crate_graph, mut regex_proc_macros) = to_crate_graph(regex_workspace, &mut file_map);
assert_eq!(regex_crate_graph.iter().count(), 50);
crate_graph.sort_deps();
crate_graph.extend(regex_crate_graph, &mut regex_proc_macros); crate_graph.extend(regex_crate_graph, &mut regex_proc_macros);
assert_eq!(crate_graph.iter().count(), 118); assert_eq!(crate_graph.iter().count(), 108);
} }
#[test] #[test]