Skip to content

Commit

Permalink
test: cover _pure_prompt_k8s and add mocks helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
edouard-lopez committed Aug 14, 2023
1 parent 4abd9a7 commit 020a1a7
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
5 changes: 4 additions & 1 deletion functions/_pure_prompt_k8s.fish
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
38 changes: 38 additions & 0 deletions tests/_pure_prompt_k8s.test.fish
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions tests/mocks/kubectl.mock.fish
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions tests/mocks/mocks.fish
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 020a1a7

Please sign in to comment.