diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..dfd874c --- /dev/null +++ b/.github/workflows/test.yml @@ -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 diff --git a/README.rst b/README.rst index c2a94ac..0b8b4d4 100644 --- a/README.rst +++ b/README.rst @@ -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:: diff --git a/chezetc b/chezetc index 08bec96..1639f77 100755 --- a/chezetc +++ b/chezetc @@ -67,10 +67,6 @@ assert_cmd() { test_cmd $1 || err "Command \"$_\" not found" } -test_pymod() { - python3 -c "import $1" 2>/dev/null -} - #------------------------------------------------------------------------------- # Meta informations. #------------------------------------------------------------------------------- @@ -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" && \ @@ -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") + sudo --preserve-env=PATH,EDITOR,VISUAL -- \ chezmoi "$@" \ - --config="$MOI_CFG" \ - --persistent-state="$MOI_CFG_DIR/chezmoistate.boltdb" \ - --cache="$MOI_CACHE_DIR" + "${MOI_ARGS[@]}" diff --git a/tests/common.bash b/tests/common.bash new file mode 100644 index 0000000..023ae44 --- /dev/null +++ b/tests/common.bash @@ -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" +} diff --git a/tests/test-envsubst.bash b/tests/test-envsubst.bash new file mode 100644 index 0000000..d451d0c --- /dev/null +++ b/tests/test-envsubst.bash @@ -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 diff --git a/tests/test-override-data.bash b/tests/test-override-data.bash new file mode 100644 index 0000000..79d38a1 --- /dev/null +++ b/tests/test-override-data.bash @@ -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 diff --git a/utils/install.sh b/utils/install.sh index 7c0ed18..93842c3 100755 --- a/utils/install.sh +++ b/utils/install.sh @@ -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 diff --git a/utils/toml-merge.py b/utils/toml-merge.py deleted file mode 100755 index 86cada9..0000000 --- a/utils/toml-merge.py +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env python3 - -import sys -from os import path -import tomli -import tomli_w - - -def load(fn): - if path.isfile(fn): - with open(fn, 'rb') as f: - return tomli.load(f) - else: - return {} - - -def merge(src, dst): - """Recursively merge two dicts instead of using ``d1 | d2``""" - for key, value in src.items(): - if isinstance(value, dict): - # get node or create one - node = dst.setdefault(key, {}) - merge(value, node) - else: - dst[key] = value - - return dst - -a = load(sys.argv[1]) -b = load(sys.argv[2]) -merge(a, b) -print(tomli_w.dumps(b))