alf_tui 0.5.0

A smolderingly-nimble Rust TUI to rediscover your custom shell.
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
//! Tests for the CLI config command (source file resolution)

use super::{add_source_files, resolve_new_source_files};
use crate::config::{get_config_path, load_config, save_config, Config, ConfigLock, GeneralConfig};
use crate::test_support::TempHome;
use std::fs;

/// Comment stamped onto a seeded config so tests can detect whether it was rewritten
///
/// `toml::to_string_pretty` never emits comments, so a surviving marker proves `save_config` was
/// not called — which a value comparison alone cannot show when the values are meant to be equal.
const CONFIG_MARKER: &str = "\n# alf-test-marker\n";

fn config_with(shell_files: &[&str]) -> Config {
   Config {
      general: GeneralConfig {
         shell_files: shell_files.iter().map(|file| file.to_string()).collect(),
         ..Default::default()
      },
      ..Default::default()
   }
}

/// Create `<home>/dotfiles/<target_name>` and point `<home>/<link_name>` at it as a symlink,
/// returning the absolute path of the target
#[cfg(unix)]
fn link_to_dotfile(
   home: &TempHome,
   target_name: &str,
   link_name: &str,
) -> String {
   let dotfiles = home.path().join("dotfiles");
   fs::create_dir_all(&dotfiles).expect("Should create dotfiles dir");
   let target = dotfiles.join(target_name);
   fs::File::create(&target).expect("Should create target file");
   std::os::unix::fs::symlink(&target, home.path().join(link_name)).expect("Should create symlink");
   target.to_string_lossy().to_string()
}

fn raw(paths: &[&str]) -> Vec<String> {
   paths.iter().map(|path| path.to_string()).collect()
}

// ===== Adding new source files =====

#[test]
fn test_resolve_new_source_files_adds_a_single_existing_file() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   let config = config_with(&[]);
   let (to_add, duplicates) =
      resolve_new_source_files(&config, &raw(&["~/.work_aliases"])).expect("Should resolve existing file");
   assert_eq!(to_add, vec!["~/.work_aliases".to_string()]);
   assert!(duplicates.is_empty());
}

#[test]
fn test_resolve_new_source_files_adds_multiple_existing_files() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   home.touch(".personal_aliases");
   let config = config_with(&[]);
   let paths = raw(&["~/.work_aliases", "~/.personal_aliases"]);
   let (to_add, duplicates) = resolve_new_source_files(&config, &paths).expect("Should resolve existing files");
   assert_eq!(to_add, paths);
   assert!(duplicates.is_empty());
}

#[test]
fn test_resolve_new_source_files_preserves_the_raw_path_form() {
   let home = TempHome::new();
   home.touch(".zshrc");

   for path_form in ["~/.zshrc", "$HOME/.zshrc"] {
      let config = config_with(&[]);
      let (to_add, _) = resolve_new_source_files(&config, &raw(&[path_form])).expect("Should resolve existing file");
      assert_eq!(to_add, vec![path_form.to_string()], "Expected {path_form} to be stored as typed");
   }
}

// ===== Relative paths =====

#[test]
fn test_resolve_new_source_files_rejects_relative_paths() {
   let _home = TempHome::new();
   let config = config_with(&[]);

   for relative in ["aliases.sh", "./aliases.sh", "../aliases.sh", "nested/aliases.sh", "../../deep/aliases.sh"] {
      let error = resolve_new_source_files(&config, &raw(&[relative])).expect_err("Should reject a relative path");
      assert!(
         error.to_string().contains("Relative paths are not allowed"),
         "Expected a relative-path error for {relative}, got: {error}"
      );
   }
}

