shine-cli 2.0.3

Give personal automation a reviewable lifecycle
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
use serde::Deserialize;
use std::path::{Path, PathBuf};

use super::{GLOBAL_CONFIG_FILE, PROJECT_CONFIG_FILE, tilde_expand};
use crate::home::default_config_and_presets_dir;

#[derive(Clone, Debug)]
pub(crate) struct ProjectConfig {
    pub(crate) path: PathBuf,
    pub(crate) root: PathBuf,
}

pub(crate) fn find_project_config(start: &Path) -> Option<ProjectConfig> {
    find_ancestor_file(start, PROJECT_CONFIG_FILE).and_then(|path| {
        let root = path.parent()?.to_path_buf();
        Some(ProjectConfig { root, path })
    })
}

fn find_ancestor_file(start: &Path, file_name: &str) -> Option<PathBuf> {
    for dir in start.ancestors() {
        let path = dir.join(file_name);
        if path.is_file() {
            return Some(path);
        }
    }
    None
}

/// Minimal view of a config file used before full `Config` deserialization.
#[derive(Deserialize)]
pub(super) struct MinimalConfig {
    #[serde(default)]
    pub(super) presets_dir: Option<PathBuf>,
    #[serde(default)]
    pub(super) presets_overlay_dir: Option<PathBuf>,
    #[serde(default)]
    pub(super) presets_overlay_git: Option<String>,
    #[serde(default)]
    pub(super) schema_version: u32,
}

pub(super) fn read_minimal_config(content: &str) -> Result<MinimalConfig, toml::de::Error> {
    toml::from_str(content)
}

/// Return the shine root dir implied by `SHINE_CONFIG_DIR`, or `default` if unset.
/// Used for a preliminary read of global config before full resolution.
pub(super) fn preliminary_shine_dir_from_env(default: &Path) -> PathBuf {
    if let Ok(val) = std::env::var("SHINE_CONFIG_DIR") {
        let val = val.trim().to_string();
        if !val.is_empty() {
            return PathBuf::from(tilde_expand(&val));
        }
    }
    default.to_owned()
}

/// Attempt to read the `presets_dir` key from an existing config without
/// doing a full parse. Returns `None` if the file is absent, unreadable, or the
/// key is not set.
pub(super) async fn read_presets_override_from_toml(config_path: &Path) -> Option<PathBuf> {
    let content = tokio::fs::read_to_string(config_path).await.ok()?;
    read_minimal_config(&content).ok()?.presets_dir
}

pub(super) fn resolve_config_presets_path(path: &Path, config_dir: &Path) -> PathBuf {
    if let Some(s) = path.to_str()
        && s.starts_with('~')
    {
        return PathBuf::from(tilde_expand(s));
    }

    if path.is_absolute() {
        path.to_path_buf()
    } else {
        config_dir.join(path)
    }
}

/// Paths needed by latency-sensitive, read-only commands such as dynamic shell
/// completion. Discovery never creates config or preset directories.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ReadOnlyRuntimePaths {
    pub(crate) shine_dir: PathBuf,
    pub(crate) presets_dir: PathBuf,
    pub(crate) presets_overlay_dir: Option<PathBuf>,
    pub(crate) is_external_presets: bool,
    pub(crate) managed_overlay: bool,
}

