|
| 1 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 2 | +// See end of file for extended copyright information. |
| 3 | + |
| 4 | +#include <cerrno> |
| 5 | +#include <cstring> |
| 6 | +#include <gtest/gtest.h> |
| 7 | +#include <quick-lint-js/file-matcher.h> |
| 8 | +#include <quick-lint-js/file.h> |
| 9 | +#include <quick-lint-js/temporary-directory.h> |
| 10 | +#include <string> |
| 11 | + |
| 12 | +#if QLJS_HAVE_UNISTD_H |
| 13 | +#include <unistd.h> |
| 14 | +#endif |
| 15 | + |
| 16 | +namespace quick_lint_js { |
| 17 | +namespace { |
| 18 | +#if QLJS_HAVE_UNISTD_H |
| 19 | +TEST(test_temporary_directory, delete_directory_containing_unwritable_file) { |
| 20 | + std::string temp_dir = make_temporary_directory(); |
| 21 | + std::string sub_dir = temp_dir + "/subdir"; |
| 22 | + create_directory(sub_dir); |
| 23 | + std::string unwritable_file = sub_dir + "/unwritable"; |
| 24 | + write_file(unwritable_file, u8"unwritable file"); |
| 25 | + EXPECT_EQ(::chmod(unwritable_file.c_str(), 0000), 0) |
| 26 | + << "failed to make " << unwritable_file |
| 27 | + << " inaccessible: " << std::strerror(errno); |
| 28 | + |
| 29 | + delete_directory_recursive(sub_dir); |
| 30 | + |
| 31 | + EXPECT_FILE_DOES_NOT_EXIST(unwritable_file); |
| 32 | + EXPECT_FILE_DOES_NOT_EXIST(sub_dir); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(test_temporary_directory, |
| 36 | + delete_directory_containing_non_empty_untraversable_directory) { |
| 37 | + std::string temp_dir = make_temporary_directory(); |
| 38 | + std::string sub_dir = temp_dir + "/sub_dir"; |
| 39 | + create_directory(sub_dir); |
| 40 | + std::string untraversable_dir = sub_dir + "/untraversable_dir"; |
| 41 | + create_directory(untraversable_dir); |
| 42 | + std::string unfindable_file = untraversable_dir + "/unfindable_file"; |
| 43 | + write_file(unfindable_file, u8"can't see me!"); |
| 44 | + EXPECT_EQ(::chmod(untraversable_dir.c_str(), 0600), 0) |
| 45 | + << "failed to make " << untraversable_dir |
| 46 | + << " untraversable: " << std::strerror(errno); |
| 47 | + |
| 48 | + delete_directory_recursive(sub_dir); |
| 49 | + |
| 50 | + EXPECT_FILE_DOES_NOT_EXIST(unfindable_file); |
| 51 | + EXPECT_FILE_DOES_NOT_EXIST(untraversable_dir); |
| 52 | + EXPECT_FILE_DOES_NOT_EXIST(sub_dir); |
| 53 | +} |
| 54 | +#endif |
| 55 | +} |
| 56 | +} |
| 57 | + |
| 58 | +// quick-lint-js finds bugs in JavaScript programs. |
| 59 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 60 | +// |
| 61 | +// This file is part of quick-lint-js. |
| 62 | +// |
| 63 | +// quick-lint-js is free software: you can redistribute it and/or modify |
| 64 | +// it under the terms of the GNU General Public License as published by |
| 65 | +// the Free Software Foundation, either version 3 of the License, or |
| 66 | +// (at your option) any later version. |
| 67 | +// |
| 68 | +// quick-lint-js is distributed in the hope that it will be useful, |
| 69 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 70 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 71 | +// GNU General Public License for more details. |
| 72 | +// |
| 73 | +// You should have received a copy of the GNU General Public License |
| 74 | +// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>. |
0 commit comments