Skip to content

Commit d6bb60a

Browse files
authored
Fix snforge new (#3906)
commit-id:13eded66 --- **Stack**: - #3907 - #3906⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
1 parent 16f5708 commit d6bb60a

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

crates/forge/src/new.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static TEMPLATES_DIR: Dir = include_dir!("snforge_templates");
1717

1818
const DEFAULT_ASSERT_MACROS: Version = Version::new(0, 1, 0);
1919
const MINIMAL_SCARB_FOR_CORRESPONDING_ASSERT_MACROS: Version = Version::new(2, 8, 0);
20+
const SCARB_WITHOUT_CAIRO_TEST_TEMPLATE: Version = Version::new(2, 13, 0);
2021

2122
struct Dependency {
2223
name: String,
@@ -263,8 +264,9 @@ fn add_assert_macros(document: &mut DocumentMut) -> Result<()> {
263264
};
264265

265266
document
266-
.get_mut("dev-dependencies")
267-
.and_then(|dep| dep.as_table_mut())
267+
.entry("dev-dependencies")
268+
.or_insert(Item::Table(Table::new()))
269+
.as_table_mut()
268270
.context("Failed to get dev-dependencies from Scarb.toml")?
269271
.insert("assert_macros", value(version.to_string()));
270272

@@ -377,19 +379,29 @@ pub fn new(
377379
cmd.arg("--no-vcs");
378380
}
379381

380-
cmd.env("SCARB_INIT_TEST_RUNNER", "cairo-test")
382+
// TODO(#3910)
383+
let test_runner = if scarb_version < SCARB_WITHOUT_CAIRO_TEST_TEMPLATE {
384+
"cairo-test"
385+
} else {
386+
"none"
387+
};
388+
389+
cmd.env("SCARB_INIT_TEST_RUNNER", test_runner)
381390
.run()
382391
.context("Failed to initialize a new project")?;
383392

384-
ScarbCommand::new_with_stdio()
385-
.current_dir(&project_path)
386-
.manifest_path(scarb_manifest_path.clone())
387-
.offline()
388-
.arg("remove")
389-
.arg("--dev")
390-
.arg("cairo_test")
391-
.run()
392-
.context("Failed to remove cairo_test dependency")?;
393+
// TODO(#3910)
394+
if scarb_version < SCARB_WITHOUT_CAIRO_TEST_TEMPLATE {
395+
ScarbCommand::new_with_stdio()
396+
.current_dir(&project_path)
397+
.manifest_path(scarb_manifest_path.clone())
398+
.offline()
399+
.arg("remove")
400+
.arg("--dev")
401+
.arg("cairo_test")
402+
.run()
403+
.context("Failed to remove cairo_test dependency")?;
404+
}
393405
}
394406

395407
add_template_to_scarb_manifest(&scarb_manifest_path)?;

crates/forge/tests/e2e/new.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use forge::CAIRO_EDITION;
55
use forge::Template;
66
use forge::scarb::config::SCARB_MANIFEST_TEMPLATE_CONTENT;
77
use indoc::{formatdoc, indoc};
8+
use itertools::Itertools;
89
use regex::Regex;
910
use scarb_api::ScarbCommand;
1011
use shared::test_utils::output_assert::assert_stdout_contains;
@@ -117,7 +118,7 @@ fn validate_init(project_path: &PathBuf, validate_snforge_std: bool, template: &
117118
let scarb_toml = fs::read_to_string(manifest_path.clone()).unwrap();
118119

119120
let expected = get_expected_manifest_content(template, validate_snforge_std);
120-
assert_matches(&expected, &scarb_toml);
121+
assert_manifest_matches(&expected, &scarb_toml);
121122

122123
let mut scarb_toml = DocumentMut::from_str(&scarb_toml).unwrap();
123124

@@ -331,3 +332,20 @@ fn create_new_project_and_check_gitignore() {
331332

332333
assert_eq!(gitignore_content, expected_gitignore_content);
333334
}
335+
336+
/// Asserts that two manifest contents match, ignoring the order of lines.
337+
///
338+
/// # Why Line Order Can Vary
339+
///
340+
/// Starting from Scarb version 2.13, `scarb init` no longer inserts the `[dev-dependencies]` section.
341+
/// When tools like `forge new` generate a new project, they insert this section later
342+
/// in the manifest. As a result, the relative placement of `[dev-dependencies]` and other sections
343+
/// might differ depending on the version of Scarb used.
344+
fn assert_manifest_matches(expected: &str, actual: &str) {
345+
// TODO(#3910)
346+
fn sort_lines(s: &str) -> String {
347+
s.lines().sorted().join("\n")
348+
}
349+
350+
assert_matches(sort_lines(expected), sort_lines(actual));
351+
}

0 commit comments

Comments
 (0)