From ef61f1f40591829a99cd4d3704f58384cd6616f4 Mon Sep 17 00:00:00 2001 From: Emmanuel Valverde Ramos Date: Thu, 17 Oct 2024 18:53:33 +0200 Subject: [PATCH] chore: add test of spies failing to fix on main --- tests/functional/doubles_test.sh | 49 +++++++++++++++++++ tests/functional/fixtures/doubles_function.sh | 5 ++ tests/functional/fixtures/doubles_ps_output | 8 +++ tests/functional/fixtures/doubles_script.sh | 3 ++ ...t_mock_ps_when_executing_a_script.snapshot | 3 ++ ...when_executing_a_sourced_function.snapshot | 3 ++ 6 files changed, 71 insertions(+) create mode 100644 tests/functional/doubles_test.sh create mode 100644 tests/functional/fixtures/doubles_function.sh create mode 100644 tests/functional/fixtures/doubles_ps_output create mode 100644 tests/functional/fixtures/doubles_script.sh create mode 100644 tests/functional/snapshots/doubles_test_sh_.test_mock_ps_when_executing_a_script.snapshot create mode 100644 tests/functional/snapshots/doubles_test_sh_.test_mock_ps_when_executing_a_sourced_function.snapshot diff --git a/tests/functional/doubles_test.sh b/tests/functional/doubles_test.sh new file mode 100644 index 00000000..82780983 --- /dev/null +++ b/tests/functional/doubles_test.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +function test_mock_ps_when_executing_a_script() { + skip "The mock is not been able to cat the output of the cat" + return + mock ps cat ./tests/functional/fixtures/doubles_ps_output + + assert_match_snapshot "$(./tests/functional/fixtures/doubles_script.sh)" +} + +function test_mock_ps_when_executing_a_sourced_function() { + skip "The mock is not been able to cat the output of the cat" + return + source ./tests/functional/fixtures/doubles_function.sh + mock ps cat ./tests/functional/fixtures/doubles_ps_output + + assert_match_snapshot "$(top_mem)" +} + +function test_spy_commands_called_once_when_executing_a_script() { + skip "not working while executing commands outside a function" + return + + spy ps + spy awk + spy head + + ./tests/functional/fixtures/doubles_script.sh + + assert_have_been_called_times 1 ps + assert_have_been_called_times 1 awk + assert_have_been_called_times 1 head +} + +function test_spy_commands_called_once_when_executing_a_sourced_function() { + skip "The spies can not spy inside the scope of the function" + return + + source ./tests/functional/fixtures/doubles_function.sh + spy ps + spy awk + spy head + + top_mem + + assert_have_been_called_times 1 ps + assert_have_been_called_times 1 awk + assert_have_been_called_times 1 head +} diff --git a/tests/functional/fixtures/doubles_function.sh b/tests/functional/fixtures/doubles_function.sh new file mode 100644 index 00000000..34ced26e --- /dev/null +++ b/tests/functional/fixtures/doubles_function.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +function top_mem() { + ps -eo cmd,%mem --sort=-%mem | awk '$2 >= 1.0 {print $0}' | head -n 3 +} diff --git a/tests/functional/fixtures/doubles_ps_output b/tests/functional/fixtures/doubles_ps_output new file mode 100644 index 00000000..d2e071eb --- /dev/null +++ b/tests/functional/fixtures/doubles_ps_output @@ -0,0 +1,8 @@ +CMD %MEM +firefox 2.0 +Xorg 1.6 +jetbrains-toolbox 1.3 +vitepress 1.0 +bashunit 0.1 +bash 0.0 +ps 0.0 diff --git a/tests/functional/fixtures/doubles_script.sh b/tests/functional/fixtures/doubles_script.sh new file mode 100644 index 00000000..224ee384 --- /dev/null +++ b/tests/functional/fixtures/doubles_script.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +ps -eo cmd,%mem --sort=-%mem | awk '$2 >= 1.0 {print $0}' | head -n 3 diff --git a/tests/functional/snapshots/doubles_test_sh_.test_mock_ps_when_executing_a_script.snapshot b/tests/functional/snapshots/doubles_test_sh_.test_mock_ps_when_executing_a_script.snapshot new file mode 100644 index 00000000..77519f53 --- /dev/null +++ b/tests/functional/snapshots/doubles_test_sh_.test_mock_ps_when_executing_a_script.snapshot @@ -0,0 +1,3 @@ +firefox 2.0 +Xorg 1.6 +jetbrains-toolbox 1.3 diff --git a/tests/functional/snapshots/doubles_test_sh_.test_mock_ps_when_executing_a_sourced_function.snapshot b/tests/functional/snapshots/doubles_test_sh_.test_mock_ps_when_executing_a_sourced_function.snapshot new file mode 100644 index 00000000..77519f53 --- /dev/null +++ b/tests/functional/snapshots/doubles_test_sh_.test_mock_ps_when_executing_a_sourced_function.snapshot @@ -0,0 +1,3 @@ +firefox 2.0 +Xorg 1.6 +jetbrains-toolbox 1.3