Skip to content

Commit 1557545

Browse files
Redesigned init_new_project_test (#2400)
Closes [#2274](#2274 (comment)), Closes [#2393](#2393) ## Introduced changes - `init_new_project_test` only expects `snforge test` to pass on a newly created project and no longer asserts concrete Starknet/Foundry versions - `test_init_project_with_custom_snforge_dependency_git` tests downloading dustom `snforge_std` dependency from a remote branch, as `init_new_project_test` would before. The test is marked with `#cfg[feature = "smoke"]` so it can be ran conditionally, e.g. on CI or smoke tests ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [ ] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md`
1 parent 4286bf7 commit 1557545

File tree

6 files changed

+134
-63
lines changed

6 files changed

+134
-63
lines changed

Cargo.lock

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

crates/forge/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ edition.workspace = true
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

8+
[features]
9+
smoke = []
10+
811
[dependencies]
912
anyhow.workspace = true
1013
blockifier.workspace = true
@@ -61,6 +64,7 @@ num-integer.workspace = true
6164
url.workspace = true
6265
trace-data.workspace = true
6366
fs_extra.workspace = true
67+
project-root.workspace = true
6468

6569
[[bin]]
6670
name = "snforge"

crates/forge/src/init.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::CAIRO_EDITION;
22
use anyhow::{anyhow, Context, Ok, Result};
33
use include_dir::{include_dir, Dir};
44
use scarb_api::ScarbCommand;
5+
use std::env;
56
use std::fs::{self, OpenOptions};
67
use std::io::Write;
78
use std::path::Path;
@@ -103,8 +104,6 @@ pub fn run(project_name: &str) -> Result<()> {
103104
.context("Failed to initialize a new project")?;
104105
}
105106

106-
let version = env!("CARGO_PKG_VERSION");
107-
108107
ScarbCommand::new_with_stdio()
109108
.current_dir(&project_path)
110109
.manifest_path(manifest_path.clone())
@@ -115,19 +114,23 @@ pub fn run(project_name: &str) -> Result<()> {
115114
.run()
116115
.context("Failed to remove cairo_test")?;
117116

118-
ScarbCommand::new_with_stdio()
119-
.current_dir(&project_path)
120-
.manifest_path(manifest_path.clone())
121-
.offline()
122-
.arg("add")
123-
.arg("--dev")
124-
.arg("snforge_std")
125-
.arg("--git")
126-
.arg("https://github.com/foundry-rs/starknet-foundry.git")
127-
.arg("--tag")
128-
.arg(format!("v{version}"))
129-
.run()
130-
.context("Failed to add snforge_std")?;
117+
let version = env!("CARGO_PKG_VERSION");
118+
119+
if env::var("DEV_DISABLE_SNFORGE_STD_DEPENDENCY").is_err() {
120+
ScarbCommand::new_with_stdio()
121+
.current_dir(&project_path)
122+
.manifest_path(manifest_path.clone())
123+
.offline()
124+
.arg("add")
125+
.arg("--dev")
126+
.arg("snforge_std")
127+
.arg("--git")
128+
.arg("https://github.com/foundry-rs/starknet-foundry.git")
129+
.arg("--tag")
130+
.arg(format!("v{version}"))
131+
.run()
132+
.context("Failed to add snforge_std")?;
133+
}
131134

132135
let cairo_version = ScarbCommand::version().run()?.cairo;
133136

crates/forge/test_utils/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
pub mod runner;
22
pub mod running_tests;
33

4+
use std::path::PathBuf;
5+
46
use anyhow::Result;
57
use assert_fs::fixture::PathCopy;
8+
use project_root::get_project_root;
9+
10+
pub fn get_local_snforge_std_absolute_path() -> Result<PathBuf> {
11+
Ok(get_project_root()?.canonicalize()?.join("snforge_std"))
12+
}
613

714
pub fn tempdir_with_tool_versions() -> Result<assert_fs::TempDir> {
8-
let project_root = project_root::get_project_root()?;
15+
let project_root = get_project_root()?;
916
let temp_dir = assert_fs::TempDir::new()?;
1017
temp_dir.copy_from(project_root, &[".tool-versions"])?;
1118
Ok(temp_dir)

crates/forge/tests/e2e/running.rs

Lines changed: 98 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use camino::Utf8PathBuf;
66
use forge::CAIRO_EDITION;
77
use indoc::{formatdoc, indoc};
88
use shared::test_utils::output_assert::assert_stdout_contains;
9-
use std::{env, fs, path::Path, str::FromStr};
10-
use test_utils::tempdir_with_tool_versions;
11-
use toml_edit::{value, DocumentMut, Item};
9+
use snapbox::assert_matches;
10+
use std::{fs, path::Path, str::FromStr};
11+
use test_utils::{get_local_snforge_std_absolute_path, tempdir_with_tool_versions};
12+
use toml_edit::{value, DocumentMut, Formatted, InlineTable, Item, Value};
1213

1314
#[test]
1415
fn simple_package() {
@@ -655,90 +656,143 @@ fn with_exit_first_flag() {
655656
);
656657
}
657658

658-
// TODO (2274): This test has inherently flawed logic, needs to be re-written
659-
#[ignore]
660659
#[test]
661660
fn init_new_project_test() {
662661
let temp = tempdir_with_tool_versions().unwrap();
663662

664-
runner(&temp).args(["init", "test_name"]).assert().success();
665-
let manifest_path = temp.child("test_name/Scarb.toml");
663+
runner(&temp)
664+
.args(["init", "test_name"])
665+
.env("DEV_DISABLE_SNFORGE_STD_DEPENDENCY", "true")
666+
.assert()
667+
.success();
668+
669+
let manifest_path = temp.join("test_name/Scarb.toml");
670+
let scarb_toml = std::fs::read_to_string(manifest_path.clone()).unwrap();
666671

667-
let generated_toml = std::fs::read_to_string(manifest_path.path()).unwrap();
668-
let version = env!("CARGO_PKG_VERSION");
669-
let expected_toml = formatdoc!(
672+
let expected = formatdoc!(
670673
r#"
671674
[package]
672675
name = "test_name"
673676
version = "0.1.0"
674-
edition = "{}"
677+
edition = "{CAIRO_EDITION}"
675678
676679
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html
677680
678681
[dependencies]
679-
starknet = "2.6.4"
682+
starknet = "[..]"
680683
681684
[dev-dependencies]
682-
snforge_std = {{ git = "https://github.com/foundry-rs/starknet-foundry", tag = "v{}" }}
683685
684686
[[target.starknet-contract]]
685687
sierra = true
686688
687689
[scripts]
688690
test = "snforge test"
689-
"#,
690-
CAIRO_EDITION,
691-
version,
691+
"#
692692
);
693693

694-
assert_eq!(generated_toml, expected_toml);
694+
assert_matches(&expected, &scarb_toml);
695695

696-
let remote_url = get_remote_url();
697-
let branch = get_current_branch();
698-
manifest_path
699-
.write_str(&formatdoc!(
700-
r#"
701-
[package]
702-
name = "test_name"
703-
version = "0.1.0"
696+
let mut scarb_toml = DocumentMut::from_str(&scarb_toml).unwrap();
704697

705-
[[target.starknet-contract]]
698+
let dependencies = scarb_toml
699+
.get_mut("dev-dependencies")
700+
.unwrap()
701+
.as_table_mut()
702+
.unwrap();
706703

707-
[dependencies]
708-
starknet = "2.5.4"
704+
let local_snforge_std = get_local_snforge_std_absolute_path()
705+
.unwrap()
706+
.to_str()
707+
.unwrap()
708+
.to_string();
709709

710-
[dev-dependencies]
711-
snforge_std = {{ git = "https://github.com/{}", branch = "{}" }}
712-
"#,
713-
remote_url,
714-
branch
715-
))
716-
.unwrap();
710+
let mut snforge_std = InlineTable::new();
711+
snforge_std.insert("path", Value::String(Formatted::new(local_snforge_std)));
712+
713+
dependencies.remove("snforge_std");
714+
dependencies.insert("snforge_std", Item::Value(Value::InlineTable(snforge_std)));
715+
716+
std::fs::write(manifest_path, scarb_toml.to_string()).unwrap();
717717

718-
// Check if template works with current version of snforge_std
719718
let output = test_runner(&temp)
720719
.current_dir(temp.child(Path::new("test_name")))
721720
.assert()
722721
.success();
723-
assert_stdout_contains(
724-
output,
725-
formatdoc!(
726-
r"
727-
[..]Updating git repository https://github.com/{}
722+
723+
let expected = indoc!(
724+
r"
728725
[..]Compiling test_name v0.1.0[..]
729726
[..]Finished[..]
730727
728+
Collected 2 test(s) from test_name package
729+
Running 0 test(s) from src/
730+
Running 2 test(s) from tests/
731+
[PASS] test_name_integrationtest::test_contract::test_increase_balance [..]
732+
[PASS] test_name_integrationtest::test_contract::test_cannot_increase_balance_with_zero_value [..]
733+
Tests: 2 passed, 0 failed, 0 skipped, 0 ignored, 0 filtered out
734+
"
735+
);
736+
737+
assert_stdout_contains(output, expected);
738+
}
739+
740+
#[test]
741+
#[cfg(feature = "smoke")]
742+
fn test_init_project_with_custom_snforge_dependency_git() {
743+
let temp = tempdir_with_tool_versions().unwrap();
744+
745+
runner(&temp)
746+
.args(["init", "test_name"])
747+
.env("DEV_DISABLE_SNFORGE_STD_DEPENDENCY", "true")
748+
.assert()
749+
.success();
750+
751+
let manifest_path = temp.child("test_name/Scarb.toml");
752+
753+
let scarb_toml = std::fs::read_to_string(manifest_path.path()).unwrap();
754+
let mut scarb_toml = DocumentMut::from_str(&scarb_toml).unwrap();
755+
756+
let dependencies = scarb_toml
757+
.get_mut("dev-dependencies")
758+
.unwrap()
759+
.as_table_mut()
760+
.unwrap();
761+
762+
let branch = get_current_branch();
763+
let remote_url = format!("https://github.com/{}", get_remote_url());
764+
765+
let mut snforge_std = InlineTable::new();
766+
snforge_std.insert("git", Value::String(Formatted::new(remote_url.clone())));
767+
snforge_std.insert("branch", Value::String(Formatted::new(branch)));
768+
769+
dependencies.remove("snforge_std");
770+
dependencies.insert("snforge_std", Item::Value(Value::InlineTable(snforge_std)));
771+
772+
std::fs::write(manifest_path.path(), scarb_toml.to_string()).unwrap();
773+
774+
let output = test_runner(&temp)
775+
.current_dir(temp.child(Path::new("test_name")))
776+
.assert()
777+
.success();
778+
779+
let expected = formatdoc!(
780+
r"
781+
[..]Updating git repository {}
782+
[..]Compiling test_name v0.1.0[..]
783+
[..]Finished[..]
731784
732785
Collected 2 test(s) from test_name package
733786
Running 0 test(s) from src/
734787
Running 2 test(s) from tests/
735788
[PASS] test_name_integrationtest::test_contract::test_increase_balance [..]
736789
[PASS] test_name_integrationtest::test_contract::test_cannot_increase_balance_with_zero_value [..]
737790
Tests: 2 passed, 0 failed, 0 skipped, 0 ignored, 0 filtered out
738-
",
739-
remote_url.trim_end_matches(".git")
740-
),
791+
",
792+
remote_url.trim_end_matches(".git")
741793
);
794+
795+
assert_stdout_contains(output, expected);
742796
}
743797

744798
#[test]

crates/scarb-api/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ pub fn package_matches_version_requirement(
202202
.filter(|package| package.name == name);
203203

204204
match (packages.next(), packages.next()) {
205-
(None, None) => Ok(true),
206205
(Some(package), None) => Ok(version_req.matches(&package.version)),
206+
(None, None) => Err(anyhow!("Package {name} is not present in dependencies.")),
207207
_ => Err(anyhow!("Package {name} is duplicated in dependencies")),
208208
}
209209
}
@@ -246,7 +246,7 @@ mod tests {
246246
[dependencies]
247247
starknet = "2.4.0"
248248
snforge_std = {{ path = "{}" }}
249-
249+
250250
[[target.starknet-contract]]
251251
252252
[[tool.snforge.fork]]
@@ -328,12 +328,14 @@ mod tests {
328328
&VersionReq::parse("2.5").unwrap(),
329329
)
330330
.unwrap());
331+
331332
assert!(package_matches_version_requirement(
332333
&scarb_metadata,
333334
"not_existing",
334335
&VersionReq::parse("2.5").unwrap(),
335336
)
336-
.unwrap());
337+
.is_err());
338+
337339
assert!(!package_matches_version_requirement(
338340
&scarb_metadata,
339341
"starknet",

0 commit comments

Comments
 (0)