Skip to content

Commit

Permalink
Extract some common code for both outputs of bindings_output tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jul 24, 2024
1 parent f9b1135 commit 200c17b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
17 changes: 8 additions & 9 deletions crates/solidity/outputs/cargo/tests/src/bindings_output/graph.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::fmt;
use std::path::Path;

use infra_utils::paths::PathExtensions;
use metaslang_graph_builder::graph::{Graph, Value};
use semver::Version;
use slang_solidity::bindings;
use slang_solidity::cst::KindTypes;
use slang_solidity::parse_output::ParseOutput;

Expand All @@ -12,20 +11,20 @@ const LOCATION_DEBUG_ATTR: &str = "debug_msgb_location";
const MATCH_DEBUG_ATTR: &str = "debug_msgb_match_node";

pub(crate) fn render_graph(
version: &Version,
graph: &Graph<KindTypes>,
parse_output: &ParseOutput,
source_path: &Path,
) -> String {
let mut bindings = bindings::create(version.clone());
let tree = parse_output.create_tree_cursor();
let graph = bindings.add_file_returning_graph(source_path.to_str().unwrap(), tree);

let note = if parse_output.is_valid() {
""
} else {
"%% WARNING: Parsing failed, graph may be incomplete\n"
"PARSING FAILED: "
};
format!("{note}{graph}", graph = print_graph_as_mermaid(&graph))
let title = source_path.strip_repo_root().unwrap().to_str().unwrap();
format!(
"---\ntitle: {note}{title}\n---\n{graph}",
graph = print_graph_as_mermaid(graph)
)
}

fn print_graph_as_mermaid(graph: &Graph<KindTypes>) -> impl fmt::Display + '_ {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@ use std::path::Path;
use anyhow::Result;
use ariadne::{Color, Config, Label, Report, ReportBuilder, ReportKind, Source};
use infra_utils::paths::PathExtensions;
use semver::Version;
use slang_solidity::bindings::{self, Handle};
use slang_solidity::bindings::{Bindings, Handle};
use slang_solidity::parse_output::ParseOutput;

pub(crate) fn render_bindings(
version: &Version,
bindings: &Bindings,
parse_output: &ParseOutput,
source: &str,
source_path: &Path,
) -> Result<String> {
let mut bindings = bindings::create(version.clone());
bindings.add_file(
source_path.to_str().unwrap(),
parse_output.create_tree_cursor(),
);

let source_id = source_path.strip_repo_root()?.unwrap_str();
let mut builder: ReportBuilder<'_, (&str, Range<usize>)> = Report::build(
ReportKind::Custom("References and definitions", Color::Unset),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use infra_utils::cargo::CargoWorkspace;
use infra_utils::codegen::CodegenFileSystem;
use infra_utils::github::GitHub;
use infra_utils::paths::PathExtensions;
use slang_solidity::bindings;
use slang_solidity::language::Language;

use super::graph::render_graph;
Expand All @@ -26,14 +27,19 @@ pub fn run(group_name: &str, test_name: &str) -> Result<()> {

for version in &VERSION_BREAKS {
let language = Language::new(version.clone())?;
let mut bindings = bindings::create(version.clone());

let parse_output = language.parse(Language::ROOT_KIND, &source);
let graph = bindings.add_file_returning_graph(
input_path.to_str().unwrap(),
parse_output.create_tree_cursor(),
);

if !GitHub::is_running_in_ci() {
// Don't run this in CI, since the graph outputs are not committed
// to the repository and hence we cannot verify their contents,
// which is what `fs.write_file` does in CI.
let graph_output = render_graph(version, &parse_output, &input_path);
let graph_output = render_graph(&graph, &parse_output, &input_path);
match last_graph_output {
Some(ref last) if last == &graph_output => (),
_ => {
Expand All @@ -45,7 +51,7 @@ pub fn run(group_name: &str, test_name: &str) -> Result<()> {
};
}

let bindings_output = render_bindings(version, &parse_output, &source, &input_path)?;
let bindings_output = render_bindings(&bindings, &parse_output, &source, &input_path)?;
match last_bindings_output {
Some(ref last) if last == &bindings_output => (),
_ => {
Expand Down

0 comments on commit 200c17b

Please sign in to comment.