1#!/usr/bin/env bash
2## Test for issue64 - Should store patch metadata in UTF-8
3##
4## Copyright (C) 2009  Reinier Lamers
5##
6## Permission is hereby granted, free of charge, to any person
7## obtaining a copy of this software and associated documentation
8## files (the "Software"), to deal in the Software without
9## restriction, including without limitation the rights to use, copy,
10## modify, merge, publish, distribute, sublicense, and/or sell copies
11## of the Software, and to permit persons to whom the Software is
12## furnished to do so, subject to the following conditions:
13##
14## The above copyright notice and this permission notice shall be
15## included in all copies or substantial portions of the Software.
16##
17## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19## MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21## BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22## ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23## CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24## SOFTWARE.
25
26. lib
27
28# Helper function: do a darcs changes --xml and grep the result for the first
29# argument. If it is not found, exit with status 1. Otherwise, continue. The
30# second argument is a text that describes what we're grepping for.
31# If a third argument is given, it is used as the value for a --last option for
32# darcs changes.
33grep_changes () {
34    switch_to_utf8_locale
35    if [ -z "$3" ]; then
36            last=""
37    else
38            last="--last $3"
39    fi
40
41    darcs changes $last --xml > changes.xml
42    if grep "$1" changes.xml ; then
43        echo "$2 OK"
44    else
45        cat changes.xml
46        echo "$2 not UTF-8-encoded!"
47        exit 1
48    fi
49    switch_to_latin9_locale
50}
51
52
53# This file is encoded in ISO-8859-15 aka latin9. It was crafted with a hex editor.
54# Please leave it this way :-)
55switch_to_latin9_locale
56
57# This test clobbers the global darcs author
58-f $HOME/.darcs/author && exit 200
59
60rm -rf temp1
61mkdir temp1
62cd temp1
63
64darcs init
65
66# Test recording non-UTF-8-encoded non-latin1 ("funny") metadata from
67# interactive input
68
69echo 'Selbstverst�ndlich �berraschend' > something.txt
70darcs add something.txt
71
72echo 'l33tking�0r@example.org' > interaction_script.txt
73echo y >> interaction_script.txt
74echo y >> interaction_script.txt
75echo y >> interaction_script.txt
76echo '�uroh4xx0rz' >> interaction_script.txt
77echo n >> interaction_script.txt
78
79unset DARCSEMAIL
80unset EMAIL
81set
82darcs record -i --skip-long-comment < interaction_script.txt
83grep_changes 'l33tkingž0r@example.org' 'patch author from interactive prompt'
84grep_changes '€uroh4xx0rz' 'patch name from interactive prompt'
85
86# Test recording funny metadata from command line
87
88echo 'Sogar �berraschender' >> something.txt
89
90darcs record -a -A 'J�r�me Leb�uf' -m 'that will be � 15, sir'
91
92if echo $OS | not grep -i windows; then # issue2591
93    grep_changes 'that will be € 15, sir' 'patch name from command line'
94    grep_changes 'Jérôme Lebœuf' 'patch author from command line'
95fi
96
97# Test recording funny metadata from a log file
98
99echo 'Am aller�berraschendsten' >> something.txt
100
101echo 'darcs is soms wat na�ef aangaande tekstcodering' > log.txt
102echo 'en zulke na�viteit is tegenwoordig pass�, aldus �i�ek' >> log.txt
103darcs record -a -A 'J�r�me Leb�uf' --logfile=log.txt
104
105grep_changes 'darcs is soms wat naïef aangaande tekstcodering' 'patch name from log file'
106grep_changes 'en zulke naïviteit is tegenwoordig passé, aldus Žižek' 'patch log from log file'
107
108# Test recording funny metadata from environment,
109export EMAIL='Slavoj �i�ek <zizek@example.edu>'
110rm $HOME/.darcs/author
111echo 'La la la, the more lines the better!' >> something.txt
112darcs record -a -m 'Patch names are overrated'
113
114if echo $OS | not grep -i windows; then # issue2591
115    grep_changes 'Slavoj Žižek' 'author name from environment'
116fi
117
118# Test recording funny metadata from prefs files
119echo '�ed is dead' > _darcs/prefs/author
120echo '483 bottles of beer on the wall' >> something.txt
121darcs record -a -m 'Patch by �ed'
122
123grep_changes 'Žed is dead' 'author name from prefs file'
124
125# Test amend-recording funny metadata
126echo 'No, it is really 484' >> something.txt
127echo y | darcs amend-record -p 'Patch by ' -A '�ed is even deader' -a
128if echo $OS | not grep -i windows; then # issue2591
129    grep_changes 'Žed is even deader' 'author name from amend-record command line flag'
130fi
131
132cat <<FAKE > editor.hs # create an 'editor' that writes latin9
133import System.Environment
134import qualified Data.ByteString as B
135str = B.pack [65,108,108,32,109,121,32,164,115,32,97,114,101,32,103,111,110,101]
136main = getArgs >>= \[x] -> B.writeFile x str
137FAKE
138ghc --make -o editor editor.hs
139export DARCS_EDITOR="`pwd`/editor"
140if echo $OS | not grep -i windows; then # issue2591
141    printf "y\ny\n" | darcs amend --edit -p 'Patch by '
142    grep_changes 'All my €s are gone' 'description edited from amend-record'
143    grep_changes 'Žed is even deader' 'author name taken from draft in amend'
144fi
145
146# Test tag recording funny metadata
147rm _darcs/prefs/author # Make tag be taken from EMAIL env variable
148if echo $OS | not grep -i windows; then # issue2591
149    darcs tag -m '� is my favorite letter'
150    grep_changes 'Slavoj Žižek' 'author name from environment with tag command' 1
151    grep_changes 'Ž is my favorite letter' 'Tag name from command line'
152fi
153
154unset EMAIL
155printf "�ors\ninitialcomment\n" | darcs tag --edit-long-comment
156grep_changes Žors 'Author name from interactive prompt from tag command'
157grep_changes 'All my €s are gone' 'Tag name from editor from tag command' 1
158
159if grepors $HOME/.darcs/author ; then
160    echo 'Author name stored locale-encoded in prefs file after tag command, OK'
161else
162    echo 'No locale-encoded author in prefs file after tag command!'
163    exit 1
164fi
165rm $HOME/.darcs/author
166
167if echo $OS | not grep -i windows; then # issue2591
168    darcs tag -A Ade -m 'La�t call'
169    grep_changes Adže 'Author name from tag command line' 1
170    grep_changes 'Lažt call' 'Tag name from tag command line (take 2)' 1
171fi
172
173cd ..
174
175# test that UTF-8 metadata doesn't get mangled on get
176rm -rf temp2
177darcs get temp1 temp2
178darcs changes --repodir temp1 --xml > temp1/changes.xml
179darcs changes --repodir temp2 --xml > temp2/changes.xml
180diff temp1/changes.xml temp2/changes.xml
181
182# and that it doesn't get mangled on push
183rm -rf temp2
184mkdir temp2; darcs init --repodir temp2
185darcs push --repodir temp1 -a temp2 --set-default
186darcs changes --repodir temp1 --xml > temp1/changes.xml
187darcs changes --repodir temp2 --xml > temp2/changes.xml
188diff temp1/changes.xml temp2/changes.xml
189
190# and that it doesn't get mangled on pull
191rm -rf temp2
192mkdir temp2; darcs init --repodir temp2
193darcs pull --repodir temp2 -a temp1
194darcs changes --repodir temp1 --xml > temp1/changes.xml
195darcs changes --repodir temp2 --xml > temp2/changes.xml
196diff temp1/changes.xml temp2/changes.xml
197
198# and that it doesn't get mangled on send
199rm -rf temp2
200mkdir temp2; darcs init --repodir temp2
201darcs send --repodir temp1 -a -o temp2/patch.dpatch
202darcs apply --repodir temp2 -a temp2/patch.dpatch
203darcs changes --repodir temp1 --xml > temp1/changes.xml
204darcs changes --repodir temp2 --xml > temp2/changes.xml
205diff temp1/changes.xml temp2/changes.xml
206