#[test]
fn test_resolve_new_source_files_rejects_a_relative_path_that_exists() {
   let _home = TempHome::new();
   let config = config_with(&[]);

   // Cargo runs tests from the package root, so these resolve to a file that really is on disk
   for relative in ["Cargo.toml", "./Cargo.toml"] {
      let error =
         resolve_new_source_files(&config, &raw(&[relative])).expect_err("Should reject a relative path on disk");
      assert!(
         error.to_string().contains("Relative paths are not allowed"),
         "Existence should not excuse the relative path {relative}, got: {error}"
      );
   }
}

#[test]
fn test_resolve_new_source_files_reports_relativeness_before_missing_files() {
   let _home = TempHome::new();
   let config = config_with(&[]);
   let error = resolve_new_source_files(&config, &raw(&["~/missing.sh", "./aliases.sh"]))
      .expect_err("Should reject the invocation");
   assert!(
      error.to_string().contains("Relative paths are not allowed"),
      "Relative paths should be reported before missing files, got: {error}"
   );
}

#[test]
fn test_resolve_new_source_files_rejects_a_relative_path_mixed_with_valid_paths() {
   let home = TempHome::new();
   home.touch(".zshrc");
   let config = config_with(&[]);
   let result = resolve_new_source_files(&config, &raw(&["~/.zshrc", "../aliases.sh"]));
   assert!(result.is_err(), "A single relative path should reject the whole invocation");
}

// ===== Missing files =====

#[test]
fn test_resolve_new_source_files_errors_when_a_path_is_missing() {
   let _home = TempHome::new();
   let config = config_with(&[]);

   for missing in ["~/missing.sh", "$HOME/missing.sh", "/nope/missing.sh"] {
      let error = resolve_new_source_files(&config, &raw(&[missing])).expect_err("Should reject a missing file");
      assert!(
         error.to_string().contains("Shell file not found"),
         "Expected a not-found error for {missing}, got: {error}"
      );
   }
}

#[test]
fn test_resolve_new_source_files_reports_the_expanded_path_when_missing() {
   let home = TempHome::new();
   let config = config_with(&[]);
   let error = resolve_new_source_files(&config, &raw(&["~/missing.sh"])).expect_err("Should reject a missing file");
   assert!(
      error.to_string().contains(&home.absolute("missing.sh")),
      "Expected the expanded path in the error, got: {error}"
   );
}

#[test]
fn test_resolve_new_source_files_errors_before_adding_when_the_list_is_mixed() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   let config = config_with(&[]);
   let result = resolve_new_source_files(&config, &raw(&["~/.work_aliases", "~/missing.sh"]));
   assert!(result.is_err(), "A single missing path should reject the whole invocation");
}

// ===== Non-regular files =====

#[test]
fn test_resolve_new_source_files_errors_when_a_path_is_a_directory() {
   let home = TempHome::new();
   fs::create_dir_all(home.path().join("dotfiles")).expect("Should create dotfiles dir");
   let config = config_with(&[]);
   let error = resolve_new_source_files(&config, &raw(&["~/dotfiles"])).expect_err("Should reject a directory");
   assert!(
      error.to_string().contains("not a regular file"),
      "Expected a not-a-regular-file error for a directory, got: {error}"
   );
}

#[cfg(unix)]
#[test]
fn test_resolve_new_source_files_errors_when_a_path_is_a_fifo() {
   let home = TempHome::new();
   let fifo = home.path().join(".fifo_aliases");
   let status = std::process::Command::new("mkfifo").arg(&fifo).status().expect("Should run mkfifo");
   assert!(status.success(), "Should create the FIFO");
   let config = config_with(&[]);
   let error = resolve_new_source_files(&config, &raw(&["~/.fifo_aliases"])).expect_err("Should reject a FIFO");
   assert!(
      error.to_string().contains("not a regular file"),
      "Expected a not-a-regular-file error for a FIFO, got: {error}"
   );
}

