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
//! Feature Combination Tests for `workspace_tools`
//!
//! ## Test Matrix: Feature Combination Coverage
//!
//! | Test ID | Features | Scenario | Expected Behavior |
//! |---------|----------|----------|-------------------|
//! | FC.1 | cargo + serde | Load config from cargo workspace | Success |
//! | FC.2 | glob + secret_management | Find secret files with patterns | Success |
//! | FC.3 | cargo + glob | Find resources in cargo workspace | Success |
//! | FC.4 | serde + secret_management | Config with secrets | Success |
//! | FC.5 | All features | Full integration scenario | All work together |
//! | FC.6 | No features (minimal) | Basic workspace operations | Core works |
//! | FC.7 | cargo + serde + secrets | Complete workspace setup | Full functionality |
//! | FC.8 | Performance | All features enabled | No significant overhead |

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

/// Test FC.1 : Cargo + Serde integration
#[ cfg( all( feature = "serde", feature = "serde" ) ) ]
#[ test ]
fn test_cargo_serde()
{
  use serde :: { Serialize, Deserialize };
  
  #[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
  struct ProjectConfig
  {
  name: String,
  version: String,
  features: Vec< String >,
 }
  
  let temp_dir = TempDir ::new().unwrap();
  
  // Create a cargo workspace
  let cargo_toml = r#"
[workspace]
members = [ "test_crate" ]

[workspace.package]
version = "0.1.0"
edition = "2021"
"#;
  fs ::write( temp_dir.path().join( "Cargo.toml" ), cargo_toml ).unwrap();
  
  // Create a test crate member
  let member_dir = temp_dir.path().join( "test_crate" );
  fs ::create_dir_all( member_dir.join( "src" ) ).unwrap();
  fs ::write( member_dir.join( "Cargo.toml" ), r#"
[package]
name = "test_crate"
version.workspace = true
edition.workspace = true
"# ).unwrap();
  fs ::write( member_dir.join( "src/lib.rs" ), "// test crate" ).unwrap();
  
  // Create workspace using cargo integration
  let workspace = Workspace ::from_cargo_manifest( temp_dir.path().join( "Cargo.toml" ) ).unwrap();
  
  // Create config directory
  fs ::create_dir_all( workspace.config_dir() ).unwrap();
  
  // Test serde functionality within cargo workspace
  let config = ProjectConfig {
  name: "test_project".to_string(),
  version: "0.1.0".to_string(),
  features: vec![ "default".to_string(), "serde".to_string() ],
 };
  
  // Save config using serde
  let save_result = workspace.save_config( "project", &config );
  assert!( save_result.is_ok(), "Should save config in cargo workspace" );
  
  // Load config using serde
  let loaded: Result< ProjectConfig, WorkspaceError > = workspace.load_config( "project" );
  assert!( loaded.is_ok(), "Should load config from cargo workspace" );
  assert_eq!( loaded.unwrap(), config );
  
  // Verify cargo metadata works
  let metadata = workspace.cargo_metadata();
  if let Err( ref e ) = metadata
  {
  println!( "Cargo metadata error: {e}" );
 }
  assert!( metadata.is_ok(), "Should get cargo metadata" );
}

/// Test FC.2 : Glob + Secret Management integration
#[ cfg( all( feature = "glob", feature = "secrets" ) ) ]
#[ test ]
fn test_glob_secret_management_integration()
{
  let temp_dir = TempDir ::new().unwrap();
  
  // Use temp directory directly instead of environment variable manipulation
  let workspace = Workspace ::new( temp_dir.path() );
  
  // Create secret directory structure
  fs ::create_dir_all( workspace.secret_dir() ).unwrap();
  
  // Create multiple secret files
  let secret_files = vec![
  ( "api.env", "API_KEY=secret123\nDATABASE_URL=postgres: //localhost\n" ),
  ( "auth.env", "JWT_SECRET=jwt456\nOAUTH_CLIENT=oauth789\n" ),
  ( "config.env", "DEBUG=true\nLOG_LEVEL=info\n" ),
 ];
  
  for ( filename, content ) in &secret_files
  {
  fs ::write( workspace.secret_dir().join( filename ), content ).unwrap();
 }
  
  // Use glob to find all secret files
  let secret_pattern = format!( "{}/*.env", workspace.secret_dir().display() );
  let found_files = workspace.find_resources( &secret_pattern );
  
  assert!( found_files.is_ok(), "Should find secret files with glob pattern" );
  let files = found_files.unwrap();
  assert_eq!( files.len(), 3, "Should find all 3 secret files" );
  
  // Load secrets from found files
  for file in &files
  {
  if let Some( filename ) = file.file_name()
  {
   let secrets = workspace.load_secrets_from_file( &filename.to_string_lossy() );
   assert!( secrets.is_ok(), "Should load secrets from file: {filename:?}" );
   assert!( !secrets.unwrap().is_empty(), "Secret file should not be empty" );
 }
 }
  
  // Test loading specific keys
  let api_key = workspace.load_secret_key( "API_KEY", "api.env" );
  assert!( api_key.is_ok(), "Should load API_KEY from api.env" );
  assert_eq!( api_key.unwrap(), "secret123" );
}

/// Test FC.3 : Cargo + Glob integration  
#[ cfg( all( feature = "serde", feature = "glob" ) ) ]
#[ test ]
fn test_cargo_glob_integration()
{
  let temp_dir = TempDir ::new().unwrap();
  
  // Create cargo workspace with members
  let cargo_toml = r#"
[workspace]
members = [ "lib1", "lib2" ]

[workspace.package]
version = "0.1.0"
edition = "2021"
"#;
  fs ::write( temp_dir.path().join( "Cargo.toml" ), cargo_toml ).unwrap();
  
  // Create workspace members
  for member in [ "lib1", "lib2" ]
  {
  let member_dir = temp_dir.path().join( member );
  fs ::create_dir_all( member_dir.join( "src" ) ).unwrap();
  
  let member_cargo = format!( r#"
[package]
name = "{member}"
version.workspace = true
edition.workspace = true
"# );
  fs ::write( member_dir.join( "Cargo.toml" ), member_cargo ).unwrap();
  fs ::write( member_dir.join( "src/lib.rs" ), "// library code" ).unwrap();
 }
  
  let workspace = Workspace ::from_cargo_manifest( temp_dir.path().join( "Cargo.toml" ) ).unwrap();
  
  // Use glob to find all Cargo.toml files
  let cargo_files = workspace.find_resources( "**/Cargo.toml" );
  assert!( cargo_files.is_ok(), "Should find Cargo.toml files" );
  
  let files = cargo_files.unwrap();
  assert!( files.len() >= 3, "Should find at least workspace + member Cargo.toml files" );
  
  // Use glob to find all Rust source files
  let rust_files = workspace.find_resources( "**/*.rs" );
  assert!( rust_files.is_ok(), "Should find Rust source files" );
  
  let rs_files = rust_files.unwrap();
  assert!( rs_files.len() >= 2, "Should find at least member lib.rs files" );
  
  // Verify cargo workspace members
  let members = workspace.workspace_members();
  assert!( members.is_ok(), "Should get workspace members" );
  assert_eq!( members.unwrap().len(), 2, "Should have 2 workspace members" );
}

/// Test FC.4 : Serde + Secret Management integration
#[ cfg( all( feature = "serde", feature = "secrets" ) ) ]
#[ test ]
fn test_serde_secret_management_integration()
{
  use serde :: { Serialize, Deserialize };
  
  #[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
  struct DatabaseConfig
  {
  host: String,
  port: u16,
  username: String,
  password: String,
 }
  
  let temp_dir = TempDir ::new().unwrap();
  
  // Use temp directory directly instead of environment variable manipulation
  let workspace = Workspace ::new( temp_dir.path() );
  
  // Create directories
  fs ::create_dir_all( workspace.config_dir() ).unwrap();
  fs ::create_dir_all( workspace.secret_dir() ).unwrap();
  
  // Create secret file with database password
  let secret_content = "DB_PASSWORD=super_secret_password\nDB_USERNAME=admin\n";
  fs ::write( workspace.secret_dir().join( "database.env" ), secret_content ).unwrap();
  
  // Load secrets
  let username = workspace.load_secret_key( "DB_USERNAME", "database.env" ).unwrap();
  let password = workspace.load_secret_key( "DB_PASSWORD", "database.env" ).unwrap();
  
  // Create config with secrets
  let db_config = DatabaseConfig {
  host: "localhost".to_string(),
  port: 5432,
  username,
  password,
 };
  
  // Save config using serde
  let save_result = workspace.save_config( "database", &db_config );
  assert!( save_result.is_ok(), "Should save database config" );
  
  // Load config using serde
  let loaded: Result< DatabaseConfig, WorkspaceError > = workspace.load_config( "database" );
  assert!( loaded.is_ok(), "Should load database config" );
  
  let loaded_config = loaded.unwrap();
  assert_eq!( loaded_config.username, "admin" );
  assert_eq!( loaded_config.password, "super_secret_password" );
  assert_eq!( loaded_config, db_config );
}

/// Test FC.5 : All features integration
#[ cfg( all( 
  feature = "serde", 
  feature = "serde", 
  feature = "glob", 
  feature = "secrets"
) ) ]
#[ test ]
fn test_all_features_integration()
{
  use serde :: { Serialize, Deserialize };
  
  #[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
  struct FullConfig
  {
  project_name: String,
  database_url: String,
  api_keys: Vec< String >,
  debug_mode: bool,
 }
  
  let temp_dir = TempDir ::new().unwrap();
  
  // Create cargo workspace
  let cargo_toml = r#"
[workspace]
members = [ "app" ]

[workspace.package]  
version = "0.2.0"
edition = "2021"
"#;
  fs ::write( temp_dir.path().join( "Cargo.toml" ), cargo_toml ).unwrap();
  
  // Create app member
  let app_dir = temp_dir.path().join( "app" );
  fs ::create_dir_all( app_dir.join( "src" ) ).unwrap();
  fs ::write( app_dir.join( "Cargo.toml" ), r#"
[package]
name = "app"
version.workspace = true
edition.workspace = true
"# ).unwrap();
  fs ::write( app_dir.join( "src/main.rs" ), "fn main() {}" ).unwrap();
  
  // Create workspace from cargo
  let workspace = Workspace ::from_cargo_manifest( temp_dir.path().join( "Cargo.toml" ) ).unwrap();
  
  // Create all necessary directories
  fs ::create_dir_all( workspace.config_dir() ).unwrap();
  fs ::create_dir_all( workspace.secret_dir() ).unwrap();
  
  // Create secret files
  let api_secrets = "API_KEY_1=key123\nAPI_KEY_2=key456\nDATABASE_URL=postgres: //user: pass@localhost/db\n";
  fs ::write( workspace.secret_dir().join( "api.env" ), api_secrets ).unwrap();
  
  // Load secrets
  let db_url = workspace.load_secret_key( "DATABASE_URL", "api.env" ).unwrap();
  let api_key_1 = workspace.load_secret_key( "API_KEY_1", "api.env" ).unwrap();
  let api_key_2 = workspace.load_secret_key( "API_KEY_2", "api.env" ).unwrap();
  
  // Create full configuration
  let config = FullConfig {
  project_name: "integration_test".to_string(),
  database_url: db_url,
  api_keys: vec![ api_key_1, api_key_2 ],
  debug_mode: true,
 };
  
  // Save using serde
  let save_result = workspace.save_config( "full_app", &config );
  assert!( save_result.is_ok(), "Should save full configuration" );
  
  // Use glob to find all config files
  let config_pattern = format!( "{}/*.toml", workspace.config_dir().display() );
  let config_files = workspace.find_resources( &config_pattern );
  assert!( config_files.is_ok(), "Should find config files" );
  assert!( !config_files.unwrap().is_empty(), "Should have config files" );
  
  // Use glob to find all secret files  
  let secret_pattern = format!( "{}/*.env", workspace.secret_dir().display() );
  let secret_files = workspace.find_resources( &secret_pattern );
  assert!( secret_files.is_ok(), "Should find secret files" );
  assert!( !secret_files.unwrap().is_empty(), "Should have secret files" );
  
  // Load config back
  let loaded: Result< FullConfig, WorkspaceError > = workspace.load_config( "full_app" );
  assert!( loaded.is_ok(), "Should load full configuration" );
  assert_eq!( loaded.unwrap(), config );
  
  // Verify cargo functionality
  let metadata = workspace.cargo_metadata();
  assert!( metadata.is_ok(), "Should get cargo metadata" );
  
  let members = workspace.workspace_members();
  assert!( members.is_ok(), "Should get workspace members" );
  assert_eq!( members.unwrap().len(), 1, "Should have 1 member" );
}

/// Test FC.6 : Minimal functionality (no optional features)
#[ test ]
fn test_minimal_functionality()
{
  let temp_dir = TempDir ::new().unwrap();
  
  // Use temp directory directly instead of environment variable manipulation
  let workspace = Workspace ::new( temp_dir.path() );
  
  // Basic workspace operations should always work
  assert!( workspace.validate().is_ok() );
  assert_eq!( workspace.root(), temp_dir.path() );
  
  // Standard directory paths should work
  assert_eq!( workspace.config_dir(), temp_dir.path().join( "config" ) );
  assert_eq!( workspace.data_dir(), temp_dir.path().join( "data" ) );
  assert_eq!( workspace.logs_dir(), temp_dir.path().join( "logs" ) );
  
  // Path operations should work
  let joined = workspace.join( "test.txt" );
  assert_eq!( joined, temp_dir.path().join( "test.txt" ) );
  
  // Basic path operations should work
  assert!( joined.is_absolute() );
  
  // Boundary checking should work
  assert!( workspace.is_workspace_file( &joined ) );
  assert!( !workspace.is_workspace_file( "/etc/passwd" ) );
  
  // Convenience function should work - it will use the current working directory
  // since we didn't set up environment variables in this minimal test
  let ws_result = workspace_tools ::workspace();
  assert!( ws_result.is_ok() );
  let ws = ws_result.unwrap();
  // The convenience function returns the current workspace, not the temp dir
  assert!( ws.root().exists() );
}

/// Test FC.7 : Performance with all features enabled
#[ cfg( all( 
  feature = "serde", 
  feature = "serde", 
  feature = "glob", 
  feature = "secrets"
) ) ]
#[ test ]
fn test_all_features_performance()
{
  use std ::time ::Instant;
  
  let temp_dir = TempDir ::new().unwrap();
  
  // Create cargo workspace
  fs ::write( temp_dir.path().join( "Cargo.toml" ), "[workspace]\nmembers = []\n" ).unwrap();
  
  let start = Instant ::now();
  
  // Create workspace using cargo
  let workspace = Workspace ::from_cargo_manifest( temp_dir.path().join( "Cargo.toml" ) ).unwrap();
  
  // Perform multiple operations quickly
  for i in 0..100
  {
  let _joined = workspace.join( format!( "file_{i}.txt" ) );
  let _config_dir = workspace.config_dir();
  let _is_cargo = workspace.is_cargo_workspace();
 }
  
  let duration = start.elapsed();
  
  // Should complete quickly (within reasonable time)
  assert!( duration.as_millis() < 1000, "Operations should complete within 1 second" );
}

