-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit-tests
executable file
·273 lines (172 loc) · 6.25 KB
/
unit-tests
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/sh
#
# UNIT TESTS FOR STANDARD(?) UNIX NOTES
#
if [ "${TESTKEY}" = "" ]; then
echo You need to setup \$TESTKEY to your GPG key to use 'unit-tests' script
exit 1
fi
# ALTGPGKEY is used to test the 'notes newkey' command with a second key
ALTGPGKEY="${ALTGPGKEY:-${TESTKEY}}"
echo Setting up test environment
TESTDIR="`pwd`/tests"
ERRORLOG="${TESTDIR}/error_log_for_unit_tests"
rm $ERRORLOG
log () {
echo $@ >> $ERRORLOG
}
cat << EOF
Running unit tests for Standard(?) Unix Notes
EOF
createsample () {
SAMPLE="${USE_POINTER}/${1}.asc"
cat LICENCE | gpg -ear $TESTKEY -o ${SAMPLE}
}
pagebreak () {
cat << EOF
=================================================================
EOF
}
unit () {
shift && testdesc="$@"
pagebreak
cat << EOF
Unit Test = ${testdesc}
EOF
}
returned () {
echo "Function returned" $@
}
handle () {
echo Return Error = $?
}
trap handle 1 2 3 6 15
rm -rf ${TESTDIR}/*
echo TESTDIR = $TESTDIR
NOTESDIR="${TESTDIR}/unit_test_dir"
echo NOTESDIR = $NOTESDIR
pagebreak
. ./notes
echo Return code from sourcing './notes' = $?
#
# Unit Tests
#
unit Testing init function
echo "2" | cmd_init
returned $?
echo "Fixing up $CONFIGFILE to use \$TESTKEY $TESTKEY"
sed -i s/^KEY.*$/KEY\\t${TESTKEY}/ ${CONFIGFILE}
cat $CONFIGFILE
test ! -f $CONFIGFILE && log ERROR create config file failed
test ! -h $USE_POINTER && log ERROR setup USE notebook link failed
test ! -h $DEFAULT_POINTER && log ERROR setup DEFAULT notebook link failed
test ! -d $INITIAL_NOTEBOOK && log ERROR INITIAL_NOTEBOOK not created
test ! -d $JOURNALDIR && log ERROR failed to create JOURNALDIR
unit Testing config function
cmd_config
unit Testing add function
MYNOTE="My Note Title"
ALTMYNOTE="${USE_POINTER}/My_Note_Title.asc"
if [ "$1" = "skip" ] ; then
echo skipping interactive tests i.e. note_add
else
note_add ${MYNOTE}
test -f ${ALTMYNOTE} && echo Created $ALTMYNOTE
test ! -f ${ALTMYNOTE} && log failed to create or encrypt note ${ALTMYNOTE}
fi
unit Testing import function
IMPNOTE="${USE_POINTER}/README.md.asc"
note_import README.md
test -f ${IMPNOTE} && echo Imported $IMPNOTE
test ! -f ${IMPNOTE} && log failed to import $IMPNOTE
unit Testing rename function
RENAMETEST="b4rename"
AFTERTEST="afterrename"
echo Creating $RENAMETEST and then rename it
cat LICENCE | gpg -ear $TESTKEY -o ${USE_POINTER}/${RENAMETEST}.asc
echo $AFTERTEST | note_rename mv $RENAMETEST
# notes(1) sets the variable $newname to the name of the new file
# as all notes(1) functions have been imported then $newname is
# still available
TESTRENAMED="${USE_POINTER}/afterrename.asc"
test -f $TESTRENAMED && echo ${RENAMETEST}.asc renamed to ${newname}.asc
test ! -f $TESTRENAMED && log ERROR: Failed to rename ${RENAMETEST}.asc to ${newname}.asc
unit Testing copy function
COPYTEST="b4copy"
AFTERTEST="copy test"
echo Creating $COPYTEST and then copy it
createsample ${COPYTEST}
echo $AFTERTEST | note_rename cp $COPYTEST
tree test
COPIEDTEST="${USE_POINTER}/copy_test.asc"
test -f $COPIEDTEST && echo ${COPYTEST}.asc copied to ${newname}.asc
test ! -f $COPIEDTEST && log ERROR: Failed to copy ${COPYTEST}.asc to ${newname}.asc
unit Testing copy file not exist
NOTEXIST="not_a_note"
AFTERTEST="copy not exist"
echo $AFTERTEST | note_rename cp $NOTEXIST
test ! $? = 1 && log Error in note_rename .. should exit with 1 when copying non-existing note
echo Return code $?
# unit Testing delete function
#
# notebook function testing
#
unit Testing add notebook
NEWNOTEBOOK="${NOTESDIR}/testing"
notebook_add testing
test ! -d $NEWNOTEBOOK && log Failed to create new notebook $NEWNOTEBOOK
unit Testing delete notebook
DELNB="book_2b_del"
notebook_add $DELNB
notebook_delete $DELNB
test -d ${NOTESDIR}/$DELNB && log ERROR: Failed to delete notebook
unit Testing notebook_rename mv
BEFORE="old_notebook"
AFTER="new_notebook_name"
notebook_add $BEFORE
echo $AFTER | notebook_rename mv $BEFORE
test ! -d ${NOTESDIR}/$AFTER && log notebook_rename\(\) failed to rename $BEFORE to $AFTER
test -d ${NOTESDIR}/$BEFORE && log notebook_rename failed to remove $BEFORE
unit Testing notebook_rename cp
# copy back $AFTER to $BEFORE so we have both
echo $BEFORE | notebook_rename cp $AFTER
test ! -d ${NOTESDIR}/$BEFORE && log notebook_rename cp failed to copy $AFTER to $BEFORE
unit Testing notebook_list
notebook_list
unit Testing notebook USE and DEFAULT
PREVIOUSLY="`cmd_config | grep USE | sed s/^The\ USE\ notebook.*-\>\ // `"
cmd_use testing
# ave to use cmd_config to pick up testing environment
USED="`cmd_config | grep USE | sed s/^The\ USE\ notebook.*-\>\ // `"
test -d ${NOTESDIR}/${USED} && echo $USED exists
test ! "$PREVIOUSLY" = "$USED" && echo Confirmed change from $PREVIOUSLY to $USED
test "$PREVIOUSLY" = "$USED" && log ERROR notebook use command failed to change from $PREVIOUSLY to $USED
echo Importing a note into $USED
note_import makefile
test ! -f ${NOTESDIR}/testing/makefile.asc && log Failed to import a note into USE-d notebook
echo Now switch back to DEFAULT
cmd_use
NOWUSED="`cmd_config | grep USE | sed s/^The\ USE\ notebook.*-\>\ // `"
DEFAULTED="`cmd_config | grep DEFAULT | sed s/The\ DEFAULT\ notebook.*-\>\ // `"
test "$DEFAULTED" = "$NOWUSED" && echo Defaulted back to DEFAULT \(${DEFAULTED}\)
test ! "$DEFAULTED" = "$NOWUSED" && log ERROR: failed to default back to \(${DEFAULTED}\)
unit Testing changing default notebook
ORIG="`cmd_config | grep DEFAULT | sed s/The\ DEFAULT\ notebook.*-\>\ // `"
cmd_default testing
NOW="`cmd_config | grep DEFAULT | sed s/The\ DEFAULT\ notebook.*-\>\ // `"
test "$NOW" = "$ORIG" && log ERROR: Failed to switch DEFAULT from $ORIG to $NOW
cmd_default notes
AGAIN="`cmd_config | grep DEFAULT | sed s/The\ DEFAULT\ notebook.*-\>\ // `"
test "$NOW" = "$AGAIN" && log ERROR Failed to switch DEFAULT from $NOW to $AGAIN
test ! "$NOW" = "$AGAIN" && test ! "$ORIG" = "$NOW" && echo Able to switch DEFAULT between notebooks
unit Testing notes\(1\) help
notes help
unit Testing notebook\(1\) help
notebook help
unit Testing notes versions
fixversion
echo If versions are not all equal you may use \"fixversion fix\" to automatically fixup all versions to current git tag
pagebreak
tree tests
# Display error log if available
test -f $ERRORLOG && more $ERRORLOG