#[cfg(unix)]
#[test]
fn test_resolve_new_source_files_accepts_a_symlink_to_a_regular_file() {
   let home = TempHome::new();
   link_to_dotfile(&home, "work_aliases", ".work_aliases");
   let config = config_with(&[]);
   let (to_add, duplicates) =
      resolve_new_source_files(&config, &raw(&["~/.work_aliases"])).expect("Should accept a symlinked file");
   assert_eq!(to_add, vec!["~/.work_aliases".to_string()]);
   assert!(duplicates.is_empty());
}

#[test]
fn test_add_source_files_does_not_write_when_a_path_is_a_directory() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   fs::create_dir_all(home.path().join("dotfiles")).expect("Should create dotfiles dir");
   seed_marked_config(&[]);
   add_source_files(&raw(&["~/.work_aliases", "~/dotfiles"])).expect_err("Should reject the directory");
   assert!(is_config_unwritten(), "A rejected add should leave the config untouched");
}

// ===== Duplicates =====

#[test]
fn test_resolve_new_source_files_detects_duplicates_across_path_forms() {
   let home = TempHome::new();
   home.touch(".zshrc");
   let absolute = home.absolute(".zshrc");

   for (stored, argument) in [
      ("~/.zshrc", absolute.as_str()),
      ("$HOME/.zshrc", absolute.as_str()),
      (absolute.as_str(), "~/.zshrc"),
      ("~/.zshrc", "~/.zshrc"),
   ] {
      let config = config_with(&[stored]);
      let (to_add, duplicates) =
         resolve_new_source_files(&config, &raw(&[argument])).expect("Should resolve existing file");
      assert!(to_add.is_empty(), "Expected no additions for stored {stored} and argument {argument}");
      assert_eq!(duplicates, vec![argument.to_string()]);
   }
}

#[cfg(unix)]
#[test]
fn test_resolve_new_source_files_detects_a_symlink_and_its_target_as_one_file() {
   let home = TempHome::new();
   let target = link_to_dotfile(&home, "zshrc", ".zshrc");

   for (stored, argument) in [("~/.zshrc", target.as_str()), (target.as_str(), "~/.zshrc")] {
      let config = config_with(&[stored]);
      let (to_add, duplicates) =
         resolve_new_source_files(&config, &raw(&[argument])).expect("Should resolve a symlinked file");
      assert!(to_add.is_empty(), "Expected no additions for stored {stored} and argument {argument}");
      assert_eq!(duplicates, vec![argument.to_string()]);
   }
}

#[cfg(unix)]
#[test]
fn test_resolve_new_source_files_dedupes_a_symlink_and_its_target_in_one_invocation() {
   let home = TempHome::new();
   let target = link_to_dotfile(&home, "zshrc", ".zshrc");
   let config = config_with(&[]);
   let (to_add, duplicates) =
      resolve_new_source_files(&config, &raw(&["~/.zshrc", target.as_str()])).expect("Should resolve a symlinked file");
   assert_eq!(to_add, vec!["~/.zshrc".to_string()]);
   assert_eq!(duplicates, vec![target]);
}

#[test]
fn test_resolve_new_source_files_treats_a_dot_dot_segment_as_the_same_file() {
   let home = TempHome::new();
   home.touch(".zshrc");
   let round_trip = home.absolute(".config/../.zshrc");
   fs::create_dir_all(home.path().join(".config")).expect("Should create dir");
   let config = config_with(&["~/.zshrc"]);
   let (to_add, duplicates) =
      resolve_new_source_files(&config, &raw(&[round_trip.as_str()])).expect("Should resolve the file");
   assert!(to_add.is_empty(), "A `..` round trip should not add a second entry");
   assert_eq!(duplicates, vec![round_trip]);
}

#[test]
fn test_resolve_new_source_files_tolerates_a_stale_configured_entry() {
   let home = TempHome::new();
   home.touch(".zshrc");
   let config = config_with(&["~/gone.sh"]);
   let (to_add, duplicates) =
      resolve_new_source_files(&config, &raw(&["~/.zshrc"])).expect("An unresolvable entry should not fail the add");
   assert_eq!(to_add, vec!["~/.zshrc".to_string()]);
   assert!(duplicates.is_empty());
}

