]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc
1 // Copyright (C) 2015-2024 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-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
19 // { dg-do run { target c++11 } }
20 // { dg-require-filesystem-ts "" }
22 #include <experimental/filesystem>
25 #include <testsuite_hooks.h>
26 #include <testsuite_fs.h>
31 #if defined(__MINGW32__) || defined(__MINGW64__)
37 ::unsetenv("TEMPDIR");
43 set_env(const char* name
, std::string value
)
45 #if defined(__MINGW32__) || defined(__MINGW64__)
49 return !::_putenv(s
.c_str());
51 return !::setenv(name
, value
.c_str(), 1);
55 namespace fs
= std::experimental::filesystem
;
62 if (!fs::exists("/tmp"))
64 puts("/tmp doesn't exist, not testing it for temp_directory_path");
65 return; // just give up
68 std::error_code ec
= make_error_code(std::errc::invalid_argument
);
69 fs::path p1
= fs::temp_directory_path(ec
);
73 fs::path p2
= fs::temp_directory_path();
82 if (!set_env("TMP", __gnu_test::nonexistent_path().string()))
84 puts("Cannot set environment variables, not testing temp_directory_path");
85 return; // just give up
89 fs::path p
= fs::temp_directory_path(ec
);
91 VERIFY( p
== fs::path() );
95 p
= fs::temp_directory_path();
96 } catch (const fs::filesystem_error
& e
) {
105 if (!__gnu_test::permissions_are_testable())
110 auto p
= __gnu_test::nonexistent_path();
111 create_directories(p
/"tmp");
112 permissions(p
, fs::perms::none
);
113 set_env("TMPDIR", (p
/"tmp").string());
115 auto r
= fs::temp_directory_path(ec
); // libstdc++/PR71337
116 VERIFY( ec
== std::make_error_code(std::errc::permission_denied
) );
117 VERIFY( r
== fs::path() );
121 (void) fs::temp_directory_path();
122 } catch (const fs::filesystem_error
& e
) {
127 permissions(p
, fs::perms::owner_all
, ec
);
136 __gnu_test::scoped_file f
;
137 set_env("TMP", f
.path
.string());
139 auto r
= fs::temp_directory_path(ec
);
140 VERIFY( ec
== std::make_error_code(std::errc::not_a_directory
) );
141 VERIFY( r
== fs::path() );
144 std::string failed_path
;
146 (void) fs::temp_directory_path();
147 } catch (const fs::filesystem_error
& e
) {
149 // On Windows the returned path will be in preferred form, i.e. using L'\\'
150 // and will have a trailing slash, so compare generic forms.
151 failed_path
= e
.path1().generic_string();
154 VERIFY( failed_path
.find(f
.path
.generic_string()) != std::string::npos
);