Skip to content

Commit

Permalink
Merge 60fc9a7 into 28669da
Browse files Browse the repository at this point in the history
  • Loading branch information
kayagokalp committed Mar 2, 2024
2 parents 28669da + 60fc9a7 commit 5ad8bc8
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 9 deletions.
107 changes: 98 additions & 9 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ use std::{
str::FromStr,
sync::{atomic::AtomicBool, Arc},
};
use sway_core::language::ty::{TyAstNodeContent, TyExpression, TyExpressionVariant};
pub use sway_core::Programs;
use sway_core::TypeInfo;
use sway_core::{
abi_generation::{
evm_abi,
Expand All @@ -51,7 +53,7 @@ use sway_core::{
};
use sway_error::{error::CompileError, handler::Handler, warning::CompileWarning};
use sway_types::constants::{CORE, PRELUDE, STD};
use sway_types::{Ident, Span, Spanned};
use sway_types::{Ident, SourceId, Span, Spanned};
use sway_utils::{constants, time_expr, PerformanceData, PerformanceMetric};
use tracing::{debug, info};

Expand Down Expand Up @@ -162,6 +164,7 @@ pub struct PkgTestEntry {
pub pass_condition: TestPassCondition,
pub span: Span,
pub file_path: Arc<PathBuf>,
pub log_params: Vec<(SourceId, Option<Arc<TypeInfo>>)>,
}

/// The result of successfully compiling a workspace.
Expand Down Expand Up @@ -2011,16 +2014,55 @@ impl PkgTestEntry {
bail!("Invalid test argument(s) for test: {test_name}.")
}?;

/// Given an AST node, checks:
/// - it is function,
/// - it is a log,
///
/// If condition holds, returns type of log's param.
fn collect_log_type_from_decl(
expr: &TyExpression,
log_param_types: &mut Vec<(SourceId, Option<Arc<TypeInfo>>)>,
engines: &Engines,
) {
if let TyExpressionVariant::FunctionApplication {
call_path,
arguments,
..
} = &expr.expression
{
let suffix = call_path.suffix.to_string();
if suffix == "log" {
let arg_type = arguments
.iter()
.map(|(_, arg_expr)| arg_expr.return_type)
.map(|type_id| engines.te().get(type_id))
.next();
log_param_types.push((expr.span.source_id().cloned().unwrap(), arg_type));
}
}
}

let mut log_params = Vec::new();
for ast_node in test_function_decl.body.contents.iter() {
if let TyAstNodeContent::Expression(expr) = &ast_node.content {
// We found an expression node, try to extract the arguments if this is an assert.
collect_log_type_from_decl(expr, &mut log_params, engines);
}
}

let file_path = Arc::new(
engines.se().get_path(
span.source_id()
.ok_or_else(|| anyhow::anyhow!("Missing span for test function"))?,
),
);

println!("{log_params:?}");
Ok(Self {
pass_condition,
span,
file_path,
log_params,
})
}
}
Expand Down Expand Up @@ -2754,13 +2796,11 @@ pub fn fuel_core_not_running(node_url: &str) -> anyhow::Error {
mod test {
use super::*;
use regex::Regex;
use sway_types::integer_bits::IntegerBits;

fn setup_build_plan() -> BuildPlan {
fn setup_build_plan(path: &str) -> BuildPlan {
let current_dir = env!("CARGO_MANIFEST_DIR");
let manifest_dir = PathBuf::from(current_dir)
.parent()
.unwrap()
.join("test/src/e2e_vm_tests/test_programs/should_pass/forc/workspace_building/");
let manifest_dir = PathBuf::from(current_dir).parent().unwrap().join(path);
let manifest_file = ManifestFile::from_dir(manifest_dir).unwrap();
let member_manifests = manifest_file.member_manifests().unwrap();
let lock_path = manifest_file.lock_path().unwrap();
Expand All @@ -2774,9 +2814,54 @@ mod test {
.unwrap()
}

#[test]
fn test_collect_test_entry_logs() {
let current_dir = env!("CARGO_MANIFEST_DIR");
let manifest_dir = format!("{current_dir}/tests/test_pkg_entry");
let build_opts = BuildOpts {
pkg: PkgOpts {
path: Some(manifest_dir),
..Default::default()
},
build_target: BuildTarget::Fuel,
tests: true,
experimental: ExperimentalFlags { new_encoding: true },
..Default::default()
};
let built_pkgs = build_with_options(build_opts).unwrap();
let built_pkg = built_pkgs.expect_pkg().unwrap();
let test_entry = built_pkg
.bytecode
.entries
.iter()
.find_map(|entry| {
if let PkgEntryKind::Test(test_entry) = &entry.kind {
Some(test_entry)
} else {
None
}
})
.unwrap();

let log_params: Vec<_> = test_entry
.log_params
.iter()
.map(|(_, type_info)| type_info)
.collect();
assert_eq!(log_params.len(), 1);
let type_info = log_params[0].clone().unwrap();

assert!(matches!(
type_info.as_ref(),
TypeInfo::UnsignedInteger(IntegerBits::SixtyFour)
))
}

#[test]
fn test_root_pkg_order() {
let build_plan = setup_build_plan();
let build_plan = setup_build_plan(
"test/src/e2e_vm_tests/test_programs/should_pass/forc/workspace_building",
);
let graph = build_plan.graph();
let order: Vec<String> = build_plan
.member_nodes()
Expand All @@ -2787,7 +2872,9 @@ mod test {

#[test]
fn test_visualize_with_url_prefix() {
let build_plan = setup_build_plan();
let build_plan = setup_build_plan(
"test/src/e2e_vm_tests/test_programs/should_pass/forc/workspace_building",
);
let result = build_plan.visualize(Some("some-prefix::".to_string()));
let re = Regex::new(r#"digraph \{
0 \[ label = "test_contract" shape = box URL = "some-prefix::/[[:ascii:]]+/test_contract/Forc.toml"\]
Expand All @@ -2803,7 +2890,9 @@ mod test {

#[test]
fn test_visualize_without_prefix() {
let build_plan = setup_build_plan();
let build_plan = setup_build_plan(
"test/src/e2e_vm_tests/test_programs/should_pass/forc/workspace_building",
);
let result = build_plan.visualize(None);
let expected = r#"digraph {
0 [ label = "test_contract" shape = box ]
Expand Down
2 changes: 2 additions & 0 deletions forc-pkg/tests/test_pkg_entry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
13 changes: 13 additions & 0 deletions forc-pkg/tests/test_pkg_entry/Forc.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'core'
source = 'path+from-root-B2871353A775FFA4'

[[package]]
name = 'std'
source = 'path+from-root-B2871353A775FFA4'
dependencies = ['core']

[[package]]
name = 'test_library'
source = 'member'
dependencies = ['std']
8 changes: 8 additions & 0 deletions forc-pkg/tests/test_pkg_entry/Forc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "lib.sw"
license = "Apache-2.0"
name = "test_pkg_entry"

[dependencies]
std = { path = "../../../sway-lib-std/" }
8 changes: 8 additions & 0 deletions forc-pkg/tests/test_pkg_entry/src/lib.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library;

#[test]
fn test_bum() {
let a = 10;
log(a);
assert(a == 10)
}

0 comments on commit 5ad8bc8

Please sign in to comment.