Skip to content

Commit b2bd5c2

Browse files
authored
Merge pull request #382 from TypedDevs/spike-mock-not-working
chore: improve the test test_mock_ps_when_executing_a_script
2 parents 7e15f54 + bd7197d commit b2bd5c2

5 files changed

Lines changed: 49 additions & 2 deletions

src/test_doubles.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ function assert_have_been_called_times() {
127127
times=$(cat "${!file_var}")
128128
fi
129129
local label="${3:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
130-
131130
if [[ -z "${!actual-}" && $expected -ne 0 || $times -ne $expected ]]; then
132131
state::add_assertions_failed
133132
console_results::print_failed_test "${label}" "${command}" \

tests/functional/doubles_test.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
function test_mock_ps_when_executing_a_script() {
4+
mock ps cat ./tests/functional/fixtures/doubles_ps_output
5+
6+
assert_match_snapshot "$(source ./tests/functional/fixtures/doubles_script.sh)"
7+
}
8+
9+
function test_mock_ps_when_executing_a_sourced_function() {
10+
source ./tests/functional/fixtures/doubles_function.sh
11+
mock ps cat ./tests/functional/fixtures/doubles_ps_output
12+
13+
assert_match_snapshot "$(top_mem)"
14+
}
15+
16+
function test_spy_commands_called_once_when_executing_a_script() {
17+
skip "not working while executing commands outside a function" && return
18+
19+
spy ps
20+
spy awk
21+
spy head
22+
23+
./tests/functional/fixtures/doubles_script.sh
24+
25+
assert_have_been_called_times 1 ps
26+
assert_have_been_called_times 1 awk
27+
assert_have_been_called_times 1 head
28+
}
29+
30+
function test_spy_commands_called_once_when_executing_a_sourced_function() {
31+
32+
source ./tests/functional/fixtures/doubles_function.sh
33+
spy ps
34+
spy awk
35+
spy head
36+
37+
top_mem
38+
39+
assert_have_been_called_times 1 ps
40+
assert_have_been_called_times 1 awk
41+
assert_have_been_called_times 1 head
42+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
22

3-
ps -eo cmd,%mem --sort=-%mem | awk '$2 >= 1.0 {print $0}' | head -n 3
3+
ps | awk '$2 >= 1.0 {print $0}' | head -n 3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
firefox 2.0
2+
Xorg 1.6
3+
jetbrains-toolbox 1.3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
firefox 2.0
2+
Xorg 1.6
3+
jetbrains-toolbox 1.3

0 commit comments

Comments
 (0)