/// Resolve the active runtime paths without initializing `Config` or writing
/// any state. This mirrors the global/project preset layering in
/// `Config::load_or_init` while deliberately ignoring unrelated config keys.
pub(crate) fn discover_runtime_paths_read_only() -> Option<ReadOnlyRuntimePaths> {
    let (default_shine_dir, default_presets_dir) = default_config_and_presets_dir().ok()?;
    let preliminary_shine_dir = preliminary_shine_dir_from_env(&default_shine_dir);
    let global_path = preliminary_shine_dir.join(GLOBAL_CONFIG_FILE);
    let global_dir = global_path.parent().unwrap_or_else(|| Path::new("."));
    let global = read_minimal_config_file(&global_path);
    let global_presets = global
        .as_ref()
        .and_then(|config| config.presets_dir.as_deref())
        .map(|path| resolve_config_presets_path(path, global_dir));
    let (global_shine_dir, global_presets_dir, global_is_external) = resolve_runtime_config_dirs(
        &default_shine_dir,
        &default_presets_dir,
        global_presets.as_deref(),
        false,
    );
    let global_overlay = global.as_ref().and_then(|config| {
        config
            .presets_overlay_dir
            .as_deref()
            .map(|path| resolve_config_presets_path(path, global_dir))
            .or_else(|| {
                config
                    .presets_overlay_git
                    .as_ref()
                    .map(|_| global_shine_dir.join("overlay"))
                    .filter(|path| path.exists())
            })
    });
    let global_managed_overlay = global.as_ref().is_some_and(|config| {
        config.presets_overlay_dir.is_none() && config.presets_overlay_git.is_some()
    }) && global_overlay.is_some();

    let current_dir = std::env::current_dir().ok()?;
    let Some(project) = find_project_config(&current_dir) else {
        return Some(ReadOnlyRuntimePaths {
            shine_dir: global_shine_dir,
            presets_dir: global_presets_dir,
            presets_overlay_dir: global_overlay,
            is_external_presets: global_is_external,
            managed_overlay: global_managed_overlay,
        });
    };

    let project_config = read_minimal_config_file(&project.path);
    let project_presets = project_config
        .as_ref()
        .and_then(|config| config.presets_dir.as_deref())
        .map(|path| resolve_config_presets_path(path, &project.root));
    let effective_presets = project_presets
        .clone()
        .or_else(|| global_is_external.then_some(global_presets_dir.clone()));
    let runtime_presets = if project_presets.is_none()
        && std::env::var("SHINE_CONFIG_DIR").is_ok_and(|value| !value.trim().is_empty())
    {
        None
    } else {
        effective_presets
    };
    let (shine_dir, presets_dir, is_external_presets) = resolve_runtime_config_dirs(
        &default_shine_dir,
        &default_presets_dir,
        runtime_presets.as_deref(),
        true,
    );
    let presets_overlay_dir = project_config
        .as_ref()
        .and_then(|config| config.presets_overlay_dir.as_deref())
        .map(|path| resolve_config_presets_path(path, &project.root))
        .or(global_overlay);
    let project_has_manual_overlay = project_config
        .as_ref()
        .is_some_and(|config| config.presets_overlay_dir.is_some());

    Some(ReadOnlyRuntimePaths {
        shine_dir,
        presets_dir,
        presets_overlay_dir,
        is_external_presets,
        managed_overlay: global_managed_overlay && !project_has_manual_overlay,
    })
}

fn read_minimal_config_file(path: &Path) -> Option<MinimalConfig> {
    let content = std::fs::read_to_string(path).ok()?;
    read_minimal_config(&content).ok()
}

