Skip to content

Commit

Permalink
Generate VERSION_BREAKS in a separate module for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jul 17, 2024
1 parent 98e63dd commit 949fdb7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
37 changes: 37 additions & 0 deletions crates/codegen/testing/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,40 @@ pub(crate) fn generate_unit_test_file(

fs.write_file(unit_test_file_path, contents)
}

pub(crate) fn generate_version_breaks(
language: &Language,
input_dir: &Path,
output_dir: &Path,
) -> Result<()> {
let mut fs = CodegenFileSystem::new(input_dir)?;

let version_breaks = language.collect_breaking_versions();
let version_breaks_len = version_breaks.len();
let version_breaks_str = version_breaks
.iter()
.fold(String::new(), |mut buffer, version| {
writeln!(
buffer,
"Version::new({}, {}, {}),",
version.major, version.minor, version.patch
)
.unwrap();
buffer
});

let contents = format!(
"
use semver::Version;
pub const VERSION_BREAKS: [Version; {version_breaks_len}] = [
{version_breaks_str}
];
",
);

let mod_file_path = output_dir.join("mod.rs");
fs.write_file(mod_file_path, contents)?;

Ok(())
}
5 changes: 5 additions & 0 deletions crates/codegen/testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ use std::path::Path;

use anyhow::Result;
use codegen_language_definition::model::Language;
use common::generate_version_breaks;

use crate::bindings_assertions::generate_bindings_assertions_tests;
use crate::bindings_output::generate_bindings_output_tests;
use crate::cst_output::generate_cst_output_tests;

pub trait TestingGeneratorExtensions {
fn generate_version_breaks(&self, lang_def_dir: &Path, output_dir: &Path) -> Result<()>;
fn generate_bindings_assertions_tests(
&self,
sources_dir: &Path,
Expand All @@ -24,6 +26,9 @@ pub trait TestingGeneratorExtensions {
}

impl TestingGeneratorExtensions for Language {
fn generate_version_breaks(&self, lang_def_dir: &Path, output_dir: &Path) -> Result<()> {
generate_version_breaks(self, lang_def_dir, output_dir)
}
fn generate_bindings_assertions_tests(&self, data_dir: &Path, output_dir: &Path) -> Result<()> {
generate_bindings_assertions_tests(self, data_dir, output_dir)
}
Expand Down
6 changes: 6 additions & 0 deletions crates/solidity/outputs/cargo/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ use solidity_language::SolidityDefinition;

fn main() -> Result<()> {
let lang_def = SolidityDefinition::create();
let lang_def_crate = CargoWorkspace::locate_source_crate("solidity_language")?;
let snapshots_crate = CargoWorkspace::locate_source_crate("solidity_testing_snapshots")?;
let tests_crate = CargoWorkspace::locate_source_crate("solidity_cargo_tests")?;

lang_def.generate_version_breaks(
&lang_def_crate.join("src"),
&tests_crate.join("src/generated"),
)?;

lang_def.generate_bindings_assertions_tests(
&snapshots_crate.join("bindings_assertions"),
&tests_crate.join("src/bindings_assertions/generated"),
Expand Down
39 changes: 39 additions & 0 deletions crates/solidity/outputs/cargo/tests/src/generated/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 949fdb7

Please sign in to comment.