From a0fba83208d390b9f58ae4ad9b4162b0b28b0c2f Mon Sep 17 00:00:00 2001 From: Tomasz Rejowski <34059895+Arcticae@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:53:00 +0100 Subject: [PATCH] Fix `snforge-test-collector`'s default `available_gas` value (#1057) This needs to be `None` because older versions of snforge would crash on `u32::max` (which seems to be a default value), with an error that the attribute is not supported. --- .../src/compilation/test_collector/config.rs | 27 +++++++++++++------ .../tests/test.rs | 5 +--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/extensions/scarb-snforge-test-collector/src/compilation/test_collector/config.rs b/extensions/scarb-snforge-test-collector/src/compilation/test_collector/config.rs index 68c03c85d..581b65e88 100644 --- a/extensions/scarb-snforge-test-collector/src/compilation/test_collector/config.rs +++ b/extensions/scarb-snforge-test-collector/src/compilation/test_collector/config.rs @@ -15,7 +15,7 @@ use serde::Serialize; const FORK_ATTR: &str = "fork"; const FUZZER_ATTR: &str = "fuzzer"; - +const AVAILABLE_GAS_ATTR: &str = "available_gas"; /// Expectation for a panic case. #[derive(Debug, Clone, PartialEq, Serialize)] pub enum ExpectedPanicValue { @@ -147,15 +147,26 @@ pub fn forge_try_extract_test_config( let result = maybe_test_config.map( |TestConfig { - available_gas, + mut available_gas, expectation, ignored, - }| SingleTestConfig { - available_gas, - expected_result: expectation.into(), - ignored, - fork_config, - fuzzer_config, + }| { + // Older versions will crash if the default is passed through + let available_gas_attr = attrs + .iter() + .find(|attr| attr.id.as_str() == AVAILABLE_GAS_ATTR); + + if available_gas_attr.is_none() { + available_gas = None + } + + SingleTestConfig { + available_gas, + expected_result: expectation.into(), + ignored, + fork_config, + fuzzer_config, + } }, ); Ok(result) diff --git a/extensions/scarb-snforge-test-collector/tests/test.rs b/extensions/scarb-snforge-test-collector/tests/test.rs index 5ee1d46f4..c589f1099 100644 --- a/extensions/scarb-snforge-test-collector/tests/test.rs +++ b/extensions/scarb-snforge-test-collector/tests/test.rs @@ -48,10 +48,7 @@ fn forge_test_locations() { assert_eq!(&json[1]["test_cases"][0]["name"], "tests::tests::test"); assert_eq!(&json[1]["tests_location"], "Tests"); - assert_eq!( - &json[0]["test_cases"][0]["available_gas"], - &Value::Number(Number::from(u32::MAX)) - ); + assert_eq!(&json[0]["test_cases"][0]["available_gas"], &Value::Null); assert_eq!(&json[0]["test_cases"][0]["expected_result"], "Success"); assert_eq!(&json[0]["test_cases"][0]["fork_config"], &Value::Null); assert_eq!(&json[0]["test_cases"][0]["fuzzer_config"], &Value::Null);