summaryrefslogtreecommitdiff
path: root/t/t4124-apply-ws-rule.sh
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2007-11-24 04:14:20 (GMT)
committerJunio C Hamano <gitster@pobox.com>2007-11-25 00:47:08 (GMT)
commitd5a4164140c52a2d267d90e2413d1fe4a326a386 (patch)
tree5d122def3266d8e14fdffbc629351000030f1609 /t/t4124-apply-ws-rule.sh
parent81bf96bb2ecb8c869cc365a7fee2c4d2e937a43c (diff)
downloadgit-d5a4164140c52a2d267d90e2413d1fe4a326a386.zip
git-d5a4164140c52a2d267d90e2413d1fe4a326a386.tar.gz
git-d5a4164140c52a2d267d90e2413d1fe4a326a386.tar.bz2
builtin-apply: teach whitespace_rules
We earlier introduced core.whitespace to allow users to tweak the definition of what the "whitespace errors" are, for the purpose of diff output highlighting. This teaches the same to git-apply, so that the command can both detect (when --whitespace=warn option is given) and fix (when --whitespace=fix option is given) as configured. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 't/t4124-apply-ws-rule.sh')
-rwxr-xr-xt/t4124-apply-ws-rule.sh133
1 files changed, 133 insertions, 0 deletions
diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
new file mode 100755
index 0000000..f53ac46
--- /dev/null
+++ b/t/t4124-apply-ws-rule.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+test_description='core.whitespace rules and git-apply'
+
+. ./test-lib.sh
+
+prepare_test_file () {
+
+ # A line that has character X is touched iff RULE is in effect:
+ # X RULE
+ # ! trailing-space
+ # @ space-before-tab
+ # # indent-with-non-tab
+ sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF
+ An_SP in an ordinary line>and a HT.
+ >A HT.
+ _>A SP and a HT (@).
+ _>_A SP, a HT and a SP (@).
+ _______Seven SP.
+ ________Eight SP (#).
+ _______>Seven SP and a HT (@).
+ ________>Eight SP and a HT (@#).
+ _______>_Seven SP, a HT and a SP (@).
+ ________>_Eight SP, a HT and a SP (@#).
+ _______________Fifteen SP (#).
+ _______________>Fifteen SP and a HT (@#).
+ ________________Sixteen SP (#).
+ ________________>Sixteen SP and a HT (@#).
+ _____a__Five SP, a non WS, two SP.
+ A line with a (!) trailing SP_
+ A line with a (!) trailing HT>
+ EOF
+}
+
+apply_patch () {
+ >target &&
+ sed -e "s|\([ab]\)/file|\1/target|" <patch |
+ git apply "$@"
+}
+
+test_fix () {
+
+ # fix should not barf
+ apply_patch --whitespace=fix || return 1
+
+ # find touched lines
+ diff file target | sed -n -e "s/^> //p" >fixed
+
+ # the changed lines are all expeced to change
+ fixed_cnt=$(wc -l <fixed)
+ case "$1" in
+ '') expect_cnt=$fixed_cnt ;;
+ ?*) expect_cnt=$(grep "[$1]" <fixed | wc -l) ;;
+ esac
+ test $fixed_cnt -eq $expect_cnt || return 1
+
+ # and we are not missing anything
+ case "$1" in
+ '') expect_cnt=0 ;;
+ ?*) expect_cnt=$(grep "[$1]" <file | wc -l) ;;
+ esac
+ test $fixed_cnt -eq $expect_cnt || return 1
+
+ # Get the patch actually applied
+ git diff-files -p target >fixed-patch
+ test -s fixed-patch && return 0
+
+ # Make sure it is complaint-free
+ >target
+ git apply --whitespace=error-all <fixed-patch
+
+}
+
+test_expect_success setup '
+
+ >file &&
+ git add file &&
+ prepare_test_file >file &&
+ git diff-files -p >patch &&
+ >target &&
+ git add target
+
+'
+
+test_expect_success 'whitespace=nowarn, default rule' '
+
+ apply_patch --whitespace=nowarn &&
+ diff file target
+
+'
+
+test_expect_success 'whitespace=warn, default rule' '
+
+ apply_patch --whitespace=warn &&
+ diff file target
+
+'
+
+test_expect_success 'whitespace=error-all, default rule' '
+
+ apply_patch --whitespace=error-all && return 1
+ test -s target && return 1
+ : happy
+
+'
+
+test_expect_success 'whitespace=error-all, no rule' '
+
+ git config core.whitespace -trailing,-space-before,-indent &&
+ apply_patch --whitespace=error-all &&
+ diff file target
+
+'
+
+for t in - ''
+do
+ case "$t" in '') tt='!' ;; *) tt= ;; esac
+ for s in - ''
+ do
+ case "$s" in '') ts='@' ;; *) ts= ;; esac
+ for i in - ''
+ do
+ case "$i" in '') ti='#' ;; *) ti= ;; esac
+ rule=${t}trailing,${s}space,${i}indent &&
+ test_expect_success "rule=$rule" '
+ git config core.whitespace "$rule" &&
+ test_fix "$tt$ts$ti"
+ '
+ done
+ done
+done
+
+test_done