wfxr.forgit/tests/switch-branch.test.sh
sandroid 0fbd9edd87
Some checks failed
ci / build (macos-latest) (push) Has been cancelled
ci / build (ubuntu-latest) (push) Has been cancelled
ci / commitlint (push) Has been cancelled
ci / format (push) Has been cancelled
ci / rumdl-check (push) Has been cancelled
ci / actionlint (push) Has been cancelled
feat: add option to disable auto creating branches (#552)
Adds `$FORGIT_SWITCH_AUTO_CREATE_BRANCH` and `$FORGIT_CHECKOUT_BRANCH_AUTO_CREATE_BRANCH`
environment variables that can be set to `false` to prevent `checkout_branch <BRANCH_NAME>` and
`switch_brach <BRANCH_NAME>` from creating new branches when they do not exist. With the environment
variables set to `false`, new branches can still be created explicitly using
`switch_branch -c <BRANCH_NAME>` and `<checkout_branch> -c <BRANCH_NAME>` respectively.
2026-09-02 00:58:17 +02:00

52 lines
1.8 KiB
Bash

#!/usr/bin/env bash
function set_up_before_script() {
source bin/git-forgit
# Ignore global git config files
export GIT_CONFIG_SYSTEM=/dev/null
export GIT_CONFIG_GLOBAL=/dev/null
# Create a temporary git repository for testing
cd "$(bashunit::temp_dir)" || return 1
git init -q
git config user.email "test@example.com"
git config user.name "Test User"
git commit -q --allow-empty -m "initial commit"
git branch existing-branch
}
function test_switch_branch_creates_branch_when_it_does_not_exist() {
_forgit_switch_branch auto-created-branch &>/dev/null
assert_exit_code "0"
assert_same "auto-created-branch" "$(git branch --show-current)"
}
function test_switch_branch_does_not_create_branch_when_auto_create_is_disabled() {
FORGIT_SWITCH_AUTO_CREATE_BRANCH=false
_forgit_switch_branch not-created-branch &>/dev/null
assert_exit_code "128"
assert_not_contains "not-created-branch" "$(git branch --list --format='%(refname:short)')"
}
function test_switch_branch_switches_to_existing_branch() {
FORGIT_SWITCH_AUTO_CREATE_BRANCH=false
_forgit_switch_branch existing-branch &>/dev/null
assert_exit_code "0"
assert_same "existing-branch" "$(git branch --show-current)"
}
function test_switch_branch_switches_to_existing_branch_when_auto_create_is_disabled() {
FORGIT_SWITCH_AUTO_CREATE_BRANCH=false
_forgit_switch_branch existing-branch &>/dev/null
assert_exit_code "0"
assert_same "existing-branch" "$(git branch --show-current)"
}
function test_switch_branch_creates_branch_when_requested_when_auto_create_is_disabled() {
FORGIT_SWITCH_AUTO_CREATE_BRANCH=false
_forgit_switch_branch -c explicitly-created-branch &>/dev/null
assert_exit_code "0"
assert_same "explicitly-created-branch" "$(git branch --show-current)"
}