workspace_tools 0.12.0

Reliable workspace-relative path resolution for Rust projects. Automatically finds your workspace root and provides consistent file path handling regardless of execution context. Features memory-safe secret management, configuration loading with validation, and resource discovery.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Task 022: Extend workspace resolution for installed applications
//!
//! ## Problem Context
//!
//! **Real-world issue**: CLI tools installed via `cargo install` (like `wip2`) could not
//! load secrets because `WORKSPACE_PATH` environment variable is only set during cargo
//! operations (via `.cargo/config.toml`). This prevented installed binaries from accessing
//! workspace-level secrets stored in `secret/-secrets.sh`.
//!
//! **Solution**: Extended fallback chain to include user-configured locations:
//! - `$PRO` environment variable (for multi-project users)
//! - `$HOME` directory (universal fallback)
//!
//! This enables installed CLI tools to work seamlessly in both development contexts
//! (via `cargo run`) and installed contexts (via `cargo install`).
//!
//! ## Testing Challenges & Solutions
//!
//! ### Challenge 1: Mutex Poisoning
//! **Problem**: Tests that manipulate environment variables need serialization to avoid
//! race conditions. When a test panics while holding the mutex, the mutex becomes "poisoned"
//! and subsequent tests fail even though they're correct.
//!
//! **Solution**: The `lock_env_mutex()` helper uses `unwrap_or_else(PoisonError::into_inner)`
//! to recover from poisoned mutexes. This allows the test suite to continue even if one
//! test panics during development.
//!
//! ### Challenge 2: Cargo Workspace Interference
//! **Problem**: Tests for `$PRO` and `$HOME` fallbacks were finding the actual cargo
//! workspace first (because tests run inside the workspace), making it impossible to test
//! the fallback chain order.
//!
//! **Solution**: Tests that need to verify fallback behavior change the current working
//! directory to a temporary directory outside any cargo workspace using
//! `env::set_current_dir(temp_dir)`. This isolates the test from the development workspace.
//!
//! ## Design Decisions Captured
//!
//! **Backward Compatibility over Spec**: The task specification suggested making
//! `workspace()` return `Workspace` (infallible), but the implementation keeps it as
//! `Result<Workspace>` to avoid breaking all existing code that uses `workspace()?`.
//! This is intentional and better than the spec suggestion.
//!
//! ## Test Matrix
//!
//! ### `from_pro_env()` Tests
//! | id     | test case                  | conditions               | expected result          |
//! |--------|---------------------------|--------------------------|--------------------------|
//! | p1.1   | valid $PRO path           | env set, path exists     | success with path        |
//! | p1.2   | nonexistent $PRO path     | env set, path missing    | `PathNotFound` error       |
//! | p1.3   | missing $PRO env var      | env not set              | `EnvironmentMissing` error |
//! | p1.4   | $PRO path normalization   | path with /./ and /..    | normalized path          |
//!
//! ### `from_home_dir()` Tests
//! | id     | test case                  | conditions               | expected result          |
//! |--------|---------------------------|--------------------------|--------------------------|
//! | h1.1   | valid $HOME path          | HOME set, path exists    | success with HOME path   |
//! | h1.2   | valid $USERPROFILE path   | USERPROFILE set (Win)    | success with profile     |
//! | h1.3   | both env vars missing     | neither set              | `EnvironmentMissing` error |
//! | h1.4   | nonexistent home path     | env set, path missing    | `PathNotFound` error       |
//! | h1.5   | $HOME priority over prof  | both set                 | prefers HOME             |
//!
//! ### `resolve_with_extended_fallbacks()` Tests
//! | id     | test case                  | conditions               | expected result          |
//! |--------|---------------------------|--------------------------|--------------------------|
//! | f1.1   | cargo workspace exists    | in cargo workspace       | uses cargo workspace     |
//! | f1.2   | `WORKSPACE_PATH` set      | env var set              | uses `WORKSPACE_PATH`    |
//! | f1.3   | git root fallback         | .git dir exists          | uses git root            |
//! | f1.4   | $PRO fallback             | PRO set, others fail     | uses $PRO                |
//! | f1.5   | $HOME fallback            | HOME set, others fail    | uses $HOME               |
//! | f1.6   | cwd final fallback        | all fail                 | uses current dir         |
//! | f1.7   | fallback chain order      | multiple available       | correct priority order   |
//!
//! ### Integration Tests (Real-World Scenarios)
//! | id     | test case                  | scenario                 | expected result          |
//! |--------|---------------------------|--------------------------|--------------------------|
//! | i1.1   | developer in workspace    | cargo run context        | finds workspace secrets  |
//! | i1.2   | installed app with $PRO   | cargo install + PRO set  | finds $PRO/secret/       |
//! | i1.3   | installed app without PRO | cargo install, no PRO    | finds $HOME/secret/      |
//! | i1.4   | secret loading via PRO    | load secrets from PRO    | successfully loads       |
//! | i1.5   | secret loading via HOME   | load secrets from HOME   | successfully loads       |

