app-path 1.1.2

Create file paths relative to your executable for truly portable applications
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use crate::{app_path, AppPath};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

// === Path Component Tests ===

#[test]
fn test_file_name() {
    let path = app_path!("config.toml");
    assert_eq!(path.file_name(), Some(OsStr::new("config.toml")));

    let path_with_dir = app_path!("config/app.toml");
    assert_eq!(path_with_dir.file_name(), Some(OsStr::new("app.toml")));

    let dir_path = app_path!("config/");
    assert_eq!(dir_path.file_name(), Some(OsStr::new("config")));
}

#[test]
fn test_file_stem() {
    let path = app_path!("config.toml");
    assert_eq!(path.file_stem(), Some(OsStr::new("config")));

    let complex_name = app_path!("app.config.toml");
    assert_eq!(complex_name.file_stem(), Some(OsStr::new("app.config")));

    let no_extension = app_path!("README");
    assert_eq!(no_extension.file_stem(), Some(OsStr::new("README")));
}

#[test]
fn test_extension() {
    let toml_file = app_path!("config.toml");
    assert_eq!(toml_file.extension(), Some(OsStr::new("toml")));

    let json_file = app_path!("data.json");
    assert_eq!(json_file.extension(), Some(OsStr::new("json")));

    let no_extension = app_path!("README");
    assert_eq!(no_extension.extension(), None);

    let multiple_dots = app_path!("archive.tar.gz");
    assert_eq!(multiple_dots.extension(), Some(OsStr::new("gz")));
}

#[test]
fn test_parent() {
    let nested_path = app_path!("config/app.toml");
    let parent = nested_path.parent().unwrap();
    assert!(parent.ends_with("config"));

    let root_file = app_path!("app.toml");
    let parent_of_root = root_file.parent().unwrap();
    // Parent should be the exe directory
    assert_eq!(
        &*parent_of_root,
        std::env::current_exe().unwrap().parent().unwrap()
    );
}

// === Path Joining and Manipulation ===

#[test]
fn test_join() {
    let base = app_path!("config");
    let joined = base.join("app.toml");
    assert!(joined.ends_with("config/app.toml") || joined.ends_with("config\\app.toml"));

    let base_file = app_path!("config.toml");
    let joined_to_file = base_file.join("nested");
    assert!(
        joined_to_file.ends_with("config.toml/nested")
            || joined_to_file.ends_with("config.toml\\nested")
    );
}

#[test]
fn test_with_file_name() {
    let original = app_path!("config.toml");
    let renamed = AppPath::with(original.with_file_name("settings.toml"));
    assert!(renamed.ends_with("settings.toml"));
    assert!(!renamed.ends_with("config.toml"));

    // Should maintain the same parent directory
    assert_eq!(original.parent(), renamed.parent());
}

#[test]
fn test_with_extension() {
    let toml_file = app_path!("config.toml");
    let json_file = toml_file.with_extension("json");
    assert!(json_file.ends_with("config.json"));
    assert!(!json_file.ends_with("config.toml"));

    let no_ext_file = app_path!("README");
    let with_ext = no_ext_file.with_extension("md");
    assert!(with_ext.ends_with("README.md"));
}

// === Path Comparison and Relationships ===

#[test]
fn test_starts_with() {
    let exe_exe = std::env::current_exe().unwrap();
    let exe_path = exe_exe.parent().unwrap();
    let config_path = app_path!("config.toml");

    // App paths should start with the exe directory
    assert!(config_path.starts_with(exe_path));

    let nested_path = app_path!("config/app.toml");
    assert!(nested_path.starts_with(exe_path));
    assert!(nested_path.starts_with(config_path.parent().unwrap()));
}

#[test]
fn test_ends_with() {
    let config_path = app_path!("config.toml");
    assert!(config_path.ends_with("config.toml"));

    let nested_path = app_path!("data/settings/app.toml");
    assert!(nested_path.ends_with("app.toml"));
    assert!(nested_path.ends_with("settings/app.toml"));
    assert!(nested_path.ends_with("data/settings/app.toml"));
}

