Skip to content

Commit

Permalink
Add missing eqwalizer OTP version check in tests
Browse files Browse the repository at this point in the history
Summary:
Eqwalizer is not supported for OTP < 26.
The previous diff to skip running eqwalizer tests for OTP25 missed one.

Reviewed By: RobinMorisset

Differential Revision: D61711285

fbshipit-source-id: 93c496fc21c0ed1d60cd69bc82c84a1f3eadfbb8
  • Loading branch information
alanz authored and facebook-github-bot committed Aug 23, 2024
1 parent eeebe7b commit 5c00958
Showing 1 changed file with 99 additions and 94 deletions.
193 changes: 99 additions & 94 deletions crates/elp/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,108 +311,113 @@ mod tests {
// This function is a simplified/inlined version of eqwalizer_cli::eqwalize_app,
// with panics in case of failures, and checks eqWAlization results per module.
pub fn eqwalize_all_snapshots(project: &str, app: &str, buck: bool, config: EqwalizerConfig) {
if !buck || cfg!(feature = "buck") {
let mut cli = Fake::default();
let project_config = DiscoverConfig::new(!buck, "test");
let str_path = project_path(project);
let project_path: &Path = Path::new(&str_path);
let mut loaded = load::load_project_at(
&mut cli,
project_path,
project_config,
IncludeOtp::Yes,
Mode::Cli,
&BUCK_QUERY_CONFIG,
)
.with_context(|| format!("Failed to load project at {}", str_path))
.unwrap();
loaded
.analysis_host
.raw_database_mut()
.set_eqwalizer_config(Arc::new(config));
build::compile_deps(&loaded, &mut cli)
.with_context(|| format!("Failed to compile deps for project {}", project))
.unwrap();

let analysis = loaded.analysis();
let module_index = analysis
.module_index(loaded.project_id)
.with_context(|| format!("No module index for project {}", project))
if otp_supported_by_eqwalizer() {
if !buck || cfg!(feature = "buck") {
let mut cli = Fake::default();
let project_config = DiscoverConfig::new(!buck, "test");
let str_path = project_path(project);
let project_path: &Path = Path::new(&str_path);
let mut loaded = load::load_project_at(
&mut cli,
project_path,
project_config,
IncludeOtp::Yes,
Mode::Cli,
&BUCK_QUERY_CONFIG,
)
.with_context(|| format!("Failed to load project at {}", str_path))
.unwrap();
let file_ids: Vec<FileId> = module_index
.iter_own()
.filter_map(|(_name, _source, file_id)| {
if analysis.file_app_name(file_id).ok()? == Some(AppName(app.into())) {
Some(file_id)
} else {
None
}
})
.collect();

let files_count = file_ids.len();
let project_id = loaded.project_id;
// Use 4 instances for tests
let chunk_size = (files_count + 3) / 4;
let output = file_ids
.clone()
.chunks(chunk_size)
.par_bridge()
.map_with(analysis.clone(), move |analysis, file_ids| {
analysis
.eqwalizer_diagnostics(project_id, file_ids.to_vec())
.expect("cancelled")
})
.fold(EqwalizerDiagnostics::default, |acc, output| {
acc.combine((*output).clone())
})
.reduce(EqwalizerDiagnostics::default, |acc, other| {
acc.combine(other)
});

match output {
EqwalizerDiagnostics::Diagnostics {
errors: diagnostics_by_module,
..
} => {
for file_id in file_ids {
let mut cli = Fake::default();
let pretty_reporter =
&mut reporting::PrettyReporter::new(&analysis, &loaded, &mut cli);
let module = module_index.module_for_file(file_id).unwrap();
if let Some(diagnostics) = diagnostics_by_module.get(module.as_str()) {
loaded
.analysis_host
.raw_database_mut()
.set_eqwalizer_config(Arc::new(config));
build::compile_deps(&loaded, &mut cli)
.with_context(|| format!("Failed to compile deps for project {}", project))
.unwrap();

let analysis = loaded.analysis();
let module_index = analysis
.module_index(loaded.project_id)
.with_context(|| format!("No module index for project {}", project))
.unwrap();
let file_ids: Vec<FileId> = module_index
.iter_own()
.filter_map(|(_name, _source, file_id)| {
if analysis.file_app_name(file_id).ok()? == Some(AppName(app.into())) {
Some(file_id)
} else {
None
}
})
.collect();

let files_count = file_ids.len();
let project_id = loaded.project_id;
// Use 4 instances for tests
let chunk_size = (files_count + 3) / 4;
let output = file_ids
.clone()
.chunks(chunk_size)
.par_bridge()
.map_with(analysis.clone(), move |analysis, file_ids| {
analysis
.eqwalizer_diagnostics(project_id, file_ids.to_vec())
.expect("cancelled")
})
.fold(EqwalizerDiagnostics::default, |acc, output| {
acc.combine((*output).clone())
})
.reduce(EqwalizerDiagnostics::default, |acc, other| {
acc.combine(other)
});

match output {
EqwalizerDiagnostics::Diagnostics {
errors: diagnostics_by_module,
..
} => {
for file_id in file_ids {
let mut cli = Fake::default();
let pretty_reporter =
&mut reporting::PrettyReporter::new(&analysis, &loaded, &mut cli);
let module = module_index.module_for_file(file_id).unwrap();
if let Some(diagnostics) = diagnostics_by_module.get(module.as_str()) {
pretty_reporter
.write_eqwalizer_diagnostics(file_id, &diagnostics)
.with_context(|| {
format!(
"Failed to write diagnostics for {}",
module.as_str()
)
})
.unwrap();
}
pretty_reporter
.write_eqwalizer_diagnostics(file_id, &diagnostics)
.write_error_count()
.with_context(|| {
format!("Failed to write diagnostics for {}", module.as_str())
})
.unwrap();

let exp_path = expect_file!(format!(
"../resources/test/{}/{}/{}.pretty",
project,
app,
module.as_str()
));
let (stdout, _) = cli.to_strings();
assert_normalised_file(exp_path, &stdout, project_path.into());
}
pretty_reporter
.write_error_count()
.with_context(|| {
format!("Failed to write diagnostics for {}", module.as_str())
})
.unwrap();

let exp_path = expect_file!(format!(
"../resources/test/{}/{}/{}.pretty",
project,
app,
module.as_str()
));
let (stdout, _) = cli.to_strings();
assert_normalised_file(exp_path, &stdout, project_path.into());
}
}
EqwalizerDiagnostics::NoAst { module } => {
panic!(
"Could not run tests because module {} was not found",
module
)
}
EqwalizerDiagnostics::Error(error) => {
panic!("Could not run tests: {}", error)
EqwalizerDiagnostics::NoAst { module } => {
panic!(
"Could not run tests because module {} was not found",
module
)
}
EqwalizerDiagnostics::Error(error) => {
panic!("Could not run tests: {}", error)
}
}
}
}
Expand Down

0 comments on commit 5c00958

Please sign in to comment.