Skip to content

Commit 8464eec

Browse files
authored
Migrate gas report tests to use insta (#3890)
<!-- Reference any GitHub issues resolved by this PR --> Related #3594 ## Introduced changes <!-- A brief description of the changes --> - Use insta snapshots for gas report tests
1 parent 6a477e8 commit 8464eec

22 files changed

+351
-116
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ tracing = "0.1.41"
131131
tracing-chrome = "0.7.2"
132132
tracing-log = "0.2.0"
133133
comfy-table = "7"
134+
insta = { version = "1.43.1", features = ["filters"] }

crates/forge/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ cairo-lang-starknet-classes.workspace = true
7070
walkdir.workspace = true
7171
test-case.workspace = true
7272
itertools.workspace = true
73+
insta.workspace = true
7374
docs = { workspace = true, features = ["testing"] }
7475
packages_validation = { path = "../testing/packages_validation" }
7576
project-root.workspace = true

crates/forge/tests/e2e/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use cairo_annotations::trace_data::{
33
CallTraceNode as ProfilerCallTraceNode, CallTraceV1 as ProfilerCallTrace,
44
};
55

6+
mod output;
67
pub mod runner;
78

89
#[cfg(not(feature = "cairo-native"))]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// Asserts a cleaned `stdout` snapshot using `insta`, filtered for non-deterministic lines.
2+
/// Uses the current Scarb version as a snapshot suffix.
3+
#[macro_export]
4+
macro_rules! assert_cleaned_output {
5+
($output:expr) => {{
6+
let stdout = String::from_utf8_lossy(&$output.get_output().stdout);
7+
8+
let scarb_version = scarb_api::version::scarb_version()
9+
.expect("Failed to get scarb version")
10+
.scarb;
11+
12+
insta::with_settings!({
13+
snapshot_suffix => scarb_version.to_string(),
14+
filters => vec![
15+
(r"\x1B\[[0-?]*[ -/]*[@-~]", ""), // ANSI escape regex - needed for CI
16+
(r"(?m)^\s*(Compiling|Finished|Blocking).*", ""), // scarb output
17+
(r"(?m)^\s*(Collected|Running|Tests:|Latest block number).*", ""), // snforge output
18+
]},
19+
{
20+
insta::assert_snapshot!(stdout);
21+
}
22+
);
23+
}};
24+
}

crates/forge/tests/e2e/gas_report.rs

Lines changed: 7 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::assert_cleaned_output;
12
use crate::e2e::common::runner::{
23
BASE_FILE_PATTERNS, Package, setup_package, setup_package_with_file_patterns, test_runner,
34
};
@@ -13,66 +14,19 @@ fn basic() {
1314
.assert()
1415
.code(0);
1516

16-
assert_stdout_contains(
17-
output,
18-
indoc! {r"
19-
[..]Compiling[..]
20-
[..]Finished[..]
21-
22-
23-
Collected 1 test(s) from simple_package package
24-
Running 0 test(s) from src/
25-
Running 1 test(s) from tests/
26-
[PASS] simple_package_integrationtest::contract::call_and_invoke (l1_gas: ~0, l1_data_gas: ~[..], l2_gas: ~[..])
27-
╭------------------------+-------+-------+-------+---------+---------╮
28-
| HelloStarknet Contract | | | | | |
29-
+====================================================================+
30-
| Function Name | Min | Max | Avg | Std Dev | # Calls |
31-
|------------------------+-------+-------+-------+---------+---------|
32-
| get_balance | 13340 | 13340 | 13340 | 0 | 2 |
33-
|------------------------+-------+-------+-------+---------+---------|
34-
| increase_balance | 24940 | 24940 | 24940 | 0 | 1 |
35-
╰------------------------+-------+-------+-------+---------+---------╯
36-
37-
Tests: 1 passed, 0 failed, 0 ignored, [..] filtered out
38-
"},
39-
);
17+
assert_cleaned_output!(output);
4018
}
4119

4220
#[test]
4321
fn recursive_calls() {
4422
let temp = setup_package("debugging");
45-
4623
let output = test_runner(&temp)
47-
.arg("test_debugging_trace_success")
4824
.arg("--gas-report")
25+
.arg("test_debugging_trace_success")
4926
.assert()
5027
.code(0);
5128

52-
assert_stdout_contains(
53-
output,
54-
indoc! {r"
55-
[..]Compiling[..]
56-
[..]Finished[..]
57-
58-
59-
Collected 1 test(s) from debugging package
60-
Running 0 test(s) from src/
61-
Running 1 test(s) from tests/
62-
[PASS] debugging_integrationtest::test_trace::test_debugging_trace_success (l1_gas: ~0, l1_data_gas: ~[..], l2_gas: ~[..])
63-
╭-------------------------+-------+--------+--------+---------+---------╮
64-
| SimpleContract Contract | | | | | |
65-
+=======================================================================+
66-
| Function Name | Min | Max | Avg | Std Dev | # Calls |
67-
|-------------------------+-------+--------+--------+---------+---------|
68-
| execute_calls | 11680 | 609080 | 183924 | 235859 | 5 |
69-
|-------------------------+-------+--------+--------+---------+---------|
70-
| fail | 17950 | 17950 | 17950 | 0 | 1 |
71-
╰-------------------------+-------+--------+--------+---------+---------╯
72-
73-
Tests: 1 passed, 0 failed, 0 ignored, [..] filtered out
74-
"},
75-
);
29+
assert_cleaned_output!(output);
7630
}
7731

7832
#[test]
@@ -84,38 +38,7 @@ fn multiple_contracts_and_constructor() {
8438
.assert()
8539
.code(0);
8640

87-
assert_stdout_contains(
88-
output,
89-
indoc! {r"
90-
[..]Compiling[..]
91-
[..]Finished[..]
92-
93-
94-
Collected 1 test(s) from simple_package_with_cheats package
95-
Running 0 test(s) from src/
96-
Running 1 test(s) from tests/
97-
[PASS] simple_package_with_cheats_integrationtest::contract::call_and_invoke_proxy (l1_gas: ~0, l1_data_gas: ~[..], l2_gas: ~[..])
98-
╭------------------------+-------+-------+-------+---------+---------╮
99-
| HelloStarknet Contract | | | | | |
100-
+====================================================================+
101-
| Function Name | Min | Max | Avg | Std Dev | # Calls |
102-
|------------------------+-------+-------+-------+---------+---------|
103-
| get_block_number | 15780 | 15780 | 15780 | 0 | 2 |
104-
╰------------------------+-------+-------+-------+---------+---------╯
105-
106-
╭-----------------------------+--------+--------+--------+---------+---------╮
107-
| HelloStarknetProxy Contract | | | | | |
108-
+============================================================================+
109-
| Function Name | Min | Max | Avg | Std Dev | # Calls |
110-
|-----------------------------+--------+--------+--------+---------+---------|
111-
| constructor | 14650 | 14650 | 14650 | 0 | 1 |
112-
|-----------------------------+--------+--------+--------+---------+---------|
113-
| get_block_number | 125280 | 125280 | 125280 | 0 | 2 |
114-
╰-----------------------------+--------+--------+--------+---------+---------╯
115-
116-
Tests: 1 passed, 0 failed, 0 ignored, [..] filtered out
117-
"},
118-
);
41+
assert_cleaned_output!(output);
11942
}
12043

12144
#[test]
@@ -124,44 +47,12 @@ fn fork() {
12447
setup_package_with_file_patterns(Package::Name("forking".to_string()), BASE_FILE_PATTERNS);
12548

12649
let output = test_runner(&temp)
127-
.arg("test_track_resources")
12850
.arg("--gas-report")
51+
.arg("test_track_resources")
12952
.assert()
13053
.code(0);
13154

132-
assert_stdout_contains(
133-
output,
134-
indoc! {r"
135-
[..]Compiling[..]
136-
[..]Finished[..]
137-
138-
Collected 1 test(s) from forking package
139-
Running 1 test(s) from src/
140-
[PASS] forking::tests::test_track_resources (l1_gas: ~0, l1_data_gas: ~[..], l2_gas: ~[..])
141-
╭---------------------------+-------+-------+-------+---------+---------╮
142-
| forked contract | | | | | |
143-
| (class hash: 0x06a7…1550) | | | | | |
144-
+=======================================================================+
145-
| Function Name | Min | Max | Avg | Std Dev | # Calls |
146-
|---------------------------+-------+-------+-------+---------+---------|
147-
| get_balance | 40000 | 40000 | 40000 | 0 | 1 |
148-
|---------------------------+-------+-------+-------+---------+---------|
149-
| increase_balance | 40000 | 40000 | 40000 | 0 | 1 |
150-
╰---------------------------+-------+-------+-------+---------+---------╯
151-
╭---------------------------+-------+-------+-------+---------+---------╮
152-
| forked contract | | | | | |
153-
| (class hash: 0x07aa…af4b) | | | | | |
154-
+=======================================================================+
155-
| Function Name | Min | Max | Avg | Std Dev | # Calls |
156-
|---------------------------+-------+-------+-------+---------+---------|
157-
| get_balance | 13840 | 13840 | 13840 | 0 | 1 |
158-
|---------------------------+-------+-------+-------+---------+---------|
159-
| increase_balance | 25840 | 25840 | 25840 | 0 | 1 |
160-
╰---------------------------+-------+-------+-------+---------+---------╯
161-
162-
Tests: 1 passed, 0 failed, 0 ignored, [..] filtered out
163-
"},
164-
);
55+
assert_cleaned_output!(output);
16556
}
16657

16758
#[test]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/forge/tests/e2e/gas_report.rs
3+
expression: stdout
4+
---
5+
[PASS] simple_package_integrationtest::contract::call_and_invoke (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~524000)
6+
------------------------+-------+-------+-------+---------+---------
7+
| HelloStarknet Contract | | | | | |
8+
+====================================================================+
9+
| Function Name | Min | Max | Avg | Std Dev | # Calls |
10+
|------------------------+-------+-------+-------+---------+---------|
11+
| get_balance | 13840 | 13840 | 13840 | 0 | 2 |
12+
|------------------------+-------+-------+-------+---------+---------|
13+
| increase_balance | 25640 | 25640 | 25640 | 0 | 1 |
14+
------------------------+-------+-------+-------+---------+---------
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/forge/tests/e2e/gas_report.rs
3+
expression: stdout
4+
---
5+
[PASS] simple_package_integrationtest::contract::call_and_invoke (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~510880)
6+
------------------------+-------+-------+-------+---------+---------
7+
| HelloStarknet Contract | | | | | |
8+
+====================================================================+
9+
| Function Name | Min | Max | Avg | Std Dev | # Calls |
10+
|------------------------+-------+-------+-------+---------+---------|
11+
| get_balance | 13340 | 13340 | 13340 | 0 | 2 |
12+
|------------------------+-------+-------+-------+---------+---------|
13+
| increase_balance | 24940 | 24940 | 24940 | 0 | 1 |
14+
------------------------+-------+-------+-------+---------+---------
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/forge/tests/e2e/gas_report.rs
3+
expression: stdout
4+
---
5+
[PASS] simple_package_integrationtest::contract::call_and_invoke (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~510880)
6+
------------------------+-------+-------+-------+---------+---------
7+
| HelloStarknet Contract | | | | | |
8+
+====================================================================+
9+
| Function Name | Min | Max | Avg | Std Dev | # Calls |
10+
|------------------------+-------+-------+-------+---------+---------|
11+
| get_balance | 13340 | 13340 | 13340 | 0 | 2 |
12+
|------------------------+-------+-------+-------+---------+---------|
13+
| increase_balance | 24940 | 24940 | 24940 | 0 | 1 |
14+
------------------------+-------+-------+-------+---------+---------
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: crates/forge/tests/e2e/gas_report.rs
3+
expression: stdout
4+
---
5+
[PASS] simple_package_integrationtest::contract::call_and_invoke (l1_gas: ~0, l1_data_gas: ~192, l2_gas: ~510880)
6+
------------------------+-------+-------+-------+---------+---------
7+
| HelloStarknet Contract | | | | | |
8+
+====================================================================+
9+
| Function Name | Min | Max | Avg | Std Dev | # Calls |
10+
|------------------------+-------+-------+-------+---------+---------|
11+
| get_balance | 13340 | 13340 | 13340 | 0 | 2 |
12+
|------------------------+-------+-------+-------+---------+---------|
13+
| increase_balance | 24940 | 24940 | 24940 | 0 | 1 |
14+
------------------------+-------+-------+-------+---------+---------

0 commit comments

Comments
 (0)