#[test]
fn test_strip_prefix() {
    let exe_exe = std::env::current_exe().unwrap();
    let exe_path = exe_exe.parent().unwrap();
    let config_path = app_path!("config/app.toml");

    let relative = config_path.strip_prefix(exe_path).unwrap();
    assert_eq!(relative, Path::new("config/app.toml"));
}

// === Path Canonicalization and Absolute Paths ===

#[test]
fn test_is_absolute() {
    let app_path = app_path!("config.toml");
    assert!(app_path.is_absolute());

    let nested_path = app_path!("config/deep/nested/file.toml");
    assert!(nested_path.is_absolute());
}

#[test]
fn test_is_relative() {
    let app_path = app_path!("config.toml");
    assert!(!app_path.is_relative());

    // All app paths should be absolute
    let any_path = app_path!("any/path/structure.toml");
    assert!(!any_path.is_relative());
}

// === Component Iteration ===

#[test]
fn test_components() {
    let path = app_path!("config/nested/file.toml");
    let components: Vec<_> = path.components().collect();

    // Should have multiple components including the file name
    assert!(components.len() > 1);

    // Last component should be the file
    let last = components.last().unwrap();
    assert_eq!(last.as_os_str(), "file.toml");
}

#[test]
fn test_iter() {
    let path = app_path!("config/app.toml");
    let parts: Vec<_> = path.iter().collect();

    // Should contain at least the config directory and filename
    assert!(parts.contains(&OsStr::new("config")));
    assert!(parts.contains(&OsStr::new("app.toml")));
}

// === Path Creation and Ancestors ===

#[test]
fn test_ancestors() {
    let nested_path = app_path!("config/deep/nested/file.toml");
    let ancestors: Vec<_> = nested_path.ancestors().collect();

    // Should include the path itself and all parent directories
    assert!(ancestors.len() > 3);
    assert_eq!(ancestors[0], &*nested_path);
    assert!(ancestors[1].ends_with("nested"));
    assert!(ancestors[2].ends_with("deep"));
    assert!(ancestors[3].ends_with("config"));
}

// === String Conversion and Display ===

#[test]
fn test_to_string_lossy() {
    let path = app_path!("config.toml");
    let string_repr = path.to_string_lossy();
    assert!(string_repr.ends_with("config.toml"));
}

#[test]
fn test_to_path_buf() {
    let app_path = app_path!("config.toml");
    let path_buf: PathBuf = app_path.to_path_buf();
    assert_eq!(&*app_path, path_buf.as_path());
}

#[test]
fn test_as_os_str() {
    let path = app_path!("config.toml");
    let os_str = path.as_os_str();
    assert!(os_str.to_string_lossy().ends_with("config.toml"));
}

// === Complex Path Manipulations ===

#[test]
fn test_complex_path_building() {
    let base = app_path!("data");
    let config_dir = base.join("config");
    let settings_file = config_dir.join("settings.toml");
    let backup_file = settings_file.with_extension("backup");

    assert!(
        backup_file.ends_with("data/config/settings.backup")
            || backup_file.ends_with("data\\config\\settings.backup")
    );
    assert!(backup_file.starts_with(std::env::current_exe().unwrap().parent().unwrap()));
}

#[test]
fn test_path_normalization() {
    // Test that redundant path components are handled
    let path = app_path!("config/../config/app.toml");
    let normalized = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());

    // Should still be valid and end with the expected file
    assert!(normalized.ends_with("config/app.toml") || normalized.ends_with("config\\app.toml"));
}

#[test]
fn test_path_with_special_characters() {
    let special_path = app_path!("config with spaces.toml");
    assert!(special_path.ends_with("config with spaces.toml"));
    assert_eq!(
        special_path.file_name(),
        Some(OsStr::new("config with spaces.toml"))
    );

    let unicode_path = app_path!("configürâtion.toml");
    assert!(unicode_path.ends_with("configürâtion.toml"));
    assert_eq!(unicode_path.file_stem(), Some(OsStr::new("configürâtion")));
}

// === Platform-Specific Path Tests ===

#[cfg(windows)]
#[test]
fn test_windows_path_separators() {
    let path = app_path!("config\\app.toml");
    assert!(path.ends_with("config\\app.toml") || path.ends_with("config/app.toml"));

    // Test that forward slashes are normalized on Windows
    let forward_slash_path = app_path!("config/app.toml");
    let backslash_path = app_path!("config\\app.toml");

    // Both should reference the same logical path
    assert_eq!(forward_slash_path.file_name(), backslash_path.file_name());
}

