LCOV - code coverage report
Current view: top level - tests - catch_file_contents.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 66 66 100.0 %
Date: 2023-05-29 16:11:08 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2018-2023  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/snapdev
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software: you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation, either version 3 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License
      17             : // along with this program.  If not, see <https://www.gnu.org/licenses/>.
      18             : 
      19             : /** \file
      20             :  * \brief Verify that the file_contents class works.
      21             :  *
      22             :  * This file implements tests for the file_content class.
      23             :  */
      24             : 
      25             : // self
      26             : //
      27             : #include    <snapdev/file_contents.h>
      28             : 
      29             : #include    "catch_main.h"
      30             : 
      31             : 
      32             : // last include
      33             : //
      34             : #include    <snapdev/poison.h>
      35             : 
      36             : 
      37             : 
      38             : 
      39           4 : CATCH_TEST_CASE("file_contents", "[os]")
      40             : {
      41           4 :     CATCH_START_SECTION("file_contents: write -> read contents")
      42             :     {
      43             :         // try to create file, but directory is missing
      44             :         {
      45           2 :             std::string const content("Open fails in this case\n");
      46           1 :             std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/inexcistant-directory/test-file.txt");
      47             : 
      48             :             {
      49           1 :                 snapdev::file_contents content_test_output(filename, false);
      50           1 :                 content_test_output.contents(content);
      51           1 :                 CATCH_REQUIRE_FALSE(content_test_output.write_all());
      52           1 :                 CATCH_REQUIRE(content_test_output.last_error() == "could not open file \""
      53             :                                                                       + filename
      54             :                                                                       + "\" for writing.");
      55           1 :             }
      56             : 
      57             :             {
      58           1 :                 snapdev::file_contents content_test_input(filename);
      59           1 :                 CATCH_REQUIRE_FALSE(content_test_input.read_all());
      60           1 :                 CATCH_REQUIRE_FALSE(content_test_input.contents() == content);
      61           1 :             }
      62           1 :         }
      63             : 
      64             :         // create directory & file
      65             :         {
      66           2 :             std::string const content("We're testing a write...\nand then a read\n");
      67           1 :             std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents/test-file.txt");
      68             : 
      69             :             {
      70           1 :                 snapdev::file_contents content_test_output(filename, true);
      71           1 :                 content_test_output.contents(content);
      72           1 :                 CATCH_REQUIRE(content_test_output.write_all());
      73           1 :             }
      74             : 
      75             :             {
      76           1 :                 snapdev::file_contents content_test_input(filename);
      77           1 :                 CATCH_REQUIRE(content_test_input.read_all());
      78           1 :                 CATCH_REQUIRE(static_cast<snapdev::file_contents const &>(content_test_input).contents() == content);
      79           1 :             }
      80           1 :         }
      81             : 
      82             :         // check that if the directory exists, it works as expected
      83             :         {
      84           2 :             std::string const content("Try again without the create directory and it still works\n");
      85           1 :             std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents/dir-exists.txt");
      86             : 
      87             :             {
      88           1 :                 snapdev::file_contents content_test_output(filename, false);
      89           1 :                 content_test_output.contents(content);
      90           1 :                 CATCH_REQUIRE(content_test_output.write_all());
      91           1 :             }
      92             : 
      93             :             {
      94           1 :                 snapdev::file_contents content_test_input(filename);
      95           1 :                 CATCH_REQUIRE(content_test_input.read_all());
      96           1 :                 CATCH_REQUIRE(content_test_input.contents() == content);
      97           1 :             }
      98           1 :         }
      99             : 
     100             :         {
     101           1 :             std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents/dir-exists.txt/object.java");
     102             : 
     103           1 :             CATCH_REQUIRE_THROWS_MATCHES(
     104             :                       snapdev::file_contents(filename, true, true)
     105             :                     , std::ios::failure
     106             :                     , Catch::Matchers::ExceptionMessage(
     107             :                               "snapdev::file_contents: the full path to filename for a file_contents object could not be created: iostream error"));
     108           1 :         }
     109             : 
     110             :         // this one tries to delete the "contents" directory and that fails
     111             :         // since it's a directory (and it's not even empty)
     112             :         {
     113           1 :             std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents");
     114           1 :             snapdev::file_contents content_test_output(filename, false, true);
     115           1 :         }
     116             :     }
     117           4 :     CATCH_END_SECTION()
     118             : 
     119           4 :     CATCH_START_SECTION("file_contents: write temporary -> read fails")
     120             :     {
     121           2 :         std::string const content("We're testing a write...\nand then a read but the file is gone!\n");
     122           1 :         std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir() + "/contents/temporary-file.txt");
     123             : 
     124             :         {
     125           1 :             snapdev::file_contents content_test_output(filename, true, true);
     126           1 :             content_test_output.contents(content);
     127           1 :             CATCH_REQUIRE(content_test_output.write_all());
     128           1 :         }
     129             : 
     130             :         {
     131           1 :             snapdev::file_contents content_test_input(filename);
     132           1 :             CATCH_REQUIRE_FALSE(content_test_input.read_all());
     133           1 :             CATCH_REQUIRE(content_test_input.contents() == std::string());
     134           1 :         }
     135           1 :     }
     136           4 :     CATCH_END_SECTION()
     137             : 
     138           4 :     CATCH_START_SECTION("file_contents: empty filename is not accepted")
     139             :     {
     140           3 :         CATCH_REQUIRE_THROWS_MATCHES(
     141             :                   snapdev::file_contents(std::string(), true, true)
     142             :                 , std::invalid_argument
     143             :                 , Catch::Matchers::ExceptionMessage(
     144             :                           "snapdev::file_contents: the filename of a file_contents object cannot be the empty string."));
     145             :     }
     146           4 :     CATCH_END_SECTION()
     147             : 
     148           4 :     CATCH_START_SECTION("file_contents: read from /proc/self/cmd")
     149             :     {
     150           3 :         snapdev::file_contents comm("/proc/self/comm");
     151           1 :         CATCH_REQUIRE(comm.read_all());
     152           1 :         CATCH_REQUIRE(static_cast<snapdev::file_contents const &>(comm).contents() == "unittest\n");
     153           1 :     }
     154           4 :     CATCH_END_SECTION()
     155           4 : }
     156             : 
     157             : 
     158             : 
     159             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.14