Repository navigation
feat: Replace toml-merge.py with chezmoi --override-data-file
#5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For users who have the documented
~/.config/chezetc/chezetc.tomlfile, 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/supportsETC_CFGas 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 👍 / 👎.