Line data Source code
1 : // Copyright (c) 2018-2021 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 2 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 along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : /** \file
21 : * \brief Verify that the file_contents class works.
22 : *
23 : * This file implements tests for the file_content class.
24 : */
25 :
26 : // self
27 : //
28 : #include <snapdev/file_contents.h>
29 :
30 : //#include <snapdev/user_groups.h>
31 : //#include <snapdev/join_strings.h>
32 : //#include <snapdev/ostream_to_buf.h>
33 : //#include <snapdev/tokenize_string.h>
34 :
35 : #include "catch_main.h"
36 :
37 :
38 : // last include
39 : //
40 : #include <snapdev/poison.h>
41 :
42 :
43 :
44 :
45 5 : CATCH_TEST_CASE("file_contents", "[os]")
46 : {
47 6 : CATCH_START_SECTION("file_contents: write -> read contents")
48 : {
49 : // try to create file, but directory is missing
50 : {
51 2 : std::string const content("Open fails in this case\n");
52 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/inexcistant-directory/test-file.txt");
53 :
54 : {
55 2 : snap::file_contents content_test_output(filename, false);
56 1 : content_test_output.contents(content);
57 1 : CATCH_REQUIRE_FALSE(content_test_output.write_all());
58 1 : CATCH_REQUIRE(content_test_output.last_error() == "could not open file \""
59 : + filename
60 : + "\" for writing.");
61 : }
62 :
63 : {
64 2 : snap::file_contents content_test_input(filename);
65 1 : CATCH_REQUIRE_FALSE(content_test_input.read_all());
66 1 : CATCH_REQUIRE_FALSE(content_test_input.contents() == content);
67 : }
68 : }
69 :
70 : // create directory & file
71 : {
72 2 : std::string const content("We're testing a write...\nand then a read\n");
73 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/contents/test-file.txt");
74 :
75 : {
76 2 : snap::file_contents content_test_output(filename, true);
77 1 : content_test_output.contents(content);
78 1 : CATCH_REQUIRE(content_test_output.write_all());
79 : }
80 :
81 : {
82 2 : snap::file_contents content_test_input(filename);
83 1 : CATCH_REQUIRE(content_test_input.read_all());
84 1 : CATCH_REQUIRE(static_cast<snap::file_contents const &>(content_test_input).contents() == content);
85 : }
86 : }
87 :
88 : // check that if the directory exists, it works as expected
89 : {
90 2 : std::string const content("Try again without the create directory and it still works\n");
91 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/contents/dir-exists.txt");
92 :
93 : {
94 2 : snap::file_contents content_test_output(filename, false);
95 1 : content_test_output.contents(content);
96 1 : CATCH_REQUIRE(content_test_output.write_all());
97 : }
98 :
99 : {
100 2 : snap::file_contents content_test_input(filename);
101 1 : CATCH_REQUIRE(content_test_input.read_all());
102 1 : CATCH_REQUIRE(content_test_input.contents() == content);
103 : }
104 : }
105 :
106 : {
107 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/contents/dir-exists.txt/object.java");
108 :
109 1 : CATCH_REQUIRE_THROWS_MATCHES(
110 : snap::file_contents(filename, true, true)
111 : , std::ios::failure
112 : , Catch::Matchers::ExceptionMessage(
113 : "snapdev::file_contents: the full path to filename for a file_contents object could not be created: iostream error"));
114 : }
115 :
116 : // this one tries to delete the "contents" directory and that fails
117 : // since it's a directory (and it's not even empty)
118 : {
119 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/contents");
120 1 : snap::file_contents content_test_output(filename, false, true);
121 : }
122 : }
123 : CATCH_END_SECTION()
124 :
125 6 : CATCH_START_SECTION("file_contents: write temporary -> read fails")
126 : {
127 2 : std::string const content("We're testing a write...\nand then a read but the file is gone!\n");
128 2 : std::string const filename(SNAP_CATCH2_NAMESPACE::g_tmp_dir + "/contents/temporary-file.txt");
129 :
130 : {
131 2 : snap::file_contents content_test_output(filename, true, true);
132 1 : content_test_output.contents(content);
133 1 : CATCH_REQUIRE(content_test_output.write_all());
134 : }
135 :
136 : {
137 2 : snap::file_contents content_test_input(filename);
138 1 : CATCH_REQUIRE_FALSE(content_test_input.read_all());
139 1 : CATCH_REQUIRE(content_test_input.contents() == std::string());
140 : }
141 : }
142 : CATCH_END_SECTION()
143 :
144 6 : CATCH_START_SECTION("file_contents: empty filename is not accepted")
145 : {
146 1 : CATCH_REQUIRE_THROWS_MATCHES(
147 : snap::file_contents(std::string(), true, true)
148 : , std::invalid_argument
149 : , Catch::Matchers::ExceptionMessage(
150 : "snapdev::file_contents: the filename of a file_contents object cannot be the empty string."));
151 : }
152 : CATCH_END_SECTION()
153 9 : }
154 :
155 :
156 :
157 : // vim: ts=4 sw=4 et
|