add bash-it

This commit is contained in:
2022-05-05 22:20:02 +02:00
parent 6a62a35d0d
commit 43701f0590
490 changed files with 33047 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
# shellcheck shell=bats
load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"
function local_setup_file() {
setup_libs "helpers"
load "${BASH_IT?}/plugins/available/base.plugin.bash"
}
@test 'plugins base: ips()' {
declare -r localhost='127.0.0.1'
run ips
assert_success
assert_line $localhost
}
@test 'plugins base: myip()' {
run myip
assert_success
declare -r mask_ip=$(echo $output | tr -s '[0-9]' '?')
[[ $mask_ip == 'Your public IP is:'*'?.?.?.?'* ]]
}
@test 'plugins base: pickfrom()' {
stub_file="${BATS_TEST_TMPDIR}/stub_file"
printf "l1\nl2\nl3" > $stub_file
run pickfrom $stub_file
assert_success
[[ $output == l? ]]
}
@test 'plugins base: mkcd()' {
cd "${BATS_TEST_TMPDIR}"
declare -r dir_name="-dir_with_dash"
# Make sure that the directory does not exist prior to the test
rm -rf "${BATS_TEST_TMPDIR}/${dir_name}"
run mkcd "${dir_name}"
assert_success
assert_dir_exist "${BATS_TEST_TMPDIR}/${dir_name}"
mkcd "${dir_name}"
assert_equal "${PWD}" "${BATS_TEST_TMPDIR//\/\///}/${dir_name}"
}
@test 'plugins base: lsgrep()' {
for i in 1 2 3; do mkdir -p "${BASH_IT}/${i}"; done
cd $BASH_IT
run lsgrep 2
assert_success
assert_equal $output 2
}
@test 'plugins base: buf()' {
declare -r file="${BATS_TEST_TMPDIR}/file"
touch $file
# Take one timestamp before running the `buf` function
declare -r stamp1=$(date +%Y%m%d_%H%M%S)
run buf $file
# Take another timestamp after running `buf`.
declare -r stamp2=$(date +%Y%m%d_%H%M%S)
# Verify that the backup file ends with one of the two timestamps.
# This is done to avoid race conditions where buf is run close to the end
# of a second, in which case the second timestamp might be in the next second,
# causing the test to fail.
# By using `or` for the two checks, we can verify that one of the two files is present.
# In most cases, it's going to have the same timestamp anyway.
# We can't use `assert_file_exist` here, since it only checks for a single file name.
assert [ -e "${file}_${stamp1}" \
-o -e "${file}_${stamp2}" ]
}

View File

