]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/experimental/filesystem/operations/temp_directory_path.cc
PR libstdc++/78870 support std::filesystem on Windows
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / filesystem / operations / temp_directory_path.cc
CommitLineData
85ec4feb 1// Copyright (C) 2015-2018 Free Software Foundation, Inc.
9caf7b27
JW
2//
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)
7// any later version.
8
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.
13
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/>.
17
ef0e80d2 18// { dg-options "-DUSE_FILESYSTEM_TS -lstdc++fs" }
8152d6ef 19// { dg-do run { target c++11 } }
9caf7b27
JW
20// { dg-require-filesystem-ts "" }
21
22#include <experimental/filesystem>
23#include <stdlib.h>
24#include <testsuite_hooks.h>
25#include <testsuite_fs.h>
26
27void
28clean_env()
29{
9534a5e6
JW
30#if defined(__MINGW32__) || defined(__MINGW64__)
31 ::_putenv("TMP=");
32 ::_putenv("TEMP=");
33#else
9caf7b27
JW
34 ::unsetenv("TMPDIR");
35 ::unsetenv("TMP");
36 ::unsetenv("TEMPDIR");
37 ::unsetenv("TEMP");
9534a5e6
JW
38#endif
39}
40
41bool
42set_env(const char* name, std::string value)
43{
44#if defined(__MINGW32__) || defined(__MINGW64__)
45 std::string s = name;
46 s += '=';
47 s += value;
48 return !::_putenv(s.c_str());
49#else
50 return !::setenv(name, value.c_str(), 1);
51#endif
9caf7b27
JW
52}
53
54namespace fs = std::experimental::filesystem;
55
56void
57test01()
58{
59 clean_env();
60
61 if (!fs::exists("/tmp"))
62 return; // just give up
63
db62ad7c 64 std::error_code ec = make_error_code(std::errc::invalid_argument);
9caf7b27 65 fs::path p1 = fs::temp_directory_path(ec);
6daff2d9 66 VERIFY( !ec );
9caf7b27
JW
67 VERIFY( exists(p1) );
68
69 fs::path p2 = fs::temp_directory_path();
70 VERIFY( p1 == p2 );
71}
72
73void
74test02()
75{
76 clean_env();
77
9534a5e6 78 if (set_env("TMPDIR", __gnu_test::nonexistent_path().string()))
9caf7b27
JW
79 return; // just give up
80
81 std::error_code ec;
82 fs::path p = fs::temp_directory_path(ec);
83 VERIFY( ec );
6daff2d9 84 VERIFY( p == fs::path() );
9caf7b27
JW
85
86 std::error_code ec2;
87 try {
88 p = fs::temp_directory_path();
89 } catch (const fs::filesystem_error& e) {
90 ec2 = e.code();
91 }
92 VERIFY( ec2 == ec );
93}
94
6daff2d9
JW
95void
96test03()
97{
98 auto p = __gnu_test::nonexistent_path();
99 create_directories(p/"tmp");
100 permissions(p, fs::perms::none);
9534a5e6 101 set_env("TMPDIR", (p/"tmp").string());
6daff2d9
JW
102 std::error_code ec;
103 auto r = fs::temp_directory_path(ec); // libstdc++/PR71337
104 VERIFY( ec == std::make_error_code(std::errc::permission_denied) );
105 VERIFY( r == fs::path() );
106
107 std::error_code ec2;
108 try {
109 fs::temp_directory_path();
110 } catch (const fs::filesystem_error& e) {
111 ec2 = e.code();
112 }
113 VERIFY( ec2 == ec );
114
115 permissions(p, fs::perms::owner_all, ec);
116 remove_all(p, ec);
117}
118
119void
120test04()
121{
122 __gnu_test::scoped_file f;
9534a5e6 123 set_env("TMPDIR", f.path.string());
6daff2d9
JW
124 std::error_code ec;
125 auto r = fs::temp_directory_path(ec);
126 VERIFY( ec == std::make_error_code(std::errc::not_a_directory) );
127 VERIFY( r == fs::path() );
128
129 std::error_code ec2;
130 try {
131 fs::temp_directory_path();
132 } catch (const fs::filesystem_error& e) {
133 ec2 = e.code();
134 }
135 VERIFY( ec2 == ec );
136}
9caf7b27
JW
137
138int
139main()
140{
141 test01();
142 test02();
6daff2d9
JW
143 test03();
144 test04();
9caf7b27 145}