Skip to content

Commit f66f9db

Browse files
t/testhelpers.sh: use Bash pattern matching
In commit 5e654f2 in PR git-lfs#565 a pair of test assertion functions were added to the forerunner of our current t/testhelpers.sh shell library. These assert_local_object() and refute_local_object() functions check for the presence or absence of a file in the object cache maintained by the Git LFS client in a local repository. To perform these checks, the functions capture the output of the "git lfs env" command and parse the contents of the LocalMediaDir line, which reports the full path to the Git LFS object cache location. To retrieve the path, the functions ignore the first 14 characters of the line, as that corresponds to the length of the LocalMediaDir field name (13 characters) plus one character in order to account for the equals sign which follows the field name. Later PRs have added three other assertion functions that follow the same design. The delete_local_object() function was added in commit 97434fe of PR git-lfs#742 to help test the "git lfs fetch" command's --prune option, the corrupt_local_object() function was added in commit 4b0f50e of PR git-lfs#2082 to help test the detection of corrupted local objects during push operations, and most recently, the assert_remote_object() function was added in commit 9bae8eb of PR git-lfs#5905 to improve our tests of the SSH object transfer protocol for Git LFS. All of these functions retrieve the object cache location by ignoring the first 14 characters from the LocalMediaDir line in the output of the "git lfs env" command. However, the refute_local_object() function contains a hint of an alternative approach to parsing this line's data. A local "regex" variable is defined in the refute_local_object() function, which matches the LocalMediaDir field name and equals sign and captures the subsequent object cache path value. Although this "regex" variable was included when the function was first introduced, it has never been used, and does not appear in any of the other similar functions. While reviewing PR git-lfs#5905, larsxschneider suggested an even simpler option than using a regular expression to extract the object cache path from the LocalMediaDir line. Rather than asking the Bash shell to start its parameter expansion at a fixed offset of 14 characters into the string, we can define a pattern which matches the leading LocalMediaDir field name and equals sign and specify that the shell should remove that portion of the string during parameter expansion. See also the discussion in this review comment from PR git-lfs#5905: git-lfs#5905 (comment) In addition to these changes, we can remove the definition of the "regex" variable from the refute_local_object() function, as it remains unused. Co-authored-by: Lars Schneider <[email protected]>
1 parent 3990c7a commit f66f9db

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

t/testhelpers.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ assert_local_object() {
6262
local oid="$1"
6363
local size="$2"
6464
local cfg=`git lfs env | grep LocalMediaDir`
65-
local f="${cfg:14}/${oid:0:2}/${oid:2:2}/$oid"
65+
local f="${cfg#LocalMediaDir=}/${oid:0:2}/${oid:2:2}/$oid"
6666
actualsize=$(wc -c <"$f" | tr -d '[[:space:]]')
6767
if [ "$size" != "$actualsize" ]; then
6868
exit 1
@@ -79,8 +79,7 @@ refute_local_object() {
7979
local oid="$1"
8080
local size="$2"
8181
local cfg=`git lfs env | grep LocalMediaDir`
82-
local regex="LocalMediaDir=(\S+)"
83-
local f="${cfg:14}/${oid:0:2}/${oid:2:2}/$oid"
82+
local f="${cfg#LocalMediaDir=}/${oid:0:2}/${oid:2:2}/$oid"
8483
if [ -e $f ]; then
8584
if [ -z "$size" ]; then
8685
exit 1
@@ -99,7 +98,7 @@ refute_local_object() {
9998
delete_local_object() {
10099
local oid="$1"
101100
local cfg=`git lfs env | grep LocalMediaDir`
102-
local f="${cfg:14}/${oid:0:2}/${oid:2:2}/$oid"
101+
local f="${cfg#LocalMediaDir=}/${oid:0:2}/${oid:2:2}/$oid"
103102
rm "$f"
104103
}
105104

@@ -108,7 +107,7 @@ delete_local_object() {
108107
corrupt_local_object() {
109108
local oid="$1"
110109
local cfg=`git lfs env | grep LocalMediaDir`
111-
local f="${cfg:14}/${oid:0:2}/${oid:2:2}/$oid"
110+
local f="${cfg#LocalMediaDir=}/${oid:0:2}/${oid:2:2}/$oid"
112111
cp /dev/null "$f"
113112
}
114113

@@ -186,7 +185,7 @@ assert_remote_object() {
186185

187186
pushd "$destination"
188187
local cfg="$(git lfs env | grep LocalMediaDir)"
189-
local f="${cfg:14}/${oid:0:2}/${oid:2:2}/$oid"
188+
local f="${cfg#LocalMediaDir=}/${oid:0:2}/${oid:2:2}/$oid"
190189
actualsize="$(wc -c <"$f" | tr -d '[[:space:]]')"
191190
[ "$size" -eq "$actualsize" ]
192191
popd

0 commit comments

Comments
 (0)