Branch data Line data Source code
1 : : #include "sute.h"
2 : :
3 : : /**
4 : : * The Example 1 from README:
5 : : * Now some tests could be running:
6 : : * Stage 1. Adding:
7 : : * precizer --progress --database=database1.db tests/fixtures/diffs/diff1
8 : : * Stage 2. Adding:
9 : : * precizer --progress --database=database2.db tests/fixtures/diffs/diff2
10 : : * Final stage. Comparing:
11 : : * precizer --compare database1.db database2.db
12 : : */
13 : 2 : static Return test0011_1(void)
14 : : {
15 : 2 : INITTEST;
16 : :
17 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
18 : :
19 : : // Create memory for the result
20 : 2 : m_create(char,result,MEMORY_STRING);
21 : 2 : m_create(char,chunk,MEMORY_STRING);
22 : :
23 : 2 : const char *arguments = "--progress --database=database1.db "
24 : : "tests/fixtures/diffs/diff1";
25 : :
26 : 2 : ASSERT(SUCCESS == runit(arguments,chunk,NULL,COMPLETED,ALLOW_BOTH));
27 : 2 : ASSERT(SUCCESS == m_copy(result,chunk));
28 : :
29 : 2 : arguments = "--progress --database=database2.db "
30 : : "tests/fixtures/diffs/diff2";
31 : :
32 : 2 : ASSERT(SUCCESS == runit(arguments,chunk,NULL,COMPLETED,ALLOW_BOTH));
33 : 2 : ASSERT(SUCCESS == m_concat_strings(result,chunk));
34 : :
35 : 2 : arguments = "--compare database1.db database2.db";
36 : :
37 : 2 : ASSERT(SUCCESS == runit(arguments,chunk,NULL,COMPLETED,ALLOW_BOTH));
38 : 2 : ASSERT(SUCCESS == m_concat_strings(result,chunk));
39 : :
40 : 2 : m_create(char,pattern,MEMORY_STRING);
41 : :
42 : 2 : const char *filename = "templates/0011_001.txt";
43 : :
44 : 2 : ASSERT(SUCCESS == get_file_content(filename,pattern));
45 : :
46 : : // Match the result against the pattern
47 : 2 : ASSERT(SUCCESS == match_pattern(result,pattern,filename));
48 : :
49 : : // Clean up test results
50 : 2 : ASSERT(SUCCESS == delete_path("database1.db"));
51 : 2 : ASSERT(SUCCESS == delete_path("database2.db"));
52 : :
53 : 2 : m_del(pattern);
54 : 2 : m_del(chunk);
55 : 2 : m_del(result);
56 : :
57 : 2 : RETURN_STATUS;
58 : : }
59 : :
60 : : /**
61 : : * The Example 2 from README
62 : : * Updating the database:
63 : : * Stage 1. Adding:
64 : : * precizer --progress --database=database1.db tests/fixtures/diffs/diff1
65 : : * Stage 2. Reuse previous example once agan. The first try. The warning message.
66 : : * precizer --progress --database=database1.db tests/fixtures/diffs/diff1
67 : : * Stage 3. Run of database update without making actual changes to disk:
68 : : * precizer --update --progress --database=database1.db tests/fixtures/diffs/diff1
69 : : * Stage 4. Now let's make some changes:
70 : : * # Backup
71 : : * prepare_mutable_fixture("tests/fixtures/diffs/diff1")
72 : : * # Modify a file
73 : : * echo -n " " >> tests/fixtures/diffs/diff1/1/AAA/BCB/CCC/a.txt
74 : : * # Add a new file by truncating the file with the target name
75 : : * truncate_file_to_zero_size("tests/fixtures/diffs/diff1/1/AAA/BCB/CCC/c.txt")
76 : : * # Remove a file
77 : : * delete_path("tests/fixtures/diffs/diff1/path2/AAA/ZAW/D/e/f/b_file.txt")
78 : : * Stage 5. Run the precizer once again:
79 : : * precizer --update --progress --database=database1.db tests/fixtures/diffs/diff1
80 : : * Final stage. Recover from backup:
81 : : * restore_mutable_fixture("tests/fixtures/diffs/diff1")
82 : : */
83 : 2 : static Return test0011_2(void)
84 : : {
85 : 2 : INITTEST;
86 : :
87 : : // Preparation for tests
88 : 2 : ASSERT(SUCCESS == prepare_mutable_fixture("tests/fixtures/diffs/diff1"));
89 : :
90 : 2 : const char *arguments = "--progress --database=database1.db "
91 : : "tests/fixtures/diffs/diff1";
92 : :
93 : : // Create memory for the result
94 : 2 : m_create(char,result,MEMORY_STRING);
95 : :
96 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
97 : :
98 : 2 : m_create(char,pattern,MEMORY_STRING);
99 : :
100 : 2 : const char *filename = "templates/0011_002_1.txt";
101 : :
102 : 2 : ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
103 : :
104 : 2 : ASSERT(SUCCESS == get_file_content(filename,pattern));
105 : :
106 : : // Match the result against the pattern
107 : 2 : ASSERT(SUCCESS == match_pattern(result,pattern,filename));
108 : :
109 : : // Clean to use it iteratively
110 : 2 : m_del(pattern);
111 : 2 : m_del(result);
112 : :
113 : 2 : arguments = "--progress --database=database1.db "
114 : : "tests/fixtures/diffs/diff1";
115 : :
116 : 2 : filename = "templates/0011_002_2.txt";
117 : :
118 : 2 : ASSERT(SUCCESS == runit(arguments,result,NULL,WARNING,ALLOW_BOTH));
119 : :
120 : 2 : ASSERT(SUCCESS == get_file_content(filename,pattern));
121 : :
122 : 2 : ASSERT(SUCCESS == match_pattern(result,pattern,filename));
123 : :
124 : : // Clean to use it iteratively
125 : 2 : m_del(pattern);
126 : 2 : m_del(result);
127 : :
128 : 2 : m_create(char,chunk,MEMORY_STRING);
129 : :
130 : 2 : arguments = "--update --progress --database=database1.db "
131 : : "tests/fixtures/diffs/diff1";
132 : :
133 : 2 : ASSERT(SUCCESS == runit(arguments,chunk,NULL,COMPLETED,ALLOW_BOTH));
134 : :
135 : 2 : ASSERT(SUCCESS == m_copy(result,chunk));
136 : :
137 : 2 : ASSERT(SUCCESS == add_string_to(" ","tests/fixtures/diffs/diff1/1/AAA/BCB/CCC/a.txt"));
138 : 2 : ASSERT(SUCCESS == delete_path("tests/fixtures/diffs/diff1/path2/AAA/ZAW/D/e/f/b_file.txt"));
139 : :
140 : : // Create the file as empty without using shell touch
141 : 2 : ASSERT(SUCCESS == truncate_file_to_zero_size("tests/fixtures/diffs/diff1/1/AAA/BCB/CCC/c.txt"));
142 : :
143 : 2 : arguments = "--watch-timestamps --update --progress "
144 : : "--database=database1.db tests/fixtures/diffs/diff1";
145 : :
146 : 2 : ASSERT(SUCCESS == runit(arguments,chunk,NULL,COMPLETED,ALLOW_BOTH));
147 : :
148 : 2 : ASSERT(SUCCESS == m_concat_strings(result,chunk));
149 : :
150 : 2 : filename = "templates/0011_002_3.txt";
151 : :
152 : 2 : ASSERT(SUCCESS == get_file_content(filename,pattern));
153 : :
154 : 2 : ASSERT(SUCCESS == match_pattern(result,pattern,filename));
155 : :
156 : 2 : m_del(pattern);
157 : 2 : m_del(result);
158 : 2 : m_del(chunk);
159 : :
160 : : // Don't clean up test results to use on the next test
161 : :
162 : 2 : RETURN_STATUS;
163 : : }
164 : :
165 : : /**
166 : : * The Example 3 from README
167 : : * Using the --silent mode. When this mode is enabled, the program suppresses
168 : : * normal output after command execution. This makes sense when using the
169 : : * program inside scripts. An exception is --compare: with --silent, compare
170 : : * results remain
171 : : * visible. Paths with differences are printed directly, and category headings
172 : : * are kept only when more than one compare category is active
173 : : * Let's add the --silent option to the previous example:
174 : : *
175 : : * precizer --silent --update --progress --database=database1.db tests/fixtures/diffs/diff1
176 : : *
177 : : *
178 : : */
179 : 2 : static Return test0011_3(void)
180 : : {
181 : 2 : INITTEST;
182 : :
183 : : // Create memory for the result
184 : 2 : m_create(char,result,MEMORY_STRING);
185 : :
186 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
187 : :
188 : 2 : const char *arguments = "--silent --update --progress --database=database1.db "
189 : : "tests/fixtures/diffs/diff1";
190 : :
191 : 2 : ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
192 : :
193 : : // Verify that silent mode produced no stdout after command execution
194 : 2 : ASSERT(result->length == 0);
195 : :
196 : : #if 0
197 : : // Verify that silent mode produced no stdout after command execution
198 : : if(result->length != 0)
199 : : {
200 : : echo(STDERR,"ERROR: In silent mode stdout must be empty\n");
201 : : echo(STDERR,YELLOW "Output:\n>>" RESET "%s" YELLOW "<<\n" RESET,m_text(result));
202 : : status = FAILURE;
203 : : }
204 : : #endif
205 : :
206 : 2 : call(m_del(result));
207 : :
208 : 2 : m_create(char,pattern,MEMORY_STRING);
209 : :
210 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
211 : :
212 : 2 : const char *filename = "templates/0011_003.txt";
213 : :
214 : 2 : ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
215 : 2 : ASSERT(SUCCESS == get_file_content(filename,pattern));
216 : 2 : ASSERT(SUCCESS == match_pattern(result,pattern,filename));
217 : :
218 : 2 : m_del(pattern);
219 : 2 : m_del(result);
220 : :
221 : 2 : RETURN_STATUS;
222 : : }
223 : :
224 : : /**
225 : : *
226 : : * The Example 4 from README
227 : : * Additional information with --verbose mode
228 : : *
229 : : */
230 : 2 : static Return test0011_4(void)
231 : : {
232 : 2 : INITTEST;
233 : :
234 : : // Create memory for the result
235 : 2 : m_create(char,result,MEMORY_STRING);
236 : :
237 : 2 : m_create(char,pattern,MEMORY_STRING);
238 : :
239 : 2 : const char *filename = "templates/0011_004_1.txt";
240 : :
241 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
242 : :
243 : 2 : const char *arguments = "--verbose --update --progress --database=database1.db "
244 : : "tests/fixtures/diffs/diff1";
245 : :
246 : 2 : ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
247 : 2 : ASSERT(SUCCESS == get_file_content(filename,pattern));
248 : 2 : ASSERT(SUCCESS == match_pattern(result,pattern,filename));
249 : :
250 : : // Clean up test results
251 : 2 : m_del(pattern);
252 : 2 : m_del(result);
253 : :
254 : 2 : ASSERT(SUCCESS == delete_path("database1.db"));
255 : 2 : ASSERT(SUCCESS == restore_mutable_fixture("tests/fixtures/diffs/diff1"));
256 : :
257 : 2 : RETURN_STATUS;
258 : : }
259 : :
260 : : /**
261 : : *
262 : : * The Example 5 from README
263 : : * Disable recursion with --maxdepth=0 option
264 : : *
265 : : *
266 : : */
267 : 2 : static Return test0011_5(void)
268 : : {
269 : 2 : INITTEST;
270 : :
271 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
272 : :
273 : 2 : const char *arguments = "--maxdepth=0 tests/fixtures/4";
274 : :
275 : 2 : const char *filename = "templates/0011_005_1.txt";
276 : :
277 : 2 : const char *template = "%DB_NAME%";
278 : :
279 : 2 : const char *replacement = getenv("DBNAME"); // Database name
280 : :
281 : 2 : ASSERT(replacement != NULL);
282 : :
283 : 2 : ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
284 : :
285 : : /* At the second stage, the --maxdepth=0 option is not used.
286 : : Therefore, all files that were not previously included
287 : : will be added to the database. */
288 : :
289 : 2 : arguments = "--update tests/fixtures/4";
290 : :
291 : 2 : filename = "templates/0011_005_2.txt";
292 : :
293 : 2 : ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
294 : :
295 : 2 : ASSERT(SUCCESS == delete_path(replacement));
296 : :
297 : 2 : RETURN_STATUS;
298 : : }
299 : :
300 : : /**
301 : : *
302 : : * The Example 6 from README
303 : : * Relative path to ignore with --ignore
304 : : *
305 : : *
306 : : */
307 : 2 : static Return test0011_6(void)
308 : : {
309 : 2 : INITTEST;
310 : :
311 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
312 : :
313 : 2 : const char *arguments = "--ignore=\"^diff1/1/.*\" tests/fixtures/diffs";
314 : :
315 : 2 : const char *filename = "templates/0011_006_1.txt";
316 : :
317 : 2 : const char *template = "%DB_NAME%";
318 : :
319 : 2 : const char *replacement = getenv("DBNAME"); // Database name
320 : :
321 : 2 : ASSERT(replacement != NULL);
322 : :
323 : 2 : ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
324 : :
325 : 2 : filename = "templates/0011_006_2.txt";
326 : :
327 : 2 : arguments = "--update tests/fixtures/diffs";
328 : :
329 : 2 : ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
330 : :
331 : 2 : RETURN_STATUS;
332 : : }
333 : :
334 : : /**
335 : : *
336 : : * The Example 7 from README
337 : : * Multiple regular expressions for ignoring can be specified
338 : : * using many --ignore options
339 : : *
340 : : */
341 : 2 : static Return test0011_7(void)
342 : : {
343 : 2 : INITTEST;
344 : :
345 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
346 : :
347 : 2 : const char *arguments = "--update --db-drop-ignored"
348 : : " --ignore=\"^diff1/1/.*\""
349 : : " --ignore=\"^diff2/1/.*\" tests/fixtures/diffs";
350 : :
351 : 2 : const char *filename = "templates/0011_007.txt";
352 : :
353 : 2 : const char *template = "%DB_NAME%";
354 : :
355 : 2 : const char *replacement = getenv("DBNAME"); // Database name
356 : :
357 : 2 : ASSERT(replacement != NULL);
358 : :
359 : 2 : ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
360 : :
361 : 2 : ASSERT(SUCCESS == delete_path(replacement));
362 : :
363 : 2 : RETURN_STATUS;
364 : : }
365 : :
366 : : /**
367 : : *
368 : : * The Example 8 from README
369 : : * Using the --ignore options together with --include
370 : : * Also covers the compare example that narrows the reported comparison scope
371 : : *
372 : : *
373 : : */
374 : 2 : static Return test0011_8(void)
375 : : {
376 : 2 : INITTEST;
377 : :
378 : 2 : const char *arguments = "tests/fixtures/diffs";
379 : :
380 : 2 : ASSERT(SUCCESS == runit(arguments,NULL,NULL,COMPLETED,ALLOW_BOTH));
381 : :
382 : 2 : ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
383 : :
384 : 2 : arguments = "--update"
385 : : " --progress"
386 : : " --ignore=\"^.*/path2/.*\""
387 : : " --ignore=\"^diff2/.*\""
388 : : " --include=\"^diff2/1/AAA/ZAW/A/b/c/.*\""
389 : : " --include=\"^diff2/path1/AAA/ZAW/.*\""
390 : : " --include=\"^diff1/path2/AAA/ZAW/A/b/c/a_file\\..*\""
391 : : " --db-drop-ignored"
392 : : " tests/fixtures/diffs";
393 : :
394 : 2 : const char *filename = "templates/0011_008_1.txt";
395 : :
396 : 2 : const char *template = "%DB_NAME%";
397 : :
398 : 2 : const char *replacement = getenv("DBNAME"); // Database name
399 : :
400 : 2 : ASSERT(replacement != NULL);
401 : :
402 : 2 : ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
403 : :
404 : 2 : ASSERT(SUCCESS == delete_path(replacement));
405 : :
406 : 2 : m_create(char,result,MEMORY_STRING);
407 : 2 : m_create(char,pattern,MEMORY_STRING);
408 : :
409 : 2 : arguments = "--progress --database=database1.db tests/fixtures/diffs/diff1";
410 : :
411 : 2 : ASSERT(SUCCESS == runit(arguments,NULL,NULL,COMPLETED,ALLOW_BOTH));
412 : :
413 : 2 : arguments = "--progress --database=database2.db tests/fixtures/diffs/diff2";
414 : :
415 : 2 : ASSERT(SUCCESS == runit(arguments,NULL,NULL,COMPLETED,ALLOW_BOTH));
416 : :
417 : 2 : arguments = "--compare"
418 : : " --ignore=\"^(?:2|3|4)/.*\""
419 : : " --ignore=\"^path1/.*\""
420 : : " --ignore=\"^path2/.*\""
421 : : " --include=\"^2/AAA/BBB/CZC/a\\.txt$\""
422 : : " database1.db database2.db";
423 : :
424 : 2 : filename = "templates/0011_008_2.txt";
425 : :
426 : 2 : ASSERT(SUCCESS == runit(arguments,result,NULL,COMPLETED,ALLOW_BOTH));
427 : 2 : ASSERT(SUCCESS == get_file_content(filename,pattern));
428 : 2 : ASSERT(SUCCESS == match_pattern(result,pattern,filename));
429 : :
430 : 2 : m_del(pattern);
431 : 2 : m_del(result);
432 : :
433 : 2 : ASSERT(SUCCESS == delete_path("database1.db"));
434 : 2 : ASSERT(SUCCESS == delete_path("database2.db"));
435 : :
436 : 2 : RETURN_STATUS;
437 : : }
438 : :
439 : : /**
440 : : *
441 : : * User's Manual and examples from README test set
442 : : *
443 : : */
444 : 2 : Return test0011(void)
445 : : {
446 : : /// The status that will be passed to return() before exiting.
447 : : /// By default, the function worked without errors.
448 : 2 : INITTEST;
449 : :
450 : 2 : TEST(test0011_1,"README Example 1 Adding and comparing");
451 : 2 : TEST(test0011_2,"README Example 2 Updating the data in DB");
452 : 2 : TEST(test0011_3,"README Example 3 --silent mode");
453 : 2 : TEST(test0011_4,"README Example 4 --verbose mode");
454 : 2 : TEST(test0011_5,"README Example 5 Disable recursion with --maxdepth");
455 : 2 : TEST(test0011_6,"README Example 6 Relative path to ignore with --ignore");
456 : 2 : TEST(test0011_7,"README Example 7 Multiple regexp for ignoring");
457 : 2 : TEST(test0011_8,"README Example 8 The --ignore options together with --include, including the compare-scope example");
458 : 2 : SUTE(test0030,"README Examples 9 & 10: --lock-checksum with --rehash-locked and --watch-timestamps");
459 : 2 : SUTE(test0029,"README Example 11: Testing how the application behaves with inaccessible files");
460 : :
461 : 2 : RETURN_STATUS;
462 : : }
|