Skip to content

Commit 6ba9cd4

Browse files
authored
Run tests on native (#3730)
1 parent eb4a768 commit 6ba9cd4

File tree

19 files changed

+136
-22
lines changed

19 files changed

+136
-22
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ jobs:
3535
with:
3636
3737
- name: Build and archive tests
38-
run: cargo nextest archive --release -p forge --features skip_plugin_checks --archive-file 'nextest-archive-${{ runner.os }}.tar.zst'
38+
# TODO: run native only on scheduled workflow
39+
run: cargo nextest archive --release -p forge --features skip_plugin_checks,run-native --archive-file 'nextest-archive-${{ runner.os }}.tar.zst'
3940
- name: Upload archive to workflow
4041
uses: actions/upload-artifact@v4
4142
with:
@@ -175,7 +176,8 @@ jobs:
175176
- uses: software-mansion/setup-scarb@v1
176177
- uses: software-mansion/setup-universal-sierra-compiler@v1
177178
- name: Run Cheatnet tests
178-
run: cargo test --release -p cheatnet
179+
# TODO: run native only on scheduled workflow
180+
run: cargo test --release -p cheatnet --features run-native
179181

180182
test-data-transformer:
181183
name: Test Data Transformer
@@ -195,7 +197,8 @@ jobs:
195197
- uses: software-mansion/setup-scarb@v1
196198
- uses: software-mansion/setup-universal-sierra-compiler@58146c4184fa6ec5e8aaf02309ab85e35f782ed0 # v1.0.0
197199
- name: Run Forge Debugging tests
198-
run: cargo test --release -p forge --features debugging --test main e2e::debugging
200+
# TODO: run native only on scheduled workflow
201+
run: cargo test --release -p forge --features debugging,run-native --test main e2e::debugging
199202

200203
test-forge-scarb-plugin:
201204
name: Test Forge Scarb Plugin
@@ -261,7 +264,8 @@ jobs:
261264
- uses: ./.github/actions/setup-rust-llvm
262265
- uses: software-mansion/setup-scarb@v1
263266
- uses: software-mansion/setup-universal-sierra-compiler@v1
264-
- run: cargo test --release -p scarb-api
267+
# TODO: run native only on scheduled workflow
268+
- run: cargo test --release -p scarb-api --features run-native
265269

266270
scarbfmt:
267271
runs-on: ubuntu-latest

crates/cheatnet/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition.workspace = true
55

66
[features]
77
testing = []
8+
run-native = []
89

910
[dependencies]
1011
anyhow.workspace = true

crates/cheatnet/tests/common/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ pub fn get_contracts() -> ContractsData {
9090
&target_dir,
9191
package,
9292
&ui,
93-
CompilationOpts::default(),
93+
CompilationOpts {
94+
use_test_target_contracts: false,
95+
run_native: cfg!(feature = "run-native"),
96+
},
9497
)
9598
.unwrap();
9699
ContractsData::try_from(contracts).unwrap()

crates/forge/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ debugging = []
1313
assert_non_exact_gas = ["test_utils/assert_non_exact_gas"]
1414
skip_plugin_checks = []
1515
interact-with-state = []
16+
run-native = ["test_utils/run-native"]
1617

1718
[dependencies]
1819
anyhow.workspace = true

crates/forge/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub struct TestArgs {
151151
#[command(flatten)]
152152
trace_args: TraceArgs,
153153

154-
/// Run contracts on `cairo-native` instead of the default `cairo-vm`.
154+
/// Run contracts on `cairo-native` instead of the default `cairo-vm`. This will set `tracked-resource` to `sierra-gas`.
155155
///
156156
/// Note: Only contracts execution through native is supported, test code itself will still run on `cairo-vm`.
157157
#[arg(long)]
@@ -184,7 +184,7 @@ pub struct TestArgs {
184184
include_ignored: bool,
185185

186186
/// Display more detailed info about used resources
187-
#[arg(long, conflicts_with = "run_native")]
187+
#[arg(long)]
188188
detailed_resources: bool,
189189

190190
/// Control when colored output is used
@@ -231,6 +231,20 @@ pub struct TestArgs {
231231
scarb_args: ScarbArgs,
232232
}
233233

234+
impl TestArgs {
235+
/// Adjust dependent arguments based on related flags.
236+
///
237+
/// This function mutates the `TestArgs` instance to enforce logical coherence
238+
/// between fields.
239+
pub fn normalize(&mut self) {
240+
// Force using `SierraGas` as tracked resource when running with `cairo-native`,
241+
// as otherwise it would run on vm.
242+
if self.run_native {
243+
self.tracked_resource = ForgeTrackedResource::SierraGas;
244+
}
245+
}
246+
}
247+
234248
#[derive(Parser, Debug)]
235249
pub struct ScarbArgs {
236250
#[command(flatten)]
@@ -306,7 +320,8 @@ pub fn main_execution(ui: Arc<UI>) -> Result<ExitStatus> {
306320

307321
Ok(ExitStatus::Success)
308322
}
309-
ForgeSubcommand::Test { args } => {
323+
ForgeSubcommand::Test { mut args } => {
324+
args.normalize();
310325
check_requirements(false, args.tracked_resource, &ui)?;
311326
let cores = if let Ok(available_cores) = available_parallelism() {
312327
available_cores.get()

crates/forge/test_utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition.workspace = true
77

88
[features]
99
assert_non_exact_gas = []
10+
run-native = []
1011

1112
[dependencies]
1213
walkdir.workspace = true

crates/forge/test_utils/src/runner.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ impl Contract {
109109
&artifacts_dir,
110110
package,
111111
ui,
112-
CompilationOpts::default(),
112+
CompilationOpts {
113+
use_test_target_contracts: false,
114+
run_native: cfg!(feature = "run-native"),
115+
},
113116
)
114117
.unwrap()
115118
.remove(&self.name)

crates/forge/tests/e2e/clean.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct CleanComponentsState {
2121
}
2222

2323
#[test]
24+
#[cfg_attr(feature = "run-native", ignore = "Native doesn't support coverage yet")]
2425
fn test_clean_coverage() {
2526
let temp_dir = setup_package("coverage_project");
2627

@@ -54,6 +55,7 @@ fn test_clean_coverage() {
5455
}
5556

5657
#[test]
58+
#[cfg_attr(feature = "run-native", ignore = "Native doesn't support profiler yet")]
5759
fn test_clean_profile() {
5860
let temp_dir = setup_package("coverage_project");
5961

@@ -118,6 +120,10 @@ fn test_clean_cache() {
118120
}
119121

120122
#[test]
123+
#[cfg_attr(
124+
feature = "run-native",
125+
ignore = "Native doesn't support trace, coverage and profiler yet"
126+
)]
121127
fn test_clean_all() {
122128
let temp_dir = setup_package("coverage_project");
123129

@@ -152,7 +158,7 @@ fn test_clean_all_and_component() {
152158
let clean_components_state = CleanComponentsState {
153159
coverage: false,
154160
cache: true,
155-
trace: true,
161+
trace: false,
156162
profile: false,
157163
};
158164
generate_clean_components(clean_components_state, &temp_dir);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#[cfg(not(feature = "run-native"))]
12
use cairo_annotations::trace_data::{
23
CallTraceNode as ProfilerCallTraceNode, CallTraceV1 as ProfilerCallTrace,
34
};
45

56
pub mod runner;
67

8+
#[cfg(not(feature = "run-native"))]
79
pub fn get_trace_from_trace_node(trace_node: &ProfilerCallTraceNode) -> &ProfilerCallTrace {
810
if let ProfilerCallTraceNode::EntryPointCall(trace) = trace_node {
911
trace

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,29 @@ pub fn snforge_test_bin_path() -> PathBuf {
3131
cargo_bin!("snforge").to_path_buf()
3232
}
3333

34+
/// Returns a command that runs `snforge test` in the given temporary directory.
35+
/// If the `run-native` feature is enabled, it adds the `--run-native` flag.
3436
pub(crate) fn test_runner<T: AsRef<Path>>(temp_dir: T) -> SnapboxCommand {
37+
if cfg!(feature = "run-native") {
38+
test_runner_native(temp_dir)
39+
} else {
40+
test_runner_vm(temp_dir)
41+
}
42+
}
43+
44+
/// Returns a command that runs `snforge test --run-native` in the given temporary directory.
45+
///
46+
/// This is useful for testing behavior that occurs only when the `--run-native` flag is passed.
47+
/// If the behavior is not specific to native execution, use `test_runner` instead.
48+
pub(crate) fn test_runner_native<T: AsRef<Path>>(temp_dir: T) -> SnapboxCommand {
49+
runner(temp_dir).arg("test").arg("--run-native")
50+
}
51+
52+
/// Returns a command that runs `snforge test` in the given temporary directory.
53+
///
54+
/// This is useful for testing behavior that occurs only in the VM execution.
55+
/// If the behavior is not specific to VM execution, use `test_runner` instead.
56+
pub(crate) fn test_runner_vm<T: AsRef<Path>>(temp_dir: T) -> SnapboxCommand {
3557
runner(temp_dir).arg("test")
3658
}
3759

0 commit comments

Comments
 (0)