#[cfg(unix)]
#[test]
fn test_unix_path_separators() {
    let path = app_path!("config/app.toml");
    assert!(path.ends_with("config/app.toml"));
    assert_eq!(path.file_name(), Some(OsStr::new("app.toml")));
}

// === Edge Cases ===

#[test]
fn test_root_file_manipulation() {
    let root_file = app_path!("app.toml");

    // Should be able to get parent (exe directory)
    let parent = root_file.parent().unwrap();
    assert_eq!(&*parent, std::env::current_exe().unwrap().parent().unwrap());

    // Should be able to change extension
    let json_version = root_file.with_extension("json");
    assert!(json_version.ends_with("app.json"));

    // Should be able to rename
    let renamed = AppPath::with(root_file.with_file_name("settings.toml"));
    assert!(renamed.ends_with("settings.toml"));
    assert_eq!(renamed.parent(), root_file.parent());
}

#[test]
fn test_empty_path_components() {
    // Test paths with empty components
    let path_with_double_slash = app_path!("config//app.toml");
    assert!(path_with_double_slash.ends_with("app.toml"));

    let path_with_dot = app_path!("config/./app.toml");
    assert!(path_with_dot.ends_with("app.toml"));
}

#[test]
fn test_path_comparison() {
    let path1 = app_path!("config.toml");
    let path2 = app_path!("config.toml");
    let path3 = app_path!("settings.toml");

    assert_eq!(&*path1, &*path2);
    assert_ne!(&*path1, &*path3);

    // Test lexicographic ordering
    assert!(*path1 < *path3); // "config" < "settings"
}

// === into_inner() Method Tests ===

#[test]
fn test_into_inner_basic() {
    let app_path = app_path!("config.toml");
    let expected_path = app_path.to_path_buf();

    let inner_path: PathBuf = app_path.into_inner();

    assert_eq!(inner_path, expected_path);
    assert!(inner_path.is_absolute());
    assert!(inner_path.ends_with("config.toml"));
}

#[test]
fn test_into_path_buf_equivalence() {
    let app_path1 = app_path!("config.toml");
    let app_path2 = app_path!("config.toml");

    // Both methods should return equivalent results
    let via_into_inner = app_path1.into_inner();
    let via_into_path_buf = app_path2.into_path_buf();

    assert_eq!(via_into_inner, via_into_path_buf);
    assert!(via_into_path_buf.is_absolute());
    assert!(via_into_path_buf.ends_with("config.toml"));
}

#[test]
fn test_into_inner_with_nested_path() {
    let app_path = app_path!("config/settings/app.toml");
    let expected_path = app_path.to_path_buf();

    let inner_path: PathBuf = app_path.into_inner();

    assert_eq!(inner_path, expected_path);
    assert!(inner_path.is_absolute());
    assert!(inner_path.ends_with("config/settings/app.toml"));
}

#[test]
fn test_into_inner_with_directory_path() {
    let app_path = app_path!("data/cache/");
    let expected_path = app_path.to_path_buf();

    let inner_path: PathBuf = app_path.into_inner();

    assert_eq!(inner_path, expected_path);
    assert!(inner_path.is_absolute());
    assert!(inner_path.ends_with("data/cache"));
}

#[test]
fn test_into_inner_type_consistency() {
    let app_path = app_path!("test.txt");

    // Verify the returned type is exactly PathBuf
    let inner: PathBuf = app_path.into_inner();

    // Should be able to use all PathBuf methods
    let _display = inner.display();
    let _components: Vec<_> = inner.components().collect();
    let _extension = inner.extension();
    let _file_name = inner.file_name();

    // Should be convertible to standard path types
    let _path_ref: &Path = inner.as_path();
    let _os_str = inner.as_os_str();
}

#[test]
fn test_into_inner_ownership_transfer() {
    let app_path = app_path!("owned.txt");
    let original_path = app_path.to_path_buf();

    // Move ownership with into_inner
    let inner_path = app_path.into_inner();

    // Verify the path is the same
    assert_eq!(inner_path, original_path);

    // app_path is now consumed and cannot be used
    // This test verifies that we truly get ownership of the inner PathBuf
    drop(inner_path); // Explicit drop to show ownership
}

