git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] tests: Test how well "git apply" copes with weird filenames

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jul 24, 2010, 01:11 UTC
Message-ID
<20100724011114.GC13670@burratino>
In-Reply-To
<20100724010618.GA13670@burratino>

The tests assume a reasonably well-behaved “diff -u” and “pr” to produce the (possibly whitespace-damaged) patches. On platforms without those commands, skip the tests.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 t/t4135-apply-weird-filenames.sh |  119 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 119 insertions(+), 0 deletions(-)
 create mode 100755 t/t4135-apply-weird-filenames.sh
diff --git a/t/t4135-apply-weird-filenames.sh b/t/t4135-apply-weird-filenames.sh
new file mode 100755
index 0000000..2dcb040
--- /dev/null
+++ b/t/t4135-apply-weird-filenames.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+
+test_description='git apply with weird postimage filenames'
+
+. ./test-lib.sh
+
+test_expect_success 'setup: empty commit' '
+	test_tick &&
+	git commit --allow-empty -m preimage &&
+	git tag preimage
+'
+
+test_expect_success 'setup: clean-up functions' '
+	reset_preimage() {
+		git checkout -f preimage^0 &&
+		git read-tree -u --reset HEAD &&
+		git update-index --refresh
+	} &&
+
+	reset_subdirs() {
+		rm -fr a b &&
+		mkdir a b
+	}
+'
+
+test_expect_success 'setup: test prerequisites' '
+	echo one >1 &&
+	echo one >2 &&
+	if diff -u 1 2
+	then
+		test_set_prereq UNIDIFF
+	fi &&
+
+	if diff -pruN 1 2
+	then
+		test_set_prereq FULLDIFF
+	fi &&
+
+	echo "tab ->  ." >expected &&
+	echo "tab ->	." >with-tab &&
+
+	pr -tT -e8 with-tab >actual &&
+	if test_cmp expected actual
+	then
+		test_set_prereq PR
+	fi
+'
+
+try_filename() {
+	desc=$1
+	postimage=$2
+	exp1=${3:-success}
+	exp2=${4:-success}
+	exp3=${5:-success}
+
+	test_expect_$exp1 "$desc, git-style file creation patch" "
+		reset_preimage &&
+		echo postimage >'$postimage' &&
+		git add -N '$postimage' &&
+		git diff HEAD >'git-$desc.diff' &&
+
+		git rm -f --cached '$postimage' &&
+		mv '$postimage' postimage.saved &&
+		git apply -v 'git-$desc.diff' &&
+
+		test_cmp postimage.saved '$postimage'
+	"
+
+	test_expect_$exp2 UNIDIFF "$desc, traditional patch" "
+		reset_preimage &&
+		echo preimage >'$postimage.orig' &&
+		echo postimage >'$postimage' &&
+		! diff -u '$postimage.orig' '$postimage' >'diff-$desc.diff' &&
+
+		mv '$postimage' postimage.saved &&
+		mv '$postimage.orig' '$postimage' &&
+		git apply -v 'diff-$desc.diff' &&
+
+		test_cmp postimage.saved '$postimage'
+	"
+
+	test_expect_$exp3 FULLDIFF "$desc, traditional file creation patch" "
+		reset_preimage &&
+		reset_subdirs &&
+		echo postimage >b/'$postimage' &&
+		! diff -pruN a b >'add-$desc.diff' &&
+
+		rm -f '$postimage' &&
+		mv b/'$postimage' postimage.saved &&
+		git apply -v 'add-$desc.diff' &&
+
+		test_cmp postimage.saved '$postimage'
+	"
+}
+
+try_filename 'plain'            'postimage.txt'
+try_filename 'with spaces'      'post image.txt' success failure failure
+try_filename 'with tab'         'post	image.txt' success failure failure
+try_filename 'with backslash'   'post\image.txt'
+try_filename 'with quote'       '"postimage".txt' success failure success
+
+if test_have_prereq FULLDIFF && test_have_prereq PR
+then
+	test_set_prereq FULLDIFFPR
+fi
+test_expect_success FULLDIFFPR 'whitespace-damaged traditional patch' '
+	reset_preimage &&
+	reset_subdirs &&
+	echo postimage >b/postimage.txt &&
+	! diff -pruN a b >diff-plain.txt &&
+	pr -tT -e8 diff-plain.txt >damaged.diff &&
+
+	mv postimage.txt postimage.saved &&
+	git apply -v damaged.diff &&
+
+	test_cmp postimage.saved postimage.txt
+'
+
+test_done
-- 
1.7.2.rc3
Previous: Jonathan NiederNext: Andreas Schwab
Message 3 of 6 in “apply: handle traditional patches with spaces in filename”
  1. 0/3 apply: handle traditional patches with spaces in filenameJonathan Nieder, Jul 24, 2010
  2. 1/3 apply: Split quoted filename handling into new functionJonathan Nieder, Jul 24, 2010
  3. 2/3 tests: Test how well "git apply" copes with weird filenamesJonathan Nieder, Jul 24, 2010
  4. Andreas SchwabJul 24, 2010
  5. Jonathan NiederJul 24, 2010
  6. 3/3 apply: Handle traditional patches with space in filenameJonathan Nieder, Jul 24, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.