From 28bcea9850b3c0b0c823e3ab280c8efd43f4acee Mon Sep 17 00:00:00 2001 From: bcherb2 Date: Wed, 18 Nov 2026 03:03:24 -0500 Subject: [PATCH] test: lab 30 checks that each tier has its own chezmoi state The private init's --persistent-state (chezmoistate-private.boltdb) and its unit assertion landed in 06b03c9. chezmoi keeps its state next to the config file, so the tiers used to share chezmoistate.boltdb and each wrote its config-template hash over the other's: after the private init every public command warned "config file template has changed". Lab 30 now asserts the private state file exists, private.toml names it, and neither tier warns and both statuses are clean after both inits. The fake private tier is resynced with the real template and cmp alias. --- .tests/lab/fake-private/.chezmoi.toml.tmpl | 12 +++++++++++ .../private_zsh/private_local.zsh.tmpl | 14 ++++++++++--- .tests/lab/scenarios/30-private-tier.sh | 20 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/.tests/lab/fake-private/.chezmoi.toml.tmpl b/.tests/lab/fake-private/.chezmoi.toml.tmpl index f7b12b8..dbed178 100644 --- a/.tests/lab/fake-private/.chezmoi.toml.tmpl +++ b/.tests/lab/fake-private/.chezmoi.toml.tmpl @@ -24,6 +24,18 @@ # so the symptom was `git commit` not knowing who you are, days later, with # nothing pointing back at the install that caused it. A loud failure would # have been a smaller bug. +# +# The same goes for the PERSISTENT STATE. chezmoi keeps it next to the config +# file, so both tiers shared ~/.config/chezmoi/chezmoistate.boltdb: each wrote +# its own config-template hash and run_onchange hashes over the other's, and +# after the private init every public command warned "config file template has +# changed". Running the init it suggests only moved the warning to this tier. +# So this tier gets its own state file, beside private.toml. This key covers a +# bare `chezmoi -c private.toml`; `dotup` and `cmp` also pass the same path as +# --persistent-state, because `init` opens the state BEFORE it writes this key, +# so the first init (or a re-init from a config that predates it) would +# otherwise record its hash in the public tier's file. +persistentState = {{ joinPath (dir .chezmoi.configFile) "chezmoistate-private.boltdb" | quote }} [data] gitName = {{ promptStringOnce . "gitName" "git user.name" | quote }} diff --git a/.tests/lab/fake-private/private_dot_config/private_zsh/private_local.zsh.tmpl b/.tests/lab/fake-private/private_dot_config/private_zsh/private_local.zsh.tmpl index 713c16a..2422371 100644 --- a/.tests/lab/fake-private/private_dot_config/private_zsh/private_local.zsh.tmpl +++ b/.tests/lab/fake-private/private_dot_config/private_zsh/private_local.zsh.tmpl @@ -43,6 +43,7 @@ # ~/.config/chezmoi/chezmoi.toml # private dotup, --source ... -c .../private.toml -> ~/.local/share/dotfiles-private # ~/.config/chezmoi/private.toml +# ~/.config/chezmoi/chezmoistate-private.boltdb # # The -c is not cosmetic. Both tiers used to render their .chezmoi.toml.tmpl to # the same default path, so re-running the PUBLIC installer overwrote the config @@ -51,10 +52,17 @@ # politely when their data is missing, so the first symptom was `git commit` not # knowing who you are, days later and unconnected to the install that caused it. # -# -S and -c must stay in step with dotup's PRIV_SRC and PRIV_CFG. If you move one, -# move the other, or cmp reads a config that describes a different source tree. +# The --persistent-state is the same lesson for chezmoi's state file: shared, each +# tier overwrote the other's config-template hash, and the public tier warned +# "config file template has changed" after every private command. private.toml +# names this file too, but `cmp init` opens the state before it rewrites that, so +# the alias has to carry it. +# +# -S, -c and --persistent-state must stay in step with dotup's PRIV_SRC, PRIV_CFG +# and PRIV_STATE. If you move one, move the others, or cmp reads a config that +# describes a different source tree. alias cm='chezmoi' -alias cmp='chezmoi -S ${XDG_DATA_HOME:-$HOME/.local/share}/dotfiles-private -c ${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/private.toml' +alias cmp='chezmoi -S ${XDG_DATA_HOME:-$HOME/.local/share}/dotfiles-private -c ${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/private.toml --persistent-state ${XDG_CONFIG_HOME:-$HOME/.config}/chezmoi/chezmoistate-private.boltdb' # Refresh ~/.config/zsh/secrets.zsh from Bitwarden Secrets Manager on demand. # `cmp apply` does the same thing via run_after_50-secrets.sh. diff --git a/.tests/lab/scenarios/30-private-tier.sh b/.tests/lab/scenarios/30-private-tier.sh index af9210a..9a52da6 100644 --- a/.tests/lab/scenarios/30-private-tier.sh +++ b/.tests/lab/scenarios/30-private-tier.sh @@ -202,4 +202,24 @@ git config --get 'url.git@github.com:.pushinsteadof' >/dev/null 2>&1 \ || fail "the GitHub push-over-ssh rewrite is gone entirely" ok "GitHub reads stay https; only pushes are rewritten to ssh" +# ---- two tiers, two state files: neither warns about the other -------------- +# Both tiers used to share ~/.config/chezmoi/chezmoistate.boltdb, and each wrote +# its config-template hash over the other's, so after this init every public +# command said "config file template has changed". `run_` scripts always show +# as " R"; anything else in either status is drift. +CZ=$HOME/.config/chezmoi +[ -f "$CZ/chezmoistate-private.boltdb" ] || fail "the private tier has no state file of its own" +grep -q '^persistentState = ' "$CZ/private.toml" || fail "private.toml does not name its state file" +pub=$(chezmoi status 2>&1) +prv=$(chezmoi -S "$HOME/.local/share/dotfiles-private" -c "$CZ/private.toml" \ + --persistent-state "$CZ/chezmoistate-private.boltdb" status 2>&1) +for t in pub prv; do + eval "o=\$$t" + case $o in *"template has changed"*|*warning*) + printf '%s\n' "$o" | sed 's/^/ | /'; fail "$t tier warns after both inits" ;; esac + d=$(printf '%s\n' "$o" | grep -v '^ R ' | grep -v '^$' || true) + [ -z "$d" ] || { printf '%s\n' "$d" | sed 's/^/ | /'; fail "$t tier status is not clean"; } +done +ok "each tier has its own state file; neither warns and both statuses are clean" + echo "PRIVATE TIER PASS"