t6300: use function to test trailer options

Add a function to test trailer options. This will make tests look cleaner, as well as will make it easier to add new tests for trailers in the future. Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Hariom Verma <hariom18599@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Hariom Verma committed Feb 13, 2021 at 01:52 UTC 727331dce16d88735a5e3a8050b1520d03c6e77a
1 file changed +47 -43
t/t6300-for-each-ref.sh
+47 -43
@@ -814,53 +814,57 @@ test_expect_success 'set up trailers for next test' '
814 EOF
815 '
816
817 -test_expect_success '%(trailers:unfold) unfolds trailers' '
818 - {
819 - unfold <trailers
820 - echo
821 - } >expect &&
822 - git for-each-ref --format="%(trailers:unfold)" refs/heads/main >actual &&
823 - test_cmp expect actual &&
824 - git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/main >actual &&
825 - test_cmp expect actual
826 -'
817 +test_trailer_option () {
818 + title=$1 option=$2
819 + cat >expect
820 + test_expect_success "$title" '
821 + git for-each-ref --format="%($option)" refs/heads/main >actual &&
822 + test_cmp expect actual &&
823 + git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
824 + test_cmp expect actual
825 + '
826 +}
827
828 -test_expect_success '%(trailers:only) shows only "key: value" trailers' '
829 - {
830 - grep -v patch.description <trailers &&
831 - echo
832 - } >expect &&
833 - git for-each-ref --format="%(trailers:only)" refs/heads/main >actual &&
834 - test_cmp expect actual &&
835 - git for-each-ref --format="%(contents:trailers:only)" refs/heads/main >actual &&
836 - test_cmp expect actual
837 -'
828 +test_trailer_option '%(trailers:unfold) unfolds trailers' \
829 + 'trailers:unfold' <<-EOF
830 + $(unfold <trailers)
831
839 -test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
840 - {
841 - grep -v patch.description <trailers | unfold &&
842 - echo
843 - } >expect &&
844 - git for-each-ref --format="%(trailers:only,unfold)" refs/heads/main >actual &&
845 - test_cmp expect actual &&
846 - git for-each-ref --format="%(trailers:unfold,only)" refs/heads/main >actual &&
847 - test_cmp actual actual &&
848 - git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/main >actual &&
849 - test_cmp expect actual &&
850 - git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/main >actual &&
851 - test_cmp actual actual
852 -'
853 -
854 -test_expect_success '%(trailers) rejects unknown trailers arguments' '
855 - # error message cannot be checked under i18n
856 - cat >expect <<-EOF &&
832 + EOF
833 +
834 +test_trailer_option '%(trailers:only) shows only "key: value" trailers' \
835 + 'trailers:only' <<-EOF
836 + $(grep -v patch.description <trailers)
837 +
838 + EOF
839 +
840 +test_trailer_option '%(trailers:only) and %(trailers:unfold) work together' \
841 + 'trailers:only,unfold' <<-EOF
842 + $(grep -v patch.description <trailers | unfold)
843 +
844 + EOF
845 +
846 +test_trailer_option '%(trailers:unfold) and %(trailers:only) work together' \
847 + 'trailers:unfold,only' <<-EOF
848 + $(grep -v patch.description <trailers | unfold)
849 +
850 + EOF
851 +
852 +test_failing_trailer_option () {
853 + title=$1 option=$2
854 + cat >expect
855 + test_expect_success "$title" '
856 + # error message cannot be checked under i18n
857 + test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
858 + test_i18ncmp expect actual &&
859 + test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
860 + test_i18ncmp expect actual
861 + '
862 +}
863 +
864 +test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \
865 + 'trailers:unsupported' <<-\EOF
866 fatal: unknown %(trailers) argument: unsupported
867 EOF
859 - test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
860 - test_i18ncmp expect actual &&
861 - test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
862 - test_i18ncmp expect actual
863 -'
868
869 test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
870 cat >expect <<-EOF &&