Skip to content

Commit 335c576

Browse files
authored
Merge pull request #282 from TypedDevs/feat/support-clock-without-perl
Support displaying clock without perl (for non-macOS)
2 parents f85379a + 4f82903 commit 335c576

4 files changed

Lines changed: 71 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
- Fix echo does not break test execution results
66
- Add bashunit facade to enable custom assertions
77
- Document how to verify the `sha256sum` of the final executable
8-
- Enable display execution time on MacOS with `SHOW_EXECUTION_TIME`
8+
- Enable display execution time on macOS with `SHOW_EXECUTION_TIME`
9+
- Support for displaying the clock without `perl` (for non-macOS)
910
- Add `-l|--log-junit <log.xml>` option
1011
- Add `-r|--report-html <report.html>` option
1112

src/clock.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
#!/bin/bash
22

33
function clock::now() {
4-
perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000)'
4+
if perl --version > /dev/null 2>&1; then
5+
perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000)'
6+
elif [[ "$_OS" != "OSX" ]]; then
7+
date +%s%N
8+
else
9+
echo ""
10+
fi
511
}
612

713
_START_TIME=$(clock::now)
814

915
function clock::runtime_in_milliseconds() {
10-
echo $(( $(clock::now) - _START_TIME ))
16+
end_time=$(clock::now)
17+
if [[ -n $end_time ]]; then
18+
echo $(( end_time - _START_TIME ))
19+
else
20+
echo ""
21+
fi
1122
}

tests/unit/clock_test.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
__ORIGINAL_OS=""
4+
5+
function set_up_before_script() {
6+
__ORIGINAL_OS=$_OS
7+
}
8+
9+
function tear_down_after_script() {
10+
export _OS=$__ORIGINAL_OS
11+
}
12+
13+
function mock_non_existing_fn() {
14+
return 127;
15+
}
16+
17+
function test_now_with_perl() {
18+
mock perl echo "1720705883457"
19+
20+
assert_equals "1720705883457" "$(clock::now)"
21+
}
22+
23+
function test_now_on_linux_without_perl() {
24+
export _OS="Linux"
25+
mock perl mock_non_existing_fn
26+
mock date echo "1720705883457"
27+
28+
assert_equals "1720705883457" "$(clock::now)"
29+
}
30+
function test_now_on_windows_without_perl() {
31+
export _OS="Windows"
32+
mock perl mock_non_existing_fn
33+
mock date echo "1720705883457"
34+
35+
assert_equals "1720705883457" "$(clock::now)"
36+
}
37+
38+
function test_now_on_osx_without_perl() {
39+
export _OS="OSX"
40+
mock perl mock_non_existing_fn
41+
42+
assert_equals "" "$(clock::now)"
43+
}
44+
45+
function test_runtime_in_milliseconds_when_not_empty_time() {
46+
mock perl echo "1720705883457"
47+
48+
assert_not_empty "$(clock::runtime_in_milliseconds)"
49+
}
50+
51+
function test_runtime_in_milliseconds_when_empty_time() {
52+
export _OS="OSX"
53+
mock perl mock_non_existing_fn
54+
55+
assert_empty "$(clock::runtime_in_milliseconds)"
56+
}

tests/unit/console_results_test.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,6 @@ function test_total_asserts_is_the_sum_of_passed_skipped_incomplete_snapshot_and
266266
}
267267

268268
function test_render_execution_time() {
269-
if [[ $_OS == "OSX" ]]; then
270-
skip "Skipping in OSX"
271-
return
272-
fi
273-
274269
local render_result
275270
render_result=$(
276271
# shellcheck disable=SC2034
@@ -282,11 +277,6 @@ function test_render_execution_time() {
282277
}
283278

284279
function test_not_render_execution_time() {
285-
if [[ $_OS == "OSX" ]]; then
286-
skip "Skipping in OSX"
287-
return
288-
fi
289-
290280
local render_result
291281
render_result=$(
292282
# shellcheck disable=SC2034

0 commit comments

Comments
 (0)