@@ -0,0 +1,384 @@
# shellcheck shell=bats
load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"
function local_setup_file() {
setup_libs "helpers"
load "${BASH_IT?}/plugins/available/battery.plugin.bash"
}
# Sets up the `_command_exists` function so that it only responds `true` if called with
# the name of the function that was passed in as an argument to `setup_command_exists`.
# This is used to ensure that the test cases can test the various energy management tools
# without actually having them. When called like
#
# setup_command_exists "pmset"
#
# then calling `_command_exists "pmset"` will return `true`,
# while calling `_command_exists "ioreg"` (or other commands) will return `false`.
#
# It's cool that Bash allows to define functions within functions, works almost like
# a closure in JavaScript.
function setup_command_exists {
success_command="$1"
function _command_exists {
case "$1" in
"${success_command}")
true
;;
*)
false
;;
esac
}
}
#######################
#
# no tool
#
@test 'plugins battery: battery-percentage with no tool' {
setup_command_exists "fooooo"
run battery_percentage
assert_output "no"
}
#######################
#
# pmset
#
# Creates a `pmset` function that simulates output like the real `pmset` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_pmset {
percent="$1"
function pmset {
printf "\-InternalBattery-0 (id=12345) %s; discharging; 16:00 remaining present: true" "${percent}"
}
}
@test 'plugins battery: battery-percentage with pmset, 100%' {
setup_command_exists "pmset"
setup_pmset "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with pmset, 98%' {
setup_command_exists "pmset"
setup_pmset "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with pmset, 98.5%' {
setup_command_exists "pmset"
setup_pmset "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with pmset, 4%' {
setup_command_exists "pmset"
setup_pmset "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with pmset, no status' {
setup_command_exists "pmset"
setup_pmset ""
run battery_percentage
assert_output "-1"
}
#######################
#
# acpi
#
# Creates a `acpi` function that simulates output like the real `acpi` command.
# The passed in parameters are used for
# 1) the remaining battery percentage.
# 2) the battery status
function setup_acpi {
percent="$1"
status="$2"
function acpi {
printf "Battery 0: %s, %s, 01:02:48 until charged" "${status}" "${percent}"
}
}
@test 'plugins battery: battery-percentage with acpi, 100% Full' {
setup_command_exists "acpi"
setup_acpi "100%" "Full"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with acpi, 98% Charging' {
setup_command_exists "acpi"
setup_acpi "98%" "Charging"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 98% Discharging' {
setup_command_exists "acpi"
setup_acpi "98%" "Discharging"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 98% Unknown' {
setup_command_exists "acpi"
setup_acpi "98%" "Unknown"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 4% Charging' {
setup_command_exists "acpi"
setup_acpi "4%" "Charging"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with acpi, 4% no status' {
setup_command_exists "acpi"
setup_acpi "4%" ""
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with acpi, no status' {
setup_command_exists "acpi"
setup_acpi "" ""
run battery_percentage
assert_output "-1"
}
#######################
#
# upower
#
# Creates a `upower` function that simulates output like the real `upower` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_upower {
percent="$1"
BAT0="/org/freedesktop/UPower/devices/battery_BAT$RANDOM"
function upower {
case $1 in
'-e'|'--enumerate')
printf '%s\n' "$BAT0" "/org/freedesktop/UPower/devices/mouse_hid_${RANDOM}_battery"
;;
'-i'|'--show-info')
if [[ $2 == "$BAT0" ]]
then
printf "voltage: 12.191 V\n time to full: 57.3 minutes\n percentage: %s\n capacity: 84.6964" "${percent}"
else
false
fi
;;
esac
}
}
@test 'plugins battery: battery-percentage with upower, 100%' {
setup_command_exists "upower"
setup_upower "100.00%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with upower, 98%' {
setup_command_exists "upower"
setup_upower "98.4567%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with upower, 98.5%' {
setup_command_exists "upower"
setup_upower "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with upower, 4%' {
setup_command_exists "upower"
setup_upower "4.2345%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with upower, no output' {
setup_command_exists "upower"
setup_upower ""
run battery_percentage
assert_output "-1"
}
#######################
#
# ioreg
#
# Creates a `ioreg` function that simulates output like the real `ioreg` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_ioreg {
percent="$1"
function ioreg {
printf "\"MaxCapacity\" = 100\n\"CurrentCapacity\" = %s" "${percent}"
}
}
@test 'plugins battery: battery-percentage with ioreg, 100%' {
setup_command_exists "ioreg"
setup_ioreg "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with ioreg, 98%' {
setup_command_exists "ioreg"
setup_ioreg "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with ioreg, 98.5%' {
setup_command_exists "ioreg"
setup_ioreg "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with ioreg, 4%' {
setup_command_exists "ioreg"
setup_ioreg "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with ioreg, no status' {
setup_command_exists "ioreg"
# Simulate that no battery is present
function ioreg {
printf ""
}
run battery_percentage
assert_output "-1"
}
#######################
#
# WMIC
#
# Creates a `WMIC` function that simulates output like the real `WMIC` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_WMIC {
percent="$1"
function WMIC {
printf "Charge: %s" "${percent}"
}
}
@test 'plugins battery: battery-percentage with WMIC, 100%' {
setup_command_exists "WMIC"
setup_WMIC "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with WMIC, 98%' {
setup_command_exists "WMIC"
setup_WMIC "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with WMIC, 98.5%' {
setup_command_exists "WMIC"
setup_WMIC "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with WMIC, 4%' {
setup_command_exists "WMIC"
setup_WMIC "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with WMIC, no status' {
setup_command_exists "WMIC"
setup_WMIC ""
run battery_percentage
assert_output "-1"
}

View File

@@ -0,0 +1,41 @@
# shellcheck shell=bats
load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"
function local_setup_file() {
setup_libs "command_duration"
load "${BASH_IT?}/plugins/available/cmd-returned-notify.plugin.bash"
}
@test "plugins cmd-returned-notify: notify after elapsed time" {
export NOTIFY_IF_COMMAND_RETURNS_AFTER=0
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
sleep 1
run precmd_return_notification
assert_success
assert_output $'\a'
}
@test "plugins cmd-returned-notify: do not notify before elapsed time" {
export NOTIFY_IF_COMMAND_RETURNS_AFTER=10
export COMMAND_DURATION_START_SECONDS="${EPOCHREALTIME:-$SECONDS}"
sleep 1
run precmd_return_notification
assert_success
assert_output $''
}
@test "lib command_duration: preexec no output" {
export COMMAND_DURATION_START_SECONDS=
run _command_duration_pre_exec
assert_success
assert_output ""
}
@test "lib command_duration: preexec set COMMAND_DURATION_START_SECONDS" {
export COMMAND_DURATION_START_SECONDS=
assert_equal "${COMMAND_DURATION_START_SECONDS}" ""
NOW="${EPOCHREALTIME:-$SECONDS}"
_command_duration_pre_exec
# We need to make sure to account for nanoseconds...
assert_equal "${COMMAND_DURATION_START_SECONDS%.*}" "${NOW%.*}"
}

