diff --git a/.github/workflows/test_script.yml b/.github/workflows/test_script.yml index 0a2a410..f7a971f 100755 --- a/.github/workflows/test_script.yml +++ b/.github/workflows/test_script.yml @@ -41,6 +41,7 @@ jobs: sudo apt-get install -y tmux sudo apt-get install -y fd-find sudo apt-get install -y fzf + sudo apt-get install -y bats - name: Set up tmux environment run: | @@ -51,15 +52,13 @@ jobs: run: | python aactivator.py - # - name: Test tmux_sessionizer - # run: | - # chmod +x tmux_sessionizer - # ./tmux_sessionizer - # - # - name: Test tmux_windownizer - # run: | - # chmod +x tmux_windownizer - # ./tmux_windownizer + - name: Test tmux_sessionizer + run: | + bats tests/test_tmux_sessionizer.bats + + - name: Test tmux_windownizer + run: | + bats tests/test_tmux_windownizer.bats - name: Test setup_dev_environment.sh run: | diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c82e688 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.log +__pycache__* diff --git a/docker_check.sh b/docker_check.sh new file mode 100755 index 0000000..5714ec2 --- /dev/null +++ b/docker_check.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# Function to start Docker if not running +start_docker() { + echo "Docker is not running. Starting Docker..." + open --background -a Docker + sleep 3 # Wait for Docker to start +} + +# Check if Docker is installed +if ! command -v docker &> /dev/null; then + echo "Docker is not installed." + read -p "Do you want to install Docker using Homebrew? (y/n): " choice + if [ "$choice" == "y" ]; then + brew install --cask docker + echo "Docker has been installed. Please restart this script." + exit 0 + else + echo "Docker is required to run this script. Exiting." + exit 1 + fi +fi + +# Check if Docker is running +if [ "$(docker info >/dev/null 2>&1 && echo "running")" != "running" ]; then + start_docker +fi + +# Check if docker-compose is called +if [ "$1" == "docker-compose" ]; then + # If docker-compose is called, check if Docker is running again + if [ "$(docker info >/dev/null 2>&1 && echo "running")" != "running" ]; then + start_docker + fi +fi + +# Run the provided command +"$@" + diff --git a/tests/test_tmux_sessionizer b/tests/test_tmux_sessionizer new file mode 100755 index 0000000..8b9dfee --- /dev/null +++ b/tests/test_tmux_sessionizer @@ -0,0 +1,26 @@ +#!/usr/bin/env bats + +@test "Script creates a tmux session" { + TMP_DIR=$(mktemp -d) + ORIGINAL_SESSION=$(tmux display-message -p "#S") + SESSION_NAME="test_session" + + # Run your script with predefined selection + run ./tmux_sessionizer "$TMP_DIR/$SESSION_NAME" + + # Check if the script exited successfully + [ "$status" -eq 0 ] + + # Check if tmux session was created + run tmux has-session -t=$SESSION_NAME + + # Check if the tmux session exists + [ "$status" -eq 0 ] + + # Return to the original session + tmux switch-client -n -t="$ORIGINAL_SESSION" + + # Clean up + tmux kill-session -t="$SESSION_NAME" + rm -r "$TMP_DIR" +} diff --git a/tests/test_tmux_windownizer b/tests/test_tmux_windownizer new file mode 100755 index 0000000..cf8bebc --- /dev/null +++ b/tests/test_tmux_windownizer @@ -0,0 +1,40 @@ +#!/usr/bin/env bats + +@test "Script creates a new tmux window with the correct branch name" { + BRANCH_NAME="test_branch" + SESSION_NAME="test_session" + + # Check if the created session exists + run tmux has-session -t "$SESSION_NAME" + + if [ "$status" -ne 0 ]; then + # Create a new tmux session if it doesn't exist + run tmux new-session -d -s "$SESSION_NAME" + # Check if the session was created successfully + [ "$status" -eq 0 ] + + # Switch into the new test session + if [ "$status" -eq 0 ]; then + run tmux switch-client -t "$SESSION_NAME" + [ "$status" -eq 0 ] + fi + fi + + # Run your script with predefined branch name and session name + run ./tmux_windownizer "$BRANCH_NAME" + + # Check if the script exited successfully + [ "$status" -eq 0 ] + + # Check if tmux window was created + run tmux list-windows -t="$SESSION_NAME" | grep "$BRANCH_NAME" + [ "$status" -eq 0 ] + + # Remove the created tmux window + run tmux kill-window -t "$SESSION_NAME:$BRANCH_NAME" + [ "$status" -eq 0 ] + + # Remove the test session + run tmux kill-session -t "$SESSION_NAME" + [ "$status" -eq 0 ] +} diff --git a/tmux_sessionizer b/tmux_sessionizer index 403171d..f0b5c66 100755 --- a/tmux_sessionizer +++ b/tmux_sessionizer @@ -6,6 +6,8 @@ else selected=$(fd -H --full-path '/' --min-depth 1 --max-depth 3 --type d | fzf) fi +echo $selected + if [[ -z $selected ]]; then exit 0 fi diff --git a/tmux_windownizer b/tmux_windownizer index 5235857..5a147d5 100755 --- a/tmux_windownizer +++ b/tmux_windownizer @@ -1,26 +1,13 @@ #!/usr/bin/env bash -# Explicitly start tmux server -tmux start-server - -# Get the branch name without using 'basename' -branch_name=$(git rev-parse --abbrev-ref HEAD) - -# Set up tmux +branch_name=$(basename $1) session_name=$(tmux display-message -p "#S") -clean_name=$(echo "$branch_name" | tr "./" "__") +clean_name=$(echo $branch_name | tr "./" "__") target="$session_name:$clean_name" -# Start tmux server if not already running -if [ -z "$(tmux list-sessions 2>/dev/null)" ]; then - sleep 1 - tmux new-session -d -s "$session_name" -fi - -# Create a new window if it doesn't exist -if [ -z "$(tmux list-windows -t "$session_name" | grep "$clean_name")" ]; then - tmux neww -dn "$clean_name" +if ! tmux has-session -t $target 2> /dev/null; then + tmux neww -dn $clean_name fi shift -tmux send-keys -t "$target" "$*" +tmux send-keys -t $target "$*"