|
4 | 4 | //! and template variable substitution. |
5 | 5 |
|
6 | 6 | use anyhow::Result; |
7 | | -use git_workers::infrastructure::hooks::{execute_hooks, HookContext}; |
| 7 | +use git_workers::infrastructure::hooks::{execute_hooks, execute_hooks_with_ui, HookContext}; |
| 8 | +use git_workers::ui::MockUI; |
8 | 9 | use std::fs; |
9 | 10 | use std::path::PathBuf; |
10 | 11 | use tempfile::TempDir; |
@@ -236,3 +237,193 @@ fn test_hook_execution_flow_simulation() -> Result<()> { |
236 | 237 |
|
237 | 238 | Ok(()) |
238 | 239 | } |
| 240 | + |
| 241 | +// ============================================================================ |
| 242 | +// Hook Confirmation Tests |
| 243 | +// ============================================================================ |
| 244 | + |
| 245 | +#[test] |
| 246 | +#[ignore = "Hook execution requires specific command availability"] |
| 247 | +fn test_hook_confirmation_accepted() -> Result<()> { |
| 248 | + let temp_dir = TempDir::new()?; |
| 249 | + |
| 250 | + // Initialize git repository |
| 251 | + git2::Repository::init(temp_dir.path())?; |
| 252 | + |
| 253 | + // Create config with hooks |
| 254 | + let config_content = r#" |
| 255 | +[hooks] |
| 256 | +post-create = ["echo 'test hook'"] |
| 257 | +"#; |
| 258 | + fs::write(temp_dir.path().join(".git-workers.toml"), config_content)?; |
| 259 | + |
| 260 | + let original_dir = std::env::current_dir()?; |
| 261 | + std::env::set_current_dir(temp_dir.path())?; |
| 262 | + |
| 263 | + let context = HookContext { |
| 264 | + worktree_name: "test".to_string(), |
| 265 | + worktree_path: temp_dir.path().to_path_buf(), |
| 266 | + }; |
| 267 | + |
| 268 | + // Mock UI that accepts confirmation |
| 269 | + let ui = MockUI::new().with_confirm(true); |
| 270 | + |
| 271 | + // Execute hooks with UI - should succeed when confirmation is accepted |
| 272 | + let result = execute_hooks_with_ui("post-create", &context, &ui); |
| 273 | + |
| 274 | + std::env::set_current_dir(original_dir)?; |
| 275 | + |
| 276 | + assert!(result.is_ok()); |
| 277 | + Ok(()) |
| 278 | +} |
| 279 | + |
| 280 | +#[test] |
| 281 | +#[ignore = "Hook execution requires specific command availability"] |
| 282 | +fn test_hook_confirmation_rejected() -> Result<()> { |
| 283 | + let temp_dir = TempDir::new()?; |
| 284 | + |
| 285 | + // Initialize git repository |
| 286 | + git2::Repository::init(temp_dir.path())?; |
| 287 | + |
| 288 | + // Create config with hooks |
| 289 | + let config_content = r#" |
| 290 | +[hooks] |
| 291 | +post-create = ["echo 'test hook'"] |
| 292 | +"#; |
| 293 | + fs::write(temp_dir.path().join(".git-workers.toml"), config_content)?; |
| 294 | + |
| 295 | + let original_dir = std::env::current_dir()?; |
| 296 | + std::env::set_current_dir(temp_dir.path())?; |
| 297 | + |
| 298 | + let context = HookContext { |
| 299 | + worktree_name: "test".to_string(), |
| 300 | + worktree_path: temp_dir.path().to_path_buf(), |
| 301 | + }; |
| 302 | + |
| 303 | + // Mock UI that rejects confirmation |
| 304 | + let ui = MockUI::new().with_confirm(false); |
| 305 | + |
| 306 | + // Execute hooks with UI - should succeed but skip execution |
| 307 | + let result = execute_hooks_with_ui("post-create", &context, &ui); |
| 308 | + |
| 309 | + std::env::set_current_dir(original_dir)?; |
| 310 | + |
| 311 | + // Should still return Ok even when hooks are skipped |
| 312 | + assert!(result.is_ok()); |
| 313 | + Ok(()) |
| 314 | +} |
| 315 | + |
| 316 | +#[test] |
| 317 | +#[ignore = "Hook execution requires specific command availability"] |
| 318 | +fn test_hook_with_template_variables_confirmation() -> Result<()> { |
| 319 | + let temp_dir = TempDir::new()?; |
| 320 | + |
| 321 | + // Initialize git repository |
| 322 | + git2::Repository::init(temp_dir.path())?; |
| 323 | + |
| 324 | + // Create config with hooks using template variables |
| 325 | + let config_content = r#" |
| 326 | +[hooks] |
| 327 | +post-create = [ |
| 328 | + "echo 'Created worktree: {{worktree_name}}'", |
| 329 | + "echo 'At path: {{worktree_path}}'" |
| 330 | +] |
| 331 | +"#; |
| 332 | + fs::write(temp_dir.path().join(".git-workers.toml"), config_content)?; |
| 333 | + |
| 334 | + let original_dir = std::env::current_dir()?; |
| 335 | + std::env::set_current_dir(temp_dir.path())?; |
| 336 | + |
| 337 | + let context = HookContext { |
| 338 | + worktree_name: "feature-xyz".to_string(), |
| 339 | + worktree_path: PathBuf::from("/workspace/feature-xyz"), |
| 340 | + }; |
| 341 | + |
| 342 | + // Mock UI that accepts confirmation |
| 343 | + let ui = MockUI::new().with_confirm(true); |
| 344 | + |
| 345 | + // Execute hooks - template variables should be expanded in display |
| 346 | + let result = execute_hooks_with_ui("post-create", &context, &ui); |
| 347 | + |
| 348 | + std::env::set_current_dir(original_dir)?; |
| 349 | + |
| 350 | + assert!(result.is_ok()); |
| 351 | + Ok(()) |
| 352 | +} |
| 353 | + |
| 354 | +#[test] |
| 355 | +#[ignore = "Hook execution requires specific command availability"] |
| 356 | +fn test_multiple_hook_types_with_confirmation() -> Result<()> { |
| 357 | + let temp_dir = TempDir::new()?; |
| 358 | + |
| 359 | + // Initialize git repository |
| 360 | + git2::Repository::init(temp_dir.path())?; |
| 361 | + |
| 362 | + // Create config with multiple hook types |
| 363 | + let config_content = r#" |
| 364 | +[hooks] |
| 365 | +post-create = ["echo 'post-create'"] |
| 366 | +pre-remove = ["echo 'pre-remove'"] |
| 367 | +post-switch = ["echo 'post-switch'"] |
| 368 | +"#; |
| 369 | + fs::write(temp_dir.path().join(".git-workers.toml"), config_content)?; |
| 370 | + |
| 371 | + let original_dir = std::env::current_dir()?; |
| 372 | + std::env::set_current_dir(temp_dir.path())?; |
| 373 | + |
| 374 | + let context = HookContext { |
| 375 | + worktree_name: "test".to_string(), |
| 376 | + worktree_path: temp_dir.path().to_path_buf(), |
| 377 | + }; |
| 378 | + |
| 379 | + // Test each hook type with different confirmation responses |
| 380 | + let hook_types = vec![ |
| 381 | + ("post-create", true), |
| 382 | + ("pre-remove", false), |
| 383 | + ("post-switch", true), |
| 384 | + ]; |
| 385 | + |
| 386 | + for (hook_type, confirm) in hook_types { |
| 387 | + let ui = MockUI::new().with_confirm(confirm); |
| 388 | + let result = execute_hooks_with_ui(hook_type, &context, &ui); |
| 389 | + assert!(result.is_ok(), "Hook type {hook_type} should succeed"); |
| 390 | + } |
| 391 | + |
| 392 | + std::env::set_current_dir(original_dir)?; |
| 393 | + Ok(()) |
| 394 | +} |
| 395 | + |
| 396 | +#[test] |
| 397 | +#[ignore = "Hook execution requires specific command availability"] |
| 398 | +fn test_empty_hooks_no_confirmation_needed() -> Result<()> { |
| 399 | + let temp_dir = TempDir::new()?; |
| 400 | + |
| 401 | + // Initialize git repository |
| 402 | + git2::Repository::init(temp_dir.path())?; |
| 403 | + |
| 404 | + // Create config with empty hooks |
| 405 | + let config_content = r#" |
| 406 | +[hooks] |
| 407 | +post-create = [] |
| 408 | +"#; |
| 409 | + fs::write(temp_dir.path().join(".git-workers.toml"), config_content)?; |
| 410 | + |
| 411 | + let original_dir = std::env::current_dir()?; |
| 412 | + std::env::set_current_dir(temp_dir.path())?; |
| 413 | + |
| 414 | + let context = HookContext { |
| 415 | + worktree_name: "test".to_string(), |
| 416 | + worktree_path: temp_dir.path().to_path_buf(), |
| 417 | + }; |
| 418 | + |
| 419 | + // Mock UI without any confirmations configured |
| 420 | + let ui = MockUI::new(); |
| 421 | + |
| 422 | + // Should succeed without asking for confirmation |
| 423 | + let result = execute_hooks_with_ui("post-create", &context, &ui); |
| 424 | + |
| 425 | + std::env::set_current_dir(original_dir)?; |
| 426 | + |
| 427 | + assert!(result.is_ok()); |
| 428 | + Ok(()) |
| 429 | +} |
0 commit comments