/// Resolve the runtime (shine_dir, presets_dir) pair.
///
/// Priority (highest first):
///   1. `SHINE_CONFIG_DIR` — overrides both shine_dir and presets_dir unless project config is active
///   2. `SHINE_PRESETS`    — overrides presets_dir only
///   3. `config_toml_presets` — presets_dir from active config `presets_dir` key
///   4. defaults
///
/// Returns `(shine_dir, presets_dir, is_external_presets)`.
pub(super) fn resolve_runtime_config_dirs(
    default_shine_dir: &Path,
    default_presets_dir: &Path,
    config_toml_presets: Option<&Path>,
    has_local_config: bool,
) -> (PathBuf, PathBuf, bool) {
    if let Ok(val) = std::env::var("SHINE_CONFIG_DIR") {
        let val = val.trim().to_string();
        if !val.is_empty() {
            let dir = PathBuf::from(tilde_expand(&val));
            if !has_local_config {
                return (dir.clone(), dir.join("presets"), true);
            }
            if let Ok(val) = std::env::var("SHINE_PRESETS") {
                let val = val.trim().to_string();
                if !val.is_empty() {
                    let presets = PathBuf::from(tilde_expand(&val));
                    return (dir, presets, true);
                }
            }
            if let Some(p) = config_toml_presets
                && let Some(s) = p.to_str()
            {
                let presets = PathBuf::from(tilde_expand(s));
                return (dir, presets, true);
            }
            return (dir.clone(), dir.join("presets"), false);
        }
    }

    if let Ok(val) = std::env::var("SHINE_PRESETS") {
        let val = val.trim().to_string();
        if !val.is_empty() {
            let presets = PathBuf::from(tilde_expand(&val));
            return (default_shine_dir.to_owned(), presets, true);
        }
    }

    if let Some(p) = config_toml_presets
        && let Some(s) = p.to_str()
    {
        let presets = PathBuf::from(tilde_expand(s));
        return (default_shine_dir.to_owned(), presets, true);
    }

    (
        default_shine_dir.to_owned(),
        default_presets_dir.to_owned(),
        false,
    )
}

#[cfg(test)]
mod tests {
    use super::super::test_util::make_temp_dir;
    use super::*;
    use crate::test_support::env_lock;
    use tokio::fs;

    #[tokio::test]
    async fn project_config_discovery_finds_shine_config() {
        let project_dir = make_temp_dir().await;
        let child_dir = project_dir.join("subdir");
        fs::create_dir_all(&child_dir).await.unwrap();
        fs::write(
            project_dir.join("shine.config.toml"),
            "presets_dir = \".\"\n",
        )
        .await
        .unwrap();
        let config = find_project_config(&child_dir).expect("project config should be found");

        assert_eq!(config.path, project_dir.join("shine.config.toml"));

        fs::remove_dir_all(&project_dir).await.unwrap();
    }

    #[tokio::test]
    async fn generic_config_toml_is_not_project_config() {
        let project_dir = make_temp_dir().await;
        fs::write(
            project_dir.join("config.toml"),
            "presets_dir = \"legacy\"\n",
        )
        .await
        .unwrap();

        assert!(find_project_config(&project_dir).is_none());

        fs::remove_dir_all(&project_dir).await.unwrap();
    }

    #[test]
    fn read_only_discovery_marks_existing_git_overlay_as_managed() {
        let _guard = env_lock();
        let root = std::env::temp_dir().join(format!(
            "shine-discovery-managed-overlay-{}",
            uuid::Uuid::new_v4()
        ));
        let previous_dir = std::env::current_dir().unwrap();
        let previous_config = std::env::var_os("SHINE_CONFIG_DIR");
        let previous_presets = std::env::var_os("SHINE_PRESETS");
        std::fs::create_dir_all(root.join("overlay")).unwrap();
        std::fs::write(
            root.join(GLOBAL_CONFIG_FILE),
            "presets_overlay_git = 'https://example.invalid/presets.git'\n",
        )
        .unwrap();
        // SAFETY: env_lock serializes process-global environment and cwd mutation.
        unsafe {
            std::env::set_var("SHINE_CONFIG_DIR", &root);
            std::env::remove_var("SHINE_PRESETS");
        }
        std::env::set_current_dir(&root).unwrap();

        let runtime = discover_runtime_paths_read_only().unwrap();

        std::env::set_current_dir(previous_dir).unwrap();
        // SAFETY: env_lock remains held while the previous environment is restored.
        unsafe {
            match previous_config {
                Some(value) => std::env::set_var("SHINE_CONFIG_DIR", value),
                None => std::env::remove_var("SHINE_CONFIG_DIR"),
            }
            match previous_presets {
                Some(value) => std::env::set_var("SHINE_PRESETS", value),
                None => std::env::remove_var("SHINE_PRESETS"),
            }
        }

        assert!(runtime.managed_overlay);
        assert_eq!(runtime.presets_overlay_dir, Some(root.join("overlay")));
        std::fs::remove_dir_all(root).unwrap();
    }

