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
//! Shared helpers for integration tests.
//!
//! Use via `mod common;` at the top of a test file. (`common/mod.rs`, not
//! `common.rs`, so cargo does not treat it as its own test binary.)
//!
//! Each test binary compiles this module independently and uses only a subset
//! of it, so unused-item warnings are expected and silenced.
use PathBuf;
use OnceLock;
/// Redirect all mati state — every project store, the daemon socket, logs, and
/// the device id — to a process-unique temp dir via `MATI_HOME`, and return it.
///
/// Integration tests spawn the real `mati` binary against tempdir repos; without
/// this, each spawn derives a slug and creates a permanent `~/.mati/<slug>/` in
/// the developer's real home. Setting `MATI_HOME` in the test process means:
/// - every child `mati` inherits it from the environment, and
/// - the test's own root computation (e.g. waiting on `<root>/mati.sock`)
/// resolves to the SAME place the child writes — call this instead of
/// `dirs::home_dir().join(".mati")`.
///
/// Idempotent: computed once per process (nextest runs each test in its own
/// process, so tests never share a home). The dir lives for the process
/// lifetime under `$TMPDIR`; the OS reclaims it.
/// Set `MATI_HOME` for this test process (and inherited by spawned children)
/// without needing the returned path. Convenience wrapper over [`mati_home`].
/// Stops the daemon for a scratch mati root on drop — including when the test
/// panics, since `Drop` still runs during unwinding.
///
/// A test that spawns `mati <cmd>` against a scratch home touches the store
/// through `StoreProxy::open`, which calls `ensure_daemon` and spawns a
/// *detached* `mati daemon start` process: it is not a child of the test's own
/// `Command`, so `kill_on_drop` on that `Command` (or killing a `mati serve`
/// wrapping it) never reaches it. Keep one of these alive for the scope that
/// owns the scratch home; it shells out to `mati daemon stop` against that
/// exact root on drop, so the daemon comes down no matter how the test exits.
///
/// Never point this at a developer's real `~/.mati` — it unconditionally
/// stops whatever daemon answers for the given root.
///
/// Needs the same `cwd` the original commands ran with, not just the home
/// override: the store root is `<home>/<slug>`, and the slug is derived from
/// cwd. Pointing `mati daemon stop` at the wrong cwd computes a different
/// slug and silently misses the daemon.
/// `mati_home` exports `MATI_HOME` as a side effect, so asserting it is set
/// after calling it proves nothing. What is worth pinning is where it points.