Skip to content

Commit

Permalink
refactor: leverage spy and mock methods in _pure_is_inside_container.…
Browse files Browse the repository at this point in the history
…test.fish
  • Loading branch information
edouard-lopez committed Feb 16, 2024
1 parent 34b1672 commit 646b262
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
30 changes: 14 additions & 16 deletions tests/_pure_is_inside_container.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ function before_all
end
before_all

function cleanup_detection_methods
functions --erase _pure_detect_container_by_pid_method
functions --erase _pure_detect_container_by_cgroup_method
set --global container ""
end

function before_each
set --global container ""
_clean_all_mocks
_clean_all_spy_calls
functions --erase uname
end

function after_all
Expand All @@ -37,18 +32,20 @@ function after_all
set --erase called
end

before_each
@test "pure_enable_container_detection: feature is disabled" (
set --universal pure_enable_container_detection false
_mock_response uname "fake-os"
_spy_response uname "fake-os"

_pure_is_inside_container

_has_called uname # should NOT be called
) $status -eq $FAILURE

before_each
@test "pure_enable_container_detection: feature is enabled" (
set --universal pure_enable_container_detection true
_mock_response uname "fake-os"
_spy_response uname "fake-os"

_pure_is_inside_container

Expand Down Expand Up @@ -88,27 +85,28 @@ before_each
_pure_is_inside_container
) $status -eq $FAILURE

cleanup_detection_methods
before_each
if test (command 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 current-function); end # spy
_mock_exit_status _pure_detect_container_by_cgroup_method $FAILURE
_spy _pure_detect_container_by_pid_method

_pure_is_inside_container
) = "called: _pure_detect_container_by_pid_method"

_has_called _pure_detect_container_by_pid_method
) $status -eq $SUCCESS
end

cleanup_detection_methods
before_each
if test (command 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 current-function); end # spy
_spy _pure_detect_container_by_cgroup_method

_pure_is_inside_container
) = "called: _pure_detect_container_by_cgroup_method"
_has_called _pure_detect_container_by_cgroup_method
) $status -eq $SUCCESS
end


Expand Down
9 changes: 9 additions & 0 deletions tests/mocks/spying.fish
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ function _spy \
end # spy
end

# Usage:
# _clean_all_spy_calls
# @test "example using _spy method" (
# _spy foo
# … # do something that calls foo
# _has_called foo "--some --option"
# )
function _has_called \
--description "check spy method has been called, i.e has written to the /tmp/$function_name.mock_calls" \
--argument-names \
Expand All @@ -24,3 +31,5 @@ function _has_called \
return $FAILURE
end
end

alias _spy_response _mock_response

0 comments on commit 646b262

Please sign in to comment.