zenops 0.20.0

Declarative system configuration management for shell config and dotfiles.
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
use xshell::{Shell, cmd};
use zenops::{
    Cmd,
    output::{PkgStatus, Status},
};
use zenops_safe_relative_path::srpath;

use test_env::{Entry, Output, paths};

mod test_env;

/// Filter `entries` down to just the `Entry::Status` variants — doctor's
/// non-pkg sections are tested separately via `Entry::Doctor`.
fn status_entries_only(out: &Output) -> Vec<&Entry> {
    out.entries
        .iter()
        .filter(|e| matches!(e, Entry::Status(_)))
        .collect()
}

#[test]
fn doctor_runs_without_config() {
    // No config.toml, zenops dir is not a git repo. Where `status` bails
    // with Error::OpenDb, `doctor` must swallow the load failure and
    // finish successfully — that's the whole point of the command.
    let env = test_env::TestEnv::load();

    let out = env
        .run(&Cmd::Doctor)
        .expect("doctor must not fail when config.toml is missing");
    // No Status events — push_pkg_health is never reached without a
    // config. Doctor's narrative checks live under `Entry::Doctor`.
    assert!(
        status_entries_only(&out).is_empty(),
        "expected no status events without a config, got: {:?}",
        status_entries_only(&out),
    );
}

#[test]
fn doctor_runs_with_broken_config() {
    // Syntactically invalid TOML. Any other command returns Err(ParseDb);
    // `doctor` must keep going and report the failure inline.
    let env = test_env::TestEnv::load();
    env.write_zenops_file(srpath!("config.toml"), "[[[ not toml", None);

    let out = env
        .run(&Cmd::Doctor)
        .expect("doctor must not fail on a malformed config.toml");
    assert!(
        status_entries_only(&out).is_empty(),
        "expected no status events on a malformed config, got: {:?}",
        status_entries_only(&out),
    );
}

#[test]
fn doctor_runs_with_unknown_field_in_config() {
    // `deny_unknown_fields` on StoredConfig catches typos / renamed fields.
    // Doctor must render the error instead of propagating.
    let env = test_env::TestEnv::load();
    env.init_config(
        r#"
        [shell]
        type = "bash"
        totally_not_a_real_field = 1
        "#,
    );

    let out = env
        .run(&Cmd::Doctor)
        .expect("doctor must not fail on an unknown-field ParseDb error");
    assert!(
        status_entries_only(&out).is_empty(),
        "expected no status events when config fails to parse, got: {:?}",
        status_entries_only(&out),
    );
}

#[test]
fn doctor_emits_pkg_missing_for_enable_on_with_missing_detect() {
    // With a valid config that declares `enable = "on"` for a pkg whose
    // detect strategy can't match on the test host, doctor reuses
    // Config::push_pkg_health and emits a Status::Pkg::Missing event —
    // same channel the `status` command uses.
    let env = test_env::TestEnv::load();
    env.init_config(
        r#"
        [pkg.zenops-doctor-test]
        enable = "on"
        [pkg.zenops-doctor-test.install_hint.brew]
        packages = ["zenops-doctor-fake"]
        [pkg.zenops-doctor-test.install_hint.dnf]
        packages = []
        [pkg.zenops-doctor-test.install_hint.apt]
        packages = []
        [pkg.zenops-doctor-test.install_hint.pacman]
        packages = []
        [pkg.zenops-doctor-test.install_hint.cargo]
        packages = []
        [pkg.zenops-doctor-test.detect]
        exists = "/definitely/does/not/exist/zenops-doctor-test"
        "#,
    );

    let out = env
        .run(&Cmd::Doctor)
        .expect("doctor must succeed when config loads");

    let status_only = status_entries_only(&out);
    // The exact set of Status::Pkg events depends on whether the host
    // running the test has brew / cargo / sk / etc. installed; but our
    // fake pkg with its guaranteed-missing detect must appear.
    let has_expected = status_only.iter().any(|e| {
        matches!(
            e,
            Entry::Status(Status::Pkg {
                pkg,
                status: PkgStatus::Missing { .. },
            }) if pkg == "zenops-doctor-test"
        )
    });
    assert!(
        has_expected,
        "expected a Status::Pkg::Missing for zenops-doctor-test, got: {status_only:?}",
    );
    // Every Status entry should be a Status::Pkg (doctor never emits Git
    // or ConfigFile events — those are status' territory).
    for e in &status_only {
        assert!(
            matches!(e, Entry::Status(Status::Pkg { .. })),
            "doctor emitted unexpected status event: {e:?}",
        );
    }
}

