summaryrefslogtreecommitdiff
path: root/regress/infra-unittests/patch.sh
blob: 096e157c951ce55bdd64712ab8deae5e1ebdf104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /bin/sh
# $NetBSD: patch.sh,v 1.1 2020/04/26 12:46:01 rillig Exp $
#
# Test cases for patching files using patch(1).
#

set -eu

. "./test.subr"

if test_case_begin "patch matches exactly"; then
	cd "$tmpdir"

	create_file_lines "to-be-patched" \
		"1" "2" "3" "4" "5" "6" "7"

	create_file "4-four.patch" <<EOF
--- before
+++ to-be-patched
@@ -1,7 +1,7 @@
 1
 2
 3
-4
+four
 5
 6
 7
EOF

	patch -s -p0 < "4-four.patch"

	assert_that "to-be-patched" --file-is-lines \
		"1" "2" "3" "four" "5" "6" "7"

	test_case_end
fi


if test_case_begin "line numbers in patch are way out of bounds"; then
	cd "$tmpdir"

	# BSD patch:
	#
	# If the line numbers in the patch are beyond the size of the file,
	# /usr/bin/patch logs an information that "it is ignored", but it does
	# not say exactly what "it" is.
	#
	# This may look frightening, but since the patch is applied anyway,
	# "it" probably refers to the line numbers only, not to the hunk as
	# a whole.
	#
	# https://github.com/openbsd/src/commit/328a6cea628e2916 (2003-07-16)

	create_file_lines "to-be-patched" \
		"1" "2" "3" "4" "5" "6" "7"

	create_file "4-four.patch" <<EOF
--- before
+++ to-be-patched
@@ -1000,7 +1000,7 @@
 1
 2
 3
-4
+four
 5
 6
 7
EOF

	patch -s -p0 < "4-four.patch" 2> "stderr" \
	&& exitcode=0 || exitcode=$?

	assert_that "$exitcode" --equals "0"
	assert_that "stderr" --file-is-lines \
		"No such line 999 in input file, ignoring"
	assert_that "to-be-patched" --file-is-lines \
		"1" "2" "3" "four" "5" "6" "7"

	test_case_end
fi