use workspace_tools :: { Workspace, WorkspaceError };
use std ::
{
  env,
  fs,
};
use tempfile ::TempDir;

// Global mutex to serialize environment variable tests
static ENV_TEST_MUTEX : std ::sync ::Mutex< () > = std ::sync ::Mutex ::new( () );

// =============================================================================
// Helper Functions
// =============================================================================

/// Acquire mutex lock, recovering from poison errors
fn lock_env_mutex() -> std ::sync ::MutexGuard< 'static, () >
{
  ENV_TEST_MUTEX.lock().unwrap_or_else( std ::sync ::PoisonError::into_inner )
}

/// Restore environment variable to original value or unset it
fn restore_env_var( key : &str, original : Option< String > )
{
  match original
  {
  Some( value ) => env ::set_var( key, value ),
  None => env ::remove_var( key ),
  }
}

// =============================================================================
// `from_pro_env()` Tests
// =============================================================================

mod from_pro_env_tests
{
  use super :: *;

  /// Test p1.1: valid $PRO path
  #[ test ]
  fn test_pro_env_with_valid_path()
  {
  let _lock = lock_env_mutex();
  let temp_dir = TempDir ::new().unwrap();
  let original = env ::var( "PRO" ).ok();

  env ::set_var( "PRO", temp_dir.path() );
  let result = Workspace ::from_pro_env();

  restore_env_var( "PRO", original );

  assert!( result.is_ok(), "`from_pro_env()` should succeed with valid path" );
  let workspace = result.unwrap();
  assert_eq!( workspace.root(), temp_dir.path(), "workspace root should match $PRO path" );
  }

  /// Test p1.2: nonexistent $PRO path
  #[ test ]
  fn test_pro_env_with_nonexistent_path()
  {
  let _lock = lock_env_mutex();
  let original = env ::var( "PRO" ).ok();

  // Create unique nonexistent path
  let thread_id = std ::thread ::current().id();
  let timestamp = std ::time ::SystemTime ::now()
   .duration_since( std ::time ::UNIX_EPOCH )
   .unwrap_or_default()
   .as_nanos();
  let nonexistent = env ::temp_dir()
   .join( format!( "nonexistent_pro_{thread_id:?}_{timestamp}" ) );

  env ::set_var( "PRO", &nonexistent );
  let result = Workspace ::from_pro_env();

  restore_env_var( "PRO", original );

  assert!( result.is_err(), "`from_pro_env()` should fail with nonexistent path" );
  match result.unwrap_err()
  {
   WorkspaceError ::PathNotFound( path ) =>
   {
  assert_eq!( path, nonexistent, "error should contain the nonexistent path" );
   }
   other => panic!( "expected PathNotFound error, got: {other:?}" ),
  }
  }

  /// Test p1.3: missing $PRO env var
  #[ test ]
  fn test_pro_env_missing()
  {
  let _lock = lock_env_mutex();
  let original = env ::var( "PRO" ).ok();

  env ::remove_var( "PRO" );
  let result = Workspace ::from_pro_env();

  restore_env_var( "PRO", original );

  assert!( result.is_err(), "`from_pro_env()` should fail when PRO not set" );
  match result.unwrap_err()
  {
   WorkspaceError ::EnvironmentVariableMissing( var ) =>
   {
  assert_eq!( var, "PRO", "error should mention PRO variable" );
   }
   other => panic!( "expected EnvironmentVariableMissing error, got: {other:?}" ),
  }
  }

