Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Tests

on:
push:
branches: [master]
pull_request:
branches: [master]

concurrency:
group: test-${{ github.ref }}
cancel-in-progress: true

jobs:
e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: sudo apt-get update -qq && sudo apt-get install -y -qq gettext-base
- name: Run tests
run: |
bash tests/test-override-data.bash
bash tests/test-envsubst.bash
8 changes: 2 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,8 @@ For other distributions, ensure the following dependencies are installed:
- bash
- gettext (provides ``envsubst``)

The following dependencies are optional but highly recommended. Without them,
you cannot customize the `Chezmoi Configuration`_:

- python >= 3.7
- python-tomli
- python-tomli-w
User template data can be provided via ``~/.config/chezetc/chezetc.toml``,
which is passed to chezmoi as ``--override-data-file``.

Then, clone the repository::

Expand Down
25 changes: 10 additions & 15 deletions chezetc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ assert_cmd() {
test_cmd $1 || err "Command \"$_\" not found"
}

test_pymod() {
python3 -c "import $1" 2>/dev/null
}

#-------------------------------------------------------------------------------
# Meta informations.
#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -125,15 +121,9 @@ create_dir $MOI_CFG_DIR
create_dir $MOI_CACHE_DIR

# Generate config file from template and envs.
# User data overrides are passed via --override-data-file.
export ETC_APP ETC_DIR MOI_SRC_DIR MOI_DST_DIR
if test_cmd python3 && test_pymod tomli && test_pymod tomli_w; then
CACHED_CFG="$MOI_CACHE_DIR/$ETC_APP.toml"
update_tmpl "$MOI_CFG_TMPL" "$CACHED_CFG"
update_file <("$ETC_DIR/utils/toml-merge.py" "$CACHED_CFG" "$ETC_CFG") "$MOI_CFG"
else
warn "Python3 and its modules \"tomli\", \"tomli_w\" are not installed, user CAN NOT customize chezmoi config"
update_tmpl "$MOI_CFG_TMPL" "$MOI_CFG"
fi
update_tmpl "$MOI_CFG_TMPL" "$MOI_CFG"

[[ ! -z "$EAGAIN" ]] && \
info "Configuration updated, please execute $ETC_APP again" && \
Expand All @@ -143,8 +133,13 @@ fi
# Forward it to chezmoi and let it do the real work.
#-------------------------------------------------------------------------------

MOI_ARGS=(
--config="$MOI_CFG"
--persistent-state="$MOI_CFG_DIR/chezmoistate.boltdb"
--cache="$MOI_CACHE_DIR"
)
[[ -f "$ETC_CFG" ]] && MOI_ARGS+=(--override-data-file="$ETC_CFG")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Convert TOML data before passing it to chezmoi

For users who have the documented ~/.config/chezetc/chezetc.toml file, this now passes that TOML file directly to --override-data-file. I checked chezmoi's common flag docs, and that flag reads JSON data, while this project still documents/supports ETC_CFG as a TOML file, so any existing TOML override file will make wrapped commands fail instead of providing template data. Please either convert the TOML to JSON/YAML before invoking chezmoi or change the supported override file format consistently.

Useful? React with 👍 / 👎.


sudo --preserve-env=PATH,EDITOR,VISUAL -- \
chezmoi "$@" \
--config="$MOI_CFG" \
--persistent-state="$MOI_CFG_DIR/chezmoistate.boltdb" \
--cache="$MOI_CACHE_DIR"
"${MOI_ARGS[@]}"
104 changes: 104 additions & 0 deletions tests/common.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Shared test setup/teardown for chezetc e2e tests.
#
# Source this file at the top of each test script, then call setup_chezetc_env.

set -euo pipefail

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

fail() { printf "${RED}FAIL: %s${NC}\n" "$*" >&2; exit 1; }
pass() { printf "${GREEN}PASS: %s${NC}\n" "$*" >&2; }

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="${CHEZETC_REPO:-$(dirname "$SCRIPT_DIR")}"

WORKDIR=$(mktemp -d)
trap 'rm -rf "$WORKDIR"' EXIT

MOCK_BIN="$WORKDIR/mock-bin"
CHEZETC_DIR="$WORKDIR/chezetc"
ETC_CFG_FILE="$HOME/.config/chezetc/chezetc.toml"

export HOME="$WORKDIR/home"
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"

#--------------------------------------------------------------------
# Common setup functions
#--------------------------------------------------------------------

setup_mocks() {
mkdir -p "$MOCK_BIN"

# Mock sudo: skip flags, execute the real command
cat > "$MOCK_BIN/sudo" << 'EOF'
#!/bin/bash
while [[ $# -gt 0 ]]; do
case "$1" in
--preserve-env=*|--preserve-env|-E) shift ;;
--) shift; break ;;
-*) shift ;;
*) break ;;
esac
done
exec "$@"
EOF
chmod +x "$MOCK_BIN/sudo"

