Home | History | Annotate | Line # | Download | only in patch
t_patch.sh revision 1.2.2.1
      1  1.2.2.1    cjep # $NetBSD: t_patch.sh,v 1.2.2.1 2021/05/31 22:15:23 cjep Exp $
      2      1.1  jruoho #
      3      1.2     nia # Copyright (c) 2020, 2021 The NetBSD Foundation, Inc.
      4      1.1  jruoho # All rights reserved.
      5      1.1  jruoho #
      6      1.1  jruoho # Redistribution and use in source and binary forms, with or without
      7      1.1  jruoho # modification, are permitted provided that the following conditions
      8      1.1  jruoho # are met:
      9      1.1  jruoho # 1. Redistributions of source code must retain the above copyright
     10      1.1  jruoho #    notice, this list of conditions and the following disclaimer.
     11      1.1  jruoho # 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  jruoho #    notice, this list of conditions and the following disclaimer in the
     13      1.1  jruoho #    documentation and/or other materials provided with the distribution.
     14      1.1  jruoho #
     15      1.1  jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16      1.1  jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17      1.1  jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18      1.1  jruoho # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19      1.1  jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20      1.1  jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21      1.1  jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22      1.1  jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23      1.1  jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24      1.1  jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25      1.1  jruoho # POSSIBILITY OF SUCH DAMAGE.
     26      1.1  jruoho #
     27      1.1  jruoho 
     28      1.1  jruoho patch_lines() {
     29      1.1  jruoho 
     30      1.1  jruoho 	printf "%$1s" | tr " " "a" > longlines 2>/dev/null
     31      1.1  jruoho 
     32      1.1  jruoho 	cat << EOF > longlines.patch
     33      1.1  jruoho --- ./longlines.orig 2019-10-16 09:25:30.667656644 +0000
     34      1.1  jruoho +++ ./longlines
     35      1.1  jruoho @@ -1 +1 @@
     36      1.1  jruoho EOF
     37      1.1  jruoho 	printf -- "-%$1s\n" | tr " " "a" >> longlines.patch 2>/dev/null
     38      1.1  jruoho 	printf -- "+%$1s" | tr " " "b" >> longlines.patch 2>/dev/null
     39      1.1  jruoho 
     40      1.1  jruoho 	patch longlines < longlines.patch
     41      1.1  jruoho 
     42      1.1  jruoho 	if [ ! $? -eq 0 ]; then
     43      1.1  jruoho 		atf_fail "Failed to patch long lines"
     44      1.1  jruoho 	fi
     45      1.1  jruoho }
     46      1.1  jruoho 
     47      1.1  jruoho atf_test_case lines
     48      1.1  jruoho lines_head()
     49      1.1  jruoho {
     50      1.1  jruoho 	atf_set "descr" "Test patching lines"
     51      1.1  jruoho }
     52      1.1  jruoho 
     53      1.1  jruoho lines_body()
     54      1.1  jruoho {
     55      1.1  jruoho 	lines="1 10 100 1000 8100"
     56      1.1  jruoho 
     57      1.1  jruoho 	for line in $lines; do
     58      1.1  jruoho 		patch_lines $line
     59      1.1  jruoho 	done
     60      1.1  jruoho }
     61      1.1  jruoho 
     62      1.1  jruoho atf_test_case long_lines
     63      1.1  jruoho long_lines_head()
     64      1.1  jruoho {
     65      1.1  jruoho 	atf_set "descr" "Test patching long lines (PR bin/54620)"
     66      1.1  jruoho }
     67      1.1  jruoho 
     68      1.1  jruoho long_lines_body()
     69      1.1  jruoho {
     70      1.1  jruoho 	patch_lines 10000
     71      1.1  jruoho }
     72      1.1  jruoho 
     73      1.2     nia atf_test_case backup_simple
     74      1.2     nia backup_simple_head()
     75      1.2     nia {
     76      1.2     nia 	atf_set "descr" "Test backup type 'simple' (-V simple)"
     77      1.2     nia }
     78      1.2     nia 
     79      1.2     nia backup_simple_body()
     80      1.2     nia {
     81      1.2     nia 	# Create the backup file so it's overwritten.
     82      1.2     nia 	touch to_patch.orig
     83      1.2     nia 	printf '%s\n' 'original file' > to_patch
     84      1.2     nia 
     85      1.2     nia 	cat << EOF > test_diff.patch
     86      1.2     nia --- original_file       2021-02-20 09:21:07.100869692 +0100
     87      1.2     nia +++ new_file    2021-02-20 09:21:10.912906887 +0100
     88      1.2     nia @@ -1 +1 @@
     89      1.2     nia -original text
     90      1.2     nia +new text
     91      1.2     nia EOF
     92      1.2     nia 	cksum=$(sha256 -n to_patch | cut -d' ' -f1)
     93      1.2     nia 	patch -V simple to_patch < test_diff.patch
     94      1.2     nia 	atf_check [ -f to_patch.orig ]
     95      1.2     nia 	origfile_cksum=$(sha256 -n to_patch.orig | cut -d' ' -f1)
     96      1.2     nia 	atf_check_equal "$cksum" "$origfile_cksum"
     97      1.2     nia 	atf_check grep -q -m 1 '^new text$' to_patch
     98      1.2     nia }
     99      1.2     nia 
    100      1.2     nia atf_test_case backup_none
    101      1.2     nia backup_none_head()
    102      1.2     nia {
    103      1.2     nia 	atf_set "descr" "Test backup type 'none' (-V none)"
    104      1.2     nia }
    105      1.2     nia 
    106      1.2     nia backup_none_body()
    107      1.2     nia {
    108      1.2     nia 	printf '%s\n' 'original file' > to_patch
    109      1.2     nia 
    110      1.2     nia 	cat << EOF > test_diff.patch
    111      1.2     nia --- original_file       2021-02-20 09:21:07.100869692 +0100
    112      1.2     nia +++ new_file    2021-02-20 09:21:10.912906887 +0100
    113      1.2     nia @@ -1 +1 @@
    114      1.2     nia -original text
    115      1.2     nia +new text
    116      1.2     nia EOF
    117      1.2     nia 	# Patch would mistakenly create 'simple' backup files when unwanted:
    118      1.2     nia 	# http://mail-index.netbsd.org/tech-userlevel/2021/02/19/msg012901.html
    119      1.2     nia 	patch -V none to_patch < test_diff.patch
    120      1.2     nia 	atf_check [ ! -f to_patch.orig ]
    121      1.2     nia 	atf_check grep -q -m 1 '^new text$' to_patch
    122      1.2     nia 
    123      1.2     nia 	# Environment variables should be overriden.
    124      1.2     nia 	printf '%s\n' 'original file' > to_patch
    125      1.2     nia 	VERSION_CONTROL=existing patch -V none to_patch \
    126      1.2     nia 	    < test_diff.patch
    127      1.2     nia 	atf_check [ ! -f to_patch.orig ]
    128      1.2     nia 	atf_check grep -q -m 1 '^new text$' to_patch
    129      1.2     nia 
    130      1.2     nia 	# --posix should imply -V none.
    131      1.2     nia 	printf '%s\n' 'original file' > to_patch
    132      1.2     nia 	patch --posix to_patch < test_diff.patch
    133      1.2     nia 	atf_check [ ! -f to_patch.orig ]
    134      1.2     nia 	atf_check grep -q -m 1 '^new text$' to_patch
    135      1.2     nia }
    136      1.2     nia 
    137      1.2     nia atf_test_case backup_numbered
    138      1.2     nia backup_numbered_head()
    139      1.2     nia {
    140      1.2     nia 	atf_set "descr" "Test backup type 'numbered' (-V numbered)"
    141      1.2     nia }
    142      1.2     nia 
    143      1.2     nia backup_numbered_body()
    144      1.2     nia {
    145      1.2     nia 	printf '%s\n' 'original file' > to_patch
    146      1.2     nia 
    147      1.2     nia 	cat << EOF > test_diff.patch
    148      1.2     nia --- original_file       2021-02-20 09:21:07.100869692 +0100
    149      1.2     nia +++ new_file    2021-02-20 09:21:10.912906887 +0100
    150      1.2     nia @@ -1 +1 @@
    151      1.2     nia -original text
    152      1.2     nia +new text
    153      1.2     nia EOF
    154      1.2     nia 	cksum1=$(sha256 -n to_patch | cut -d' ' -f1)
    155      1.2     nia 	patch -V numbered to_patch < test_diff.patch
    156      1.2     nia 	atf_check grep -q -m 1 '^new text$' to_patch
    157      1.2     nia 
    158      1.2     nia 	cat << EOF > test_diff2.patch
    159      1.2     nia --- new_file	2021-02-20 09:44:52.363230019 +0100
    160      1.2     nia +++ newer_file	2021-02-20 09:43:54.592863401 +0100
    161      1.2     nia @@ -1 +1 @@
    162      1.2     nia -new text
    163      1.2     nia +newer text
    164      1.2     nia EOF
    165      1.2     nia 	cksum2=$(sha256 -n to_patch | cut -d' ' -f1)
    166      1.2     nia 	patch -V numbered to_patch < test_diff2.patch
    167      1.2     nia 	atf_check grep -q -m 1 '^newer text$' to_patch
    168      1.2     nia 
    169      1.2     nia 	# Make sure the backup files match the original files.
    170      1.2     nia 	origfile_cksum1=$(sha256 -n to_patch.~1~ | cut -d' ' -f1)
    171      1.2     nia 	origfile_cksum2=$(sha256 -n to_patch.~2~ | cut -d' ' -f1)
    172      1.2     nia 	atf_check [ -f to_patch.~1~ ]
    173      1.2     nia 	atf_check_equal "$cksum1" "$origfile_cksum1"
    174      1.2     nia 	atf_check [ -f to_patch.~2~ ]
    175      1.2     nia 	atf_check_equal "$cksum2" "$origfile_cksum2"
    176      1.2     nia }
    177      1.2     nia 
    178      1.1  jruoho atf_init_test_cases()
    179      1.1  jruoho {
    180      1.1  jruoho 	atf_add_test_case lines
    181      1.1  jruoho 	atf_add_test_case long_lines
    182      1.2     nia 	atf_add_test_case backup_simple
    183      1.2     nia 	atf_add_test_case backup_none
    184      1.2     nia 	atf_add_test_case backup_numbered
    185      1.1  jruoho }
    186