  /// Test p1.4: $PRO path normalization
  #[ test ]
  fn test_pro_env_path_normalization()
  {
  let _lock = lock_env_mutex();
  let temp_dir = TempDir ::new().unwrap();
  let original = env ::var( "PRO" ).ok();

  // Create path with redundant components
  let redundant_path = temp_dir.path().join( "." );
  env ::set_var( "PRO", &redundant_path );
  let result = Workspace ::from_pro_env();

  restore_env_var( "PRO", original );

  assert!( result.is_ok(), "`from_pro_env()` should succeed with redundant path" );
  let workspace = result.unwrap();

  // Path should be normalized (no trailing "/.")
  let root_str = workspace.root().to_string_lossy();
  assert!( !root_str.ends_with( "/." ), "path should not end with '/.' after normalization" );
  assert!( !root_str.contains( "/./" ), "path should not contain '/./' after normalization" );
  }
}

// =============================================================================
// `from_home_dir()` Tests
// =============================================================================

mod from_home_dir_tests
{
  use super :: *;

  /// Test h1.1: valid $HOME path
  #[ test ]
  fn test_home_dir_with_valid_home()
  {
  let _lock = lock_env_mutex();
  let temp_dir = TempDir ::new().unwrap();
  let original_home = env ::var( "HOME" ).ok();
  let original_userprofile = env ::var( "USERPROFILE" ).ok();

  // Remove USERPROFILE to ensure HOME is used
  env ::remove_var( "USERPROFILE" );
  env ::set_var( "HOME", temp_dir.path() );
  let result = Workspace ::from_home_dir();

  restore_env_var( "HOME", original_home );
  restore_env_var( "USERPROFILE", original_userprofile );

  assert!( result.is_ok(), "`from_home_dir()` should succeed with valid HOME" );
  let workspace = result.unwrap();
  assert_eq!( workspace.root(), temp_dir.path(), "workspace root should match $HOME path" );
  }

  /// Test h1.2: valid $USERPROFILE path (Windows)
  #[ test ]
  fn test_home_dir_with_valid_userprofile()
  {
  let _lock = lock_env_mutex();
  let temp_dir = TempDir ::new().unwrap();
  let original_home = env ::var( "HOME" ).ok();
  let original_userprofile = env ::var( "USERPROFILE" ).ok();

  // Remove HOME to ensure USERPROFILE is used
  env ::remove_var( "HOME" );
  env ::set_var( "USERPROFILE", temp_dir.path() );
  let result = Workspace ::from_home_dir();

  restore_env_var( "HOME", original_home );
  restore_env_var( "USERPROFILE", original_userprofile );

  assert!( result.is_ok(), "`from_home_dir()` should succeed with valid USERPROFILE" );
  let workspace = result.unwrap();
  assert_eq!( workspace.root(), temp_dir.path(), "workspace root should match USERPROFILE path" );
  }

  /// Test h1.3: both env vars missing
  #[ test ]
  fn test_home_dir_missing_both()
  {
  let _lock = lock_env_mutex();
  let original_home = env ::var( "HOME" ).ok();
  let original_userprofile = env ::var( "USERPROFILE" ).ok();

  env ::remove_var( "HOME" );
  env ::remove_var( "USERPROFILE" );
  let result = Workspace ::from_home_dir();

  restore_env_var( "HOME", original_home );
  restore_env_var( "USERPROFILE", original_userprofile );

  assert!( result.is_err(), "`from_home_dir()` should fail when neither HOME nor USERPROFILE set" );
  match result.unwrap_err()
  {
   WorkspaceError ::EnvironmentVariableMissing( var ) =>
   {
  assert!(
   var.contains( "HOME" ) || var.contains( "USERPROFILE" ),
   "error should mention HOME or USERPROFILE, got: {var}"
  );
   }
   other => panic!( "expected EnvironmentVariableMissing error, got: {other:?}" ),
  }
  }

  /// Test h1.4: nonexistent home path
  #[ test ]
  fn test_home_dir_with_nonexistent_path()
  {
  let _lock = lock_env_mutex();
  let original_home = env ::var( "HOME" ).ok();
  let original_userprofile = env ::var( "USERPROFILE" ).ok();

  // Create unique nonexistent path
  let thread_id = std ::thread ::current().id();
  let timestamp = std ::time ::SystemTime ::now()
   .duration_since( std ::time ::UNIX_EPOCH )
   .unwrap_or_default()
   .as_nanos();
  let nonexistent = env ::temp_dir()
   .join( format!( "nonexistent_home_{thread_id:?}_{timestamp}" ) );

  env ::remove_var( "USERPROFILE" );
  env ::set_var( "HOME", &nonexistent );
  let result = Workspace ::from_home_dir();

  restore_env_var( "HOME", original_home );
  restore_env_var( "USERPROFILE", original_userprofile );

  assert!( result.is_err(), "`from_home_dir()` should fail with nonexistent path" );
  match result.unwrap_err()
  {
   WorkspaceError ::PathNotFound( path ) =>
   {
  assert_eq!( path, nonexistent, "error should contain the nonexistent path" );
   }
   other => panic!( "expected PathNotFound error, got: {other:?}" ),
  }
  }

