ls-files: update test style

Update test style in t/t30[*].sh for uniformity, that's to
keep test title the same line with helper function itself,
and fix some indentions.

Add a new section "recommended style" in t/README to
encourage people to use more modern style in test.

Signed-off-by: Li Linchao <lilinchao@oschina.cn>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Li Linchao 2022-07-03 15:49:09 +00:00 committed by Junio C Hamano
parent e4a4b31577
commit 18337d406f
5 changed files with 133 additions and 72 deletions

View File

@ -547,6 +547,61 @@ This test harness library does the following things:
consistently when command line arguments --verbose (or -v), consistently when command line arguments --verbose (or -v),
--debug (or -d), and --immediate (or -i) is given. --debug (or -d), and --immediate (or -i) is given.
Recommended style
-----------------
Here are some recommented styles when writing test case.
- Keep test title the same line with test helper function itself.
Take test_expect_success helper for example, write it like:
test_expect_success 'test title' '
... test body ...
'
Instead of:
test_expect_success \
'test title' \
'... test body ...'
- End the line with a single quote.
- Indent the body of here-document, and use "<<-" instead of "<<"
to strip leading TABs used for indentation:
test_expect_success 'test something' '
cat >expect <<-\EOF &&
one
two
three
EOF
test_something > actual &&
test_cmp expect actual
'
Instead of:
test_expect_success 'test something' '
cat >expect <<\EOF &&
one
two
three
EOF
test_something > actual &&
test_cmp expect actual
'
- Quote or escape the EOF delimiter that begins a here-document if
there is no parameter and other expansion in it, to signal readers
that they can skim it more casually:
cmd <<-\EOF
literal here-document text without any expansion
EOF
Do's & don'ts Do's & don'ts
------------- -------------

View File

@ -67,26 +67,26 @@ echo '!*.2
allignores='.gitignore one/.gitignore one/two/.gitignore' allignores='.gitignore one/.gitignore one/two/.gitignore'
test_expect_success \ test_expect_success 'git ls-files --others with various exclude options.' '
'git ls-files --others with various exclude options.' \ git ls-files --others \
'git ls-files --others \
--exclude=\*.6 \ --exclude=\*.6 \
--exclude-per-directory=.gitignore \ --exclude-per-directory=.gitignore \
--exclude-from=.git/ignore \ --exclude-from=.git/ignore \
>output && >output &&
test_cmp expect output' test_cmp expect output
'
# Test \r\n (MSDOS-like systems) # Test \r\n (MSDOS-like systems)
printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore
test_expect_success \ test_expect_success 'git ls-files --others with \r\n line endings.' '
'git ls-files --others with \r\n line endings.' \ git ls-files --others \
'git ls-files --others \
--exclude=\*.6 \ --exclude=\*.6 \
--exclude-per-directory=.gitignore \ --exclude-per-directory=.gitignore \
--exclude-from=.git/ignore \ --exclude-from=.git/ignore \
>output && >output &&
test_cmp expect output' test_cmp expect output
'
test_expect_success 'setup skip-worktree gitignore' ' test_expect_success 'setup skip-worktree gitignore' '
git add $allignores && git add $allignores &&
@ -94,14 +94,14 @@ test_expect_success 'setup skip-worktree gitignore' '
rm $allignores rm $allignores
' '
test_expect_success \ test_expect_success 'git ls-files --others with various exclude options.' '
'git ls-files --others with various exclude options.' \ git ls-files --others \
'git ls-files --others \
--exclude=\*.6 \ --exclude=\*.6 \
--exclude-per-directory=.gitignore \ --exclude-per-directory=.gitignore \
--exclude-from=.git/ignore \ --exclude-from=.git/ignore \
>output && >output &&
test_cmp expect output' test_cmp expect output
'
test_expect_success !SANITIZE_LEAK 'restore gitignore' ' test_expect_success !SANITIZE_LEAK 'restore gitignore' '
git checkout --ignore-skip-worktree-bits $allignores && git checkout --ignore-skip-worktree-bits $allignores &&
@ -283,12 +283,12 @@ test_expect_success 'pattern matches prefix completely' '
' '
test_expect_success 'ls-files with "**" patterns' ' test_expect_success 'ls-files with "**" patterns' '
cat <<\EOF >expect && cat <<-\EOF >expect &&
a.1 a.1
one/a.1 one/a.1
one/two/a.1 one/two/a.1
three/a.1 three/a.1
EOF EOF
git ls-files -o -i --exclude "**/a.1" >actual && git ls-files -o -i --exclude "**/a.1" >actual &&
test_cmp expect actual test_cmp expect actual
' '

