Skip to content

Commit

Permalink
add symbol color option and testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmosco committed May 19, 2024
1 parent 7ecd42a commit 3f09d1a
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
8 changes: 5 additions & 3 deletions kube-ps1.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -157,14 +158,15 @@ _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
"img")
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}"
Expand All @@ -173,15 +175,15 @@ _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'
symbol_img=$'\xE2\x98\xB8'
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="☸️"
;;
*)
Expand Down
23 changes: 23 additions & 0 deletions test/common.bash
Original file line number Diff line number Diff line change
@@ -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
}
95 changes: 95 additions & 0 deletions test/kube-ps1.bats
Original file line number Diff line number Diff line change
@@ -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" ]
}

0 comments on commit 3f09d1a

Please sign in to comment.