View File

@@ -0,0 +1,74 @@
# shellcheck shell=bats
load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"
function local_setup_file() {
setup_libs "helpers"
}
function setup_go_path()
{
local go_path="$1"
# Make sure that the requested GO folder is available
assert_dir_exist "$go_path/bin"
# Make sure that the requested GO folder is on the path
export GOPATH="$go_path:${GOPATH:-}"
}
# We test `go version` in each test to account for users with goenv and no system go.
@test 'ensure _bash-it-gopath-pathmunge is defined' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found'
load ../../plugins/available/go.plugin
run type -t _bash-it-gopath-pathmunge
assert_line 'function'
}
@test 'plugins go: single entry in GOPATH' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found'
setup_go_path "$BASH_IT/test/fixtures/go/gopath"
load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath/bin"
}
@test 'plugins go: single entry in GOPATH, with space' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found'
setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin"
}
@test 'plugins go: single entry in GOPATH, with escaped space' {
skip 'huh?'
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found'
setup_go_path "$BASH_IT/test/fixtures/go/go\ path"
load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/go\ path/bin"
}
@test 'plugins go: multiple entries in GOPATH' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found'
setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/gopath2"
load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath2/bin:$BASH_IT/test/fixtures/go/gopath/bin"
}
@test 'plugins go: multiple entries in GOPATH, with space' {
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found'
setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin:$BASH_IT/test/fixtures/go/gopath/bin"
}
@test 'plugins go: multiple entries in GOPATH, with escaped space' {
skip 'huh?'
{ _command_exists go && go version &>/dev/null; } || skip 'golang not found'
setup_go_path "$BASH_IT/test/fixtures/go/gopath"
setup_go_path "$BASH_IT/test/fixtures/go/go path"
load ../../plugins/available/go.plugin
assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go\ path/bin:$BASH_IT/test/fixtures/go/gopath/bin"
}

View File

@@ -0,0 +1,31 @@
# shellcheck shell=bats
load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"
function local_setup_file() {
setup_libs "helpers"
}
@test "plugins ruby: remove_gem is defined" {
run load "${BASH_IT?}/plugins/available/ruby.plugin.bash"
assert_success
load "${BASH_IT?}/plugins/available/ruby.plugin.bash"
run type remove_gem
assert_line -n 1 "remove_gem () "
}
@test "plugins ruby: PATH includes ~/.gem/ruby/bin" {
if ! type ruby >/dev/null; then
skip 'ruby not installed'
fi
mkdir -p "$(ruby -e 'print Gem.user_dir')/bin"
run load "${BASH_IT?}/plugins/available/ruby.plugin.bash"
assert_success
load "${BASH_IT?}/plugins/available/ruby.plugin.bash"
local last_path_entry="$(tail -1 <<<"${PATH//:/$'\n'}")"
[[ "${last_path_entry}" == "$(ruby -e 'print Gem.user_dir')/bin" ]]
}

View File

@@ -0,0 +1,42 @@
# shellcheck shell=bats
load "${MAIN_BASH_IT_DIR?}/test/test_helper.bash"
function local_setup_file() {
setup_libs "helpers"
load "${BASH_IT?}/plugins/available/xterm.plugin.bash"
}
@test "plugins xterm: shorten command output" {
export SHORT_TERM_LINE=true
run _short-command "${BASH_IT}/test/fixtures/plugin/xterm/files"/*
assert_success
assert_output "${BASH_IT}/test/fixtures/plugin/xterm/files/arg0"
}
@test "plugins xterm: full command output" {
export SHORT_TERM_LINE=false
run _short-command "${BASH_IT}/test/fixtures/plugin/xterm/files"/*
assert_success
assert_output "$(echo "${BASH_IT}/test/fixtures/plugin/xterm/files"/*)"
}
@test "plugins xterm: shorten dirname output" {
export SHORT_TERM_LINE=true
run _short-dirname
assert_success
assert_output "$(basename "${PWD}")"
}
@test "plugins xterm: full dirname output" {
export SHORT_TERM_LINE=false
run _short-dirname
assert_success
assert_output "${PWD}"
}
@test "plugins xterm: set xterm title" {
run set_xterm_title title
assert_success
assert_output $'\033]0;title\007'
}