#[test]
fn test_resolve_new_source_files_dedupes_repeats_within_one_invocation() {
   let home = TempHome::new();
   home.touch(".zshrc");
   let config = config_with(&[]);
   let paths = raw(&["~/.zshrc", "~/.zshrc"]);
   let (to_add, duplicates) = resolve_new_source_files(&config, &paths).expect("Should resolve existing file");
   assert_eq!(to_add, vec!["~/.zshrc".to_string()]);
   assert_eq!(duplicates, vec!["~/.zshrc".to_string()]);
}

// ===== Persisting added source files =====

/// Save a config holding `shell_files` and stamp it with [`CONFIG_MARKER`]
fn seed_marked_config(shell_files: &[&str]) {
   save_config(&config_with(shell_files)).expect("Should seed config");
   let path = get_config_path().expect("Should resolve config path");
   let mut content = fs::read_to_string(&path).expect("Should read seeded config");
   content.push_str(CONFIG_MARKER);
   fs::write(&path, content).expect("Should stamp seeded config");
}

/// The `shell_files` currently recorded on disk
fn configured_shell_files() -> Vec<String> {
   load_config().expect("Should load config").general.shell_files
}

/// Whether the seeded config still carries its marker, i.e. it was never rewritten
fn is_config_unwritten() -> bool {
   let path = get_config_path().expect("Should resolve config path");
   fs::read_to_string(&path).expect("Should read config").contains(CONFIG_MARKER)
}

#[test]
fn test_add_source_files_persists_the_added_path() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   seed_marked_config(&[]);
   add_source_files(&raw(&["~/.work_aliases"])).expect("Should add the source file");
   assert_eq!(configured_shell_files(), vec!["~/.work_aliases".to_string()]);
   assert!(!is_config_unwritten(), "Adding a new file should rewrite the config");
}

#[test]
fn test_add_source_files_appends_to_the_existing_list() {
   let home = TempHome::new();
   home.touch(".zshrc");
   home.touch(".work_aliases");
   seed_marked_config(&["~/.zshrc"]);
   add_source_files(&raw(&["~/.work_aliases"])).expect("Should add the source file");
   assert_eq!(configured_shell_files(), raw(&["~/.zshrc", "~/.work_aliases"]));
}

#[test]
fn test_add_source_files_persists_multiple_paths_in_one_invocation() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   home.touch(".personal_aliases");
   seed_marked_config(&[]);
   let paths = raw(&["~/.work_aliases", "~/.personal_aliases"]);
   add_source_files(&paths).expect("Should add the source files");
   assert_eq!(configured_shell_files(), paths);
}

#[test]
fn test_add_source_files_persists_only_the_new_paths_when_some_are_duplicates() {
   let home = TempHome::new();
   home.touch(".zshrc");
   home.touch(".work_aliases");
   seed_marked_config(&["~/.zshrc"]);
   let absolute = home.absolute(".zshrc");
   add_source_files(&raw(&[absolute.as_str(), "~/.work_aliases"])).expect("Should add the new source file");
   assert_eq!(configured_shell_files(), raw(&["~/.zshrc", "~/.work_aliases"]));
}

#[test]
fn test_add_source_files_persists_the_raw_path_form() {
   let home = TempHome::new();
   home.touch(".zshrc");
   seed_marked_config(&[]);
   add_source_files(&raw(&["$HOME/.zshrc"])).expect("Should add the source file");
   assert_eq!(configured_shell_files(), vec!["$HOME/.zshrc".to_string()], "The typed form should be what is stored");
}

// ===== Leaving the config untouched =====

#[test]
fn test_add_source_files_does_not_write_when_every_path_is_a_duplicate() {
   let home = TempHome::new();
   home.touch(".zshrc");
   seed_marked_config(&["~/.zshrc"]);
   let absolute = home.absolute(".zshrc");
   add_source_files(&raw(&[absolute.as_str()])).expect("A duplicate should not fail the add");
   assert!(is_config_unwritten(), "An all-duplicate add should not rewrite the config");
   assert_eq!(configured_shell_files(), vec!["~/.zshrc".to_string()]);
}

