LCOV - code coverage report
Current view: top level - tests - main.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 42 56 75.0 %
Date: 2019-07-15 03:11:49 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * File:
       3             :  *    tests/main.cpp
       4             :  *
       5             :  * License:
       6             :  *    Copyright (c) 2006-2019  Made to Order Software Corp.  All Rights Reserved
       7             :  *
       8             :  *    https://snapwebsites.org/
       9             :  *    contact@m2osw.com
      10             :  *
      11             :  *    This program is free software; you can redistribute it and/or modify
      12             :  *    it under the terms of the GNU General Public License as published by
      13             :  *    the Free Software Foundation; either version 2 of the License, or
      14             :  *    (at your option) any later version.
      15             :  *
      16             :  *    This program is distributed in the hope that it will be useful,
      17             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :  *    GNU General Public License for more details.
      20             :  *
      21             :  *    You should have received a copy of the GNU General Public License along
      22             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      23             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      24             :  *
      25             :  * Authors:
      26             :  *    Alexis Wilke   alexis@m2osw.com
      27             :  */
      28             : 
      29             : // Tell catch we want it to add the runner code in this file.
      30             : #define CATCH_CONFIG_RUNNER
      31             : 
      32             : // self
      33             : //
      34             : #include "main.h"
      35             : 
      36             : // advgetopt lib
      37             : //
      38             : #include <advgetopt/advgetopt.h>
      39             : #include <advgetopt/version.h>
      40             : 
      41             : // libexcept lib
      42             : //
      43             : #include <libexcept/exception.h>
      44             : 
      45             : // snapdev lib
      46             : //
      47             : #include <snapdev/not_used.h>
      48             : 
      49             : // C++ lib
      50             : //
      51             : #include <sstream>
      52             : 
      53             : 
      54             : namespace SNAP_CATCH2_NAMESPACE
      55             : {
      56             : 
      57             : 
      58             : 
      59           2 : std::string                 g_tmp_dir;
      60             : 
      61           2 : std::string                 g_config_filename;
      62           2 : std::string                 g_config_project_filename;
      63             : 
      64             : 
      65         176 : void init_tmp_dir(std::string const & project_name, std::string const & prefname, bool dir)
      66             : {
      67         352 :     std::string tmpdir(SNAP_CATCH2_NAMESPACE::g_tmp_dir);
      68         176 :     tmpdir += "/.config";
      69         352 :     std::stringstream ss;
      70         176 :     if(dir)
      71             :     {
      72           2 :         ss << "mkdir -p " << tmpdir << "/" << prefname << "/" << project_name << ".d";
      73             :     }
      74             :     else
      75             :     {
      76         174 :         ss << "mkdir -p " << tmpdir << "/" << project_name << ".d";
      77             :     }
      78         176 :     if(system(ss.str().c_str()) != 0)
      79             :     {
      80           0 :         std::cerr << "fatal error: creating sub-temporary directory \"" << ss.str() << "\" failed.\n";
      81           0 :         exit(1);
      82             :     }
      83         176 :     if(dir)
      84             :     {
      85           2 :         g_config_filename = tmpdir + "/" + prefname;
      86           2 :         g_config_project_filename = tmpdir + "/" + prefname + "/" + project_name + ".d";
      87             :     }
      88             :     else
      89             :     {
      90         174 :         g_config_filename = tmpdir + "/" + prefname + ".config";
      91         174 :         g_config_project_filename = tmpdir + "/" + project_name + ".d/" + prefname + ".config";
      92             :     }
      93         176 : }
      94             : 
      95             : 
      96             : 
      97             : }
      98             : // SNAP_CATCH2_NAMESPACE namespace
      99             : 
     100             : 
     101             : 
     102             : 
     103             : namespace
     104             : {
     105             : 
     106             : 
     107             : 
     108           2 : Catch::clara::Parser add_command_line_options(Catch::clara::Parser const & cli)
     109             : {
     110             :     return cli
     111             :          | Catch::clara::Opt(SNAP_CATCH2_NAMESPACE::g_tmp_dir, "tmp")
     112           4 :               ["-T"]["--tmp"]
     113           6 :               ("a path to a temporary directory used by the tests.");
     114             : }
     115             : 
     116             : 
     117           1 : int finish_init(Catch::Session & session)
     118             : {
     119           1 :     snap::NOTUSED(session);
     120             : 
     121           1 :     if(!SNAP_CATCH2_NAMESPACE::g_tmp_dir.empty())
     122             :     {
     123           1 :         if(SNAP_CATCH2_NAMESPACE::g_tmp_dir == "/tmp")
     124             :         {
     125           0 :             std::cerr << "fatal error: you must specify a sub-directory for your temporary directory such as /tmp/advgetopt";
     126           0 :             exit(1);
     127             :         }
     128             :     }
     129             :     else
     130             :     {
     131           0 :         SNAP_CATCH2_NAMESPACE::g_tmp_dir = "/tmp/advgetopt";
     132             :     }
     133             : 
     134             :     // delete the existing tmp directory
     135             :     {
     136           2 :         std::stringstream ss;
     137           1 :         ss << "rm -rf \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\"";
     138           1 :         if(system(ss.str().c_str()) != 0)
     139             :         {
     140           0 :             std::cerr << "fatal error: could not delete temporary directory \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\".";
     141           0 :             exit(1);
     142             :         }
     143             :     }
     144             : 
     145             :     // then re-create the directory
     146             :     {
     147           2 :         std::stringstream ss;
     148           1 :         ss << "mkdir -p \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\"";
     149           1 :         if(system(ss.str().c_str()) != 0)
     150             :         {
     151           0 :             std::cerr << "fatal error: could not create temporary directory \"" << SNAP_CATCH2_NAMESPACE::g_tmp_dir << "\".";
     152           0 :             exit(1);
     153             :         }
     154             :     }
     155             : 
     156           1 :     advgetopt::set_log_callback(SNAP_CATCH2_NAMESPACE::log_for_test);
     157             : 
     158           1 :     char const * options(getenv("ADVGETOPT_TEST_OPTIONS"));
     159           1 :     if(options != nullptr
     160           0 :     && *options != '\0')
     161             :     {
     162           0 :         std::cerr << std::endl
     163             :                   << "error:unittest: ADVGETOPT_TEST_OPTIONS already exists,"
     164             :                         " the advgetopt tests would not work as expected with such."
     165           0 :                      " Please unset that environment variable and try again."
     166           0 :                   << std::endl;
     167           0 :         throw std::runtime_error("ADVGETOPT_TEST_OPTIONS already exists");
     168             :     }
     169             : 
     170           1 :     return 0;
     171             : }
     172             : 
     173             : 
     174           1 : void tests_done()
     175             : {
     176           1 :     SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
     177           1 : }
     178             : 
     179             : 
     180             : 
     181             : }
     182             : // no name namespace
     183             : 
     184             : 
     185             : 
     186             : 
     187             : 
     188             : 
     189           2 : int main(int argc, char * argv[])
     190             : {
     191           4 :     return SNAP_CATCH2_NAMESPACE::snap_catch2_main(
     192             :               "advgetopt"
     193             :             , LIBADVGETOPT_VERSION_STRING
     194             :             , argc
     195             :             , argv
     196           6 :             , []() { libexcept::set_collect_stack(false); }
     197             :             , &add_command_line_options
     198             :             , &finish_init
     199             :             , &tests_done
     200           2 :         );
     201           6 : }
     202             : 
     203             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12