diff --git a/functions/_pure_prompt_k8s.fish b/functions/_pure_prompt_k8s.fish index 94086820..a992fee8 100644 --- a/functions/_pure_prompt_k8s.fish +++ b/functions/_pure_prompt_k8s.fish @@ -1,5 +1,8 @@ function _pure_prompt_k8s - if $pure_enable_k8s; and _pure_check_availability pure_enable_k8s kubectl + if set --query pure_enable_k8s; + and test "$pure_enable_k8s" = true; + and _pure_check_availability pure_enable_k8s kubectl + set -l context (_pure_set_color $pure_color_k8s_context)(_pure_k8s_context) set -l namespace (_pure_set_color $pure_color_k8s_namespace)(_pure_k8s_namespace) echo "$pure_symbol_k8s_prefix $context/$namespace" diff --git a/tests/_pure_prompt_k8s.test.fish b/tests/_pure_prompt_k8s.test.fish new file mode 100644 index 00000000..2783d2dc --- /dev/null +++ b/tests/_pure_prompt_k8s.test.fish @@ -0,0 +1,38 @@ +source (dirname (status filename))/fixtures/constants.fish +source (dirname (status filename))/mocks/mocks.fish +source (dirname (status filename))/../functions/_pure_prompt_k8s.fish +source (dirname (status filename))/../functions/_pure_k8s_context.fish +source (dirname (status filename))/../functions/_pure_k8s_namespace.fish +source (dirname (status filename))/../functions/_pure_check_availability.fish +@echo (_print_filename (status filename)) + +function before_each + _purge_configs + _disable_colors +end + +function teardown + _clean_all_mocks +end + + +@test "_pure_prompt_k8s: ensure default behaviour has no error" ( + _pure_prompt_k8s +) $status -eq $SUCCESS + + +@test "_pure_prompt_k8s: ensure default behaviour print nothing" ( + echo (_pure_prompt_k8s) +) = $EMPTY + + +before_each +@test "_pure_prompt_k8s: print kubernetes context and namespace" ( + set --universal pure_enable_k8s true + set --universal pure_symbol_k8s_prefix "☸" + _mock kubectl + + _pure_prompt_k8s +) = '☸ my-context/my-namespace' + +teardown diff --git a/tests/mocks/kubectl.mock.fish b/tests/mocks/kubectl.mock.fish new file mode 100644 index 00000000..e0b8fe90 --- /dev/null +++ b/tests/mocks/kubectl.mock.fish @@ -0,0 +1,17 @@ +function kubectl \ + --description 'kubectl mock for test' \ + --argument-names \ + topcommand \ + subcommand + + if test $topcommand = config + if test $subcommand = current-context + echo my-context + end + if test $subcommand = view + echo my-namespace + end + else + echo "unknown command" + end +end # mock diff --git a/tests/mocks/mocks.fish b/tests/mocks/mocks.fish new file mode 100644 index 00000000..915416d5 --- /dev/null +++ b/tests/mocks/mocks.fish @@ -0,0 +1,31 @@ +function _mock \ + --description "Invoke a mock function" \ + --argument-names \ + function_name + + set mock_filepath (dirname (status filename))/$function_name.mock.fish + if test -e $mock_filepath + source (dirname (status filename))/$function_name.mock.fish + set --global --append __mocks $function_name + end +end + + +function _clean_mock \ + --description "Clean a mock function" \ + --argument-names \ + function_name + + functions --erase $function_name +end + +function _clean_all_mocks \ + --description "Clean all mock function" + set --local new_mocks + for mock in $__mocks + if functions --query $mock + functions --erase $mock + end + end + set --global __mocks +end