  /// Test h1.5: $HOME has priority over $USERPROFILE
  #[ test ]
  fn test_home_dir_priority()
  {
  let _lock = lock_env_mutex();
  let temp_dir_home = TempDir ::new().unwrap();
  let temp_dir_userprofile = TempDir ::new().unwrap();
  let original_home = env ::var( "HOME" ).ok();
  let original_userprofile = env ::var( "USERPROFILE" ).ok();

  env ::set_var( "HOME", temp_dir_home.path() );
  env ::set_var( "USERPROFILE", temp_dir_userprofile.path() );
  let result = Workspace ::from_home_dir();

  restore_env_var( "HOME", original_home );
  restore_env_var( "USERPROFILE", original_userprofile );

  assert!( result.is_ok(), "`from_home_dir()` should succeed when both are set" );
  let workspace = result.unwrap();
  assert_eq!(
   workspace.root(),
   temp_dir_home.path(),
   "workspace root should use HOME (priority over USERPROFILE)"
  );
  }
}

// =============================================================================
// `resolve_with_extended_fallbacks()` Tests
// =============================================================================

mod resolve_with_extended_fallbacks_tests
{
  use super :: *;

  /// Test f1.4: $PRO fallback when earlier strategies fail
  #[ test ]
  fn test_extended_fallbacks_uses_pro()
  {
  let _lock = lock_env_mutex();
  let temp_dir = TempDir ::new().unwrap();
  let original_workspace = env ::var( "WORKSPACE_PATH" ).ok();
  let original_pro = env ::var( "PRO" ).ok();
  let original_home = env ::var( "HOME" ).ok();
  let original_cwd = env ::current_dir().ok();

  // Clear all higher-priority env vars
  env ::remove_var( "WORKSPACE_PATH" );
  env ::set_var( "PRO", temp_dir.path() );

  // Set HOME as well (but PRO should be used first)
  let temp_home = TempDir ::new().unwrap();
  env ::set_var( "HOME", temp_home.path() );

  // Change to temp directory (outside cargo workspace and git repo)
  let test_cwd = TempDir ::new().unwrap();
  env ::set_current_dir( test_cwd.path() ).ok();

  let workspace = Workspace ::resolve_with_extended_fallbacks();

  // Restore environment
  if let Some( cwd ) = original_cwd
  {
   env ::set_current_dir( cwd ).ok();
  }
  restore_env_var( "WORKSPACE_PATH", original_workspace );
  restore_env_var( "PRO", original_pro );
  restore_env_var( "HOME", original_home );

  // PRO should be used (has priority over HOME)
  assert_eq!(
   workspace.root(),
   temp_dir.path(),
   "`resolve_with_extended_fallbacks()` should use $PRO when WORKSPACE_PATH not set"
  );
  }

  /// Test f1.5: $HOME fallback when PRO also fails
  #[ test ]
  fn test_extended_fallbacks_uses_home()
  {
  let _lock = lock_env_mutex();
  let temp_dir = TempDir ::new().unwrap();
  let original_workspace = env ::var( "WORKSPACE_PATH" ).ok();
  let original_pro = env ::var( "PRO" ).ok();
  let original_home = env ::var( "HOME" ).ok();
  let original_userprofile = env ::var( "USERPROFILE" ).ok();
  let original_cwd = env ::current_dir().ok();

  // Clear all higher-priority env vars
  env ::remove_var( "WORKSPACE_PATH" );
  env ::remove_var( "PRO" );
  env ::remove_var( "USERPROFILE" );
  env ::set_var( "HOME", temp_dir.path() );

  // Change to temp directory (outside cargo workspace and git repo)
  let test_cwd = TempDir ::new().unwrap();
  env ::set_current_dir( test_cwd.path() ).ok();

  let workspace = Workspace ::resolve_with_extended_fallbacks();

  // Restore environment
  if let Some( cwd ) = original_cwd
  {
   env ::set_current_dir( cwd ).ok();
  }
  restore_env_var( "WORKSPACE_PATH", original_workspace );
  restore_env_var( "PRO", original_pro );
  restore_env_var( "HOME", original_home );
  restore_env_var( "USERPROFILE", original_userprofile );

  // HOME should be used
  assert_eq!(
   workspace.root(),
   temp_dir.path(),
   "`resolve_with_extended_fallbacks()` should use $HOME when PRO not set"
  );
  }