# Mock chezmoi: capture all arguments
cat > "$MOCK_BIN/chezmoi" << EOF
#!/bin/bash
printf '%s\n' "\$@" > "$WORKDIR/chezmoi-args"
EOF
chmod +x "$MOCK_BIN/chezmoi"

export PATH="$MOCK_BIN:$PATH"
}

install_chezetc() {
mkdir -p "$CHEZETC_DIR"/{utils,commands,hooks}
cp "$REPO_ROOT/chezetc" "$CHEZETC_DIR/"
cp "$REPO_ROOT/chezmoi.toml" "$CHEZETC_DIR/"
touch "$CHEZETC_DIR/commands/editor"
touch "$CHEZETC_DIR/commands/cd"
touch "$CHEZETC_DIR/hooks/post-add-chown.sh"
}

pre_render_config() {
local cfg_dir="$XDG_CONFIG_HOME/chezmoi/chezetc"
mkdir -p "$cfg_dir" "$XDG_CACHE_HOME/chezmoi/chezetc" "$XDG_DATA_HOME/chezetc"

# Pre-render the template so chezetc sees it as unchanged (avoids EAGAIN exit)
ETC_APP=chezetc \
ETC_DIR="$CHEZETC_DIR" \
MOI_SRC_DIR="$XDG_DATA_HOME/chezetc" \
MOI_DST_DIR="/etc" \
envsubst < "$CHEZETC_DIR/chezmoi.toml" > "$cfg_dir/chezmoi.toml"
}

setup_chezetc_env() {
setup_mocks
install_chezetc
pre_render_config
}

run_chezetc() {
ETC_APP=chezetc \
ETC_MODE=CHEZMOI \
ETC_SRC="$XDG_DATA_HOME/chezetc" \
ETC_DST="etc" \
ETC_CFG="$ETC_CFG_FILE" \
bash "$CHEZETC_DIR/chezetc" "$@"
}

captured_chezmoi_args() {
cat "$WORKDIR/chezmoi-args" 2>/dev/null || echo ""
}

make_user_config() {
mkdir -p "$(dirname "$ETC_CFG_FILE")"
cat > "$ETC_CFG_FILE"
}
38 changes: 38 additions & 0 deletions tests/test-envsubst.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
# Test: envsubst renders chezmoi.toml template with correct variable substitution.

source "$(dirname "$0")/common.bash"

# Simulate the envsubst invocation that update_tmpl performs
export ETC_APP=chezetc ETC_DIR="$CHEZETC_DIR"
export MOI_SRC_DIR="$XDG_DATA_HOME/chezetc"
export MOI_DST_DIR="/etc"

render_template() {
envsubst < "$REPO_ROOT/chezmoi.toml"
}

output=$(render_template)

# Assert sourceDir is substituted correctly
echo "$output" | grep -qF "sourceDir = \"$MOI_SRC_DIR\"" \
&& pass "sourceDir rendered correctly" \
|| fail "sourceDir substitution mismatch"

# Assert destDir is substituted correctly
echo "$output" | grep -qF "destDir = \"$MOI_DST_DIR\"" \
&& pass "destDir rendered correctly" \
|| fail "destDir substitution mismatch"

# Assert edit.command points to the editor shim
echo "$output" | grep -qF "command = \"$ETC_DIR/commands/editor\"" \
&& pass "edit.command rendered correctly" \
|| fail "edit.command substitution mismatch"

# Assert no leftover $variable remains unsubstituted
echo "$output" | grep -q '\$MOI_SRC_DIR\|\$MOI_DST_DIR\|\$ETC_DIR\|\$ETC_APP' \
&& fail "Unsubstituted variable found in output" \
|| pass "No unsubstituted variables"

echo >&2
printf "${GREEN}All tests passed!${NC}\n" >&2
20 changes: 20 additions & 0 deletions tests/test-override-data.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
# Test: --override-data-file is passed to chezmoi when ETC_CFG exists.

source "$(dirname "$0")/common.bash"
setup_chezetc_env

make_user_config << 'EOF'
test_variable = "e2e_override_value"
user_email = "test@example.com"
EOF

run_chezetc apply

# Assert --override-data-file was passed with correct path
captured_chezmoi_args | grep -qF -- "--override-data-file=$ETC_CFG_FILE" \
&& pass "--override-data-file passed to chezmoi" \
|| fail "--override-data-file missing"

echo >&2
printf "${GREEN}All tests passed!${NC}\n" >&2
1 change: 0 additions & 1 deletion utils/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ install -Dm775 "$SRCDIR/chezetc" "$LIBDIR/$PKGNAME"
EXEC_FILES=(
commands/cd
commands/editor
utils/toml-merge.py
hooks/post-add-chown.sh
)
for i in ${EXEC_FILES[@]}; do
Expand Down
32 changes: 0 additions & 32 deletions utils/toml-merge.py

This file was deleted.

Loading