#[test]
fn test_into_inner_with_special_characters() {
    let app_path = app_path!("files with spaces/üñíçøðé.txt");
    let expected_path = app_path.to_path_buf();

    let inner_path: PathBuf = app_path.into_inner();

    assert_eq!(inner_path, expected_path);
    assert!(inner_path.is_absolute());
    assert!(inner_path.to_string_lossy().contains("üñíçøðé.txt"));
}

#[test]
fn test_into_inner_with_override() {
    // Test case 1: Override with a custom path (completely replaces default)
    let custom_path = std::env::temp_dir().join("custom_config.toml");
    let app_path = AppPath::with_override("config.toml", Some(&custom_path));
    let inner_path: PathBuf = app_path.into_inner();

    // When override is Some, it completely replaces the default path
    assert_eq!(inner_path, custom_path);
    assert!(inner_path.is_absolute());
    assert!(inner_path.ends_with("custom_config.toml"));

    // Test case 2: No override, should use default relative to exe_dir
    let app_path_default = AppPath::with_override("config.toml", None::<&str>);
    let inner_path_default: PathBuf = app_path_default.into_inner();
    let expected_default = std::env::current_exe()
        .unwrap()
        .parent()
        .unwrap()
        .join("config.toml");

    assert_eq!(inner_path_default, expected_default);
    assert!(inner_path_default.ends_with("config.toml"));
}

// === Byte Conversion Tests ===

#[test]
fn test_to_bytes_basic() {
    let path = app_path!("config.toml");
    let bytes = path.to_bytes();

    // Basic byte functionality
    assert!(!bytes.is_empty());
    assert!(!bytes.is_empty());

    // Should be able to get bytes multiple times
    let bytes2 = path.to_bytes();
    assert_eq!(bytes, bytes2);
}

#[test]
fn test_to_bytes_returns_vec() {
    let path = app_path!("test.txt");
    let bytes = path.to_bytes();

    // Should return Vec<u8>
    let _vec: Vec<u8> = bytes.clone();

    // Should be able to iterate over bytes
    let byte_count = bytes.len();
    assert_eq!(byte_count, bytes.len());
}

#[test]
fn test_to_bytes_with_unicode() {
    let path = app_path!("配置.toml");
    let bytes = path.to_bytes();

    // Unicode paths should produce valid bytes
    assert!(!bytes.is_empty());

    // Bytes should be different from ASCII-only path
    let ascii_path = app_path!("config.toml");
    let ascii_bytes = ascii_path.to_bytes();
    assert_ne!(bytes, ascii_bytes);
}

#[test]
fn test_to_bytes_with_special_chars() {
    let path = app_path!("config with spaces.toml");
    let bytes = path.to_bytes();

    // Special characters should be encoded in bytes
    assert!(!bytes.is_empty());

    // Different from path without spaces
    let no_spaces = app_path!("config.toml");
    assert_ne!(bytes, no_spaces.to_bytes());
}

#[test]
fn test_into_bytes_basic() {
    let path = app_path!("config.toml");
    let original_bytes = path.to_bytes().to_vec();

    // Recreate path since into_bytes consumes it
    let path2 = app_path!("config.toml");
    let owned_bytes = path2.into_bytes();

    // Should return Vec<u8> with same content
    assert_eq!(owned_bytes, original_bytes);
    assert!(!owned_bytes.is_empty());
}

#[test]
fn test_into_bytes_returns_vec() {
    let path = app_path!("test.txt");
    let owned_bytes = path.into_bytes();

    // Should return Vec<u8>
    let _vec: Vec<u8> = owned_bytes.clone();

    // Should be able to use Vec methods
    assert!(owned_bytes.capacity() >= owned_bytes.len());
    let mut mutable_bytes = owned_bytes;
    mutable_bytes.push(0); // Should be able to mutate
    assert!(!mutable_bytes.is_empty());
}

