Skip to content

Commit 829c98e

Browse files
authored
Merge pull request #129 from mbland/strings
Replace @go.remove_common_path_prefix with @go.common_parent_path
2 parents dab84db + 2f9935e commit 829c98e

File tree

4 files changed

+83
-72
lines changed

4 files changed

+83
-72
lines changed

lib/strings

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
. "$_GO_USE_MODULES" 'validation'
2323

24+
# DO NOT EDIT: Number of stack trace levels to skip when validation fails.
25+
export __GO_STRINGS_VALIDATION_SKIP_CALLERS=2
26+
2427
# Splits fields from a delimited string into an array defined by the caller
2528
#
2629
# While `IFS= read -ra array_name <<<"$value"` is idiomatic, this function
@@ -72,7 +75,8 @@
7275
# var_name: Name of caller's variable to which to assign the common prefix
7376
# ...: Strings to examine to determine the common prefix
7477
@go.common_prefix() {
75-
@go.validate_identifier_or_die 'Result variable name' "$1"
78+
@go.validate_identifier_or_die 'Result variable name' "$1" \
79+
"${__GO_STRINGS_VALIDATION_SKIP_CALLERS}"
7680
local _gcp_prefix="$2"
7781
local _gcp_prefix_len="${#_gcp_prefix}"
7882
local _gcp_item
@@ -93,24 +97,20 @@
9397
printf -v "$1" -- '%s' "$_gcp_prefix"
9498
}
9599

96-
# Removes the common path prefix from a set of file paths
100+
# Determines the common parent directory path from a set of file paths
97101
#
98102
# Arguments:
99-
# array_name: Name of caller's array into which to store the updated paths
100-
# ...: File paths from which to remove the common path prefix
101-
@go.remove_common_path_prefix() {
102-
@go.validate_identifier_or_die 'Result variable name' "$1"
103-
local _grcpp_prefix
104-
local _grcpp_paths=("${@:2}")
105-
106-
@go.common_prefix '_grcpp_prefix' "${_grcpp_paths[@]}"
103+
# var_name: Name of caller's variable to which to assign the parent path
104+
# ...: File paths to examine to determine the common parent directory
105+
@go.common_parent_path() {
106+
((++__GO_STRINGS_VALIDATION_SKIP_CALLERS))
107+
@go.common_prefix "$1" "${@:2}"
108+
((--__GO_STRINGS_VALIDATION_SKIP_CALLERS))
107109

108-
if [[ "$_grcpp_prefix" =~ / ]]; then
109-
_grcpp_prefix="${_grcpp_prefix%/*}/"
110+
if [[ "${!1}" =~ / ]]; then
111+
printf -v "$1" -- '%s' "${!1%/*}/"
110112
else
111-
_grcpp_prefix=''
113+
# Bash 3.2 won't perform the assignment if the format is the empty string.
114+
printf -v "$1" '%s' ''
112115
fi
113-
114-
local IFS=$'\x1f'
115-
read -ra "$1" <<<"${_grcpp_paths[*]#$_grcpp_prefix}"
116116
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#! /usr/bin/env bats
2+
3+
load ../environment
4+
load helpers
5+
6+
setup() {
7+
test_filter
8+
create_strings_test_script 'declare parent' \
9+
'@go.common_parent_path "parent" "$@"' \
10+
'printf -- "%s\n" "$parent"'
11+
}
12+
13+
teardown() {
14+
@go.remove_test_go_rootdir
15+
}
16+
17+
@test "$SUITE: error if result variable name not a valid identifier" {
18+
create_strings_test_script '@go.common_parent_path "invalid;" "$@"'
19+
run "$TEST_GO_SCRIPT"
20+
21+
local err_msg='^Result variable name "invalid;" for @go.common_parent_path '
22+
err_msg+='contains invalid identifier characters at:$'
23+
24+
assert_lines_match "$err_msg" \
25+
"^ $TEST_GO_SCRIPT:[0-9] main$"
26+
}
27+
28+
@test "$SUITE: empty argument list produces empty string" {
29+
run "$TEST_GO_SCRIPT"
30+
assert_success
31+
}
32+
33+
@test "$SUITE: empty string produces empty string" {
34+
run "$TEST_GO_SCRIPT" ''
35+
assert_success
36+
}
37+
38+
@test "$SUITE: single string returns empty string" {
39+
run "$TEST_GO_SCRIPT" 'foo/bar/baz'
40+
assert_success ''
41+
}
42+
43+
@test "$SUITE: multiple paths with no common prefix returns empty string" {
44+
run "$TEST_GO_SCRIPT" 'foobar/baz' 'foobaz/quux' 'fooquux/xyzzy'
45+
assert_success ''
46+
}
47+
48+
@test "$SUITE: multiple absolute paths with only root dir in common" {
49+
run "$TEST_GO_SCRIPT" '/foobar/baz' '/foobaz/quux' '/fooquux/xyzzy'
50+
assert_success '/'
51+
}
52+
53+
@test "$SUITE: multiple paths with common parent path" {
54+
run "$TEST_GO_SCRIPT" 'foo/bar/baz' 'foo/bar/quux/xyzzy' 'foo/baz/plugh'
55+
assert_success 'foo/'
56+
}

tests/strings/common-prefix.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ teardown() {
1414
@go.remove_test_go_rootdir
1515
}
1616

17+
@test "$SUITE: error if result variable name not a valid identifier" {
18+
create_strings_test_script '@go.common_prefix "invalid;" "$@"'
19+
run "$TEST_GO_SCRIPT"
20+
21+
local err_msg='^Result variable name "invalid;" for @go.common_prefix '
22+
err_msg+='contains invalid identifier characters at:$'
23+
24+
assert_lines_match "$err_msg" \
25+
"^ $TEST_GO_SCRIPT:[0-9] main$"
26+
}
27+
1728
@test "$SUITE: empty argument list produces empty string" {
1829
run "$TEST_GO_SCRIPT"
1930
assert_success

tests/strings/remove-common-path-prefix.bats

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)