Skip to content

Commit

Permalink
fix edition
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 committed Feb 7, 2024
1 parent 7e127a9 commit 1ad8273
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ zip = { version = "0.6", default-features = false, features = ["deflate"] }
zstd = "0.13"

[profile.release]
lto = true
lto = true

[profile.ci]
inherits = "test"
Expand Down
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, Edition, 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_crate_edition = compilation_unit.main_package_edition();
let main_crate_id = insert_lib_entrypoint_content_into_db(
db,
crate_name,
crate_root,
lib_content,
main_crate_edition,
);

if build_diagnostics_reporter(compilation_unit).check(db) {
return Err(anyhow!(
Expand Down Expand Up @@ -180,13 +188,19 @@ fn insert_lib_entrypoint_content_into_db(
crate_name: &str,
crate_root: &Path,
lib_content: &str,
main_crate_edition: Edition,
) -> 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: CrateSettings {
edition: main_crate_edition,
cfg_set: None,
experimental_features: Default::default(),
},
}),
);

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
45 changes: 40 additions & 5 deletions extensions/scarb-snforge-test-collector/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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 @@ -66,14 +68,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(),
0,
"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 @@ -150,11 +167,29 @@ 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 source_file_path(&self) -> &Utf8Path {
&self.unit_metadata.target.source_path
pub fn main_package_source_file_path(&self) -> &Utf8Path {
&self.main_package_metadata.source_path
}

pub fn main_package_edition(&self) -> Edition {
let maybe_edition = self
.metadata
.packages
.iter()
.find(|package| package.id == self.main_package_metadata.package)
.expect("Main package not found in metadata")
.edition
.clone();

if let Some(edition) = maybe_edition {
let edition_value = serde_json::Value::String(edition);
serde_json::from_value(edition_value).unwrap()
} else {
Edition::default()
}
}
}

0 comments on commit 1ad8273

Please sign in to comment.