From 6b1afdf7bd8baacb21f6e4ab989ef03983e76d72 Mon Sep 17 00:00:00 2001 From: Kael AI Agent Date: Thu, 12 Mar 2026 09:30:07 +0100 Subject: [PATCH] c26c9298-c1b8-4120-a323-0b3ca56392ba Create Bats test suite for install.sh, uninstall.sh and backup.sh Implemented by Kael AI Agent --- .github/workflows/bats.yml | 24 ++++++++++++++++++++++ tests/backup.bats | 31 ++++++++++++++++++++++++++++ tests/install.bats | 42 ++++++++++++++++++++++++++++++++++++++ tests/uninstall.bats | 31 ++++++++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 .github/workflows/bats.yml create mode 100644 tests/backup.bats create mode 100644 tests/install.bats create mode 100644 tests/uninstall.bats diff --git a/.github/workflows/bats.yml b/.github/workflows/bats.yml new file mode 100644 index 0000000..ddc8c56 --- /dev/null +++ b/.github/workflows/bats.yml @@ -0,0 +1,24 @@ +name: Bats Test Suite + +on: + push: + branches: [ main ] + pull_request: + branches: [ '**' ] + +jobs: + bats: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Install Bats + run: | + sudo apt-get update + sudo apt-get install -y bats + + - name: Run Bats tests + run: | + chmod +x tests/*.bats + bats tests \ No newline at end of file diff --git a/tests/backup.bats b/tests/backup.bats new file mode 100644 index 0000000..a050e4e --- /dev/null +++ b/tests/backup.bats @@ -0,0 +1,31 @@ +#!/usr/bin/env bats + +setup() { + TMPDIR=$(mktemp -d) + export HOME="$TMPDIR" + REPO_ROOT="$(pwd)" + cp -r "$REPO_ROOT"/{backup.sh,dotfiles.yml,.dotfiles} "$TMPDIR"/ + cd "$TMPDIR" +} + +teardown() { + rm -rf "$TMPDIR" +} + +@test "backup rotation respects keep policy" { + # Run backup several times to generate multiple snapshots. + for i in {1..4}; do + run bash "$REPO_ROOT/backup.sh" + [ "$status" -eq 0 ] + # Ensure each backup has a unique timestamp. + sleep 1 + done + + backup_root="$HOME/.dotfiles_backup" + # Count the number of backup directories created. + count=$(find "$backup_root" -mindepth 1 -maxdepth 1 -type d | wc -l) + + # The keep policy is defined in dotfiles.yml (default to 3 if not set). + # The test asserts that we never keep more than 3 backups. + [ "$count" -le 3 ] +} \ No newline at end of file diff --git a/tests/install.bats b/tests/install.bats new file mode 100644 index 0000000..3be5358 --- /dev/null +++ b/tests/install.bats @@ -0,0 +1,42 @@ +#!/usr/bin/env bats + +setup() { + # Create an isolated HOME directory + TMPDIR=$(mktemp -d) + export HOME="$TMPDIR" + + # Preserve the repository root path + REPO_ROOT="$(pwd)" + + # Copy the scripts and configuration needed for the test + cp -r "$REPO_ROOT"/{install.sh,dotfiles.yml,.dotfiles} "$TMPDIR"/ + cd "$TMPDIR" +} + +teardown() { + rm -rf "$TMPDIR" +} + +@test "install creates symlinks" { + run bash "$REPO_ROOT/install.sh" + [ "$status" -eq 0 ] + + # The repository's dotfiles are expected to be under .dotfiles/ + # Verify that a typical dotfile (e.g., .bashrc) is linked. + [ -L "$HOME/.bashrc" ] + + target=$(readlink "$HOME/.bashrc") + # The target may be an absolute path or relative to the repo root. + [[ "$target" == "$REPO_ROOT/.dotfiles/.bashrc" ]] || [[ "$target" == ".dotfiles/.bashrc" ]] +} + +@test "install dry‑run does not create symlinks" { + run bash "$REPO_ROOT/install.sh" --dry-run + [ "$status" -eq 0 ] + + # The dry‑run output should mention the file it would link. + [[ "$output" == *".bashrc"* ]] + + # No symlink should actually exist. + [ ! -e "$HOME/.bashrc" ] +} \ No newline at end of file diff --git a/tests/uninstall.bats b/tests/uninstall.bats new file mode 100644 index 0000000..058807b --- /dev/null +++ b/tests/uninstall.bats @@ -0,0 +1,31 @@ +#!/usr/bin/env bats + +setup() { + TMPDIR=$(mktemp -d) + export HOME="$TMPDIR" + REPO_ROOT="$(pwd)" + cp -r "$REPO_ROOT"/{install.sh,uninstall.sh,dotfiles.yml,.dotfiles} "$TMPDIR"/ + cd "$TMPDIR" +} + +teardown() { + rm -rf "$TMPDIR" +} + +@test "uninstall restores original files" { + # Create a real file that the installer would back up. + echo "original content" > "$HOME/.bashrc" + + # Run the installer – it should back up the original and replace it with a symlink. + run bash "$REPO_ROOT/install.sh" + [ "$status" -eq 0 ] + [ -L "$HOME/.bashrc" ] + + # Run the uninstaller – it should restore the original file and remove the symlink. + run bash "$REPO_ROOT/uninstall.sh" + [ "$status" -eq 0 ] + + [ ! -L "$HOME/.bashrc" ] + [ -f "$HOME/.bashrc" ] + grep -q "original content" "$HOME/.bashrc" +} \ No newline at end of file