View File

@ -16,56 +16,62 @@ filesystem.
TEST_PASSES_SANITIZE_LEAK=true TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh . ./test-lib.sh
test_expect_success \ test_expect_success 'setup' '
setup \ echo frotz >path0 &&
'echo frotz >path0 &&
echo frotz >./-foo && echo frotz >./-foo &&
echo frotz >./--' echo frotz >./--
test_expect_success \
'git ls-files without path restriction.' \
'git ls-files --others >output &&
test_cmp output - <<EOF
--
-foo
output
path0
EOF
' '
test_expect_success \ test_expect_success 'git ls-files without path restriction.' '
'git ls-files with path restriction.' \ test_when_finished "rm -f expect" &&
'git ls-files --others path0 >output && git ls-files --others >output &&
test_cmp output - <<EOF cat >expect <<-\EOF &&
path0 --
EOF -foo
output
path0
EOF
test_cmp output expect
' '
test_expect_success \ test_expect_success 'git ls-files with path restriction.' '
'git ls-files with path restriction with --.' \ test_when_finished "rm -f expect" &&
'git ls-files --others -- path0 >output && git ls-files --others path0 >output &&
test_cmp output - <<EOF cat >expect <<-\EOF &&
path0 path0
EOF EOF
test_cmp output expect
' '
test_expect_success \ test_expect_success 'git ls-files with path restriction with --.' '
'git ls-files with path restriction with -- --.' \ test_when_finished "rm -f expect" &&
'git ls-files --others -- -- >output && git ls-files --others -- path0 >output &&
test_cmp output - <<EOF cat >expect <<-\EOF &&
-- path0
EOF EOF
test_cmp output expect
' '
test_expect_success \ test_expect_success 'git ls-files with path restriction with -- --.' '
'git ls-files with no path restriction.' \ test_when_finished "rm -f expect" &&
'git ls-files --others -- >output && git ls-files --others -- -- >output &&
test_cmp output - <<EOF cat >expect <<-\EOF &&
-- --
-foo EOF
output test_cmp output expect
path0 '
EOF
test_expect_success 'git ls-files with no path restriction.' '
test_when_finished "rm -f expect" &&
git ls-files --others -- >output &&
cat >expect <<-\EOF &&
--
-foo
output
path0
EOF
test_cmp output expect
' '
test_done test_done

View File

@ -19,12 +19,12 @@ test_expect_success 'setup' '
git commit -m "add foo bar" git commit -m "add foo bar"
' '
test_expect_success \ test_expect_success 'git ls-files --error-unmatch should fail with unmatched path.' '
'git ls-files --error-unmatch should fail with unmatched path.' \ test_must_fail git ls-files --error-unmatch foo bar-does-not-match
'test_must_fail git ls-files --error-unmatch foo bar-does-not-match' '
test_expect_success \ test_expect_success 'git ls-files --error-unmatch should succeed with matched paths.' '
'git ls-files --error-unmatch should succeed with matched paths.' \ git ls-files --error-unmatch foo bar
'git ls-files --error-unmatch foo bar' '
test_done test_done

View File

@ -10,7 +10,7 @@ a scenario known to trigger a crash with some versions of git.
' '
. ./test-lib.sh . ./test-lib.sh
test_expect_success setup ' test_expect_success 'setup' '
# The bug we are exercising requires a fair number of entries # The bug we are exercising requires a fair number of entries
# in a sub-directory so that add_index_entry will trigger a # in a sub-directory so that add_index_entry will trigger a
@ -62,9 +62,9 @@ test_expect_success 'git ls-files --with-tree should succeed from subdir' '
) )
' '
test_expect_success \ test_expect_success 'git ls-files --with-tree should add entries from named tree.' '
'git ls-files --with-tree should add entries from named tree.' \ test_cmp expected output
'test_cmp expected output' '
test_expect_success 'no duplicates in --with-tree output' ' test_expect_success 'no duplicates in --with-tree output' '
git ls-files --with-tree=HEAD >actual && git ls-files --with-tree=HEAD >actual &&