    // --- resolve_runtime_config_dirs unit tests ---

    #[test]
    fn shine_config_dir_overrides_everything() {
        let _guard = env_lock();
        let default_shine = PathBuf::from("/home/user/.shine");
        let default_presets = PathBuf::from("/home/user/.shine/presets");
        let custom = std::env::temp_dir().join("shine-override-test");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::set_var("SHINE_CONFIG_DIR", custom.to_str().unwrap()) };
        let (shine, presets, _) =
            resolve_runtime_config_dirs(&default_shine, &default_presets, None, false);
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };

        assert_eq!(shine, custom);
        assert_eq!(presets, custom.join("presets"));
    }

    #[test]
    fn shine_presets_overrides_presets_only() {
        let _guard = env_lock();
        let default_shine = PathBuf::from("/home/user/.shine");
        let default_presets = PathBuf::from("/home/user/.shine/presets");
        let custom_presets = std::env::temp_dir().join("my-presets");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::set_var("SHINE_PRESETS", custom_presets.to_str().unwrap()) };
        let (shine, presets, _) =
            resolve_runtime_config_dirs(&default_shine, &default_presets, None, false);
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_PRESETS") };

        assert_eq!(shine, default_shine);
        assert_eq!(presets, custom_presets);
    }

    #[test]
    fn shine_config_dir_takes_precedence_over_shine_presets() {
        let _guard = env_lock();
        let default_shine = PathBuf::from("/home/user/.shine");
        let default_presets = PathBuf::from("/home/user/.shine/presets");
        let custom_dir = std::env::temp_dir().join("shine-cfg-dir");
        let custom_presets = std::env::temp_dir().join("shine-presets-ignored");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::set_var("SHINE_CONFIG_DIR", custom_dir.to_str().unwrap()) };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::set_var("SHINE_PRESETS", custom_presets.to_str().unwrap()) };
        let (shine, presets, _) =
            resolve_runtime_config_dirs(&default_shine, &default_presets, None, false);
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_PRESETS") };

        assert_eq!(shine, custom_dir);
        assert_eq!(presets, custom_dir.join("presets"));
    }

    #[test]
    fn local_config_keeps_shine_config_dir_as_state_only() {
        let _guard = env_lock();
        let default_shine = PathBuf::from("/home/user/.shine");
        let default_presets = PathBuf::from("/home/user/.shine/presets");
        let custom_dir = std::env::temp_dir().join("shine-local-state");
        let toml_presets = PathBuf::from("/project/presets");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::set_var("SHINE_CONFIG_DIR", custom_dir.to_str().unwrap()) };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_PRESETS") };
        let (shine, presets, _) = resolve_runtime_config_dirs(
            &default_shine,
            &default_presets,
            Some(toml_presets.as_path()),
            true,
        );
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };

        assert_eq!(shine, custom_dir);
        assert_eq!(presets, toml_presets);
    }

    #[test]
    fn config_toml_presets_dir_is_used_when_no_env() {
        let _guard = env_lock();
        let default_shine = PathBuf::from("/home/user/.shine");
        let default_presets = PathBuf::from("/home/user/.shine/presets");
        let toml_presets = PathBuf::from("/custom/presets");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_PRESETS") };
        let (shine, presets, _) = resolve_runtime_config_dirs(
            &default_shine,
            &default_presets,
            Some(toml_presets.as_path()),
            false,
        );

        assert_eq!(shine, default_shine);
        assert_eq!(presets, toml_presets);
    }

    #[test]
    fn shine_presets_takes_precedence_over_config_toml() {
        let _guard = env_lock();
        let default_shine = PathBuf::from("/home/user/.shine");
        let default_presets = PathBuf::from("/home/user/.shine/presets");
        let env_presets = std::env::temp_dir().join("env-presets");
        let toml_presets = PathBuf::from("/toml/presets");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::set_var("SHINE_PRESETS", env_presets.to_str().unwrap()) };
        let (_, presets, _) = resolve_runtime_config_dirs(
            &default_shine,
            &default_presets,
            Some(toml_presets.as_path()),
            false,
        );
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_PRESETS") };

        assert_eq!(presets, env_presets);
    }

    #[tokio::test]
    async fn read_presets_override_returns_none_when_file_missing() {
        let missing =
            std::env::temp_dir().join(format!("shine-no-config-{}.toml", uuid::Uuid::new_v4()));
        let result = read_presets_override_from_toml(&missing).await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn read_presets_override_returns_none_when_key_absent() {
        let dir = make_temp_dir().await;
        let path = dir.join("config.toml");
        fs::write(&path, "schema_version = 0\n").await.unwrap();

        let result = read_presets_override_from_toml(&path).await;
        assert!(result.is_none());

        fs::remove_dir_all(&dir).await.unwrap();
    }

    #[tokio::test]
    async fn read_presets_override_returns_path_when_key_present() {
        let dir = make_temp_dir().await;
        let path = dir.join("config.toml");
        fs::write(&path, "schema_version = 0\npresets_dir = \"/my/presets\"\n")
            .await
            .unwrap();

        let result = read_presets_override_from_toml(&path).await;
        assert_eq!(result, Some(PathBuf::from("/my/presets")));

        fs::remove_dir_all(&dir).await.unwrap();
    }

    // --- is_external_presets flag tests ---

    #[test]
    fn is_external_presets_true_when_shine_config_dir_set() {
        let _guard = env_lock();
        let default = PathBuf::from("/home/user/.shine");
        let presets = PathBuf::from("/home/user/.shine/presets");
        let custom = std::env::temp_dir().join("shine-ext-test");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::set_var("SHINE_CONFIG_DIR", custom.to_str().unwrap()) };
        let (_, _, is_external) = resolve_runtime_config_dirs(&default, &presets, None, false);
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };

        assert!(
            is_external,
            "SHINE_CONFIG_DIR should set is_external_presets"
        );
    }

    #[test]
    fn is_external_presets_true_when_shine_presets_set() {
        let _guard = env_lock();
        let default = PathBuf::from("/home/user/.shine");
        let presets = PathBuf::from("/home/user/.shine/presets");
        let custom = std::env::temp_dir().join("shine-ext-presets");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::set_var("SHINE_PRESETS", custom.to_str().unwrap()) };
        let (_, _, is_external) = resolve_runtime_config_dirs(&default, &presets, None, false);
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_PRESETS") };

        assert!(is_external, "SHINE_PRESETS should set is_external_presets");
    }

    #[test]
    fn is_external_presets_true_when_toml_presets_dir_set() {
        let _guard = env_lock();
        let default = PathBuf::from("/home/user/.shine");
        let presets = PathBuf::from("/home/user/.shine/presets");
        let toml_override = PathBuf::from("/toml/presets");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_PRESETS") };
        let (_, _, is_external) =
            resolve_runtime_config_dirs(&default, &presets, Some(toml_override.as_path()), false);

        assert!(
            is_external,
            "config.toml presets_dir should set is_external_presets"
        );
    }

    #[test]
    fn is_external_presets_false_when_no_override() {
        let _guard = env_lock();
        let default = PathBuf::from("/home/user/.shine");
        let presets = PathBuf::from("/home/user/.shine/presets");

        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_CONFIG_DIR") };
        // SAFETY: env_lock() is held for the duration of this block, preventing
        //          concurrent env mutation from other threads in this test binary.
        unsafe { std::env::remove_var("SHINE_PRESETS") };
        let (_, _, is_external) = resolve_runtime_config_dirs(&default, &presets, None, false);

        assert!(
            !is_external,
            "no override should leave is_external_presets false"
        );
    }
}