Allow output files to overwrite input files (bug #21280). Thanks to
[pspp-builds.git] / tests / bugs / overwrite-input-file.sh
1 #!/bin/sh
2
3 # This program tests that simultaneous input and output to a single
4 # file properly coexist, with the output atomically replacing the
5 # input if successful.
6
7 TEMPDIR=/tmp/pspp-tst-$$
8 TESTFILE=$TEMPDIR/`basename $0`.sps
9
10 # ensure that top_builddir  are absolute
11 if [ -z "$top_builddir" ] ; then top_builddir=. ; fi
12 if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi
13 top_builddir=`cd $top_builddir; pwd`
14 PSPP=$top_builddir/src/ui/terminal/pspp
15
16 # ensure that top_srcdir is absolute
17 top_srcdir=`cd $top_srcdir; pwd`
18
19 STAT_CONFIG_PATH=$top_srcdir/config
20 export STAT_CONFIG_PATH
21
22
23 cleanup()
24 {
25      cd /
26      rm -rf $TEMPDIR
27 }
28
29
30 fail()
31 {
32     echo $activity
33     echo FAILED
34     cleanup;
35     exit 1;
36 }
37
38
39 no_result()
40 {
41     echo $activity
42     echo NO RESULT;
43     cleanup;
44     exit 2;
45 }
46
47 pass()
48 {
49     cleanup;
50     exit 0;
51 }
52
53 mkdir -p $TEMPDIR
54
55 cd $TEMPDIR
56
57 activity="create data file"
58 cat > foo.data <<EOF
59 1
60 2
61 3
62 4
63 5
64 EOF
65 if [ $? -ne 0 ] ; then no_result ; fi
66
67 activity="create program 1"
68 cat > $TESTFILE <<EOF
69 DATA LIST FILE='foo.data' /X 1.
70 SAVE OUTFILE='foo.sav'.
71 EXPORT OUTFILE='foo.por'.
72 EOF
73 if [ $? -ne 0 ] ; then no_result ; fi
74
75 activity="run program 1"
76 $SUPERVISOR $PSPP --testing-mode $TESTFILE
77 if [ $? -ne 0 ] ; then no_result ; fi
78
79 activity="check and save copy of output files"
80 # Check that the files are nonzero length.
81 test -s foo.data || fail
82 test -s foo.sav || fail
83 test -s foo.por || fail
84 # Save copies of them.
85 cp foo.data foo.data.backup || fail
86 cp foo.sav foo.sav.backup || fail
87 cp foo.por foo.por.backup || fail
88
89
90 activity="create program 2"
91 cat > $TESTFILE <<EOF
92 GET 'foo.sav'.
93 COMPUTE Y = X + 1.
94 XSAVE OUTFILE='foo.sav'.
95 XEXPORT OUTFILE='foo.por'.
96 PRINT OUTFILE='foo.data'.
97 HOST kill -TERM \$PPID
98 EOF
99 if [ $? -ne 0 ] ; then no_result ; fi
100
101 activity="run program 2"
102 { $SUPERVISOR $PSPP --testing-mode $TESTFILE -e /dev/null; } >/dev/null 2>&1
103 # PSPP should have terminated with a signal.  POSIX requires that the exit
104 # status of a process terminated by a signal be greater than 128.
105 if [ $? -le 128 ] ; then no_result ; fi
106
107 activity="check for remaining temporary files"
108 if test -e *.tmp*; then fail; fi
109
110 activity="compare output 1"
111 cmp foo.sav foo.sav.backup
112 if [ $? -ne 0 ] ; then fail ; fi
113
114 activity="compare output 2"
115 cmp foo.por foo.por.backup
116 if [ $? -ne 0 ] ; then fail ; fi
117
118 activity="compare output 3"
119 cmp foo.data foo.data.backup
120 if [ $? -ne 0 ] ; then fail ; fi
121
122
123 activity="create program 3"
124 cat > $TESTFILE <<EOF
125 DATA LIST NOTABLE LIST FILE='foo.data'/X.
126 COMPUTE Y = X + 1.
127 PRINT OUTFILE='foo.data'/X Y.
128 EXECUTE.
129
130 GET 'foo.sav'.
131 COMPUTE Y = X + 2.
132 SAVE OUTFILE='foo.sav'.
133
134 IMPORT 'foo.por'.
135 COMPUTE Y = X + 3.
136 EXPORT OUTFILE='foo.por'.
137 EOF
138 if [ $? -ne 0 ] ; then no_result ; fi
139
140 activity="run program 3"
141 $SUPERVISOR $PSPP --testing-mode $TESTFILE -e /dev/null
142 if [ $? -ne 0 ] ; then no_result ; fi
143
144 activity="check for remaining temporary files"
145 if test -e *.tmp*; then fail; fi
146
147 activity="create program 4"
148 cat > $TESTFILE <<EOF
149 DATA LIST LIST NOTABLE FILE='foo.data'/X Y.
150 LIST.
151
152 GET 'foo.sav'.
153 LIST.
154
155 IMPORT 'foo.por'.
156 LIST.
157 EOF
158 if [ $? -ne 0 ] ; then no_result ; fi
159
160 activity="run program 4"
161 $SUPERVISOR $PSPP --testing-mode $TESTFILE -e /dev/null
162 if [ $? -ne 0 ] ; then no_result ; fi
163
164 activity="compare output"
165 diff -b -B pspp.list - << EOF
166        X        Y
167 -------- --------
168     1.00     2.00
169     2.00     3.00
170     3.00     4.00
171     4.00     5.00
172     5.00     6.00
173
174 X        Y
175 - --------
176 1     3.00
177 2     4.00
178 3     5.00
179 4     6.00
180 5     7.00
181
182 X        Y
183 - --------
184 1     4.00
185 2     5.00
186 3     6.00
187 4     7.00
188 5     8.00
189 EOF
190 if [ $? -ne 0 ] ; then fail ; fi
191
192
193 pass;