Skip to content

Commit 90c05d3

Browse files
committed
Use snforge_std or snforge_std_compatibility in snforge new
Closes #3461 commit-id:182fb50c
1 parent 4807f29 commit 90c05d3

File tree

3 files changed

+86
-25
lines changed

3 files changed

+86
-25
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ jobs:
135135
- run: cargo test --package forge --features scarb_2_9_1 --test main e2e::requirements::test_warning_on_scarb_version_below_recommended
136136
- run: cargo test --package forge --features scarb_2_9_1 --lib compatibility_check::tests::warning_requirements
137137
- run: cargo test --package forge --features scarb_2_9_1 --test main e2e::running::sierra_gas_with_older_scarb
138+
- run: cargo test --package forge --features scarb_2_9_1 --test main e2e::new::init_new_project_from_scarb_with_snforge_std_compatibility
138139

139140
# TODO(#3212): Closures in Cairo are fully supported since version 2.11
140141
test-interact-with-state:

crates/forge/src/new.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,21 @@ struct TemplateManifestConfig {
4848
}
4949

5050
impl TemplateManifestConfig {
51-
fn add_dependencies(&self, scarb_manifest_path: &PathBuf) -> Result<()> {
51+
fn add_dependencies(
52+
&self,
53+
scarb_version: &Version,
54+
scarb_manifest_path: &PathBuf,
55+
) -> Result<()> {
5256
if env::var("DEV_DISABLE_SNFORGE_STD_DEPENDENCY").is_err() {
57+
// TODO: Check Scarb `2.12`
58+
let snforge_package = if scarb_version.build.is_empty() {
59+
"snforge_std_compatibility"
60+
} else {
61+
"snforge_std"
62+
};
5363
let snforge_version = env!("CARGO_PKG_VERSION");
5464
Dependency {
55-
name: "snforge_std".to_string(),
65+
name: snforge_package.to_string(),
5666
version: snforge_version.to_string(),
5767
dev: true,
5868
}
@@ -323,6 +333,7 @@ pub fn new(
323333
);
324334
}
325335
let name = infer_name(name, &path)?;
336+
let scarb_version = ScarbCommand::version().run()?.scarb;
326337

327338
fs::create_dir_all(&path)?;
328339
let project_path = path.canonicalize()?;
@@ -361,7 +372,7 @@ pub fn new(
361372
}
362373

363374
let template_config = TemplateManifestConfig::try_from(&template)?;
364-
template_config.add_dependencies(&scarb_manifest_path)?;
375+
template_config.add_dependencies(&scarb_version, &scarb_manifest_path)?;
365376
template_config.update_config(&scarb_manifest_path)?;
366377

367378
let template_dir = get_template_dir(&template)?;

crates/forge/tests/e2e/new.rs

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use forge::Template;
99
use forge::scarb::config::SCARB_MANIFEST_TEMPLATE_CONTENT;
1010
use indoc::{formatdoc, indoc};
1111
use regex::Regex;
12+
use scarb_api::ScarbCommand;
1213
use shared::consts::FREE_RPC_PROVIDER_URL;
1314
use shared::test_utils::output_assert::assert_stdout_contains;
1415
use snapbox::assert_matches;
@@ -43,7 +44,7 @@ fn init_new_project() {
4344
),
4445
);
4546

46-
validate_init(&temp.join("test_name"), false, &Template::BalanceContract);
47+
validate_init(&temp.join("test_name"), None, &Template::BalanceContract);
4748
}
4849

4950
#[test_case(&Template::CairoProgram; "cairo-program")]
@@ -66,7 +67,7 @@ fn create_new_project_dir_not_exist(template: &Template) {
6667
.assert()
6768
.success();
6869

69-
validate_init(&project_path, false, template);
70+
validate_init(&project_path, None, template);
7071
}
7172

7273
#[test]
@@ -105,35 +106,73 @@ fn create_new_project_dir_exists_and_empty() {
105106
.assert()
106107
.success();
107108

108-
validate_init(&project_path, false, &Template::BalanceContract);
109+
validate_init(&project_path, None, &Template::BalanceContract);
109110
}
110111

111112
#[test]
112113
fn init_new_project_from_scarb() {
113114
let temp = tempdir_with_tool_versions().unwrap();
114115

115-
SnapboxCommand::new("scarb")
116-
.current_dir(&temp)
117-
.args(["new", "test_name"])
118-
.env("SCARB_INIT_TEST_RUNNER", "starknet-foundry")
119-
.env(
120-
"PATH",
121-
append_to_path_var(snforge_test_bin_path().parent().unwrap()),
122-
)
123-
.assert()
124-
.success();
125-
126-
validate_init(&temp.join("test_name"), true, &Template::BalanceContract);
116+
SnapboxCommand::from_std(
117+
ScarbCommand::new()
118+
.current_dir(temp.path())
119+
.args(["new", "test_name"])
120+
.env("SCARB_INIT_TEST_RUNNER", "starknet-foundry")
121+
.env(
122+
"PATH",
123+
append_to_path_var(snforge_test_bin_path().parent().unwrap()),
124+
)
125+
.command(),
126+
)
127+
.assert()
128+
.success();
129+
130+
validate_init(
131+
&temp.join("test_name"),
132+
Some(SnforgeStd::Normal),
133+
&Template::BalanceContract,
134+
);
127135
}
128136

137+
#[test]
138+
#[cfg_attr(not(feature = "scarb_2_9_1"), ignore)]
139+
fn init_new_project_from_scarb_with_snforge_std_compatibility() {
140+
let temp = tempdir_with_tool_versions().unwrap();
141+
let tool_version_path = temp.join(".tool-versions");
142+
fs::write(tool_version_path, "scarb 2.11.4").unwrap();
143+
144+
SnapboxCommand::from_std(
145+
ScarbCommand::new()
146+
.current_dir(temp.path())
147+
.args(["new", "test_name"])
148+
.env("SCARB_INIT_TEST_RUNNER", "starknet-foundry")
149+
.env(
150+
"PATH",
151+
append_to_path_var(snforge_test_bin_path().parent().unwrap()),
152+
)
153+
.command(),
154+
)
155+
.assert()
156+
.success();
157+
158+
validate_init(
159+
&temp.join("test_name"),
160+
Some(SnforgeStd::Compatibility),
161+
&Template::BalanceContract,
162+
);
163+
}
129164
pub fn append_to_path_var(path: &Path) -> OsString {
130165
let script_path = iter::once(path.to_path_buf());
131166
let os_path = env::var_os("PATH").unwrap();
132167
let other_paths = env::split_paths(&os_path);
133168
env::join_paths(script_path.chain(other_paths)).unwrap()
134169
}
135170

136-
fn validate_init(project_path: &PathBuf, validate_snforge_std: bool, template: &Template) {
171+
fn validate_init(
172+
project_path: &PathBuf,
173+
validate_snforge_std: Option<SnforgeStd>,
174+
template: &Template,
175+
) {
137176
let manifest_path = project_path.join("Scarb.toml");
138177
let scarb_toml = fs::read_to_string(manifest_path.clone()).unwrap();
139178

@@ -160,7 +199,7 @@ fn validate_init(project_path: &PathBuf, validate_snforge_std: bool, template: &
160199
dependencies.remove("snforge_std");
161200
dependencies.insert("snforge_std", Item::Value(Value::InlineTable(snforge_std)));
162201

163-
std::fs::write(manifest_path, scarb_toml.to_string()).unwrap();
202+
fs::write(manifest_path, scarb_toml.to_string()).unwrap();
164203

165204
let output = test_runner(&TempDir::new().unwrap())
166205
.current_dir(project_path)
@@ -171,11 +210,21 @@ fn validate_init(project_path: &PathBuf, validate_snforge_std: bool, template: &
171210
assert_stdout_contains(output, expected);
172211
}
173212

174-
fn get_expected_manifest_content(template: &Template, validate_snforge_std: bool) -> String {
175-
let snforge_std_assert = if validate_snforge_std {
176-
"\nsnforge_std = \"[..]\""
177-
} else {
178-
""
213+
enum SnforgeStd {
214+
Normal,
215+
Compatibility,
216+
}
217+
218+
fn get_expected_manifest_content(
219+
template: &Template,
220+
validate_snforge_std: Option<SnforgeStd>,
221+
) -> String {
222+
let snforge_std_assert = match validate_snforge_std {
223+
None => "",
224+
Some(snforge_std) => match snforge_std {
225+
SnforgeStd::Normal => "\nsnforge_std = \"[..]\"",
226+
SnforgeStd::Compatibility => "\nsnforge_std_compatibility = \"[..]\"",
227+
},
179228
};
180229

181230
let target_contract_entry = "[[target.starknet-contract]]\nsierra = true";

0 commit comments

Comments
 (0)