Skip to content

Commit

Permalink
test: move MacOS check to pid_method and add missing imports on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edouard-lopez committed Oct 6, 2022
1 parent f161a53 commit da8af91
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
16 changes: 11 additions & 5 deletions functions/_pure_detect_container_by_pid_method.fish
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ function _pure_detect_container_by_pid_method \

set --query proc_sched[1]; or set proc_sched /proc/1/sched

head -n 1 $proc_sched \
| string match \
--quiet \
--invert \
--regex 'init|systemd'
set --local os_name (uname -s)
if test $os_name != "Darwin"
head -n 1 $proc_sched \
| string match \
--quiet \
--invert \
--regex 'init|systemd'
else
set --local failure 1
return $failure
end
end
7 changes: 2 additions & 5 deletions functions/_pure_is_inside_container.fish
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ function _pure_is_inside_container \
return $success
end

set --local os_name (uname -s)
if test $os_name != "Darwin"
if _pure_detect_container_by_pid_method
return $success
end
if _pure_detect_container_by_pid_method
return $success
end

if _pure_detect_container_by_cgroup_method $cgroup_namespace
Expand Down
19 changes: 12 additions & 7 deletions tests/_pure_detect_container_by_pid_method.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function setup
end
setup

function teardown
functions --erase uname
end

@test "_pure_detect_container_by_pid_method: true for init" (
set --local proc_sched /proc/1/sched
echo "init (1, #threads: 1)" >$proc_sched
Expand All @@ -23,16 +27,17 @@ setup
_pure_detect_container_by_pid_method $proc_sched
) $status -eq $SUCCESS

# @test "_pure_detect_container_by_pid_method: true for MacOS" (
# set --local proc_sched /proc/1/sched
# echo "launchd (1, #threads: 1)" >$proc_sched

# _pure_detect_container_by_pid_method $proc_sched
# ) $status -eq $SUCCESS

@test "_pure_detect_container_by_pid_method: true for Github Action" (
set --local proc_sched /proc/1/sched
echo "systemd (1, #threads: 1)" >$proc_sched

_pure_detect_container_by_pid_method $proc_sched
) $status -eq $SUCCESS

@test "_pure_detect_container_by_pid_method: skip pid method on MacOS" (
function uname; echo "Darwin"; end # mock

_pure_detect_container_by_pid_method
) $status -eq $FAILURE

teardown
9 changes: 0 additions & 9 deletions tests/_pure_is_inside_container.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,5 @@ before_each
_pure_is_inside_container
) = "called: _pure_detect_container_by_cgroup_method"

before_each
@test "_pure_is_inside_container: skip pid method on MacOS" (
function uname; echo "Darwin"; end # mock
function _pure_detect_container_by_pid_method; echo "should not be called"; end # spy
function _pure_detect_container_by_cgroup_method; echo "dummy output"; end # spy

_pure_is_inside_container
) != "called: _pure_detect_container_by_pid_method"


teardown
2 changes: 2 additions & 0 deletions tests/_pure_prompt_container.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ source (dirname (status filename))/fixtures/constants.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
source (dirname (status filename))/../functions/_pure_detect_container_by_pid_method.fish
source (dirname (status filename))/../functions/_pure_detect_container_by_cgroup_method.fish
@echo (_print_filename (status filename))


Expand Down

0 comments on commit da8af91

Please sign in to comment.