  /// Test f1.6: cwd final fallback
  ///
  /// Note: This test documents the fallback behavior but may find cargo workspace
  /// or git root in real development environment. The important thing is that
  /// the function always succeeds with some valid workspace root.
  #[ test ]
  fn test_extended_fallbacks_final_cwd()
  {
  let _lock = lock_env_mutex();
  let original_workspace = env ::var( "WORKSPACE_PATH" ).ok();
  let original_pro = env ::var( "PRO" ).ok();
  let original_home = env ::var( "HOME" ).ok();
  let original_userprofile = env ::var( "USERPROFILE" ).ok();

  // Clear all env vars
  env ::remove_var( "WORKSPACE_PATH" );
  env ::remove_var( "PRO" );
  env ::remove_var( "HOME" );
  env ::remove_var( "USERPROFILE" );

  let workspace = Workspace ::resolve_with_extended_fallbacks();

  restore_env_var( "WORKSPACE_PATH", original_workspace );
  restore_env_var( "PRO", original_pro );
  restore_env_var( "HOME", original_home );
  restore_env_var( "USERPROFILE", original_userprofile );

  // Should succeed (may find cargo workspace, git root, or use cwd)
  // The important thing is it always succeeds and returns a valid path
  assert!(
   workspace.root().exists(),
   "`resolve_with_extended_fallbacks()` should always succeed with valid path"
  );
  assert!(
   workspace.root().is_absolute(),
   "resolved workspace root should be absolute path"
  );
  }

  /// Test f1.7: fallback chain priority order
  #[ test ]
  fn test_extended_fallbacks_priority_order()
  {
  let _lock = lock_env_mutex();

  // Create temp directories for each strategy
  let workspace_path_dir = TempDir ::new().unwrap();
  let pro_dir = TempDir ::new().unwrap();
  let home_dir = TempDir ::new().unwrap();

  let original_workspace = env ::var( "WORKSPACE_PATH" ).ok();
  let original_pro = env ::var( "PRO" ).ok();
  let original_home = env ::var( "HOME" ).ok();
  let original_cwd = env ::current_dir().ok();

  // Set all env vars - WORKSPACE_PATH should win
  env ::set_var( "WORKSPACE_PATH", workspace_path_dir.path() );
  env ::set_var( "PRO", pro_dir.path() );
  env ::set_var( "HOME", home_dir.path() );

  // Change to temp directory (outside cargo workspace and git repo)
  let test_cwd = TempDir ::new().unwrap();
  env ::set_current_dir( test_cwd.path() ).ok();

  let workspace = Workspace ::resolve_with_extended_fallbacks();

  // Restore environment
  if let Some( cwd ) = original_cwd
  {
   env ::set_current_dir( cwd ).ok();
  }
  restore_env_var( "WORKSPACE_PATH", original_workspace );
  restore_env_var( "PRO", original_pro );
  restore_env_var( "HOME", original_home );

  // WORKSPACE_PATH should be used (highest priority)
  assert_eq!(
   workspace.root(),
   workspace_path_dir.path(),
   "`resolve_with_extended_fallbacks()` should use WORKSPACE_PATH when available (highest priority)"
  );
  }
}

// =============================================================================
// Integration Tests (Real-World Scenarios)
// =============================================================================

mod integration_tests
{
  use super :: *;