#[test]
fn test_add_source_files_leaves_the_config_untouched_when_a_path_is_missing() {
   let home = TempHome::new();
   home.touch(".zshrc");
   home.touch(".work_aliases");
   seed_marked_config(&["~/.zshrc"]);
   let error = add_source_files(&raw(&["~/.work_aliases", "~/missing.sh"])).expect_err("Should reject the invocation");
   assert!(error.to_string().contains("Shell file not found"), "Expected a not-found error, got: {error}");
   assert!(is_config_unwritten(), "A rejected add should not rewrite the config");
   assert_eq!(configured_shell_files(), vec!["~/.zshrc".to_string()]);
}

#[test]
fn test_add_source_files_leaves_the_config_untouched_when_a_path_is_relative() {
   let home = TempHome::new();
   home.touch(".zshrc");
   home.touch(".work_aliases");
   seed_marked_config(&["~/.zshrc"]);
   let error = add_source_files(&raw(&["~/.work_aliases", "../aliases.sh"])).expect_err("Should reject the invocation");
   assert!(error.to_string().contains("Relative paths are not allowed"), "Expected a relative error, got: {error}");
   assert!(is_config_unwritten(), "A rejected add should not rewrite the config");
   assert_eq!(configured_shell_files(), vec!["~/.zshrc".to_string()]);
}

// ===== First run =====

#[test]
fn test_add_source_files_bails_when_no_config_exists() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   let error = add_source_files(&raw(&["~/.work_aliases"])).expect_err("Should bail without a config");
   assert!(error.to_string().contains("Run `alf init` to create one."), "Expected an `alf init` hint, got: {error}");
}

#[test]
fn test_add_source_files_reports_the_config_path_when_no_config_exists() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   let expected = get_config_path().expect("Should resolve config path");
   let error = add_source_files(&raw(&["~/.work_aliases"])).expect_err("Should bail without a config");
   assert!(
      error.to_string().contains(&expected.display().to_string()),
      "Expected the config path in the error, got: {error}"
   );
}

#[test]
fn test_add_source_files_bails_before_validating_paths_when_no_config_exists() {
   let _home = TempHome::new();
   let error = add_source_files(&raw(&["../aliases.sh"])).expect_err("Should bail without a config");
   assert!(
      error.to_string().contains("No config found at"),
      "A missing config should be reported before path validation, got: {error}"
   );
}

// ===== Locking the config during the add =====

/// Assert that no lock is still held, by taking and releasing one
fn assert_lock_is_free(context: &str) {
   let lock = ConfigLock::acquire().unwrap_or_else(|_| panic!("The lock should be free {context}"));
   drop(lock);
}

#[test]
fn test_add_source_files_releases_the_lock_after_a_successful_add() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   seed_marked_config(&[]);
   add_source_files(&raw(&["~/.work_aliases"])).expect("Should add the source file");
   assert_lock_is_free("after a successful add");
}

#[test]
fn test_add_source_files_releases_the_lock_after_a_failed_add() {
   let home = TempHome::new();
   home.touch(".work_aliases");
   seed_marked_config(&[]);

   for paths in [raw(&["relative/path"]), raw(&["~/.does_not_exist"])] {
      add_source_files(&paths).expect_err("Should reject the path");
      assert_lock_is_free("after a failed add");
   }
}

#[test]
fn test_add_source_files_releases_the_lock_after_an_all_duplicate_add() {
   let home = TempHome::new();
   home.touch(".zshrc");
   seed_marked_config(&["~/.zshrc"]);
   add_source_files(&raw(&["~/.zshrc"])).expect("A duplicate should not fail the add");
   assert_lock_is_free("after an early return");
}