#[test]
fn test_into_bytes_ownership() {
    let path = app_path!("config.toml");
    let owned_bytes = path.into_bytes();

    // Should be able to move the bytes
    let moved_bytes = owned_bytes;
    assert!(!moved_bytes.is_empty());

    // Should be able to pass to functions expecting Vec<u8>
    fn takes_owned_bytes(bytes: Vec<u8>) -> usize {
        bytes.len()
    }
    let len = takes_owned_bytes(moved_bytes);
    assert!(len > 0);
}

#[test]
fn test_bytes_consistency_between_methods() {
    let path1 = app_path!("consistency_test.toml");
    let path2 = app_path!("consistency_test.toml");

    // Get bytes from first path (now returns Vec<u8>)
    let first_bytes = path1.to_bytes();

    // Get owned bytes from second path
    let owned_bytes = path2.into_bytes();

    // Should contain identical data
    assert_eq!(first_bytes, owned_bytes);
}

#[test]
fn test_bytes_different_paths_different_bytes() {
    let path1 = app_path!("file1.txt");
    let path2 = app_path!("file2.txt");

    let bytes1 = path1.to_bytes();
    let bytes2 = path2.to_bytes();

    // Different paths should produce different bytes
    assert_ne!(bytes1, bytes2);
}

#[test]
fn test_bytes_same_path_same_bytes() {
    let path1 = app_path!("same.txt");
    let path2 = app_path!("same.txt");

    let bytes1 = path1.to_bytes();
    let bytes2 = path2.to_bytes();

    // Same logical path should produce same bytes
    assert_eq!(bytes1, bytes2);
}

#[test]
fn test_bytes_with_path_operations() {
    let base = app_path!("config");
    let joined = base.join("app.toml");

    let base_bytes = base.to_bytes();
    let joined_bytes = joined.to_bytes();

    // Joined path bytes should be different and longer
    assert_ne!(base_bytes, joined_bytes);
    assert!(joined_bytes.len() > base_bytes.len());
}

#[test]
fn test_bytes_with_extension_changes() {
    let original = app_path!("config.toml");
    let with_json = original.with_extension("json");

    let original_bytes = original.to_bytes();
    let json_bytes = with_json.to_bytes();

    // Extension change should result in different bytes
    assert_ne!(original_bytes, json_bytes);
}

#[test]
fn test_bytes_empty_scenarios() {
    // Test with minimal path
    let minimal = app_path!("a");
    let bytes = minimal.to_bytes();
    assert!(!bytes.is_empty());

    // Even minimal paths should have some byte representation
    assert!(!bytes.is_empty());
}

#[test]
fn test_bytes_platform_encoding() {
    let path = app_path!("test.txt");
    let bytes = path.to_bytes();

    // Bytes should be valid platform-specific encoding
    assert!(!bytes.is_empty());

    // Should be consistent across multiple calls
    let bytes2 = path.to_bytes();
    assert_eq!(bytes, bytes2);

    // Length should be reasonable (not zero, not excessive)
    assert!(!bytes.is_empty());
    assert!(bytes.len() < 10000); // Reasonable upper bound for most paths
}

#[test]
fn test_bytes_cross_platform_compatibility() {
    // This test ensures our byte conversion methods use only stable Rust APIs
    // and work correctly across all platforms supported by GitHub Actions
    let path = app_path!("test-file.txt");

    // Test to_bytes() returns Vec<u8>
    let bytes = path.to_bytes();
    let _vec_check: Vec<u8> = bytes.clone(); // Verify return type
    assert!(!bytes.is_empty());

    // Test into_bytes() returns Vec<u8> and consumes the path
    let path2 = app_path!("test-file.txt");
    let owned_bytes = path2.into_bytes();
    let _vec_check2: Vec<u8> = owned_bytes.clone(); // Verify return type
    assert!(!owned_bytes.is_empty());

    // Both methods should produce identical results
    assert_eq!(bytes, owned_bytes);

    // Test with platform-specific path separators and special characters
    let complex_path = app_path!("földer/subfōlder/file-名前.txt");
    let complex_bytes = complex_path.to_bytes();
    assert!(!complex_bytes.is_empty());

    // Verify bytes are deterministic (same path = same bytes)
    let path3 = app_path!("földer/subfōlder/file-名前.txt");
    let bytes3 = path3.to_bytes();
    assert_eq!(complex_bytes, bytes3);
}