From da8af910de9c56e481d7def61cfc38d02cdc50c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89douard=20Lopez?= Date: Thu, 6 Oct 2022 16:02:28 +0200 Subject: [PATCH] test: move MacOS check to pid_method and add missing imports on tests --- .../_pure_detect_container_by_pid_method.fish | 16 +++++++++++----- functions/_pure_is_inside_container.fish | 7 ++----- ...e_detect_container_by_pid_method.test.fish | 19 ++++++++++++------- tests/_pure_is_inside_container.test.fish | 9 --------- tests/_pure_prompt_container.test.fish | 2 ++ 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/functions/_pure_detect_container_by_pid_method.fish b/functions/_pure_detect_container_by_pid_method.fish index 1ac91f25..1c47a10a 100644 --- a/functions/_pure_detect_container_by_pid_method.fish +++ b/functions/_pure_detect_container_by_pid_method.fish @@ -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 diff --git a/functions/_pure_is_inside_container.fish b/functions/_pure_is_inside_container.fish index cfee6447..7cf93f8e 100644 --- a/functions/_pure_is_inside_container.fish +++ b/functions/_pure_is_inside_container.fish @@ -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 diff --git a/tests/_pure_detect_container_by_pid_method.test.fish b/tests/_pure_detect_container_by_pid_method.test.fish index 3e33cbcd..9dea15d2 100644 --- a/tests/_pure_detect_container_by_pid_method.test.fish +++ b/tests/_pure_detect_container_by_pid_method.test.fish @@ -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 @@ -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 diff --git a/tests/_pure_is_inside_container.test.fish b/tests/_pure_is_inside_container.test.fish index 97d01ee2..f08496a1 100644 --- a/tests/_pure_is_inside_container.test.fish +++ b/tests/_pure_is_inside_container.test.fish @@ -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 diff --git a/tests/_pure_prompt_container.test.fish b/tests/_pure_prompt_container.test.fish index 6fa55ee4..8618c9a0 100644 --- a/tests/_pure_prompt_container.test.fish +++ b/tests/_pure_prompt_container.test.fish @@ -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))