/// Test FC.8 : Feature interaction edge cases
#[ cfg( all( feature = "serde", feature = "serde" ) ) ]
#[ test ]
fn test_feature_interaction_edge_cases()
{
  use serde :: { Serialize, Deserialize };
  
  #[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
  struct EdgeConfig
  {
  name: String,
  values: Vec< i32 >,
 }
  
  let temp_dir = TempDir ::new().unwrap();
  
  // Create minimal cargo workspace
  fs ::write( temp_dir.path().join( "Cargo.toml" ), "[workspace]\nmembers = []\n" ).unwrap();
  
  let workspace = Workspace ::from_cargo_manifest( temp_dir.path().join( "Cargo.toml" ) ).unwrap();
  
  // Create config directory
  fs ::create_dir_all( workspace.config_dir() ).unwrap();
  
  // Test edge case: empty config
  let empty_config = EdgeConfig {
  name: String ::new(),
  values: vec![],
 };
  
  let save_result = workspace.save_config( "empty", &empty_config );
  assert!( save_result.is_ok(), "Should save empty config" );
  
  let loaded: Result< EdgeConfig, WorkspaceError > = workspace.load_config( "empty" );
  assert!( loaded.is_ok(), "Should load empty config" );
  assert_eq!( loaded.unwrap(), empty_config );
  
  // Test edge case: large config
  let large_config = EdgeConfig {
  name: "x".repeat( 1000 ),
  values: (0..1000).collect(),
 };
  
  let save_large = workspace.save_config( "large", &large_config );
  assert!( save_large.is_ok(), "Should save large config" );
  
  let loaded_large: Result< EdgeConfig, WorkspaceError > = workspace.load_config( "large" );
  assert!( loaded_large.is_ok(), "Should load large config" );
  assert_eq!( loaded_large.unwrap(), large_config );
}