From 8dfc2c16c8037c4a815d0e8783559b27e1b8d044 Mon Sep 17 00:00:00 2001 From: Shengyu Zhang Date: Wed, 13 May 2026 17:06:43 +0800 Subject: [PATCH 1/3] feat: Replace toml-merge with chezmoi --override-data-file Drop Python3/tomli dependency by passing user template data via chezmoi's built-in --override-data-file flag instead of merging config files with a Python script. Co-authored-by: DeepSeek --- README.rst | 8 +- chezetc | 25 +++--- tests/e2e-override-data.bash | 167 +++++++++++++++++++++++++++++++++++ utils/install.sh | 1 - utils/toml-merge.py | 32 ------- 5 files changed, 179 insertions(+), 54 deletions(-) create mode 100644 tests/e2e-override-data.bash delete mode 100755 utils/toml-merge.py 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/e2e-override-data.bash b/tests/e2e-override-data.bash new file mode 100644 index 0000000..b34d5e2 --- /dev/null +++ b/tests/e2e-override-data.bash @@ -0,0 +1,167 @@ +#!/bin/bash +# e2e test: verify --override-data-file replaces toml-merge.py +# +# This test ensures that: +# 1. chezetc no longer requires Python3/tomli/tomli_w +# 2. User's ETC_CFG is passed to chezmoi as --override-data-file +# 3. The generated chezmoi config is directly from envsubst (no merge step) + +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; } + +#-------------------------------------------------------------------- +# Setup: create isolated test environment +#-------------------------------------------------------------------- + +WORKDIR=$(mktemp -d) +trap 'rm -rf "$WORKDIR"' EXIT + +export HOME="$WORKDIR/home" +export XDG_CONFIG_HOME="$HOME/.config" +export XDG_CACHE_HOME="$HOME/.cache" +export XDG_DATA_HOME="$HOME/.local/share" + +# Mock bin directory to intercept sudo and chezmoi +MOCK_BIN="$WORKDIR/mock-bin" +mkdir -p "$MOCK_BIN" + +# Mock sudo: pass-through (skip sudo's own flags, execute the real command) +cat > "$MOCK_BIN/sudo" << 'MOCKEOF' +#!/bin/bash +args=() +while [[ $# -gt 0 ]]; do + case "$1" in + --preserve-env=*|--preserve-env|-E) shift ;; + --) shift; break ;; + -*) shift ;; + *) break ;; + esac +done +exec "$@" +MOCKEOF +chmod +x "$MOCK_BIN/sudo" + +# Mock chezmoi: capture all arguments into a file +cat > "$MOCK_BIN/chezmoi" << 'MOCKEOF' +#!/bin/bash +printf '%s\n' "$@" > /tmp/chezetc-e2e-chezmoi-args +MOCKEOF +chmod +x "$MOCK_BIN/chezmoi" + +export PATH="$MOCK_BIN:$PATH" + +#-------------------------------------------------------------------- +# Set up chezetc files (simulate installed chezetc) +#-------------------------------------------------------------------- + +CHEZETC_DIR="$WORKDIR/chezetc" +mkdir -p "$CHEZETC_DIR"/{utils,commands,hooks} + +# Copy the actual chezetc script and template +cp /home/la/git/chezetc/chezetc "$CHEZETC_DIR/" +cp /home/la/git/chezetc/chezmoi.toml "$CHEZETC_DIR/" + +# Create required shim/hook files (content doesn't matter for this test) +touch "$CHEZETC_DIR/commands/editor" +touch "$CHEZETC_DIR/commands/cd" +touch "$CHEZETC_DIR/hooks/post-add-chown.sh" + +#-------------------------------------------------------------------- +# Pre-create the generated chezmoi config +# (avoids the "Configuration updated, please run again" exit) +#-------------------------------------------------------------------- + +MOI_SRC_DIR="$XDG_DATA_HOME/chezetc" +MOI_DST_DIR="/etc" +MOI_CFG_DIR="$XDG_CONFIG_HOME/chezmoi/chezetc" +MOI_CACHE_DIR="$XDG_CACHE_HOME/chezmoi/chezetc" + +mkdir -p "$MOI_SRC_DIR" +mkdir -p "$MOI_CFG_DIR" +mkdir -p "$MOI_CACHE_DIR" + +# Pre-render the template (same content update_tmpl would produce) +{ + export ETC_APP=chezetc + export ETC_DIR="$CHEZETC_DIR" + export MOI_SRC_DIR MOI_DST_DIR + cat "$CHEZETC_DIR/chezmoi.toml" | envsubst +} > "$MOI_CFG_DIR/chezmoi.toml" + +#-------------------------------------------------------------------- +# Create user's custom config (template data for --override-data-file) +#-------------------------------------------------------------------- + +ETC_CFG_FILE="$XDG_CONFIG_HOME/chezetc/chezetc.toml" +mkdir -p "$(dirname "$ETC_CFG_FILE")" + +cat > "$ETC_CFG_FILE" << 'EOF' +# User template data +test_variable = "e2e_override_value" +user_email = "test@example.com" +EOF + +pass "Test environment ready at $WORKDIR" + +#-------------------------------------------------------------------- +# Run chezetc +#-------------------------------------------------------------------- + +ETC_APP=chezetc \ +ETC_MODE=CHEZMOI \ +ETC_SRC="$MOI_SRC_DIR" \ +ETC_DST="etc" \ +ETC_CFG="$ETC_CFG_FILE" \ + bash "$CHEZETC_DIR/chezetc" apply + +#-------------------------------------------------------------------- +# Assertions +#-------------------------------------------------------------------- + +CAPTURED_FILE="/tmp/chezetc-e2e-chezmoi-args" +[[ -f "$CAPTURED_FILE" ]] || fail "Mock chezmoi was not invoked (captured args file missing)" + +CAPTURED_ARGS=$(cat "$CAPTURED_FILE") +echo "Captured chezmoi args: $CAPTURED_ARGS" >&2 + +# Check 1: --override-data-file is present with correct path +if echo "$CAPTURED_ARGS" | grep -qF -- "--override-data-file=$ETC_CFG_FILE"; then + pass "--override-data-file points to correct user config" +else + fail "--override-data-file=$ETC_CFG_FILE not found in chezmoi args" +fi + +# Check 2: User config file exists and contains test data +if [[ -f "$ETC_CFG_FILE" ]]; then + pass "User config file exists at $ETC_CFG_FILE" +else + fail "User config file does not exist" +fi + +if grep -q 'test_variable = "e2e_override_value"' "$ETC_CFG_FILE"; then + pass "User config contains expected template data" +else + fail "User config missing expected data" +fi + +# Check 3: No Python-related commands were invoked (sanity check) +if echo "$CAPTURED_ARGS" | grep -q "python3\|tomli\|toml-merge"; then + fail "Python/tomli references found in chezmoi invocation" +else + pass "No Python/tomli dependency referenced" +fi + +#-------------------------------------------------------------------- +# Cleanup captured file +#-------------------------------------------------------------------- + +rm -f "$CAPTURED_FILE" + +echo >&2 +printf "${GREEN}All e2e 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)) From 4bbbfb319f34f3cc1ec5cf7ea0e33177370e0e9d Mon Sep 17 00:00:00 2001 From: Shengyu Zhang Date: Wed, 13 May 2026 17:13:32 +0800 Subject: [PATCH 2/3] chore: Add CI workflow for e2e test Co-authored-by: DeepSeek --- .github/workflows/test.yml | 21 +++++++++++++++++++++ tests/e2e-override-data.bash | 7 +++++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..0588595 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,21 @@ +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 e2e test + run: bash tests/e2e-override-data.bash diff --git a/tests/e2e-override-data.bash b/tests/e2e-override-data.bash index b34d5e2..fb5b4a4 100644 --- a/tests/e2e-override-data.bash +++ b/tests/e2e-override-data.bash @@ -8,6 +8,9 @@ set -euo pipefail +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +REPO_ROOT="${CHEZETC_REPO:-$(dirname "$SCRIPT_DIR")}" + RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' @@ -64,8 +67,8 @@ CHEZETC_DIR="$WORKDIR/chezetc" mkdir -p "$CHEZETC_DIR"/{utils,commands,hooks} # Copy the actual chezetc script and template -cp /home/la/git/chezetc/chezetc "$CHEZETC_DIR/" -cp /home/la/git/chezetc/chezmoi.toml "$CHEZETC_DIR/" +cp "$REPO_ROOT/chezetc" "$CHEZETC_DIR/" +cp "$REPO_ROOT/chezmoi.toml" "$CHEZETC_DIR/" # Create required shim/hook files (content doesn't matter for this test) touch "$CHEZETC_DIR/commands/editor" From d3fae91c6f6654e2d3b5c447989e6d84f60bc138 Mon Sep 17 00:00:00 2001 From: Shengyu Zhang Date: Wed, 13 May 2026 18:20:07 +0800 Subject: [PATCH 3/3] test: Refactor e2e tests with shared common library Replace monolithic e2e-override-data.bash with a shared common.bash library and focused test scripts (test-override-data.bash, test-envsubst.bash). Update CI workflow to run both new tests. Co-authored-by: DeepSeek --- .github/workflows/test.yml | 6 +- tests/common.bash | 104 +++++++++++++++++++++ tests/e2e-override-data.bash | 170 ---------------------------------- tests/test-envsubst.bash | 38 ++++++++ tests/test-override-data.bash | 20 ++++ 5 files changed, 166 insertions(+), 172 deletions(-) create mode 100644 tests/common.bash delete mode 100644 tests/e2e-override-data.bash create mode 100644 tests/test-envsubst.bash create mode 100644 tests/test-override-data.bash diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0588595..dfd874c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,5 +17,7 @@ jobs: - uses: actions/checkout@v4 - name: Install dependencies run: sudo apt-get update -qq && sudo apt-get install -y -qq gettext-base - - name: Run e2e test - run: bash tests/e2e-override-data.bash + - name: Run tests + run: | + bash tests/test-override-data.bash + bash tests/test-envsubst.bash 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/e2e-override-data.bash b/tests/e2e-override-data.bash deleted file mode 100644 index fb5b4a4..0000000 --- a/tests/e2e-override-data.bash +++ /dev/null @@ -1,170 +0,0 @@ -#!/bin/bash -# e2e test: verify --override-data-file replaces toml-merge.py -# -# This test ensures that: -# 1. chezetc no longer requires Python3/tomli/tomli_w -# 2. User's ETC_CFG is passed to chezmoi as --override-data-file -# 3. The generated chezmoi config is directly from envsubst (no merge step) - -set -euo pipefail - -SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" -REPO_ROOT="${CHEZETC_REPO:-$(dirname "$SCRIPT_DIR")}" - -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; } - -#-------------------------------------------------------------------- -# Setup: create isolated test environment -#-------------------------------------------------------------------- - -WORKDIR=$(mktemp -d) -trap 'rm -rf "$WORKDIR"' EXIT - -export HOME="$WORKDIR/home" -export XDG_CONFIG_HOME="$HOME/.config" -export XDG_CACHE_HOME="$HOME/.cache" -export XDG_DATA_HOME="$HOME/.local/share" - -# Mock bin directory to intercept sudo and chezmoi -MOCK_BIN="$WORKDIR/mock-bin" -mkdir -p "$MOCK_BIN" - -# Mock sudo: pass-through (skip sudo's own flags, execute the real command) -cat > "$MOCK_BIN/sudo" << 'MOCKEOF' -#!/bin/bash -args=() -while [[ $# -gt 0 ]]; do - case "$1" in - --preserve-env=*|--preserve-env|-E) shift ;; - --) shift; break ;; - -*) shift ;; - *) break ;; - esac -done -exec "$@" -MOCKEOF -chmod +x "$MOCK_BIN/sudo" - -# Mock chezmoi: capture all arguments into a file -cat > "$MOCK_BIN/chezmoi" << 'MOCKEOF' -#!/bin/bash -printf '%s\n' "$@" > /tmp/chezetc-e2e-chezmoi-args -MOCKEOF -chmod +x "$MOCK_BIN/chezmoi" - -export PATH="$MOCK_BIN:$PATH" - -#-------------------------------------------------------------------- -# Set up chezetc files (simulate installed chezetc) -#-------------------------------------------------------------------- - -CHEZETC_DIR="$WORKDIR/chezetc" -mkdir -p "$CHEZETC_DIR"/{utils,commands,hooks} - -# Copy the actual chezetc script and template -cp "$REPO_ROOT/chezetc" "$CHEZETC_DIR/" -cp "$REPO_ROOT/chezmoi.toml" "$CHEZETC_DIR/" - -# Create required shim/hook files (content doesn't matter for this test) -touch "$CHEZETC_DIR/commands/editor" -touch "$CHEZETC_DIR/commands/cd" -touch "$CHEZETC_DIR/hooks/post-add-chown.sh" - -#-------------------------------------------------------------------- -# Pre-create the generated chezmoi config -# (avoids the "Configuration updated, please run again" exit) -#-------------------------------------------------------------------- - -MOI_SRC_DIR="$XDG_DATA_HOME/chezetc" -MOI_DST_DIR="/etc" -MOI_CFG_DIR="$XDG_CONFIG_HOME/chezmoi/chezetc" -MOI_CACHE_DIR="$XDG_CACHE_HOME/chezmoi/chezetc" - -mkdir -p "$MOI_SRC_DIR" -mkdir -p "$MOI_CFG_DIR" -mkdir -p "$MOI_CACHE_DIR" - -# Pre-render the template (same content update_tmpl would produce) -{ - export ETC_APP=chezetc - export ETC_DIR="$CHEZETC_DIR" - export MOI_SRC_DIR MOI_DST_DIR - cat "$CHEZETC_DIR/chezmoi.toml" | envsubst -} > "$MOI_CFG_DIR/chezmoi.toml" - -#-------------------------------------------------------------------- -# Create user's custom config (template data for --override-data-file) -#-------------------------------------------------------------------- - -ETC_CFG_FILE="$XDG_CONFIG_HOME/chezetc/chezetc.toml" -mkdir -p "$(dirname "$ETC_CFG_FILE")" - -cat > "$ETC_CFG_FILE" << 'EOF' -# User template data -test_variable = "e2e_override_value" -user_email = "test@example.com" -EOF - -pass "Test environment ready at $WORKDIR" - -#-------------------------------------------------------------------- -# Run chezetc -#-------------------------------------------------------------------- - -ETC_APP=chezetc \ -ETC_MODE=CHEZMOI \ -ETC_SRC="$MOI_SRC_DIR" \ -ETC_DST="etc" \ -ETC_CFG="$ETC_CFG_FILE" \ - bash "$CHEZETC_DIR/chezetc" apply - -#-------------------------------------------------------------------- -# Assertions -#-------------------------------------------------------------------- - -CAPTURED_FILE="/tmp/chezetc-e2e-chezmoi-args" -[[ -f "$CAPTURED_FILE" ]] || fail "Mock chezmoi was not invoked (captured args file missing)" - -CAPTURED_ARGS=$(cat "$CAPTURED_FILE") -echo "Captured chezmoi args: $CAPTURED_ARGS" >&2 - -# Check 1: --override-data-file is present with correct path -if echo "$CAPTURED_ARGS" | grep -qF -- "--override-data-file=$ETC_CFG_FILE"; then - pass "--override-data-file points to correct user config" -else - fail "--override-data-file=$ETC_CFG_FILE not found in chezmoi args" -fi - -# Check 2: User config file exists and contains test data -if [[ -f "$ETC_CFG_FILE" ]]; then - pass "User config file exists at $ETC_CFG_FILE" -else - fail "User config file does not exist" -fi - -if grep -q 'test_variable = "e2e_override_value"' "$ETC_CFG_FILE"; then - pass "User config contains expected template data" -else - fail "User config missing expected data" -fi - -# Check 3: No Python-related commands were invoked (sanity check) -if echo "$CAPTURED_ARGS" | grep -q "python3\|tomli\|toml-merge"; then - fail "Python/tomli references found in chezmoi invocation" -else - pass "No Python/tomli dependency referenced" -fi - -#-------------------------------------------------------------------- -# Cleanup captured file -#-------------------------------------------------------------------- - -rm -f "$CAPTURED_FILE" - -echo >&2 -printf "${GREEN}All e2e tests passed!${NC}\n" >&2 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