]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/filesystem/operations/temp_directory_path.cc
1 // Copyright (C) 2015-2023 Free Software Foundation, Inc.
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++17 } }
19 // { dg-require-filesystem-ts "" }
24 #include <testsuite_hooks.h>
25 #include <testsuite_fs.h>
30 #if defined(__MINGW32__) || defined(__MINGW64__)
36 ::unsetenv("TEMPDIR");
42 set_env(const char* name
, std::string value
)
44 #if defined(__MINGW32__) || defined(__MINGW64__)
48 return !::_putenv(s
.c_str());
50 return !::setenv(name
, value
.c_str(), 1);
54 namespace fs
= std::filesystem
;
61 if (!fs::exists("/tmp"))
63 puts("/tmp doesn't exist, not testing it for temp_directory_path");
64 return; // just give up
67 std::error_code ec
= make_error_code(std::errc::invalid_argument
);
68 fs::path p1
= fs::temp_directory_path(ec
);
72 fs::path p2
= fs::temp_directory_path();
81 if (!set_env("TMP", __gnu_test::nonexistent_path().string()))
83 puts("Cannot set environment variables, not testing temp_directory_path");
84 return; // just give up
88 fs::path p
= fs::temp_directory_path(ec
);
90 VERIFY( p
== fs::path() );
94 p
= fs::temp_directory_path();
95 } catch (const fs::filesystem_error
& e
) {
104 if (!__gnu_test::permissions_are_testable())
109 auto p
= __gnu_test::nonexistent_path();
110 create_directories(p
/"tmp");
111 permissions(p
, fs::perms::none
);
112 set_env("TMPDIR", (p
/"tmp").string());
114 auto r
= fs::temp_directory_path(ec
); // libstdc++/PR71337
115 VERIFY( ec
== std::make_error_code(std::errc::permission_denied
) );
116 VERIFY( r
== fs::path() );
120 (void) fs::temp_directory_path();
121 } catch (const fs::filesystem_error
& e
) {
126 permissions(p
, fs::perms::owner_all
, ec
);
135 __gnu_test::scoped_file f
;
136 set_env("TMP", f
.path
.string());
138 auto r
= fs::temp_directory_path(ec
);
139 VERIFY( ec
== std::make_error_code(std::errc::not_a_directory
) );
140 VERIFY( r
== fs::path() );
143 std::string failed_path
;
145 (void) fs::temp_directory_path();
146 } catch (const fs::filesystem_error
& e
) {
148 // On Windows the returned path will be in preferred form, i.e. using L'\\'
149 // and will have a trailing slash, so compare generic forms.
150 failed_path
= e
.path1().generic_string();
153 VERIFY( failed_path
.find(f
.path
.generic_string()) != std::string::npos
);