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
24 changes: 24 additions & 0 deletions .github/workflows/bats.yml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions tests/backup.bats
Original file line number Diff line number Diff line change
@@ -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 ]
}
42 changes: 42 additions & 0 deletions tests/install.bats
Original file line number Diff line number Diff line change
@@ -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" ]
}
31 changes: 31 additions & 0 deletions tests/uninstall.bats
Original file line number Diff line number Diff line change
@@ -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"
}
Loading