Skip to content

Commit

Permalink
Fix edition for main crates in test compilation in test collector (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 authored Feb 8, 2024
1 parent 75622b2 commit 5189ae0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{FreeFunctionId, FunctionWithBodyId, ModuleId, ModuleItemId};
use cairo_lang_diagnostics::ToOption;
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
use cairo_lang_filesystem::db::{AsFilesGroupMut, CrateConfiguration, FilesGroup, FilesGroupEx};
use cairo_lang_filesystem::db::{
AsFilesGroupMut, CrateConfiguration, CrateSettings, FilesGroup, FilesGroupEx,
};
use cairo_lang_filesystem::ids::{CrateId, CrateLongId, Directory};
use cairo_lang_lowering::ids::ConcreteFunctionWithBodyId;
use cairo_lang_project::{ProjectConfig, ProjectConfigContent};
Expand Down Expand Up @@ -107,8 +109,14 @@ pub fn collect_tests(
b.build()?
};

let main_crate_id =
insert_lib_entrypoint_content_into_db(db, crate_name, crate_root, lib_content);
let main_package_crate_settings = compilation_unit.main_package_crate_settings();
let main_crate_id = insert_lib_entrypoint_content_into_db(
db,
crate_name,
crate_root,
lib_content,
main_package_crate_settings,
);

if build_diagnostics_reporter(compilation_unit).check(db) {
return Err(anyhow!(
Expand Down Expand Up @@ -180,13 +188,15 @@ fn insert_lib_entrypoint_content_into_db(
crate_name: &str,
crate_root: &Path,
lib_content: &str,
main_package_crate_settings: CrateSettings,
) -> CrateId {
let main_crate_id = db.intern_crate(CrateLongId::Real(SmolStr::from(crate_name)));
db.set_crate_config(
main_crate_id,
Some(CrateConfiguration::default_for_root(Directory::Real(
crate_root.to_path_buf(),
))),
Some(CrateConfiguration {
root: Directory::Real(crate_root.to_path_buf()),
settings: main_package_crate_settings,
}),
);

let module_id = ModuleId::CrateRoot(main_crate_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub fn collect_test_compilation_targets(
package_path: &Utf8Path,
compilation_unit: &CompilationUnit,
) -> Result<Vec<TestCompilationTarget>> {
let package_source_file_path = compilation_unit.source_file_path();
let package_source_file_path = compilation_unit.main_package_source_file_path();
let mut compilation_targets = vec![TestCompilationTarget {
crate_root: compilation_unit.source_root(),
crate_root: compilation_unit.main_package_source_root(),
crate_name: package_name.to_string(),
crate_location: CrateLocation::Lib,
lib_content: std::fs::read_to_string(package_source_file_path)
Expand Down
79 changes: 59 additions & 20 deletions extensions/scarb-snforge-test-collector/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use cairo_lang_project::AllCratesConfig;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use camino::{Utf8Path, Utf8PathBuf};
use itertools::Itertools;
use scarb_metadata::{CompilationUnitMetadata, Metadata, PackageMetadata};
use scarb_metadata::{
CompilationUnitComponentMetadata, CompilationUnitMetadata, Metadata, PackageMetadata,
};
use serde_json::json;
use smol_str::SmolStr;
use std::path::PathBuf;
Expand Down Expand Up @@ -67,14 +69,29 @@ pub fn compilation_unit_for_package<'a>(
);
}

let main_package_metadata = unit_test_cu
.components
.iter()
.find(|comp| comp.package == package_metadata.id)
.into_iter()
.collect_vec();

assert_eq!(
main_package_metadata.len(),
1,
"More than one cu component with main package id found"
);

Ok(CompilationUnit {
unit_metadata: unit_test_cu,
main_package_metadata: main_package_metadata[0],
metadata,
})
}

pub struct CompilationUnit<'a> {
unit_metadata: &'a CompilationUnitMetadata,
main_package_metadata: &'a CompilationUnitComponentMetadata,
metadata: &'a Metadata,
}

Expand Down Expand Up @@ -121,21 +138,7 @@ impl CompilationUnit<'_> {
});
(
SmolStr::from(&component.name),
CrateSettings {
edition: if let Some(edition) = pkg.edition.clone() {
let edition_value = serde_json::Value::String(edition);
serde_json::from_value(edition_value).unwrap()
} else {
Edition::default()
},
cfg_set: component.cfg.clone().map(build_cfg_set),
// TODO (#1040): replace this with a macro
experimental_features: ExperimentalFeaturesConfig {
negative_impls: pkg
.experimental_features
.contains(&String::from("negative_impls")),
},
},
get_crate_settings_for_package(pkg, component.cfg.clone().map(build_cfg_set)),
)
})
.collect();
Expand All @@ -156,12 +159,48 @@ impl CompilationUnit<'_> {
.unwrap_or(true)
}

pub fn source_root(&self) -> Utf8PathBuf {
self.unit_metadata.target.source_root().to_path_buf()
pub fn main_package_source_root(&self) -> Utf8PathBuf {
self.main_package_metadata.source_root().to_path_buf()
}

pub fn main_package_source_file_path(&self) -> &Utf8Path {
&self.main_package_metadata.source_path
}

pub fn source_file_path(&self) -> &Utf8Path {
&self.unit_metadata.target.source_path
pub fn main_package_crate_settings(&self) -> CrateSettings {
let package = self
.metadata
.packages
.iter()
.find(|package| package.id == self.main_package_metadata.package)
.expect("Main package not found in metadata");

get_crate_settings_for_package(package, None)
}
}

fn get_crate_settings_for_package(
package: &PackageMetadata,
cfg_set: Option<CfgSet>,
) -> CrateSettings {
let edition = package
.edition
.clone()
.map_or(Edition::default(), |edition| {
let edition_value = serde_json::Value::String(edition);
serde_json::from_value(edition_value).unwrap()
});
// TODO (#1040): replace this with a macro
let experimental_features = ExperimentalFeaturesConfig {
negative_impls: package
.experimental_features
.contains(&String::from("negative_impls")),
};

CrateSettings {
edition,
cfg_set,
experimental_features,
}
}

Expand Down

0 comments on commit 5189ae0

Please sign in to comment.