diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dd254f..29c8739 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,10 @@ * symbol function cleanup and simplification ([#165](https://github.com/jonmosco/kube-ps1/issues/165)) * Added 2 new glyphs for the symbol -* The arguments to customize the symbol used can be passed in at runtime as an argument to kube_ps1, or set in the environment. The default is `⎈`. -* cleanup of the codebase to reduce environment variables +* The arguments to customize the symbol now include `k8s`, `oc`, and `img`. The default is `⎈`. +* cleanup of the codebase to reduce environment variable clutter * Some README cleanups. +* Start of a testing framework ## 0.8.0 (11/22/22) diff --git a/README.md b/README.md index 47d6aae..c99a3e3 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,7 @@ the following variables: | `KUBE_PS1_SYMBOL_ENABLE` | `true ` | Display the prompt Symbol. If set to `false`, this will also disable `KUBE_PS1_SEPARATOR` | | `KUBE_PS1_SYMBOL_PADDING` | `false` | Adds a space (padding) after the symbol to prevent clobbering prompt characters | | `KUBE_PS1_SYMBOL_CUSTOM` | `⎈ ` | Change the Default prompt symbol. Unicode `\u2388`. Options are `k8s`, `img`, `oc` | +| `KUBE_PS1_SYMBOL_COLOR` | `blue` | Change the Default symbol color. | | `KUBE_PS1_SEPARATOR` | | | Separator between symbol and context name | | `KUBE_PS1_DIVIDER` | `:` | Separator between context and namespace | | `KUBE_PS1_SUFFIX` | `)` | Prompt closing character | diff --git a/kube-ps1.sh b/kube-ps1.sh index 1ffd950..a493010 100644 --- a/kube-ps1.sh +++ b/kube-ps1.sh @@ -25,6 +25,7 @@ KUBE_PS1_BINARY="${KUBE_PS1_BINARY:-kubectl}" KUBE_PS1_SYMBOL_ENABLE="${KUBE_PS1_SYMBOL_ENABLE:-true}" KUBE_PS1_SYMBOL_PADDING="${KUBE_PS1_SYMBOL_PADDING:-false}" +KUBE_PS1_SYMBOL_COLOR="${KUBE_PS1_SYMBOL_COLOR:-}" KUBE_PS1_NS_ENABLE="${KUBE_PS1_NS_ENABLE:-true}" KUBE_PS1_CONTEXT_ENABLE="${KUBE_PS1_CONTEXT_ENABLE:-true}" @@ -157,6 +158,7 @@ _kube_ps1_symbol() { local k8s_symbol_color=blue local oc_glyph=$'\ue7b7' local oc_symbol_color=red + local custom_symbol_color="${KUBE_PS1_SYMBOL_COLOR:-$k8s_symbol_color}" # Choose the symbol based on the provided argument or environment variable case "${symbol_arg}" in @@ -164,7 +166,7 @@ _kube_ps1_symbol() { symbol="${symbol_img}" ;; "k8s") - symbol="$(_kube_ps1_color_fg ${k8s_symbol_color})${k8s_glyph}${KUBE_PS1_RESET_COLOR}" + symbol="$(_kube_ps1_color_fg "${custom_symbol_color}")${k8s_glyph}${KUBE_PS1_RESET_COLOR}" ;; "oc") symbol="$(_kube_ps1_color_fg ${oc_symbol_color})${oc_glyph}${KUBE_PS1_RESET_COLOR}" @@ -173,7 +175,7 @@ _kube_ps1_symbol() { case "$(_kube_ps1_shell_type)" in bash) if ((BASH_VERSINFO[0] >= 4)) && [[ $'\u2388' != "\\u2388" ]]; then - symbol="$(_kube_ps1_color_fg $k8s_symbol_color)${symbol_default}${KUBE_PS1_RESET_COLOR}" + symbol="$(_kube_ps1_color_fg $custom_symbol_color)${symbol_default}${KUBE_PS1_RESET_COLOR}" symbol_img=$'\u2638\ufe0f' else symbol=$'\xE2\x8E\x88' @@ -181,7 +183,7 @@ _kube_ps1_symbol() { fi ;; zsh) - symbol="$(_kube_ps1_color_fg $k8s_symbol_color)${symbol_default}${KUBE_PS1_RESET_COLOR}" + symbol="$(_kube_ps1_color_fg $custom_symbol_color)${symbol_default}${KUBE_PS1_RESET_COLOR}" symbol_img="☸️" ;; *) diff --git a/test/common.bash b/test/common.bash new file mode 100644 index 0000000..cdde66b --- /dev/null +++ b/test/common.bash @@ -0,0 +1,23 @@ +#!/usr/bin/env bats + +load '../../../node_modules/bats-support/load' +load '../../../node_modules/bats-assert/load' + +setup() { + source "${BATS_TEST_DIRNAME}/../kube-ps1.sh" >/dev/null 2>/dev/null + export _KUBE_PS1_DISABLE_PATH="/tmp/kube_ps1_disable" + export KUBECONFIG="/tmp/kubeconfig" + mkdir -p /tmp/kube-ps1 + touch /tmp/kubeconfig +} + +teardown() { + unset _KUBE_PS1_DISABLE_PATH + unset KUBECONFIG + unset KUBE_PS1_ENABLED + unset KUBE_PS1_CONTEXT + unset KUBE_PS1_NAMESPACE + unset KUBE_PS1_SYBBOL_COLOR + rm -rf /tmp/kube-ps1 + rm -f /tmp/kubeconfig +} diff --git a/test/kube-ps1.bats b/test/kube-ps1.bats new file mode 100644 index 0000000..3751315 --- /dev/null +++ b/test/kube-ps1.bats @@ -0,0 +1,95 @@ +#!/usr/bin/env bats + +source "${BATS_TEST_DIRNAME}/../kube-ps1.sh" >/dev/null 2>/dev/null + +load common + +@test "kubeon with no arguments" { + run bash -c 'kubeon; echo "KUBE_PS1_ENABLED=$KUBE_PS1_ENABLED"' + echo "$output" + [ "$status" -eq 0 ] +} + +@test "kubeon with --help" { + run kubeon --help + [ "$status" -eq 0 ] + [[ "$output" == *"Toggle kube-ps1 prompt on"* ]] +} + +@test "kubeon with -g" { + run kubeon -g + [ "$status" -eq 0 ] + [ ! -f "$_KUBE_PS1_DISABLE_PATH" ] +} + +@test "kubeoff with no arguments" { + run bash -c 'kubeooff; echo "$KUBE_PS1_ENABLED"' + [ "$status" -eq 0 ] + [[ "$output" == *"off"* ]] +} + +@test "kubeoff with --help" { + run kubeoff --help + [ "$status" -eq 0 ] + [[ "$output" == *"Toggle kube-ps1 prompt off"* ]] +} + +@test "kubeoff with -g" { + run kubeoff -g + [ "$status" -eq 0 ] + [ -f "$_KUBE_PS1_DISABLE_PATH" ] +} + +# @test "kubeoff with invalid flag" { +# run kubeoff --invalid +# [ "$status" -ne 0 ] +# [[ "$output" == *"error: unrecognized flag --invalid"* ]] +# } + +@test "kube_ps1_shell_type returns correct shell type" { + # Simulate bash + export BASH_VERSION="5.0.0" + run _kube_ps1_shell_type + [ "$status" -eq 0 ] + [ "$output" = "bash" ] + + # Simulate zsh + unset BASH_VERSION + export ZSH_VERSION="5.0.0" + run _kube_ps1_shell_type + [ "$status" -eq 0 ] + [ "$output" = "zsh" ] +} + +@test "_kube_ps1_binary_check returns true for existing command" { + run _kube_ps1_binary_check ls + [ "$status" -eq 0 ] +} + +@test "_kube_ps1_binary_check returns false for non-existing command" { + run _kube_ps1_binary_check nonexistingcommand + [ "$status" -ne 0 ] +} + +@test "_kube_ps1_symbol returns the default symbol" { + run _kube_ps1_symbol + assert_output --regexp '⎈' +} + +@test "kube_ps1 returns correct prompt when enabled" { + export KUBE_PS1_ENABLED="on" + export KUBE_PS1_CONTEXT="minikube" + export KUBE_PS1_NAMESPACE="default" + run kube_ps1 + [ "$status" -eq 0 ] + [[ "$output" == *"minikube"* ]] + [[ "$output" == *"default"* ]] +} + +@test "kube_ps1 returns empty prompt when disabled" { + export KUBE_PS1_ENABLED="off" + run kube_ps1 + [ "$status" -eq 0 ] + [ -z "$output" ] +} +