From a098a5b4f2607362ea2b036000056149604ded95 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Thu, 22 Feb 2024 14:29:53 -0500 Subject: [PATCH 1/7] Add testutils features to contract Cargo.toml --- .../contract-init-template/contracts/hello_world/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/soroban-cli/src/utils/contract-init-template/contracts/hello_world/Cargo.toml b/cmd/soroban-cli/src/utils/contract-init-template/contracts/hello_world/Cargo.toml index 3353ae18c..2b49f9fa0 100644 --- a/cmd/soroban-cli/src/utils/contract-init-template/contracts/hello_world/Cargo.toml +++ b/cmd/soroban-cli/src/utils/contract-init-template/contracts/hello_world/Cargo.toml @@ -15,4 +15,4 @@ doctest = false soroban-sdk = { workspace = true } [dev_dependencies] -soroban-sdk = { workspace = true } +soroban-sdk = { workspace = true, features = ["testutils"] } \ No newline at end of file From 24af9cde0fa7cbfef37c27826f1e89ac240550d3 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Fri, 23 Feb 2024 13:01:12 -0500 Subject: [PATCH 2/7] Allow init to be called from within an existing directory --- cmd/soroban-cli/src/commands/contract/init.rs | 59 ++++++++++++++++--- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/init.rs b/cmd/soroban-cli/src/commands/contract/init.rs index 26b1535f3..e7682e2dc 100644 --- a/cmd/soroban-cli/src/commands/contract/init.rs +++ b/cmd/soroban-cli/src/commands/contract/init.rs @@ -1,4 +1,5 @@ use std::{ + env, ffi::OsStr, fs::{copy, create_dir_all, metadata, read_dir, read_to_string, write}, io, @@ -387,12 +388,22 @@ fn copy_frontend_files(from: &Path, to: &Path) -> Result<(), Error> { } fn edit_package_json_files(project_path: &Path) -> Result<(), Error> { - let package_name = project_path.file_name().unwrap(); - edit_package_name(project_path, package_name, "package.json").map_err(|e| { + let package_name = if let Some(name) = project_path.file_name() { + name.to_owned() + } else { + let current_dir = env::current_dir()?; + let file_name = current_dir + .file_name() + .unwrap_or(OsStr::new("soroban-astro-template")) + .to_os_string(); + file_name + }; + + edit_package_name(project_path, &package_name, "package.json").map_err(|e| { eprintln!("Error editing package.json file in: {project_path:?}"); e })?; - edit_package_name(project_path, package_name, "package-lock.json") + edit_package_name(project_path, &package_name, "package-lock.json") } fn edit_package_name( @@ -529,7 +540,37 @@ mod tests { assert_astro_files_exist(&project_dir); assert_gitignore_includes_astro_paths(&project_dir); - assert_package_json_files_have_correct_name(&project_dir); + assert_package_json_files_have_correct_name(&project_dir, TEST_PROJECT_NAME); + + temp_dir.close().unwrap(); + } + + #[test] + fn test_init_from_within_an_existing_project() { + let temp_dir = tempfile::tempdir().unwrap(); + let project_dir = temp_dir.path().join("./"); + let with_examples = vec![]; + init( + project_dir.as_path(), + &"https://github.com/AhaLabs/soroban-astro-template".to_string(), + &with_examples, + ) + .unwrap(); + + assert_base_template_files_exist(&project_dir); + assert_default_hello_world_contract_files_exist(&project_dir); + assert_base_excluded_paths_do_not_exist(&project_dir); + + // check that the contract's Cargo.toml file uses the workspace for dependencies + assert_contract_cargo_file_uses_workspace(&project_dir, "hello_world"); + assert_base_excluded_paths_do_not_exist(&project_dir); + + assert_astro_files_exist(&project_dir); + assert_gitignore_includes_astro_paths(&project_dir); + assert_package_json_files_have_correct_name( + &project_dir, + &project_dir.file_name().unwrap().to_string_lossy(), + ); temp_dir.close().unwrap(); } @@ -595,13 +636,17 @@ mod tests { assert!(project_dir.join("tsconfig.json").exists()); } - fn assert_package_json_files_have_correct_name(project_dir: &Path) { + fn assert_package_json_files_have_correct_name( + project_dir: &Path, + expected_package_name: &str, + ) { let package_json_path = project_dir.join("package.json"); let package_json_str = read_to_string(package_json_path).unwrap(); - assert!(package_json_str.contains(&format!("\"name\":\"{TEST_PROJECT_NAME}\""))); + println!("package json: {package_json_str} "); + assert!(package_json_str.contains(&format!("\"name\":\"{expected_package_name}\""))); let package_lock_json_path = project_dir.join("package-lock.json"); let package_lock_json_str = read_to_string(package_lock_json_path).unwrap(); - assert!(package_lock_json_str.contains(&format!("\"name\":\"{TEST_PROJECT_NAME}\""))); + assert!(package_lock_json_str.contains(&format!("\"name\":\"{expected_package_name}\""))); } } From 13e18a29eb08f0aa19bdc0a5a1067aeeceab180d Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Fri, 23 Feb 2024 13:07:48 -0500 Subject: [PATCH 3/7] Write the package.json and package-lock.json files with pretty json formatting --- cmd/soroban-cli/src/commands/contract/init.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/init.rs b/cmd/soroban-cli/src/commands/contract/init.rs index e7682e2dc..ed3c0a635 100644 --- a/cmd/soroban-cli/src/commands/contract/init.rs +++ b/cmd/soroban-cli/src/commands/contract/init.rs @@ -15,7 +15,8 @@ use clap::{ }; use gix::{clone, create, open, progress, remote}; use rust_embed::RustEmbed; -use serde_json::{from_str, json, Error as JsonError, Value as JsonValue}; +use serde::Deserialize; +use serde_json::{from_str, json, to_string_pretty, Error as JsonError, Value as JsonValue}; use toml_edit::{Document, Formatted, InlineTable, Item, TomlError, Value as TomlValue}; use ureq::{get, Error as UreqError}; @@ -421,7 +422,9 @@ fn edit_package_name( doc["name"] = json!(package_name.to_string_lossy()); - write(&file_path, doc.to_string())?; + let formatted_json = to_string_pretty(&doc)?; + + write(&file_path, formatted_json)?; Ok(()) } From e22a959619784b2df10ffba84f5c287a52b43f81 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Mon, 26 Feb 2024 12:30:54 -0500 Subject: [PATCH 4/7] Add .soroban/contract-ids dir to init template --- .../utils/contract-init-template/.soroban/contract-ids/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 cmd/soroban-cli/src/utils/contract-init-template/.soroban/contract-ids/.gitkeep diff --git a/cmd/soroban-cli/src/utils/contract-init-template/.soroban/contract-ids/.gitkeep b/cmd/soroban-cli/src/utils/contract-init-template/.soroban/contract-ids/.gitkeep new file mode 100644 index 000000000..e69de29bb From 4992f9f5f4d4df3632827178231f1309e4b1a93d Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Wed, 28 Feb 2024 11:28:31 -0500 Subject: [PATCH 5/7] Fix init tests --- cmd/soroban-cli/src/commands/contract/init.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/init.rs b/cmd/soroban-cli/src/commands/contract/init.rs index ed3c0a635..3555375d8 100644 --- a/cmd/soroban-cli/src/commands/contract/init.rs +++ b/cmd/soroban-cli/src/commands/contract/init.rs @@ -645,11 +645,10 @@ mod tests { ) { let package_json_path = project_dir.join("package.json"); let package_json_str = read_to_string(package_json_path).unwrap(); - println!("package json: {package_json_str} "); - assert!(package_json_str.contains(&format!("\"name\":\"{expected_package_name}\""))); + assert!(package_json_str.contains(&format!("\"name\": \"{expected_package_name}\""))); let package_lock_json_path = project_dir.join("package-lock.json"); let package_lock_json_str = read_to_string(package_lock_json_path).unwrap(); - assert!(package_lock_json_str.contains(&format!("\"name\":\"{expected_package_name}\""))); + assert!(package_lock_json_str.contains(&format!("\"name\": \"{expected_package_name}\""))); } } From 1815c606d273fcc3af6cb6a6c4846f0b0e3a5aeb Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:55:31 -0500 Subject: [PATCH 6/7] Cleanup after rebasing --- cmd/soroban-cli/src/commands/contract/init.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/soroban-cli/src/commands/contract/init.rs b/cmd/soroban-cli/src/commands/contract/init.rs index 3555375d8..3c2a401bc 100644 --- a/cmd/soroban-cli/src/commands/contract/init.rs +++ b/cmd/soroban-cli/src/commands/contract/init.rs @@ -15,7 +15,6 @@ use clap::{ }; use gix::{clone, create, open, progress, remote}; use rust_embed::RustEmbed; -use serde::Deserialize; use serde_json::{from_str, json, to_string_pretty, Error as JsonError, Value as JsonValue}; use toml_edit::{Document, Formatted, InlineTable, Item, TomlError, Value as TomlValue}; use ureq::{get, Error as UreqError}; From ffb7a2428927dbc04fca0bdf6487abe0ec5103cf Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Mon, 4 Mar 2024 12:10:38 -0500 Subject: [PATCH 7/7] Clippy update --- cmd/soroban-cli/src/commands/contract/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/soroban-cli/src/commands/contract/init.rs b/cmd/soroban-cli/src/commands/contract/init.rs index 3c2a401bc..093cf270f 100644 --- a/cmd/soroban-cli/src/commands/contract/init.rs +++ b/cmd/soroban-cli/src/commands/contract/init.rs @@ -554,7 +554,7 @@ mod tests { let with_examples = vec![]; init( project_dir.as_path(), - &"https://github.com/AhaLabs/soroban-astro-template".to_string(), + "https://github.com/AhaLabs/soroban-astro-template", &with_examples, ) .unwrap();