Skip to content

Commit 5c94559

Browse files
committed
Use snforge_std or snforge_std_compatibility in snforge new
Closes #3461 commit-id:182fb50c
1 parent 9276fc4 commit 5c94559

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
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_snforge_std_compatibility
138139

139140
test-forge-runner:
140141
name: Test Forge Runner

crates/forge/src/new.rs

Lines changed: 15 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,8 @@ pub fn new(
323333
);
324334
}
325335
let name = infer_name(name, &path)?;
336+
let scarb_version = ScarbCommand::version().run()?.scarb;
337+
println!("Scarb version {scarb_version}");
326338

327339
fs::create_dir_all(&path)?;
328340
let project_path = path.canonicalize()?;
@@ -361,7 +373,7 @@ pub fn new(
361373
}
362374

363375
let template_config = TemplateManifestConfig::try_from(&template)?;
364-
template_config.add_dependencies(&scarb_manifest_path)?;
376+
template_config.add_dependencies(&scarb_version, &scarb_manifest_path)?;
365377
template_config.update_config(&scarb_manifest_path)?;
366378

367379
let template_dir = get_template_dir(&template)?;

crates/forge/tests/e2e/new.rs

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn init_new_project() {
4343
),
4444
);
4545

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

4949
#[test_case(&Template::CairoProgram; "cairo-program")]
@@ -66,7 +66,7 @@ fn create_new_project_dir_not_exist(template: &Template) {
6666
.assert()
6767
.success();
6868

69-
validate_init(&project_path, false, template);
69+
validate_init(&project_path, None, template);
7070
}
7171

7272
#[test]
@@ -105,7 +105,7 @@ fn create_new_project_dir_exists_and_empty() {
105105
.assert()
106106
.success();
107107

108-
validate_init(&project_path, false, &Template::BalanceContract);
108+
validate_init(&project_path, None, &Template::BalanceContract);
109109
}
110110

111111
#[test]
@@ -123,17 +123,49 @@ fn init_new_project_from_scarb() {
123123
.assert()
124124
.success();
125125

126-
validate_init(&temp.join("test_name"), true, &Template::BalanceContract);
126+
validate_init(
127+
&temp.join("test_name"),
128+
Some(SnforgeStd::Normal),
129+
&Template::BalanceContract,
130+
);
127131
}
128132

133+
#[test]
134+
#[cfg_attr(not(feature = "scarb_2_9_1"), ignore)]
135+
fn init_new_project_from_scarb_snforge_std_compatibility() {
136+
let temp = tempdir_with_tool_versions().unwrap();
137+
let tool_version_path = temp.join(".tool-versions");
138+
fs::write(tool_version_path, "scarb 2.11.4").unwrap();
139+
140+
SnapboxCommand::new("scarb")
141+
.current_dir(&temp)
142+
.args(["new", "test_name"])
143+
.env("SCARB_INIT_TEST_RUNNER", "starknet-foundry")
144+
.env(
145+
"PATH",
146+
append_to_path_var(snforge_test_bin_path().parent().unwrap()),
147+
)
148+
.assert()
149+
.success();
150+
151+
validate_init(
152+
&temp.join("test_name"),
153+
Some(SnforgeStd::Compatibility),
154+
&Template::BalanceContract,
155+
);
156+
}
129157
pub fn append_to_path_var(path: &Path) -> OsString {
130158
let script_path = iter::once(path.to_path_buf());
131159
let os_path = env::var_os("PATH").unwrap();
132160
let other_paths = env::split_paths(&os_path);
133161
env::join_paths(script_path.chain(other_paths)).unwrap()
134162
}
135163

136-
fn validate_init(project_path: &PathBuf, validate_snforge_std: bool, template: &Template) {
164+
fn validate_init(
165+
project_path: &PathBuf,
166+
validate_snforge_std: Option<SnforgeStd>,
167+
template: &Template,
168+
) {
137169
let manifest_path = project_path.join("Scarb.toml");
138170
let scarb_toml = fs::read_to_string(manifest_path.clone()).unwrap();
139171

@@ -160,7 +192,7 @@ fn validate_init(project_path: &PathBuf, validate_snforge_std: bool, template: &
160192
dependencies.remove("snforge_std");
161193
dependencies.insert("snforge_std", Item::Value(Value::InlineTable(snforge_std)));
162194

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

165197
let output = test_runner(&TempDir::new().unwrap())
166198
.current_dir(project_path)
@@ -171,11 +203,21 @@ fn validate_init(project_path: &PathBuf, validate_snforge_std: bool, template: &
171203
assert_stdout_contains(output, expected);
172204
}
173205

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-
""
206+
enum SnforgeStd {
207+
Normal,
208+
Compatibility,
209+
}
210+
211+
fn get_expected_manifest_content(
212+
template: &Template,
213+
validate_snforge_std: Option<SnforgeStd>,
214+
) -> String {
215+
let snforge_std_assert = match validate_snforge_std {
216+
None => "",
217+
Some(snforge_std) => match snforge_std {
218+
SnforgeStd::Normal => "\nsnforge_std = \"[..]\"",
219+
SnforgeStd::Compatibility => "\nsnforge_std_compatibility = \"[..]\"",
220+
},
179221
};
180222

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

0 commit comments

Comments
 (0)