Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/remove user and host when outside container #327

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion functions/_pure_prompt_container.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function _pure_prompt_container
if test -n _pure_is_inside_container
if _pure_is_inside_container
echo "$pure_symbol_container_prefix"(_pure_user_at_host)
end
end
4 changes: 2 additions & 2 deletions tests/_pure_is_inside_container.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ if test (uname -s) = Linux
@test "_pure_is_inside_container: detect with pid method" (
set --universal pure_enable_container_detection true
function _pure_detect_container_by_cgroup_method; false; end # spy
function _pure_detect_container_by_pid_method; echo "called: "(status function); end # spy
function _pure_detect_container_by_pid_method; echo "called: "(status current-function); end # spy

_pure_is_inside_container
) = "called: _pure_detect_container_by_pid_method"
Expand All @@ -88,7 +88,7 @@ cleanup_detection_methods
if test (uname -s) = Linux
@test "_pure_is_inside_container: detect with cgroup method" (
set --universal pure_enable_container_detection true
function _pure_detect_container_by_cgroup_method; echo "called: "(status function); end # spy
function _pure_detect_container_by_cgroup_method; echo "called: "(status current-function); end # spy

_pure_is_inside_container
) = "called: _pure_detect_container_by_cgroup_method"
Expand Down
4 changes: 2 additions & 2 deletions tests/_pure_k8s_context.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ before_each

before_each
@test "_pure_k8s_context: call `kubectl config current-context`" (
function kubectl; echo (status function) $argv > /tmp/called; end # spy
function kubectl; echo (status current-function) $argv > /tmp/(status current-function).mock_calls; end # spy

_pure_k8s_context
_has_called "kubectl config current-context"
_has_called kubectl "config current-context"
) $status -eq $SUCCESS
4 changes: 2 additions & 2 deletions tests/_pure_k8s_namespace.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ before_each

before_each
@test "_pure_k8s_namespace: call `kubectl config view…`" (
function kubectl; echo (status function) $argv > /tmp/called; end # spy
function kubectl; echo (status current-function) $argv > /tmp/(status current-function).mock_calls; end # spy

_pure_k8s_namespace
_has_called "kubectl config view --minify --output jsonpath={..namespace}"
_has_called kubectl "config view --minify --output jsonpath={..namespace}"
) $status -eq $SUCCESS
22 changes: 22 additions & 0 deletions tests/_pure_prompt_container.test.fish
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source (dirname (status filename))/fixtures/constants.fish
source (dirname (status filename))/mocks/mocks.fish
source (dirname (status filename))/../functions/_pure_is_inside_container.fish
source (dirname (status filename))/../functions/_pure_user_at_host.fish
source (dirname (status filename))/../functions/_pure_prompt_container.fish
Expand All @@ -12,6 +13,10 @@ function before_each
_disable_colors
end

function after_each
_clean_all_mocks
end

if test "$USER" = nemo # we need to be in a container for those to work
before_each
@test "_pure_prompt_container: displays 'user@hostname' when inside container" (
Expand All @@ -27,3 +32,20 @@ if test "$USER" = nemo # we need to be in a container for those to work
string match --quiet --regex "🐋" (_pure_prompt_container)
) $status -eq $SUCCESS
end

@test "_pure_prompt_container: print nothing when outside a container" (
set --universal pure_enable_container_detection true
_mock_response _pure_is_inside_container $FAILURE

echo (_pure_prompt_container)
) = $EMPTY
after_each

@test "_pure_prompt_container: check is inside a container" (
set --universal pure_enable_container_detection true
_mock_response _pure_is_inside_container $FAILURE

_pure_prompt_container
_has_called _pure_is_inside_container
) $status -eq $SUCCESS
after_each
64 changes: 54 additions & 10 deletions tests/mocks/mocks.fish
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@ function _mock \
end
end

function backup \
--description "Backup a function by copying to prefixed function" \
--argument-names \
function_name

if functions --query $function_name
functions --erase __backup_$function_name # force overwrite
functions --copy $function_name __backup_$function_name
functions --erase $function_name
end
end

function _mock_response \
--description "Mock a response for a mock function" \
--argument-names \
function_name \
response # response to return

echo "$response" >/tmp/$function_name.mock_response
backup $function_name

# redefine function to return mock response
function $function_name \
--description "Mocking $function_name response" \
--argument-names mock_reponse
echo (status current-function) >/tmp/(status current-function).mock_calls
return (command cat /tmp/(status current-function).mock_response)
end
set --global --append __mocks $function_name
set --global --append __mocks_backup __backup_$function_name
end


function _clean_mock \
--description "Clean a mock function" \
Expand All @@ -21,39 +53,51 @@ 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

if functions --query __backup_$function_name # restore original function
functions --copy __backup_$function_name $function_name
functions --erase __backup_$function_name
end
end
set --global __mocks
set --global __mocks # clear mocks
end

function _spy \
--description "Create a spy around method" \
--argument-names \
function_name # name of the method to spy

backup $function_name

function $function_name
echo (status function) >/tmp/called
echo (status current-function) >/tmp/(status current-function).mock_calls
end # spy
end


function _cleanup_spy_calls
if test -r /tmp/called
rm /tmp/called
for mock_calls in /tmp/*.mock_calls
if test -r $mock_calls
rm -f $mock_calls
end
end
end

function _has_called \
--description "check spy method XYZ write to the /tmp/called file when called" \
--argument-names spy # name of the method
--description "check spy method XYZ write to the /tmp/$function_name.mock_calls file when called" \
--argument-names \
function_name \
function_args # arguments to passed to the spy

if test -r /tmp/called
grep -c -q "$spy" /tmp/called \
|| printf "DEBUG: %s: received: `%s` expected: `%s`\n\n" (status function) (cat /tmp/called) $spy # check spy was called
set --query function_args[1]; or set function_args $function_name # ue
if test -r /tmp/$function_name.mock_calls
grep -c -q "$function_args" /tmp/$function_name.mock_calls \
|| printf "DEBUG: %s: received: `%s` expected: `%s`\n\n" (status current-function) (cat /tmp/$spy.mock_calls) $spy # check spy was called
else
return $FAILURE
end
Expand Down
Loading