#[test]
fn doctor_emits_doctor_check_events_for_system_section() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    let env = test_env::TestEnv::load();
    env.init_config("");

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    // The System section always includes an `os:` Info row.
    let has_os_info = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::System,
                label,
                severity: DoctorSeverity::Info,
                ..
            }) if label == "os:"
        )
    });
    assert!(
        has_os_info,
        "expected a system/os: info DoctorCheck, got: {:?}",
        out.entries,
    );
    // And a SectionHeader event opens each section so the renderer can
    // print its bold title.
    let has_system_header = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::SectionHeader {
                section: DoctorSection::System
            })
        )
    });
    assert!(
        has_system_header,
        "expected a System SectionHeader event, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_reports_missing_zenops_dir_as_bad() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    // No zenops dir at all → repo_block must emit a Bad "path: missing"
    // row pointing to `zenops init <url>`.
    let env = test_env::TestEnv::load();
    env.delete_dir_all(test_env::paths::ZENOPS_DIR);

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let has_missing = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Repo,
                label,
                severity: DoctorSeverity::Bad,
                value,
                ..
            }) if label == "path:" && value == "missing"
        )
    });
    assert!(
        has_missing,
        "expected a Bad path:missing repo check, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_reports_no_remote_warn_when_repo_has_no_origin() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    // init_config sets up a git repo but does NOT add an `origin` remote.
    let env = test_env::TestEnv::load();
    env.init_config("");

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let has_no_remote = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Repo,
                label,
                severity: DoctorSeverity::Warn,
                value,
                ..
            }) if label == "remote:" && value == "none"
        )
    });
    assert!(
        has_no_remote,
        "expected a Warn remote:none row, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_reports_remote_info_when_origin_configured() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    let env = test_env::TestEnv::load();
    env.init_config_with_remote("");

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let has_remote_info = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Repo,
                label,
                severity: DoctorSeverity::Info,
                value,
                ..
            }) if label == "remote:" && value.contains("remote.git")
        )
    });
    assert!(
        has_remote_info,
        "expected an Info remote: row with the bare repo path, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_reports_uncommitted_changes_as_warn() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};
    use zenops_safe_relative_path::srpath;

    // Clean repo first, then dirty it without committing.
    let env = test_env::TestEnv::load();
    env.init_config("");
    env.write_zenops_file(srpath!("untracked"), "stale\n", None);

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let has_uncommitted_warn = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Repo,
                label,
                severity: DoctorSeverity::Warn,
                value,
                ..
            }) if label == "uncommitted:" && value == "yes"
        )
    });
    assert!(
        has_uncommitted_warn,
        "expected a Warn uncommitted:yes row, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_user_block_warns_on_unset_name_and_email() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    // No `[user]` section in config → both name: and email: should be Warn.
    let env = test_env::TestEnv::load();
    env.init_config("");

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let has_user_name_warn = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::User,
                label,
                severity: DoctorSeverity::Warn,
                value,
                ..
            }) if label == "name:" && value == "unset"
        )
    });
    let has_user_email_warn = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::User,
                label,
                severity: DoctorSeverity::Warn,
                value,
                ..
            }) if label == "email:" && value == "unset"
        )
    });
    assert!(
        has_user_name_warn && has_user_email_warn,
        "expected Warn user:name/email unset rows, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_user_block_emits_info_when_name_and_email_set() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    let env = test_env::TestEnv::load();
    env.init_config(
        r#"
        [user]
        name = "Ada Lovelace"
        email = "ada@example.com"
        "#,
    );

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let has_name_info = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::User,
                label,
                severity: DoctorSeverity::Info,
                value,
                ..
            }) if label == "name:" && value == "Ada Lovelace"
        )
    });
    let has_email_info = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::User,
                label,
                severity: DoctorSeverity::Info,
                value,
                ..
            }) if label == "email:" && value == "ada@example.com"
        )
    });
    assert!(
        has_name_info && has_email_info,
        "expected Info user:name/email rows, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_emits_section_headers_for_every_section() {
    use zenops::output::{DoctorCheck, DoctorSection};

    // Sanity check that each section opens with a SectionHeader event so
    // the renderer always has a title to print, including Packages (which
    // has no DoctorCheck rows of its own — content comes via Status::Pkg).
    let env = test_env::TestEnv::load();
    env.init_config(
        r#"
        [user]
        name = "Ada"
        email = "ada@example.com"
        "#,
    );

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let header = |want: DoctorSection| {
        out.entries.iter().any(|e| {
            matches!(
                e,
                Entry::Doctor(DoctorCheck::SectionHeader { section }) if *section == want,
            )
        })
    };
    for section in [
        DoctorSection::System,
        DoctorSection::Repo,
        DoctorSection::Config,
        DoctorSection::PkgManager,
        DoctorSection::User,
        DoctorSection::Shell,
        DoctorSection::Packages,
    ] {
        assert!(
            header(section),
            "missing SectionHeader for {section:?}, got: {:?}",
            out.entries,
        );
    }
}

