Skip to content

Commit

Permalink
fix: remove user and host when outside container
Browse files Browse the repository at this point in the history
  • Loading branch information
edouard-lopez committed Aug 21, 2023
1 parent 8f4a68e commit 5856ccc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
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
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
18 changes: 13 additions & 5 deletions tests/mocks/mocks.fish
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ function _clean_all_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 # clear mocks
end
Expand All @@ -67,6 +72,8 @@ function _spy \
--argument-names \
function_name # name of the method to spy

backup $function_name

function $function_name
echo (status current-function) >/tmp/(status current-function).mock_calls
end # spy
Expand All @@ -82,13 +89,14 @@ function _cleanup_spy_calls
end

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

if test -r /tmp/$spy.mock_calls
grep -c -q "$spy_args" /tmp/$spy.mock_calls \
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
Expand Down

0 comments on commit 5856ccc

Please sign in to comment.