From 4a432a7c0286aac735e1bcddf165fd566b32277a Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 16:37:48 +0000 Subject: [PATCH 01/17] init new git repo --- crates/tuono/src/cli.rs | 6 +++- crates/tuono/src/scaffold_project.rs | 52 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/crates/tuono/src/cli.rs b/crates/tuono/src/cli.rs index de05360c..4a74a5d9 100644 --- a/crates/tuono/src/cli.rs +++ b/crates/tuono/src/cli.rs @@ -36,6 +36,9 @@ enum Actions { /// Load the latest commit available on the main branch #[arg(long)] head: Option, + /// Initialise a new git repo in the directory + #[arg(short, long)] + git: Option }, } @@ -154,8 +157,9 @@ pub fn app() -> std::io::Result<()> { folder_name, template, head, + git } => { - scaffold_project::create_new_project(folder_name, template, head); + scaffold_project::create_new_project(folder_name, template, head, git); } } diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index 2a8c7890..4498f86a 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -6,6 +6,7 @@ use std::env; use std::fs::{self, create_dir, File, OpenOptions}; use std::io::{self, prelude::*}; use std::path::{Path, PathBuf}; +use std::process::Command; const GITHUB_TUONO_TAGS_URL: &str = "https://api.github.com/repos/tuono-labs/tuono/git/ref/tags/"; @@ -66,9 +67,18 @@ pub fn create_new_project( folder_name: Option, template: Option, select_head: Option, + git: Option ) { let folder = folder_name.unwrap_or(".".to_string()); + // Check if git is installed the user *asks* to use git, else continue anyway + if git.is_some() == true && is_git_installed() { + exit_with_error("You specified you wanted to use git, however it is not installed.") + } + + // Use git by default + let git = git.unwrap_or(true) && is_git_installed(); + // In case of missing select the tuono example let template = template.unwrap_or("tuono-app".to_string()); let client = blocking::Client::builder() @@ -151,6 +161,11 @@ pub fn create_new_project( update_package_json_version(&folder_path).expect("Failed to update package.json version"); update_cargo_toml_version(&folder_path).expect("Failed to update Cargo.toml version"); + + if git { + init_new_git_repo(&folder_path).expect("Failed to initialise a new git repo"); + } + outro(folder); } @@ -245,6 +260,25 @@ fn update_cargo_toml_version(folder_path: &Path) -> io::Result<()> { Ok(()) } +fn is_git_installed() -> bool { + let output = Command::new("git").arg("--version").output(); + + output.is_ok() +} + +fn init_new_git_repo(folder_path: &Path) -> Result<(), ()> { + let output = Command::new("git") + .arg("init") + .arg(folder_path) + .output(); + + match output { + Ok(output) if output.status.success() => Ok(()), + Ok(output) => Err(()), + Err(e) => Err(()) + } +} + fn outro(folder_name: String) { println!("Success! 🎉"); @@ -292,4 +326,22 @@ mod tests { ); assert_eq!(expected, generated) } + + #[test] + fn test_init_new_git_repo() { + use std::fs; + use tempfile::tempdir; + + let temp_dir = tempdir().expect("Failed to create temp dir"); + let temp_path = temp_dir.path(); + + assert_eq!(fs::read_dir(temp_path).unwrap().count(), 0); + + let result = init_new_git_repo(temp_path); + + // Check if the function executed successfully + assert!(result.is_ok(), "Git repo initialization failed"); + // Check if the `.git` directory was created + assert!(temp_path.join(".git").exists(), "Git repository was not initialized"); + } } From e1de92f19ad9de580a824ccab1ce8690636b492f Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 16:47:39 +0000 Subject: [PATCH 02/17] fix logic and better errors --- crates/tuono/src/scaffold_project.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index 4498f86a..95fe6aee 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -72,7 +72,7 @@ pub fn create_new_project( let folder = folder_name.unwrap_or(".".to_string()); // Check if git is installed the user *asks* to use git, else continue anyway - if git.is_some() == true && is_git_installed() { + if git.unwrap_or(false) && is_git_installed() { exit_with_error("You specified you wanted to use git, however it is not installed.") } @@ -266,16 +266,22 @@ fn is_git_installed() -> bool { output.is_ok() } -fn init_new_git_repo(folder_path: &Path) -> Result<(), ()> { +fn init_new_git_repo(folder_path: &Path) -> Result<(), io::Error> { let output = Command::new("git") .arg("init") .arg(folder_path) - .output(); + .output()?; - match output { - Ok(output) if output.status.success() => Ok(()), - Ok(output) => Err(()), - Err(e) => Err(()) + if output.status.success() { + Ok(()) + } else { + Err(io::Error::new( + io::ErrorKind::Other, + format!( + "Git init failed: {}", + String::from_utf8_lossy(&output.stderr) + ), + )) } } From 1db394a34c01f2bee1b7c9e05868944e73df5680 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 16:49:17 +0000 Subject: [PATCH 03/17] lint --- crates/tuono/src/cli.rs | 4 ++-- crates/tuono/src/scaffold_project.rs | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/tuono/src/cli.rs b/crates/tuono/src/cli.rs index 4a74a5d9..1255623e 100644 --- a/crates/tuono/src/cli.rs +++ b/crates/tuono/src/cli.rs @@ -38,7 +38,7 @@ enum Actions { head: Option, /// Initialise a new git repo in the directory #[arg(short, long)] - git: Option + git: Option, }, } @@ -157,7 +157,7 @@ pub fn app() -> std::io::Result<()> { folder_name, template, head, - git + git, } => { scaffold_project::create_new_project(folder_name, template, head, git); } diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index 95fe6aee..a67cdf66 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -67,7 +67,7 @@ pub fn create_new_project( folder_name: Option, template: Option, select_head: Option, - git: Option + git: Option, ) { let folder = folder_name.unwrap_or(".".to_string()); @@ -267,10 +267,7 @@ fn is_git_installed() -> bool { } fn init_new_git_repo(folder_path: &Path) -> Result<(), io::Error> { - let output = Command::new("git") - .arg("init") - .arg(folder_path) - .output()?; + let output = Command::new("git").arg("init").arg(folder_path).output()?; if output.status.success() { Ok(()) @@ -348,6 +345,9 @@ mod tests { // Check if the function executed successfully assert!(result.is_ok(), "Git repo initialization failed"); // Check if the `.git` directory was created - assert!(temp_path.join(".git").exists(), "Git repository was not initialized"); + assert!( + temp_path.join(".git").exists(), + "Git repository was not initialized" + ); } } From 57666335b43afbcf0c851db391f257f6cf6fd4db Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 17:06:32 +0000 Subject: [PATCH 04/17] write integration tests --- crates/tuono/tests/cli_build.rs | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index 9b18b4ae..fb53cadb 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -200,3 +200,52 @@ fn build_fails_with_no_config() { .failure() .stderr("Cannot find tuono.config.ts - is this a tuono project?\n"); } + +#[test] +#[serial] +fn it_does_not_init_new_git_repo_with_git_false() { + let temp_tuono_project = TempTuonoProject::new(); + + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); + test_tuono_new + .arg("new") + .arg(temp_tuono_project.path()) + .arg("--git=false") + .assert() + .success(); + + // Ensure the `.git` directory does not exist + assert!(!temp_tuono_project.path().join(".git").exists()); +} + +#[test] +#[serial] +fn it_creates_project_without_git_if_not_installed() { + let temp_tuono_project = TempTuonoProject::new(); + + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); + test_tuono_new + .arg("new") + .arg(temp_tuono_project.path()) + .env("PATH", "") // Simulate git not being installed + .assert() + .success(); + + assert!(!temp_tuono_project.path().join(".git").exists()); +} + +#[test] +#[serial] +fn it_errors_if_git_not_installed_and_flag_set() { + let temp_tuono_project = TempTuonoProject::new(); + + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); + test_tuono_new + .arg("new") + .arg(temp_tuono_project.path()) + .arg("--git=true") + .env("PATH", "") // Simulate git not being installed + .assert() + .failure() + .stderr("Error: Git is required but not installed.\n"); +} From 4955a7fed07d8a79c4612a902bda92edceffe903 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 17:15:31 +0000 Subject: [PATCH 05/17] test improvements --- crates/tuono/tests/cli_build.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index fb53cadb..47ddcdc6 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -206,10 +206,13 @@ fn build_fails_with_no_config() { fn it_does_not_init_new_git_repo_with_git_false() { let temp_tuono_project = TempTuonoProject::new(); + fs::remove_dir_all(temp_tuono_project.path()).ok(); + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); test_tuono_new .arg("new") - .arg(temp_tuono_project.path()) + .arg(".") .arg("--git=false") .assert() .success(); @@ -223,10 +226,13 @@ fn it_does_not_init_new_git_repo_with_git_false() { fn it_creates_project_without_git_if_not_installed() { let temp_tuono_project = TempTuonoProject::new(); + fs::remove_dir_all(temp_tuono_project.path()).ok(); + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); test_tuono_new .arg("new") - .arg(temp_tuono_project.path()) + .arg(".") .env("PATH", "") // Simulate git not being installed .assert() .success(); @@ -239,10 +245,13 @@ fn it_creates_project_without_git_if_not_installed() { fn it_errors_if_git_not_installed_and_flag_set() { let temp_tuono_project = TempTuonoProject::new(); + fs::remove_dir_all(temp_tuono_project.path()).ok(); + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); test_tuono_new .arg("new") - .arg(temp_tuono_project.path()) + .arg(".") .arg("--git=true") .env("PATH", "") // Simulate git not being installed .assert() From da5a5227efcf71b00ab999677411abb686d06c8f Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 17:37:53 +0000 Subject: [PATCH 06/17] update logic and error matching --- crates/tuono/src/scaffold_project.rs | 2 +- crates/tuono/tests/cli_build.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index a67cdf66..236e2915 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -72,7 +72,7 @@ pub fn create_new_project( let folder = folder_name.unwrap_or(".".to_string()); // Check if git is installed the user *asks* to use git, else continue anyway - if git.unwrap_or(false) && is_git_installed() { + if git.unwrap_or(false) && !is_git_installed() { exit_with_error("You specified you wanted to use git, however it is not installed.") } diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index 47ddcdc6..a3f65e05 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -208,7 +208,7 @@ fn it_does_not_init_new_git_repo_with_git_false() { fs::remove_dir_all(temp_tuono_project.path()).ok(); std::env::set_current_dir(temp_tuono_project.path()).unwrap(); - + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); test_tuono_new .arg("new") @@ -256,5 +256,5 @@ fn it_errors_if_git_not_installed_and_flag_set() { .env("PATH", "") // Simulate git not being installed .assert() .failure() - .stderr("Error: Git is required but not installed.\n"); + .stderr("You specified you wanted to use git, however it is not installed.\n"); } From afd006161053b1951be113cb3b1d27e2da7fb384 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 17:42:49 +0000 Subject: [PATCH 07/17] debug ci --- crates/tuono/tests/cli_build.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index a3f65e05..a709376f 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -207,7 +207,15 @@ fn it_does_not_init_new_git_repo_with_git_false() { let temp_tuono_project = TempTuonoProject::new(); fs::remove_dir_all(temp_tuono_project.path()).ok(); + println!("Attempting to change directory to: {:?}", temp_tuono_project.path()); + + if !temp_tuono_project.path().exists() { + panic!("Error: Directory does not exist!"); + } + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + println!("Successfully changed directory."); + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); test_tuono_new From 69b9758d4b0e377f256520b571127fc305379002 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 17:45:49 +0000 Subject: [PATCH 08/17] fix ci --- crates/tuono/tests/cli_build.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index a709376f..b2463fb8 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -206,16 +206,7 @@ fn build_fails_with_no_config() { fn it_does_not_init_new_git_repo_with_git_false() { let temp_tuono_project = TempTuonoProject::new(); - fs::remove_dir_all(temp_tuono_project.path()).ok(); - println!("Attempting to change directory to: {:?}", temp_tuono_project.path()); - - if !temp_tuono_project.path().exists() { - panic!("Error: Directory does not exist!"); - } - std::env::set_current_dir(temp_tuono_project.path()).unwrap(); - println!("Successfully changed directory."); - let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); test_tuono_new @@ -234,7 +225,6 @@ fn it_does_not_init_new_git_repo_with_git_false() { fn it_creates_project_without_git_if_not_installed() { let temp_tuono_project = TempTuonoProject::new(); - fs::remove_dir_all(temp_tuono_project.path()).ok(); std::env::set_current_dir(temp_tuono_project.path()).unwrap(); let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); @@ -253,7 +243,6 @@ fn it_creates_project_without_git_if_not_installed() { fn it_errors_if_git_not_installed_and_flag_set() { let temp_tuono_project = TempTuonoProject::new(); - fs::remove_dir_all(temp_tuono_project.path()).ok(); std::env::set_current_dir(temp_tuono_project.path()).unwrap(); let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); From 21ee168150578c7be985270cc002ae44b271a925 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 17:49:16 +0000 Subject: [PATCH 09/17] test base case --- crates/tuono/tests/cli_build.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index b2463fb8..c7c726c1 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -201,6 +201,24 @@ fn build_fails_with_no_config() { .stderr("Cannot find tuono.config.ts - is this a tuono project?\n"); } +#[test] +#[serial] +fn it_inits_new_git_repo_by_default_with_git_installed() { + let temp_tuono_project = TempTuonoProject::new(); + + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); + test_tuono_new + .arg("new") + .arg(".") + .assert() + .success(); + + // Ensure the `.git` directory exists + assert!(temp_tuono_project.path().join(".git").exists()); +} + #[test] #[serial] fn it_does_not_init_new_git_repo_with_git_false() { From d1aff25fdc648c0d15daa4bd0ecfd3b0e4f33841 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 17:50:51 +0000 Subject: [PATCH 10/17] lint --- crates/tuono/tests/cli_build.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index c7c726c1..b0e2594d 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -209,11 +209,7 @@ fn it_inits_new_git_repo_by_default_with_git_installed() { std::env::set_current_dir(temp_tuono_project.path()).unwrap(); let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); - test_tuono_new - .arg("new") - .arg(".") - .assert() - .success(); + test_tuono_new.arg("new").arg(".").assert().success(); // Ensure the `.git` directory exists assert!(temp_tuono_project.path().join(".git").exists()); From 8f8686e2243dd80fad84105aa23d2b64384d0c4d Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 20:10:29 +0000 Subject: [PATCH 11/17] make comments clearer --- crates/tuono/src/cli.rs | 2 +- crates/tuono/src/scaffold_project.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/tuono/src/cli.rs b/crates/tuono/src/cli.rs index 1255623e..cb96effd 100644 --- a/crates/tuono/src/cli.rs +++ b/crates/tuono/src/cli.rs @@ -36,7 +36,7 @@ enum Actions { /// Load the latest commit available on the main branch #[arg(long)] head: Option, - /// Initialise a new git repo in the directory + /// Initialize a new Git repository in the specified directory #[arg(short, long)] git: Option, }, diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index 236e2915..726eeb4d 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -71,9 +71,9 @@ pub fn create_new_project( ) { let folder = folder_name.unwrap_or(".".to_string()); - // Check if git is installed the user *asks* to use git, else continue anyway + // Check if Git is installed when the user requests to use it; otherwise, continue if git.unwrap_or(false) && !is_git_installed() { - exit_with_error("You specified you wanted to use git, however it is not installed.") + exit_with_error("You requested to use Git, but it is not installed.") } // Use git by default @@ -342,9 +342,9 @@ mod tests { let result = init_new_git_repo(temp_path); - // Check if the function executed successfully + // Confirm that the initialize function has been executed successfully assert!(result.is_ok(), "Git repo initialization failed"); - // Check if the `.git` directory was created + // Ensure that the `.git` directory has been created assert!( temp_path.join(".git").exists(), "Git repository was not initialized" From d0abfad8f77cc0eb36cced4cd6cc763ffda90da1 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Wed, 12 Feb 2025 20:14:23 +0000 Subject: [PATCH 12/17] update expected error in tests --- crates/tuono/tests/cli_build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index b0e2594d..f9c433c6 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -267,5 +267,5 @@ fn it_errors_if_git_not_installed_and_flag_set() { .env("PATH", "") // Simulate git not being installed .assert() .failure() - .stderr("You specified you wanted to use git, however it is not installed.\n"); + .stderr("You requested to use Git, but it is not installed.\n"); } From 617a9551c7de53499c8bbaaec3c99eb2495b6af6 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Thu, 13 Feb 2025 19:26:28 +0000 Subject: [PATCH 13/17] rename flag --- crates/tuono/src/cli.rs | 6 +++--- crates/tuono/src/scaffold_project.rs | 6 +++--- crates/tuono/tests/cli_build.rs | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/tuono/src/cli.rs b/crates/tuono/src/cli.rs index cb96effd..2bd71ae5 100644 --- a/crates/tuono/src/cli.rs +++ b/crates/tuono/src/cli.rs @@ -38,7 +38,7 @@ enum Actions { head: Option, /// Initialize a new Git repository in the specified directory #[arg(short, long)] - git: Option, + git_init: Option, }, } @@ -157,9 +157,9 @@ pub fn app() -> std::io::Result<()> { folder_name, template, head, - git, + git_init, } => { - scaffold_project::create_new_project(folder_name, template, head, git); + scaffold_project::create_new_project(folder_name, template, head, git_init); } } diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index 726eeb4d..268e52db 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -67,17 +67,17 @@ pub fn create_new_project( folder_name: Option, template: Option, select_head: Option, - git: Option, + git_init: Option, ) { let folder = folder_name.unwrap_or(".".to_string()); // Check if Git is installed when the user requests to use it; otherwise, continue - if git.unwrap_or(false) && !is_git_installed() { + if git_init.unwrap_or(false) && !is_git_installed() { exit_with_error("You requested to use Git, but it is not installed.") } // Use git by default - let git = git.unwrap_or(true) && is_git_installed(); + let git = git_init.unwrap_or(true) && is_git_installed(); // In case of missing select the tuono example let template = template.unwrap_or("tuono-app".to_string()); diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index f9c433c6..17fb01b4 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -226,7 +226,7 @@ fn it_does_not_init_new_git_repo_with_git_false() { test_tuono_new .arg("new") .arg(".") - .arg("--git=false") + .arg("--git-init=false") .assert() .success(); @@ -263,7 +263,7 @@ fn it_errors_if_git_not_installed_and_flag_set() { test_tuono_new .arg("new") .arg(".") - .arg("--git=true") + .arg("--git-init=true") .env("PATH", "") // Simulate git not being installed .assert() .failure() From ad458a44bfc6c6b53fbb648c9812f3efb51b17fb Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Thu, 13 Feb 2025 19:31:29 +0000 Subject: [PATCH 14/17] don't panic --- crates/tuono/src/scaffold_project.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index 268e52db..6e912a4a 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -163,7 +163,8 @@ pub fn create_new_project( update_cargo_toml_version(&folder_path).expect("Failed to update Cargo.toml version"); if git { - init_new_git_repo(&folder_path).expect("Failed to initialise a new git repo"); + init_new_git_repo(&folder_path) + .unwrap_or_else(|_| exit_with_error("Failed to initialise a new git repo")); } outro(folder); From 5bb77c661eb0293618c494e047725d641a4c8584 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Thu, 13 Feb 2025 19:33:20 +0000 Subject: [PATCH 15/17] only check git install once --- crates/tuono/src/scaffold_project.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index 6e912a4a..fb020f8b 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -71,13 +71,15 @@ pub fn create_new_project( ) { let folder = folder_name.unwrap_or(".".to_string()); + let is_git_installed = is_git_installed(); + // Check if Git is installed when the user requests to use it; otherwise, continue - if git_init.unwrap_or(false) && !is_git_installed() { + if git_init.unwrap_or(false) && !is_git_installed { exit_with_error("You requested to use Git, but it is not installed.") } // Use git by default - let git = git_init.unwrap_or(true) && is_git_installed(); + let git = git_init.unwrap_or(true) && is_git_installed; // In case of missing select the tuono example let template = template.unwrap_or("tuono-app".to_string()); From ec0ef1cdefa04dd13cd45b5488567172e9d3a572 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Thu, 13 Feb 2025 19:35:49 +0000 Subject: [PATCH 16/17] remove test --- crates/tuono/src/scaffold_project.rs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/crates/tuono/src/scaffold_project.rs b/crates/tuono/src/scaffold_project.rs index fb020f8b..50dcf84d 100644 --- a/crates/tuono/src/scaffold_project.rs +++ b/crates/tuono/src/scaffold_project.rs @@ -332,25 +332,4 @@ mod tests { ); assert_eq!(expected, generated) } - - #[test] - fn test_init_new_git_repo() { - use std::fs; - use tempfile::tempdir; - - let temp_dir = tempdir().expect("Failed to create temp dir"); - let temp_path = temp_dir.path(); - - assert_eq!(fs::read_dir(temp_path).unwrap().count(), 0); - - let result = init_new_git_repo(temp_path); - - // Confirm that the initialize function has been executed successfully - assert!(result.is_ok(), "Git repo initialization failed"); - // Ensure that the `.git` directory has been created - assert!( - temp_path.join(".git").exists(), - "Git repository was not initialized" - ); - } } From d7062381c37f833193b936204b6424792630c1b7 Mon Sep 17 00:00:00 2001 From: Jacob Marshall Date: Thu, 13 Feb 2025 19:39:55 +0000 Subject: [PATCH 17/17] move tests into new file --- crates/tuono/tests/cli_build.rs | 69 ------------------------------- crates/tuono/tests/cli_new.rs | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 69 deletions(-) create mode 100644 crates/tuono/tests/cli_new.rs diff --git a/crates/tuono/tests/cli_build.rs b/crates/tuono/tests/cli_build.rs index 17fb01b4..9b18b4ae 100644 --- a/crates/tuono/tests/cli_build.rs +++ b/crates/tuono/tests/cli_build.rs @@ -200,72 +200,3 @@ fn build_fails_with_no_config() { .failure() .stderr("Cannot find tuono.config.ts - is this a tuono project?\n"); } - -#[test] -#[serial] -fn it_inits_new_git_repo_by_default_with_git_installed() { - let temp_tuono_project = TempTuonoProject::new(); - - std::env::set_current_dir(temp_tuono_project.path()).unwrap(); - - let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); - test_tuono_new.arg("new").arg(".").assert().success(); - - // Ensure the `.git` directory exists - assert!(temp_tuono_project.path().join(".git").exists()); -} - -#[test] -#[serial] -fn it_does_not_init_new_git_repo_with_git_false() { - let temp_tuono_project = TempTuonoProject::new(); - - std::env::set_current_dir(temp_tuono_project.path()).unwrap(); - - let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); - test_tuono_new - .arg("new") - .arg(".") - .arg("--git-init=false") - .assert() - .success(); - - // Ensure the `.git` directory does not exist - assert!(!temp_tuono_project.path().join(".git").exists()); -} - -#[test] -#[serial] -fn it_creates_project_without_git_if_not_installed() { - let temp_tuono_project = TempTuonoProject::new(); - - std::env::set_current_dir(temp_tuono_project.path()).unwrap(); - - let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); - test_tuono_new - .arg("new") - .arg(".") - .env("PATH", "") // Simulate git not being installed - .assert() - .success(); - - assert!(!temp_tuono_project.path().join(".git").exists()); -} - -#[test] -#[serial] -fn it_errors_if_git_not_installed_and_flag_set() { - let temp_tuono_project = TempTuonoProject::new(); - - std::env::set_current_dir(temp_tuono_project.path()).unwrap(); - - let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); - test_tuono_new - .arg("new") - .arg(".") - .arg("--git-init=true") - .env("PATH", "") // Simulate git not being installed - .assert() - .failure() - .stderr("You requested to use Git, but it is not installed.\n"); -} diff --git a/crates/tuono/tests/cli_new.rs b/crates/tuono/tests/cli_new.rs new file mode 100644 index 00000000..107e0e93 --- /dev/null +++ b/crates/tuono/tests/cli_new.rs @@ -0,0 +1,73 @@ +mod utils; +use assert_cmd::Command; +use serial_test::serial; +use utils::TempTuonoProject; + +#[test] +#[serial] +fn it_inits_new_git_repo_by_default_with_git_installed() { + let temp_tuono_project = TempTuonoProject::new(); + + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); + test_tuono_new.arg("new").arg(".").assert().success(); + + // Ensure the `.git` directory exists + assert!(temp_tuono_project.path().join(".git").exists()); +} + +#[test] +#[serial] +fn it_does_not_init_new_git_repo_with_git_false() { + let temp_tuono_project = TempTuonoProject::new(); + + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); + test_tuono_new + .arg("new") + .arg(".") + .arg("--git-init=false") + .assert() + .success(); + + // Ensure the `.git` directory does not exist + assert!(!temp_tuono_project.path().join(".git").exists()); +} + +#[test] +#[serial] +fn it_creates_project_without_git_if_not_installed() { + let temp_tuono_project = TempTuonoProject::new(); + + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); + test_tuono_new + .arg("new") + .arg(".") + .env("PATH", "") // Simulate git not being installed + .assert() + .success(); + + assert!(!temp_tuono_project.path().join(".git").exists()); +} + +#[test] +#[serial] +fn it_errors_if_git_not_installed_and_flag_set() { + let temp_tuono_project = TempTuonoProject::new(); + + std::env::set_current_dir(temp_tuono_project.path()).unwrap(); + + let mut test_tuono_new = Command::cargo_bin("tuono").unwrap(); + test_tuono_new + .arg("new") + .arg(".") + .arg("--git-init=true") + .env("PATH", "") // Simulate git not being installed + .assert() + .failure() + .stderr("You requested to use Git, but it is not installed.\n"); +}