#[test]
fn doctor_reports_unreadable_config_as_bad() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    // Hits the non-NotFound `OpenDb` arm: the file exists but can't be
    // read. chmod 0o000 produces PermissionDenied. PermGuard restores the
    // mode on drop so tempfile cleanup can recurse into the dir.
    let env = test_env::TestEnv::load();
    env.init_config("");
    let _guard = env.chmod(test_env::paths::ZENOPS_CONFIG, 0o000);

    let out = env
        .run(&Cmd::Doctor)
        .expect("doctor must not fail when config.toml is unreadable");
    let has_unreadable = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Config,
                label,
                severity: DoctorSeverity::Bad,
                value,
                ..
            }) if label == "status:" && value == "unreadable"
        )
    });
    assert!(
        has_unreadable,
        "expected a Bad status:unreadable config check, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_reports_parse_error_with_invalid_type_hint() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    // [shell] is a tagged enum; `type = 42` triggers a serde "invalid
    // type" error during deserialization. Doctor's parse-error arm should
    // attach the README hint detail line for the invalid-type / missing-
    // field family.
    let env = test_env::TestEnv::load();
    env.init_config(
        r#"
        [shell]
        type = 42
        "#,
    );

    let out = env
        .run(&Cmd::Doctor)
        .expect("doctor must not fail on a parse error");
    let detail = out
        .entries
        .iter()
        .find_map(|e| match e {
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Config,
                label,
                severity: DoctorSeverity::Bad,
                value,
                detail,
                ..
            }) if label == "status:" && value == "parse error" => Some(detail),
            _ => None,
        })
        .unwrap_or_else(|| {
            panic!(
                "expected a parse-error doctor check, got: {:?}",
                out.entries
            )
        });
    assert!(
        detail.iter().any(|line| line.contains("README.md")),
        "expected README hint in invalid-type parse error detail, got: {detail:?}",
    );
}

#[test]
fn doctor_reports_zenops_dir_not_a_git_repo() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};
    use zenops_safe_relative_path::srpath;

    // Strip the .git dir: zenops dir exists but is not a git repo. Hits
    // the `git.is_git_repo()? == false` branch in `repo_block`.
    let env = test_env::TestEnv::load();
    env.init_config("");
    env.delete_dir_all(srpath!("home/bob/.config/zenops/.git"));

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let has_no_git = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Repo,
                label,
                severity: DoctorSeverity::Warn,
                value,
                ..
            }) if label == "git repo:" && value == "no"
        )
    });
    assert!(
        has_no_git,
        "expected a Warn git repo:no row, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_omits_branch_row_on_detached_head() {
    use zenops::output::{DoctorCheck, DoctorSection};

    // After init_config, the zenops repo is on a normal branch. Detach HEAD
    // by checking out the commit's SHA directly. doctor's `repo_block`
    // filters `git rev-parse --abbrev-ref HEAD == "HEAD"` and skips the
    // `branch:` info row entirely — covers the false arm of the
    // `if let Some(b) = branch` filter.
    let env = test_env::TestEnv::load();
    env.init_config("");

    let zenops = env.resolve_path(paths::ZENOPS_DIR);
    let sh = Shell::new().unwrap();
    let _dir = sh.push_dir(&zenops);
    let head_sha = cmd!(sh, "git rev-parse HEAD").read().unwrap();
    cmd!(sh, "git checkout --detach {head_sha}")
        .ignore_stdout()
        .ignore_stderr()
        .run()
        .unwrap();
    drop(_dir);

    let out = env.run(&Cmd::Doctor).expect("doctor must succeed");
    let any_branch_row = out.entries.iter().any(|e| {
        matches!(
            e,
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Repo,
                label,
                ..
            }) if label == "branch:"
        )
    });
    assert!(
        !any_branch_row,
        "detached HEAD must not emit a branch: row, got: {:?}",
        out.entries,
    );
}

#[test]
fn doctor_emits_bad_check_with_detail_for_parse_error() {
    use zenops::output::{DoctorCheck, DoctorSection, DoctorSeverity};

    let env = test_env::TestEnv::load();
    env.write_zenops_file(srpath!("config.toml"), "[[[ not toml", None);

    let out = env
        .run(&Cmd::Doctor)
        .expect("doctor must not fail on a malformed config.toml");
    let parse_error = out
        .entries
        .iter()
        .find_map(|e| match e {
            Entry::Doctor(DoctorCheck::Check {
                section: DoctorSection::Config,
                label,
                severity: DoctorSeverity::Bad,
                value,
                detail,
                ..
            }) if label == "status:" && value == "parse error" => Some(detail),
            _ => None,
        })
        .unwrap_or_else(|| {
            panic!(
                "expected a parse-error doctor check, got: {:?}",
                out.entries
            )
        });
    assert!(
        !parse_error.is_empty(),
        "parse-error check should carry multi-line detail body, got: {parse_error:?}",
    );
}