git-lfs/t/t-attributes.sh
Chris Darroch 3c46a4641c t/t-attributes.sh: test check top-level macro rule
In commit f4b8938fd60dfb705d83a62f153acd59fe067a51 of PR #3391 the
t/t-attributes.sh file was added with an initial "macros" test, and
part of that test confirms that macro attribute definitions are only
processed when they appear in the top-level .giattributes file in
a repository.

The test does confirm this in that it creates both an "lfs2" macro
attribute definition and assignment of that attribute name to a
file pattern in a .gitattributes file in a subdirectory, and then
validates that a file matching that pattern in the subdirectory is
not converted into an LFS object.

The test also includes a second check of this logic in which it confirms
that a "git lfs track" command for the file pattern in the subdirectory
succeeds, i.e., that it does not fail because the file pattern was already
assigned the normal "filter=lfs" attribute by the "lfs2" macro attribute.

However, this particular check will always succeed, even if macro
attribute definitions like the "lfs2" one are incorrectly accepted from
.gitattributes files other than the top-level one.  This is because the
"git lfs track" command is run in the top-level directory and only sets
a pattern that includes the subdirectory in its path (i.e., "dir/*.bin").
This will succeed regardless of whether the "*.bin" pattern is assigned
to LFS attributes in the dir/.gitattributes file.

We therefore make this second check more sensitive to potential future
regressions by running the "git lfs track" command in the subdirectory.
Now if the macro definition in the .gitattributes file in that directory
is (incorrectly) read as a valid definition, and the check which tests that
the file in the subdirectory has not been converted into an LFS object is
skipped, then this second check fails as expected.
2022-11-06 22:33:19 -08:00

88 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
. "$(dirname "$0")/testlib.sh"
begin_test "macros"
(
set -e
reponame="$(basename "$0" ".sh")"
clone_repo "$reponame" repo
mkdir dir
printf '[attr]lfs filter=lfs diff=lfs merge=lfs -text\n*.dat lfs\n' \
> .gitattributes
printf '[attr]lfs2 filter=lfs diff=lfs merge=lfs -text\n*.bin lfs2\n' \
> dir/.gitattributes
git add .gitattributes dir
git commit -m 'initial import'
contents="some data"
printf "$contents" > foo.dat
git add *.dat
git commit -m 'foo.dat'
assert_local_object "$(calc_oid "$contents")" 9
contents2="other data"
printf "$contents2" > dir/foo.bin
git add dir
git commit -m 'foo.bin'
refute_local_object "$(calc_oid "$contents2")"
git lfs track '*.dat' 2>&1 | tee track.log
grep '"*.dat" already supported' track.log
cd dir
git lfs track '*.bin' 2>&1 | tee track.log
! grep '"*.bin" already supported' track.log
)
end_test
begin_test "macros with HOME"
(
set -e
reponame="$(basename "$0" ".sh")-home"
clone_repo "$reponame" repo-home
mkdir -p "$HOME/.config/git"
printf '[attr]lfs filter=lfs diff=lfs merge=lfs -text\n*.dat lfs\n' \
> "$HOME/.config/git/attributes"
contents="some data"
printf "$contents" > foo.dat
git add *.dat
git commit -m 'foo.dat'
assert_local_object "$(calc_oid "$contents")" 9
git lfs track 2>&1 | tee track.log
grep '*.dat' track.log
)
end_test
begin_test "macros with HOME split"
(
set -e
reponame="$(basename "$0" ".sh")-home-split"
clone_repo "$reponame" repo-home-split
mkdir -p "$HOME/.config/git"
printf '[attr]lfs filter=lfs diff=lfs merge=lfs -text\n' \
> "$HOME/.config/git/attributes"
printf '*.dat lfs\n' > .gitattributes
git add .gitattributes
git commit -m 'initial import'
contents="some data"
printf "$contents" > foo.dat
git add *.dat
git commit -m 'foo.dat'
assert_local_object "$(calc_oid "$contents")" 9
git lfs track '*.dat' 2>&1 | tee track.log
grep '"*.dat" already supported' track.log
)
end_test