Skip to content

Commit f0704ec

Browse files
committed
refactor: [#107] extract test setup helper in factory module
- Add create_test_setup() helper function in factory.rs tests - Refactor 6 test functions to use helper instead of repeated setup - Reduces 24 lines of duplicated setup code to 6 lines using helper - Improves test maintainability by centralizing factory test setup
1 parent b9212ac commit f0704ec

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/presentation/commands/factory.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ mod tests {
250250
Arc::new(Mutex::new(UserOutput::new(VerbosityLevel::Normal)))
251251
}
252252

253+
/// Test helper to create a test setup with factory, temp directory, and user output
254+
///
255+
/// Returns a tuple of (`CommandHandlerFactory`, `TempDir`, `PathBuf`, `Arc<Mutex<UserOutput>>`)
256+
/// The `TempDir` must be kept alive for the duration of the test.
257+
fn create_test_setup() -> (
258+
CommandHandlerFactory,
259+
TempDir,
260+
PathBuf,
261+
Arc<Mutex<UserOutput>>,
262+
) {
263+
let factory = CommandHandlerFactory::new();
264+
let temp_dir = TempDir::new().unwrap();
265+
let working_dir = temp_dir.path().to_path_buf();
266+
let user_output = create_test_user_output();
267+
(factory, temp_dir, working_dir, user_output)
268+
}
269+
253270
#[test]
254271
fn it_should_create_factory_with_default_configuration() {
255272
let factory = CommandHandlerFactory::new();
@@ -270,10 +287,7 @@ mod tests {
270287

271288
#[test]
272289
fn it_should_create_context_with_factory() {
273-
let factory = CommandHandlerFactory::new();
274-
let temp_dir = TempDir::new().unwrap();
275-
let working_dir = temp_dir.path().to_path_buf();
276-
let user_output = create_test_user_output();
290+
let (factory, _temp_dir, working_dir, user_output) = create_test_setup();
277291

278292
let context = factory.create_context(working_dir, user_output);
279293

@@ -284,10 +298,7 @@ mod tests {
284298

285299
#[test]
286300
fn it_should_create_create_handler() {
287-
let factory = CommandHandlerFactory::new();
288-
let temp_dir = TempDir::new().unwrap();
289-
let working_dir = temp_dir.path().to_path_buf();
290-
let user_output = create_test_user_output();
301+
let (factory, _temp_dir, working_dir, user_output) = create_test_setup();
291302

292303
let context = factory.create_context(working_dir, user_output);
293304
let _handler = factory.create_create_handler(&context);
@@ -297,10 +308,7 @@ mod tests {
297308

298309
#[test]
299310
fn it_should_create_destroy_handler() {
300-
let factory = CommandHandlerFactory::new();
301-
let temp_dir = TempDir::new().unwrap();
302-
let working_dir = temp_dir.path().to_path_buf();
303-
let user_output = create_test_user_output();
311+
let (factory, _temp_dir, working_dir, user_output) = create_test_setup();
304312

305313
let context = factory.create_context(working_dir, user_output);
306314
let _handler = factory.create_destroy_handler(&context);
@@ -310,9 +318,7 @@ mod tests {
310318

311319
#[test]
312320
fn it_should_create_multiple_contexts_from_same_factory() {
313-
let factory = CommandHandlerFactory::new();
314-
let temp_dir = TempDir::new().unwrap();
315-
let working_dir = temp_dir.path().to_path_buf();
321+
let (factory, _temp_dir, working_dir, _user_output) = create_test_setup();
316322

317323
// Should be able to create multiple contexts
318324
let context1 = factory.create_context(working_dir.clone(), create_test_user_output());
@@ -325,10 +331,7 @@ mod tests {
325331

326332
#[test]
327333
fn it_should_create_multiple_handlers_from_same_context() {
328-
let factory = CommandHandlerFactory::new();
329-
let temp_dir = TempDir::new().unwrap();
330-
let working_dir = temp_dir.path().to_path_buf();
331-
let user_output = create_test_user_output();
334+
let (factory, _temp_dir, working_dir, user_output) = create_test_setup();
332335

333336
let context = factory.create_context(working_dir, user_output);
334337

@@ -346,9 +349,7 @@ mod tests {
346349
let repository_factory = RepositoryFactory::new(Duration::from_millis(100));
347350
let factory = CommandHandlerFactory::new_for_testing(repository_factory);
348351

349-
let temp_dir = TempDir::new().unwrap();
350-
let working_dir = temp_dir.path().to_path_buf();
351-
let user_output = create_test_user_output();
352+
let (_factory, _temp_dir, working_dir, user_output) = create_test_setup();
352353

353354
// Should be able to create context with custom factory
354355
let context = factory.create_context(working_dir, user_output);

0 commit comments

Comments
 (0)