Skip to content

Commit

Permalink
Integrate bindings assertions checking into the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jun 18, 2024
1 parent 96c91b1 commit 6c40818
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 40 deletions.
3 changes: 2 additions & 1 deletion crates/solidity/outputs/cargo/slang_solidity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ required-features = ["cli"]

[features]
default = ["cli"]
cli = ["dep:clap", "dep:serde_json", "__private_ariadne"]
cli = ["dep:anyhow", "dep:clap", "dep:serde_json", "__private_ariadne"]
# This is meant to be used by the CLI or internally only.
__private_ariadne = ["dep:ariadne"]
# For internal development only
Expand All @@ -46,6 +46,7 @@ infra_utils = { workspace = true } # __REMOVE_THIS_LINE_DURING_CAR
solidity_language = { workspace = true } # __REMOVE_THIS_LINE_DURING_CARGO_PUBLISH__

[dependencies]
anyhow = { workspace = true, optional = true }
ariadne = { workspace = true, optional = true }
clap = { workspace = true, optional = true }
metaslang_cst = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use std::cmp::Ordering;
use std::collections::HashMap;

use regex::Regex;
use slang_solidity::bindings::Bindings;
use slang_solidity::cursor::Cursor;
use slang_solidity::kinds::TerminalKind;
use slang_solidity::query::Query;
use thiserror::Error;

use crate::bindings::Bindings;
use crate::cursor::Cursor;
use crate::kinds::TerminalKind;
use crate::query::Query;

