LCOV - code coverage report
Current view: top level - tests/src - test0011.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 98.1 % 155 152
Test Date: 2026-01-12 05:34:38 Functions: 100.0 % 9 9

            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/examples/diffs/diff1
       8              :  * Stage 2. Adding:
       9              :  * precizer --progress --database=database2.db tests/examples/diffs/diff2
      10              :  * Final stage. Comparing:
      11              :  * precizer --compare database1.db database2.db
      12              :  */
      13            2 : static Return test0011_1_readme(void)
      14              : {
      15            2 :         INITTEST;
      16              : 
      17            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
      18              : 
      19              :         // Create memory for the result
      20            2 :         create(char,result);
      21            2 :         create(char,chunk);
      22              : 
      23            2 :         const char *arguments = "--progress --database=database1.db tests/examples/diffs/diff1";
      24              : 
      25            2 :         ASSERT(SUCCESS == runit(arguments,chunk,COMPLETED,ALLOW_BOTH));
      26            2 :         ASSERT(SUCCESS == copy(result,chunk));
      27              : 
      28            2 :         arguments = "--progress --database=database2.db tests/examples/diffs/diff2";
      29              : 
      30            2 :         ASSERT(SUCCESS == runit(arguments,chunk,COMPLETED,ALLOW_BOTH));
      31            2 :         ASSERT(SUCCESS == concat_strings(result,chunk));
      32              : 
      33            2 :         arguments = "--compare database1.db database2.db";
      34              : 
      35            2 :         ASSERT(SUCCESS == runit(arguments,chunk,COMPLETED,ALLOW_BOTH));
      36            2 :         ASSERT(SUCCESS == concat_strings(result,chunk));
      37              : 
      38            2 :         create(char,pattern);
      39              : 
      40            2 :         const char *filename = "templates/0011_001.txt";
      41              : 
      42            2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
      43              : 
      44              :         // Match the result against the pattern
      45            2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
      46              : 
      47              :         // Clean up test results
      48            2 :         ASSERT(SUCCESS == external_call("cd ${TMPDIR};"
      49              :                 "rm database1.db database2.db",COMPLETED,ALLOW_BOTH));
      50              : 
      51            2 :         del(pattern);
      52            2 :         del(chunk);
      53            2 :         del(result);
      54              : 
      55            2 :         RETURN_STATUS;
      56              : }
      57              : 
      58              : /**
      59              :  * The Example 2 from README
      60              :  * Updating the database:
      61              :  * Stage 1. Adding:
      62              :  * precizer --progress --database=database1.db tests/examples/diffs/diff1
      63              :  * Stage 2. Reuse previous example once agan. The first try. The warning message.
      64              :  * precizer --progress --database=database1.db tests/examples/diffs/diff1
      65              :  * Stage 3. Run of database update without making actual changes to disk:
      66              :  * precizer --update --progress --database=database1.db tests/examples/diffs/diff1
      67              :  * Stage 4. Now let's make some changes:
      68              :  * # Backup
      69              :  * cp -a tests/examples/ tests/examples_backup
      70              :  * # Modify a file
      71              :  * echo -n "  " >> tests/examples/diffs/diff1/1/AAA/BCB/CCC/a.txt
      72              :  * # Add a new file
      73              :  * touch tests/examples/diffs/diff1/1/AAA/BCB/CCC/c.txt
      74              :  * # Remove a file
      75              :  * rm tests/examples/diffs/diff1/path2/AAA/ZAW/D/e/f/b_file.txt
      76              :  * Stage 5. Run the precizer once again:
      77              :  * precizer --update --progress --database=database1.db tests/examples/diffs/diff1
      78              :  * Final stage. Recover from backup:
      79              :  * rm -rf tests/examples/
      80              :  * mv tests/examples_backup/ tests/examples/
      81              :  */
      82            2 : static Return test0011_2_readme(void)
      83              : {
      84            2 :         INITTEST;
      85              : 
      86              :         // Preparation for tests
      87            2 :         ASSERT(SUCCESS == external_call("cd ${TMPDIR};"
      88              :                 "cp -a tests/examples/ tests/examples_backup;",COMPLETED,ALLOW_BOTH));
      89              : 
      90            2 :         const char *arguments = "--progress --database=database1.db tests/examples/diffs/diff1";
      91            2 :         const char *command = NULL;
      92              : 
      93              :         // Create memory for the result
      94            2 :         create(char,result);
      95              : 
      96            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
      97              : 
      98            2 :         create(char,pattern);
      99              : 
     100            2 :         const char *filename = "templates/0011_002_1.txt";
     101              : 
     102            2 :         ASSERT(SUCCESS == runit(arguments,result,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 :         del(pattern);
     111            2 :         del(result);
     112              : 
     113            2 :         arguments = "--progress --database=database1.db tests/examples/diffs/diff1";
     114              : 
     115            2 :         filename = "templates/0011_002_2.txt";
     116              : 
     117            2 :         ASSERT(SUCCESS == runit(arguments,result,WARNING,ALLOW_BOTH));
     118              : 
     119            2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     120              : 
     121            2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     122              : 
     123              :         // Clean to use it iteratively
     124            2 :         del(pattern);
     125            2 :         del(result);
     126              : 
     127            2 :         create(char,chunk);
     128              : 
     129            2 :         arguments = "--update --progress --database=database1.db tests/examples/diffs/diff1";
     130              : 
     131            2 :         ASSERT(SUCCESS == runit(arguments,chunk,COMPLETED,ALLOW_BOTH));
     132            2 :         ASSERT(SUCCESS == copy(result,chunk));
     133              : 
     134            2 :         command = "cd ${TMPDIR};"
     135              :                 "echo -n '  ' >> tests/examples/diffs/diff1/1/AAA/BCB/CCC/a.txt;"
     136              :                 "touch tests/examples/diffs/diff1/1/AAA/BCB/CCC/c.txt;"
     137              :                 "rm tests/examples/diffs/diff1/path2/AAA/ZAW/D/e/f/b_file.txt";
     138              : 
     139            2 :         ASSERT(SUCCESS == external_call(command,COMPLETED,ALLOW_BOTH));
     140              : 
     141            2 :         arguments = "--watch-timestamps --update --progress --database=database1.db tests/examples/diffs/diff1";
     142              : 
     143            2 :         ASSERT(SUCCESS == runit(arguments,chunk,COMPLETED,ALLOW_BOTH));
     144            2 :         ASSERT(SUCCESS == concat_strings(result,chunk));
     145              : 
     146            2 :         filename = "templates/0011_002_3.txt";
     147              : 
     148            2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     149            2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     150              : 
     151            2 :         del(pattern);
     152            2 :         del(result);
     153            2 :         del(chunk);
     154              : 
     155              :         // Don't clean up test results to use on the next test
     156              : 
     157            2 :         RETURN_STATUS;
     158              : }
     159              : 
     160              : /**
     161              :  * The Example 3 from README
     162              :  * Using the --silent mode. When this mode is enabled, the program does not display
     163              :  * anything on the screen. This makes sense when using the program inside scripts.
     164              :  * Let's add the --silent option to the previous example:
     165              :  *
     166              :  * precizer --silent --update --progress --database=database1.db tests/examples/diffs/diff1
     167              :  *
     168              :  *
     169              :  */
     170            2 : static Return test0011_3_readme(void)
     171              : {
     172            2 :         INITTEST;
     173              : 
     174              :         // Create memory for the result
     175            2 :         create(char,result);
     176              : 
     177            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
     178              : 
     179            2 :         ASSERT(SUCCESS == runit("--silent --update --progress --database=database1.db tests/examples/diffs/diff1",result,COMPLETED,ALLOW_BOTH));
     180              : 
     181              :         // Verify that silent mode produced no stdout after command execution
     182            2 :         if(result->length > 0)
     183              :         {
     184            0 :                 echo(STDERR,"ERROR: In silent mode stdout must be empty\n");
     185            0 :                 echo(STDERR,YELLOW "Output:\n>>" RESET "%s" YELLOW "<<\n" RESET,getcstring(result));
     186            0 :                 status = FAILURE;
     187              :         }
     188              : 
     189            2 :         call(del(result));
     190              : 
     191            2 :         create(char,pattern);
     192              : 
     193            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     194              : 
     195            2 :         const char *filename = "templates/0011_003.txt";
     196              : 
     197            2 :         ASSERT(SUCCESS == runit("--silent --update --progress --database=database1.db tests/examples/diffs/diff1",result,COMPLETED,ALLOW_BOTH));
     198            2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     199            2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     200              : 
     201            2 :         del(pattern);
     202            2 :         del(result);
     203              : 
     204            2 :         RETURN_STATUS;
     205              : }
     206              : 
     207              : /**
     208              :  *
     209              :  * The Example 4 from README
     210              :  * Additional information with --verbose mode
     211              :  *
     212              :  */
     213            2 : static Return test0011_4_readme(void)
     214              : {
     215            2 :         INITTEST;
     216              : 
     217              :         // Create memory for the result
     218            2 :         create(char,result);
     219              : 
     220            2 :         create(char,pattern);
     221              : 
     222            2 :         const char *filename = "templates/0011_004_1.txt";
     223              : 
     224            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
     225              : 
     226            2 :         ASSERT(SUCCESS == runit("--verbose --update --progress --database=database1.db tests/examples/diffs/diff1",result,COMPLETED,ALLOW_BOTH));
     227            2 :         ASSERT(SUCCESS == get_file_content(filename,pattern));
     228            2 :         ASSERT(SUCCESS == match_pattern(result,pattern,filename));
     229              : 
     230              :         // Clean up test results
     231            2 :         del(pattern);
     232            2 :         del(result);
     233              : 
     234            2 :         ASSERT(SUCCESS == external_call("cd ${TMPDIR} && "
     235              :                 "rm database1.db && "
     236              :                 "rm -rf tests/examples/ && "
     237              :                 "mv tests/examples_backup/ tests/examples/",COMPLETED,ALLOW_BOTH));
     238              : 
     239            2 :         RETURN_STATUS;
     240              : }
     241              : 
     242              : /**
     243              :  *
     244              :  * The Example 5 from README
     245              :  * Disable recursion with --maxdepth=0 option
     246              :  *
     247              :  *
     248              :  */
     249            2 : static Return test0011_5_readme(void)
     250              : {
     251            2 :         INITTEST;
     252              : 
     253            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     254              : 
     255            2 :         const char *arguments = "--maxdepth=0 tests/examples/4";
     256              : 
     257            2 :         const char *filename = "templates/0011_005_1.txt";
     258              : 
     259            2 :         const char *template = "%DB_NAME%";
     260              : 
     261            2 :         const char *replacement = getenv("DBNAME");  // Database name
     262              : 
     263            2 :         ASSERT(replacement != NULL);
     264              : 
     265            2 :         ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
     266              : 
     267              :         /* At the second stage, the --maxdepth=0 option is not used.
     268              :            Therefore, all files that were not previously included
     269              :            will be added to the database. */
     270              : 
     271            2 :         arguments = "--update tests/examples/4";
     272              : 
     273            2 :         filename = "templates/0011_005_2.txt";
     274              : 
     275            2 :         ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
     276              : 
     277            2 :         ASSERT(SUCCESS == external_call("rm \"${TMPDIR}/${DBNAME}\"",COMPLETED,ALLOW_BOTH));
     278              : 
     279            2 :         RETURN_STATUS;
     280              : }
     281              : 
     282              : /**
     283              :  *
     284              :  * The Example 6 from README
     285              :  * Relative path to ignore with --ignore
     286              :  *
     287              :  *
     288              :  */
     289            2 : static Return test0011_6_readme(void)
     290              : {
     291            2 :         INITTEST;
     292              : 
     293            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
     294              : 
     295            2 :         const char *arguments = "--ignore=\"^diff1/1/.*\" tests/examples/diffs";
     296              : 
     297            2 :         const char *filename = "templates/0011_006_1.txt";
     298              : 
     299            2 :         const char *template = "%DB_NAME%";
     300              : 
     301            2 :         const char *replacement = getenv("DBNAME");  // Database name
     302              : 
     303            2 :         ASSERT(replacement != NULL);
     304              : 
     305            2 :         ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
     306              : 
     307            2 :         filename = "templates/0011_006_2.txt";
     308              : 
     309            2 :         arguments = "--update tests/examples/diffs";
     310              : 
     311            2 :         ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
     312              : 
     313            2 :         RETURN_STATUS;
     314              : }
     315              : 
     316              : /**
     317              :  *
     318              :  * The Example 7 from README
     319              :  * Multiple regular expressions for ignoring can be specified
     320              :  * using many --ignore options
     321              :  *
     322              :  */
     323            2 : static Return test0011_7_readme(void)
     324              : {
     325            2 :         INITTEST;
     326              : 
     327            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
     328              : 
     329            2 :         const char *arguments = "--update --db-clean-ignored"
     330              :                 " --ignore=\"^diff1/1/.*\""
     331              :                 " --ignore=\"^diff2/1/.*\""
     332              :                 " tests/examples/diffs";
     333              : 
     334            2 :         const char *filename = "templates/0011_007.txt";
     335              : 
     336            2 :         const char *template = "%DB_NAME%";
     337              : 
     338            2 :         const char *replacement = getenv("DBNAME");  // Database name
     339              : 
     340            2 :         ASSERT(replacement != NULL);
     341              : 
     342            2 :         ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
     343              : 
     344            2 :         ASSERT(SUCCESS == external_call("rm \"${TMPDIR}/${DBNAME}\"",COMPLETED,ALLOW_BOTH));
     345              : 
     346            2 :         RETURN_STATUS;
     347              : }
     348              : 
     349              : /**
     350              :  *
     351              :  * The Example 8 from README
     352              :  * Using the --ignore option(s) together with --include
     353              :  *
     354              :  *
     355              :  */
     356            2 : static Return test0011_8_readme(void)
     357              : {
     358            2 :         INITTEST;
     359              : 
     360            2 :         const char *arguments = NULL;
     361              : 
     362            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","true"));
     363              : 
     364            2 :         ASSERT(SUCCESS == runit("tests/examples/diffs",NULL,COMPLETED,ALLOW_BOTH));
     365              : 
     366            2 :         ASSERT(SUCCESS == set_environment_variable("TESTING","false"));
     367              : 
     368            2 :         arguments = "--update"
     369              :                 " --db-clean-ignored"
     370              :                 " --ignore=\"^.*/path2/.*\""
     371              :                 " --ignore=\"^diff2/.*\""
     372              :                 " --include=\"^diff2/1/AAA/ZAW/A/b/c/.*\""
     373              :                 " --include=\"^diff2/path1/AAA/ZAW/.*\""
     374              :                 " tests/examples/diffs";
     375              : 
     376            2 :         const char *filename = "templates/0011_008.txt";
     377              : 
     378            2 :         const char *template = "%DB_NAME%";
     379              : 
     380            2 :         const char *replacement = getenv("DBNAME");  // Database name
     381              : 
     382            2 :         ASSERT(replacement != NULL);
     383              : 
     384            2 :         ASSERT(SUCCESS == match_app_output(arguments,filename,template,replacement,COMPLETED));
     385              : 
     386            2 :         ASSERT(SUCCESS == external_call("rm \"${TMPDIR}/${DBNAME}\"",COMPLETED,ALLOW_BOTH));
     387              : 
     388            2 :         RETURN_STATUS;
     389              : }
     390              : 
     391              : /**
     392              :  *
     393              :  * User's Manual and examples from README test set
     394              :  *
     395              :  */
     396            2 : Return test0011(void)
     397              : {
     398              :         /// The status that will be passed to return() before exiting.
     399              :         /// By default, the function worked without errors.
     400            2 :         INITTEST;
     401              : 
     402            2 :         TEST(test0011_1_readme,"README example 1 Adding and comparing…");
     403            2 :         TEST(test0011_2_readme,"README example 2 Updating the data in DB…");
     404            2 :         TEST(test0011_3_readme,"README example 3 --silent mode…");
     405            2 :         TEST(test0011_4_readme,"README example 4 --verbose mode…");
     406            2 :         TEST(test0011_5_readme,"README example 5 Disable recursion with --maxdepth…");
     407            2 :         TEST(test0011_6_readme,"README example 6 Relative path to ignore with --ignore…");
     408            2 :         TEST(test0011_7_readme,"README example 7 Multiple regexp for ignoring…");
     409            2 :         TEST(test0011_8_readme,"README example 8 The --ignore option(s) together with --include…");
     410              : 
     411            2 :         RETURN_STATUS;
     412              : }
        

Generated by: LCOV version 2.0-1