Skip to content

Commit b9212ac

Browse files
committed
refactor: [#107] extract test setup helper to reduce duplication
- Add create_test_setup() helper function in context.rs tests - Refactor 7 test functions to use the helper instead of repeated setup code - Reduces 21 lines of duplicated setup to 7 lines using helper - Fix clippy warnings: add backticks to doc comments and Panics section - Improves test maintainability by centralizing test setup logic
1 parent 38878bb commit b9212ac

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/presentation/commands/context.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,20 @@ mod tests {
301301
)))
302302
}
303303

304-
#[test]
305-
fn it_should_create_context_with_production_dependencies() {
304+
/// Test helper to create a test context with temporary directory
305+
///
306+
/// Returns a tuple of (`TempDir`, `PathBuf`, `Arc<Mutex<UserOutput>>`)
307+
/// The `TempDir` must be kept alive for the duration of the test.
308+
fn create_test_setup() -> (TempDir, PathBuf, Arc<std::sync::Mutex<UserOutput>>) {
306309
let temp_dir = TempDir::new().unwrap();
307310
let working_dir = temp_dir.path().to_path_buf();
308311
let user_output = create_test_user_output();
312+
(temp_dir, working_dir, user_output)
313+
}
314+
315+
#[test]
316+
fn it_should_create_context_with_production_dependencies() {
317+
let (_temp_dir, working_dir, user_output) = create_test_setup();
309318

310319
let ctx = CommandContext::new(working_dir, user_output);
311320

@@ -316,9 +325,7 @@ mod tests {
316325

317326
#[test]
318327
fn it_should_provide_access_to_repository() {
319-
let temp_dir = TempDir::new().unwrap();
320-
let working_dir = temp_dir.path().to_path_buf();
321-
let user_output = create_test_user_output();
328+
let (_temp_dir, working_dir, user_output) = create_test_setup();
322329

323330
let ctx = CommandContext::new(working_dir, user_output);
324331

@@ -328,9 +335,7 @@ mod tests {
328335

329336
#[test]
330337
fn it_should_provide_access_to_clock() {
331-
let temp_dir = TempDir::new().unwrap();
332-
let working_dir = temp_dir.path().to_path_buf();
333-
let user_output = create_test_user_output();
338+
let (_temp_dir, working_dir, user_output) = create_test_setup();
334339

335340
let ctx = CommandContext::new(working_dir, user_output);
336341

@@ -340,9 +345,7 @@ mod tests {
340345

341346
#[test]
342347
fn it_should_provide_access_to_user_output() {
343-
let temp_dir = TempDir::new().unwrap();
344-
let working_dir = temp_dir.path().to_path_buf();
345-
let user_output = create_test_user_output();
348+
let (_temp_dir, working_dir, user_output) = create_test_setup();
346349

347350
let ctx = CommandContext::new(working_dir, user_output);
348351

@@ -354,9 +357,7 @@ mod tests {
354357

355358
#[test]
356359
fn it_should_create_context_with_factory() {
357-
let temp_dir = TempDir::new().unwrap();
358-
let working_dir = temp_dir.path().to_path_buf();
359-
let user_output = create_test_user_output();
360+
let (_temp_dir, working_dir, user_output) = create_test_setup();
360361

361362
let repository_factory = RepositoryFactory::new(DEFAULT_LOCK_TIMEOUT);
362363
let ctx = CommandContext::new_with_factory(&repository_factory, working_dir, user_output);
@@ -389,9 +390,7 @@ mod tests {
389390

390391
#[test]
391392
fn it_should_allow_accessing_output_multiple_times() {
392-
let temp_dir = TempDir::new().unwrap();
393-
let working_dir = temp_dir.path().to_path_buf();
394-
let user_output = create_test_user_output();
393+
let (_temp_dir, working_dir, user_output) = create_test_setup();
395394

396395
let ctx = CommandContext::new(working_dir, user_output);
397396

@@ -404,9 +403,7 @@ mod tests {
404403

405404
#[test]
406405
fn it_should_use_default_constants() {
407-
let temp_dir = TempDir::new().unwrap();
408-
let working_dir = temp_dir.path().to_path_buf();
409-
let user_output = create_test_user_output();
406+
let (_temp_dir, working_dir, user_output) = create_test_setup();
410407

411408
// Creating context should use DEFAULT_LOCK_TIMEOUT
412409
let _ctx = CommandContext::new(working_dir, user_output);

src/presentation/commands/destroy/handler.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ use super::errors::DestroySubcommandError;
4040
/// the environment cannot be loaded, or the destruction process fails.
4141
/// All errors include detailed context and actionable troubleshooting guidance.
4242
///
43+
/// # Panics
44+
///
45+
/// This function will panic if the `UserOutput` mutex is poisoned. This can only
46+
/// occur if a panic happened while another thread held the mutex lock.
47+
///
4348
/// # Example
4449
///
4550
/// ```rust

0 commit comments

Comments
 (0)