#[derive(Debug, Error)]
pub enum AssertionError {
#[error("Invalid assertion at {0}:{1}")]
Expand Down
29 changes: 7 additions & 22 deletions crates/solidity/outputs/cargo/slang_solidity/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use std::process::ExitCode;

use clap::Subcommand;
use semver::Version;
use slang_solidity::bindings::Bindings;
use slang_solidity::cli::commands;
use slang_solidity::cli::commands::CommandError;
use thiserror::Error;

#[derive(Subcommand, Debug)]
pub enum LocalCommands {
Expand All @@ -20,19 +15,6 @@ pub enum LocalCommands {
},
}

#[derive(Error, Debug)]
pub enum LocalCommandError {
#[error(transparent)]
Command(#[from] CommandError),

#[cfg(feature = "__experimental_bindings_api")]
#[error(transparent)]
Assertion(#[from] crate::assertions::AssertionError),

#[error(transparent)]
Bindings(#[from] slang_solidity::bindings::BindingsError),
}

impl LocalCommands {
#[cfg(not(feature = "__experimental_bindings_api"))]
pub fn execute(self) -> ExitCode {
Expand All @@ -41,7 +23,7 @@ impl LocalCommands {

#[cfg(feature = "__experimental_bindings_api")]
pub fn execute(self) -> ExitCode {
let result: Result<(), LocalCommandError> = match self {
let result: anyhow::Result<()> = match self {
Self::CheckAssertions { file_path, version } => {
check_assertions_command::execute(&file_path, version)
}
Expand All @@ -58,10 +40,13 @@ impl LocalCommands {

#[cfg(feature = "__experimental_bindings_api")]
mod check_assertions_command {
use super::{commands, Bindings, LocalCommandError, Version};
use crate::assertions;
use anyhow::Result;
use semver::Version;
use slang_solidity::assertions;
use slang_solidity::bindings::Bindings;
use slang_solidity::cli::commands;

pub fn execute(file_path_string: &str, version: Version) -> Result<(), LocalCommandError> {
pub fn execute(file_path_string: &str, version: Version) -> Result<()> {
let mut bindings = Bindings::create(version.clone());
let parse_output = commands::parse::parse_source_file(file_path_string, version, |_| ())?;
let tree_cursor = parse_output.create_tree_cursor();
Expand Down
7 changes: 4 additions & 3 deletions crates/solidity/outputs/cargo/slang_solidity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub use generated::*;
// https://github.com/rust-lang/cargo/issues/1982
#[cfg(feature = "cli")]
mod supress_cli_dependencies {
#[cfg(feature = "__experimental_bindings_api")]
use regex as _;
use {ariadne as _, clap as _, serde_json as _};
use {anyhow as _, ariadne as _, clap as _, serde_json as _};
}

#[cfg(feature = "__experimental_bindings_api")]
pub mod assertions;
6 changes: 3 additions & 3 deletions crates/solidity/outputs/cargo/slang_solidity/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ use slang_solidity::cli;
// This is a known issue, and we should remove this hack once there is a better solution from Cargo.
// https://github.com/rust-lang/cargo/issues/1982
mod supress_api_dependencies {
#[cfg(not(feature = "__experimental_bindings_api"))]
use anyhow as _;
use {
ariadne as _, metaslang_cst as _, semver as _, serde as _, serde_json as _, strum as _,
strum_macros as _, thiserror as _,
};
#[cfg(feature = "__experimental_bindings_api")]
use {metaslang_graph_builder as _, stack_graphs as _};
use {metaslang_graph_builder as _, regex as _, stack_graphs as _};
}

#[cfg(feature = "__experimental_bindings_api")]
mod assertions;
mod commands;

use commands::LocalCommands;
Expand Down
31 changes: 24 additions & 7 deletions crates/solidity/outputs/cargo/tests/src/bindings/runner.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::fmt;
use std::fs::{self, create_dir_all};
use std::path::PathBuf;

use anyhow::Result;
use infra_utils::cargo::CargoWorkspace;
use infra_utils::paths::FileWalker;
use slang_solidity::assertions::{check_assertions, collect_assertions};
use slang_solidity::bindings::graph_builder::{
ExecutionConfig, Functions, Graph, NoCancellation, Variables,
};
use slang_solidity::bindings::Bindings;
use slang_solidity::language::Language;
use slang_solidity::parse_output::ParseOutput;

#[test]
pub fn run_all() -> Result<()> {
Expand All @@ -26,15 +29,33 @@ fn run(file_name: &str) -> Result<()> {
let data_dir =
CargoWorkspace::locate_source_crate("solidity_testing_snapshots")?.join("bindings");
let input_path = data_dir.join(file_name);
let input = fs::read_to_string(input_path)?;
let input = fs::read_to_string(&input_path)?;

// TODO: de-hardcode this and parse with different versions?
let latest_version = Language::SUPPORTED_VERSIONS.last().unwrap();
let language = Language::new(latest_version.clone())?;
let version = Language::SUPPORTED_VERSIONS.last().unwrap();
let language = Language::new(version.clone())?;

let parse_output = language.parse(Language::ROOT_KIND, &input);
assert!(parse_output.is_valid());

let output_dir = data_dir.join("generated");
let output_path = output_dir.join(format!("{file_name}.mmd"));
create_dir_all(&output_dir)?;
output_graph(&parse_output, &output_path)?;

let mut bindings = Bindings::create(version.clone());
bindings.add_file(
input_path.to_str().unwrap(),
parse_output.create_tree_cursor(),
)?;

let assertions = collect_assertions(parse_output.create_tree_cursor())?;
check_assertions(&bindings, &assertions)?;

Ok(())
}

fn output_graph(parse_output: &ParseOutput, output_path: &PathBuf) -> Result<()> {
let graph_builder = Bindings::get_graph_builder()?;

let functions = Functions::stdlib();
Expand All @@ -48,10 +69,6 @@ fn run(file_name: &str) -> Result<()> {
let tree = parse_output.create_tree_cursor();
let graph = graph_builder.execute(&tree, &execution_config, &NoCancellation)?;

let output_dir = data_dir.join("generated");
create_dir_all(&output_dir)?;

let output_path = output_dir.join(format!("{file_name}.mmd"));
fs::write(output_path, format!("{}", print_graph_as_mermaid(&graph)))?;

Ok(())
Expand Down

0 comments on commit 6c40818

Please sign in to comment.