Skip to content

Commit dab526a

Browse files
authored
Merge pull request #449 from TypedDevs/fix/srt-rpad
Fix negative widths with rpad
2 parents 0657f74 + be7b708 commit dab526a

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fix prevents writing in src dir during tests
6+
- Fix negative widths with rpad
67

78
## [0.22.0](https://github.com/TypedDevs/bashunit/compare/0.21.0...0.22.0) - 2025-07-20
89

src/assert_snapshot.sh

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,21 @@ function snapshot::match_with_placeholder() {
3434
fi
3535
}
3636

37+
# shellcheck disable=SC2155
3738
function assert_match_snapshot() {
3839
local actual
3940
actual=$(echo -n "$1" | tr -d '\r')
40-
local directory
41-
directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
42-
local test_file
43-
test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")"
44-
local snapshot_name
45-
snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot"
46-
local snapshot_file
47-
snapshot_file="${directory}/${test_file}.${snapshot_name}"
41+
local snapshot_file="${2-}"
42+
43+
if [[ -z "$snapshot_file" ]]; then
44+
local directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
45+
local test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")"
46+
local snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot"
47+
snapshot_file="${directory}/${test_file}.${snapshot_name}"
48+
fi
4849

4950
if [[ ! -f "$snapshot_file" ]]; then
50-
mkdir -p "$directory"
51+
mkdir -p "$(dirname "$snapshot_file")"
5152
echo "$actual" > "$snapshot_file"
5253

5354
state::add_assertions_snapshot
@@ -70,21 +71,21 @@ function assert_match_snapshot() {
7071
state::add_assertions_passed
7172
}
7273

74+
# shellcheck disable=SC2155
7375
function assert_match_snapshot_ignore_colors() {
7476
local actual
7577
actual=$(echo -n "$1" | sed -r 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')
7678

77-
local directory
78-
directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
79-
local test_file
80-
test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")"
81-
local snapshot_name
82-
snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot"
83-
local snapshot_file
84-
snapshot_file="${directory}/${test_file}.${snapshot_name}"
79+
local snapshot_file="${2-}"
80+
if [[ -z "$snapshot_file" ]]; then
81+
local directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
82+
local test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")"
83+
local snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot"
84+
snapshot_file="${directory}/${test_file}.${snapshot_name}"
85+
fi
8586

8687
if [[ ! -f "$snapshot_file" ]]; then
87-
mkdir -p "$directory"
88+
mkdir -p "$(dirname "$snapshot_file")"
8889
echo "$actual" > "$snapshot_file"
8990

9091
state::add_assertions_snapshot

src/str.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ function str::rpad() {
66
local width_padding="${3:-$TERMINAL_WIDTH}"
77
# Subtract 1 more to account for the extra space
88
local padding=$((width_padding - ${#right_word} - 1))
9+
if (( padding < 0 )); then
10+
padding=0
11+
fi
912

1013
# Remove ANSI escape sequences (non-visible characters) for length calculation
1114
# shellcheck disable=SC2155
@@ -14,7 +17,7 @@ function str::rpad() {
1417
local is_truncated=false
1518
# If the visible left text exceeds the padding, truncate it and add "..."
1619
if [[ ${#clean_left_text} -gt $padding ]]; then
17-
local truncation_length=$((padding - 3)) # Subtract 3 for "..."
20+
local truncation_length=$((padding < 3 ? 0 : padding - 3))
1821
clean_left_text="${clean_left_text:0:$truncation_length}"
1922
is_truncated=true
2023
fi

tests/unit/str_test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@ function test_rpad_custom_width_padding_text_too_long_and_special_chars() {
5050
"$(printf "%sok: %svery long... 100" "$_COLOR_PASSED" "$_COLOR_DEFAULT")" \
5151
"$actual"
5252
}
53+
54+
function test_rpad_width_smaller_than_right_word() {
55+
local actual=$(str::rpad "foo" "verylongword" 5)
56+
57+
assert_same "... verylongword" "$actual"
58+
}

0 commit comments

Comments
 (0)