1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/fs/auto_cleaners.hpp"
35 #include <atf-c++.hpp>
37 #include "utils/env.hpp"
38 #include "utils/fs/operations.hpp"
40 namespace fs
= utils::fs
;
43 ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__automatic
);
44 ATF_TEST_CASE_BODY(auto_directory__automatic
)
46 const fs::path
root("root");
47 fs::mkdir(root
, 0755);
50 fs::auto_directory
dir(root
);
51 ATF_REQUIRE_EQ(root
, dir
.directory());
53 ATF_REQUIRE(::access("root", X_OK
) == 0);
56 fs::auto_directory
dir_copy(dir
);
58 // Should still exist after a copy is destructed.
59 ATF_REQUIRE(::access("root", X_OK
) == 0);
61 ATF_REQUIRE(::access("root", X_OK
) == -1);
65 ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__explicit
);
66 ATF_TEST_CASE_BODY(auto_directory__explicit
)
68 const fs::path
root("root");
69 fs::mkdir(root
, 0755);
71 fs::auto_directory
dir(root
);
72 ATF_REQUIRE_EQ(root
, dir
.directory());
74 ATF_REQUIRE(::access("root", X_OK
) == 0);
77 ATF_REQUIRE(::access("root", X_OK
) == -1);
81 ATF_TEST_CASE_WITHOUT_HEAD(auto_directory__mkdtemp
);
82 ATF_TEST_CASE_BODY(auto_directory__mkdtemp
)
84 utils::setenv("TMPDIR", (fs::current_path() / "tmp").str());
85 fs::mkdir(fs::path("tmp"), 0755);
87 const std::string
path_template("test.XXXXXX");
89 fs::auto_directory auto_directory
= fs::auto_directory::mkdtemp(
91 ATF_REQUIRE(::access((fs::path("tmp") / path_template
).c_str(),
93 ATF_REQUIRE(::rmdir("tmp") == -1);
95 ATF_REQUIRE(::access(auto_directory
.directory().c_str(), X_OK
) == 0);
97 ATF_REQUIRE(::rmdir("tmp") != -1);
101 ATF_TEST_CASE_WITHOUT_HEAD(auto_file__automatic
);
102 ATF_TEST_CASE_BODY(auto_file__automatic
)
104 const fs::path
file("foo");
105 atf::utils::create_file(file
.str(), "");
107 fs::auto_file
auto_file(file
);
108 ATF_REQUIRE_EQ(file
, auto_file
.file());
110 ATF_REQUIRE(::access(file
.c_str(), R_OK
) == 0);
113 fs::auto_file
auto_file_copy(auto_file
);
115 // Should still exist after a copy is destructed.
116 ATF_REQUIRE(::access(file
.c_str(), R_OK
) == 0);
118 ATF_REQUIRE(::access(file
.c_str(), R_OK
) == -1);
122 ATF_TEST_CASE_WITHOUT_HEAD(auto_file__explicit
);
123 ATF_TEST_CASE_BODY(auto_file__explicit
)
125 const fs::path
file("bar");
126 atf::utils::create_file(file
.str(), "");
128 fs::auto_file
auto_file(file
);
129 ATF_REQUIRE_EQ(file
, auto_file
.file());
131 ATF_REQUIRE(::access(file
.c_str(), R_OK
) == 0);
134 ATF_REQUIRE(::access(file
.c_str(), R_OK
) == -1);
138 ATF_TEST_CASE_WITHOUT_HEAD(auto_file__mkstemp
);
139 ATF_TEST_CASE_BODY(auto_file__mkstemp
)
141 utils::setenv("TMPDIR", (fs::current_path() / "tmp").str());
142 fs::mkdir(fs::path("tmp"), 0755);
144 const std::string
path_template("test.XXXXXX");
146 fs::auto_file auto_file
= fs::auto_file::mkstemp(path_template
);
147 ATF_REQUIRE(::access((fs::path("tmp") / path_template
).c_str(),
149 ATF_REQUIRE(::rmdir("tmp") == -1);
151 ATF_REQUIRE(::access(auto_file
.file().c_str(), R_OK
) == 0);
153 ATF_REQUIRE(::rmdir("tmp") != -1);
157 ATF_INIT_TEST_CASES(tcs
)
159 ATF_ADD_TEST_CASE(tcs
, auto_directory__automatic
);
160 ATF_ADD_TEST_CASE(tcs
, auto_directory__explicit
);
161 ATF_ADD_TEST_CASE(tcs
, auto_directory__mkdtemp
);
163 ATF_ADD_TEST_CASE(tcs
, auto_file__automatic
);
164 ATF_ADD_TEST_CASE(tcs
, auto_file__explicit
);
165 ATF_ADD_TEST_CASE(tcs
, auto_file__mkstemp
);