  /// Test i1.2: installed app with $PRO can load secrets
  #[ test ]
  fn test_installed_app_with_pro_loads_secrets()
  {
  let _lock = lock_env_mutex();
  let temp_dir = TempDir ::new().unwrap();
  let original_workspace = env ::var( "WORKSPACE_PATH" ).ok();
  let original_pro = env ::var( "PRO" ).ok();
  let original_cwd = env ::current_dir().ok();

  // Simulate installed app context: no WORKSPACE_PATH, but PRO is set
  env ::remove_var( "WORKSPACE_PATH" );
  env ::set_var( "PRO", temp_dir.path() );

  // Create secret directory and file
  let secret_dir = temp_dir.path().join( "secret" );
  fs ::create_dir_all( &secret_dir ).unwrap();
  let secret_file = secret_dir.join( "-secrets.sh" );
  fs ::write( &secret_file, "GITHUB_TOKEN=test_token_123\nAPI_KEY=secret_key_456\n" ).unwrap();

  // Change to temp directory (outside cargo workspace and git repo)
  let test_cwd = TempDir ::new().unwrap();
  env ::set_current_dir( test_cwd.path() ).ok();

  // Application code: get workspace and load secrets
  #[ cfg_attr( not( feature = "secrets" ), allow( unused_variables ) ) ]
  let workspace = Workspace ::resolve_with_extended_fallbacks();

  #[ cfg( feature = "secrets" ) ]
  {
   let secrets = workspace.load_secrets_from_file( "-secrets.sh" );
   assert!( secrets.is_ok(), "should load secrets from $PRO/secret/ directory" );

   let secrets_map = secrets.unwrap();
   assert_eq!(
  secrets_map.get( "GITHUB_TOKEN" ).map( String::as_str ),
  Some( "test_token_123" ),
  "should load GITHUB_TOKEN from secrets file"
   );
   assert_eq!(
  secrets_map.get( "API_KEY" ).map( String::as_str ),
  Some( "secret_key_456" ),
  "should load API_KEY from secrets file"
   );
  }

  // Restore environment
  if let Some( cwd ) = original_cwd
  {
   env ::set_current_dir( cwd ).ok();
  }
  restore_env_var( "WORKSPACE_PATH", original_workspace );
  restore_env_var( "PRO", original_pro );
  }

  /// Test i1.3: installed app without PRO uses $HOME for secrets
  #[ test ]
  fn test_installed_app_without_pro_uses_home()
  {
  let _lock = lock_env_mutex();
  let temp_dir = TempDir ::new().unwrap();
  let original_workspace = env ::var( "WORKSPACE_PATH" ).ok();
  let original_pro = env ::var( "PRO" ).ok();
  let original_home = env ::var( "HOME" ).ok();
  let original_userprofile = env ::var( "USERPROFILE" ).ok();
  let original_cwd = env ::current_dir().ok();

  // Simulate installed app context: no WORKSPACE_PATH, no PRO, but HOME is set
  env ::remove_var( "WORKSPACE_PATH" );
  env ::remove_var( "PRO" );
  env ::remove_var( "USERPROFILE" );
  env ::set_var( "HOME", temp_dir.path() );

  // Create secret directory and file
  let secret_dir = temp_dir.path().join( "secret" );
  fs ::create_dir_all( &secret_dir ).unwrap();
  let secret_file = secret_dir.join( "-secrets.sh" );
  fs ::write( &secret_file, "GITHUB_TOKEN=home_token_789\n" ).unwrap();

  // Change to temp directory (outside cargo workspace and git repo)
  let test_cwd = TempDir ::new().unwrap();
  env ::set_current_dir( test_cwd.path() ).ok();

  // Application code: get workspace and load secrets
  #[ cfg_attr( not( feature = "secrets" ), allow( unused_variables ) ) ]
  let workspace = Workspace ::resolve_with_extended_fallbacks();

  #[ cfg( feature = "secrets" ) ]
  {
   let secrets = workspace.load_secrets_from_file( "-secrets.sh" );
   assert!( secrets.is_ok(), "should load secrets from $HOME/secret/ directory" );

   let secrets_map = secrets.unwrap();
   assert_eq!(
  secrets_map.get( "GITHUB_TOKEN" ).map( String::as_str ),
  Some( "home_token_789" ),
  "should load GITHUB_TOKEN from HOME secrets file"
   );
  }

  // Restore environment
  if let Some( cwd ) = original_cwd
  {
   env ::set_current_dir( cwd ).ok();
  }
  restore_env_var( "WORKSPACE_PATH", original_workspace );
  restore_env_var( "PRO", original_pro );
  restore_env_var( "HOME", original_home );
  restore_env_var( "USERPROFILE", original_userprofile );
  }
}