File: test_filesystem_helper_std.cpp

package info (click to toggle)
ros2-rcpputils 2.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: cpp: 5,160; xml: 31; ansic: 20; makefile: 4
file content (499 lines) | stat: -rw-r--r-- 13,995 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright 2024 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gtest/gtest.h>

#include <fstream>
#include <string>

#include "rcpputils/filesystem_helper.hpp"
#include "rcpputils/env.hpp"

#ifdef _WIN32
static constexpr const bool is_win32 = true;
#else
static constexpr const bool is_win32 = false;
#endif

using path = std::filesystem::path;

static const std::string build_extension_path()
{
  return is_win32 ? R"(C:\foo\bar\baz.yml)" : "/bar/foo/baz.yml";
}

static const std::string build_double_extension_path()
{
  return is_win32 ? R"(C:\bar\baz.bar.yml)" : "/foo/baz.bar.yml";
}

static const std::string build_no_extension_path()
{
  return is_win32 ? R"(.\test_folder)" : R"(./test_folder)";
}

static const std::string build_directory_path()
{
  return is_win32 ? R"(.\test_folder)" : R"(./test_folder)";
}

TEST(TestFilesystemHelper, join_path)
{
  {
    auto p = path("foo") / path("bar");
    if (is_win32) {
      EXPECT_EQ("foo\\bar", p.string());
    } else {
      EXPECT_EQ("foo/bar", p.string());
    }
  }

  if (is_win32) {
    auto p = path("foo") / path("C:\\bar");
    EXPECT_EQ("C:\\bar", p.string());
  } else {
    auto p = path("foo") / path("/bar");
    EXPECT_EQ("/bar", p.string());
  }

  {
    // Just expect these join operations to be allowed with const paths
    const auto p1 = path("foo");
    auto p1_baz = p1 / "baz";

    const auto p2 = path("bar");
    auto p1_2 = p1 / p2;
  }
}

TEST(TestFilesystemHelper, parent_path)
{
  {
    auto p = path("my") / path("path");
    EXPECT_EQ(p.parent_path().string(), path("my").string());
  }
  {
    auto p = path("foo");
    EXPECT_EQ(p.parent_path().string(), "");
  }
  if (is_win32) {
    {
      auto p = path("C:\\foo");
      EXPECT_EQ(p.parent_path().string(), "C:\\");
    }
    {
      auto p = path("\\foo");
      EXPECT_EQ(p.parent_path().string(), "\\");
    }
    {
      auto p = path("C:\\");
      EXPECT_EQ(p.parent_path().string(), "C:\\");
    }
    {
      auto p = path("\\");
      EXPECT_EQ(p.parent_path().string(), "\\");
    }
  } else {
    {
      auto p = path("/foo");
      EXPECT_EQ(p.parent_path().string(), "/");
    }
    {
      auto p = path("/");
      EXPECT_EQ(p.parent_path().string(), "/");
    }
  }
  {
    auto p = path("");
    EXPECT_EQ(p.parent_path().string(), "");
  }
}

TEST(TestFilesystemHelper, to_native_path)
{
  {
    auto p = path("/foo/bar/baz");
    EXPECT_EQ("/foo/bar/baz", p.string());
  }
  {
    auto p = path("\\foo\\bar\\baz");
    if (is_win32) {
      EXPECT_EQ("\\foo\\bar\\baz", p.string());
    } else {
      EXPECT_EQ("\\foo\\bar\\baz", p.string());
    }
  }
  {
    auto p = path("/foo//bar/baz");
    EXPECT_EQ("/foo//bar/baz", p.string());
  }
  {
    auto p = path("\\foo\\\\bar\\baz");
    if (is_win32) {
      EXPECT_EQ("\\foo\\\\bar\\baz", p.string());
    } else {
      EXPECT_EQ("\\foo\\\\bar\\baz", p.string());
    }
  }
}

TEST(TestFilesystemHelper, is_absolute)
{
  if (is_win32) {
    {
      auto p = path("C:\\foo\\bar\\baz");
      EXPECT_TRUE(p.is_absolute());
    }
    {
      auto p = path("D:\\foo\\bar\\baz");
      EXPECT_TRUE(p.is_absolute());
    }
    {
      auto p = path("C:/foo/bar/baz");
      EXPECT_TRUE(p.is_absolute());
    }
    {
      auto p = path("\\foo\\bar\\baz");
      EXPECT_FALSE(p.is_absolute());
    }
    {
      auto p = path("/foo/bar/baz");
      EXPECT_FALSE(p.is_absolute());
    }
    {
      auto p = path("foo/bar/baz");
      EXPECT_FALSE(p.is_absolute());
    }
    {
      auto p = path("C:");
      EXPECT_FALSE(p.is_absolute());
    }
    {
      auto p = path("C");
      EXPECT_FALSE(p.is_absolute());
    }
    {
      auto p = path("");
      EXPECT_FALSE(p.is_absolute());
    }
  } else {
    {
      auto p = path("/foo/bar/baz");
      EXPECT_TRUE(p.is_absolute());
    }
    {
      auto p = path("foo/bar/baz");
      EXPECT_FALSE(p.is_absolute());
    }
    {
      auto p = path("f");
      EXPECT_FALSE(p.is_absolute());
    }
    {
      auto p = path("");
      EXPECT_FALSE(p.is_absolute());
    }
  }
}

TEST(TestFilesystemHelper, correct_extension)
{
  {
    auto p = path(build_extension_path());
    auto ext = p.extension();
    EXPECT_EQ(".yml", ext.string());
  }
  {
    auto p = path(build_double_extension_path());
    auto ext = p.extension();
    EXPECT_EQ(".yml", ext.string());
  }
  {
    auto p = path(build_no_extension_path());
    auto ext = p.extension();
    EXPECT_EQ("", ext.string());
  }
}

TEST(TestFilesystemHelper, is_empty)
{
  auto p_no_arg = path();
  EXPECT_TRUE(p_no_arg.empty());

  auto p_empty_string = path("");
  EXPECT_TRUE(p_empty_string.empty());
}

TEST(TestFilesystemHelper, exists)
{
  {
    auto p = path("");
    EXPECT_FALSE(std::filesystem::exists(p));
  }
  {
    auto p = path(".");
    EXPECT_TRUE(std::filesystem::exists(p));
  }
  {
    auto p = path("..");
    EXPECT_TRUE(std::filesystem::exists(p));
  }
  {
    if (is_win32) {
      auto p = path("\\");
      EXPECT_TRUE(std::filesystem::exists(p));
    } else {
      auto p = path("/");
      EXPECT_TRUE(std::filesystem::exists(p));
    }
  }
}

/**
 * Test filesystem manipulation API.
 *
 * NOTE: expects the current directory to be write-only, else test will fail.
 *
 */
TEST(TestFilesystemHelper, filesystem_manipulation)
{
  auto dir = path(build_directory_path());
  std::filesystem::remove_all(dir);
  EXPECT_FALSE(std::filesystem::exists(dir));
  EXPECT_TRUE(std::filesystem::create_directories(dir));
  EXPECT_TRUE(std::filesystem::exists(dir));
  EXPECT_TRUE(std::filesystem::is_directory(dir));
  EXPECT_FALSE(std::filesystem::is_regular_file(dir));

  auto file = dir / "test_file.txt";
  uint64_t expected_file_size = 0;
  {
    std::ofstream output_buffer{file.string()};
    output_buffer << "test";
    expected_file_size = static_cast<uint64_t>(output_buffer.tellp());
  }

  EXPECT_TRUE(std::filesystem::exists(file));
  EXPECT_TRUE(std::filesystem::is_regular_file(file));
  EXPECT_FALSE(std::filesystem::is_directory(file));
  std::error_code ec;
  EXPECT_FALSE(std::filesystem::create_directories(file, ec));
  EXPECT_GE(std::filesystem::file_size(file), expected_file_size);
  if (!is_win32) {
    EXPECT_THROW((void)std::filesystem::file_size(dir), std::filesystem::filesystem_error) <<
      "file_size is only applicable for files!";
  }
  EXPECT_FALSE(std::filesystem::remove(dir, ec));
  EXPECT_TRUE(std::filesystem::remove(file, ec));
  EXPECT_THROW((void)std::filesystem::file_size(file), std::filesystem::filesystem_error);
  EXPECT_TRUE(std::filesystem::remove(dir));
  EXPECT_FALSE(std::filesystem::exists(file));
  EXPECT_FALSE(std::filesystem::exists(dir));
  auto temp_dir = std::filesystem::temp_directory_path();
  temp_dir = temp_dir / "rcpputils" / "test_folder";
  EXPECT_FALSE(std::filesystem::exists(temp_dir));
  EXPECT_TRUE(std::filesystem::create_directories(temp_dir));
  EXPECT_TRUE(std::filesystem::exists(temp_dir));
  EXPECT_TRUE(std::filesystem::remove(temp_dir));
  EXPECT_TRUE(std::filesystem::remove(temp_dir.parent_path()));

  // Remove empty directory
  EXPECT_FALSE(std::filesystem::exists(dir));
  EXPECT_TRUE(std::filesystem::create_directories(dir));
  EXPECT_TRUE(std::filesystem::exists(dir));
  EXPECT_TRUE(std::filesystem::is_directory(dir));
  EXPECT_TRUE(std::filesystem::remove_all(dir));
  EXPECT_FALSE(std::filesystem::is_directory(dir));

  // Remove non-existing directory
  EXPECT_FALSE(std::filesystem::remove_all(std::filesystem::path("some") / "nonsense" / "dir"));

  EXPECT_FALSE(std::filesystem::exists(dir));
  EXPECT_TRUE(std::filesystem::create_directories(dir));

  // Remove single file with remove_all
  file = dir / "remove_all_single_file.txt";
  {
    std::ofstream output_buffer{file.string()};
    output_buffer << "some content";
  }
  ASSERT_TRUE(std::filesystem::exists(file));
  ASSERT_TRUE(std::filesystem::is_regular_file(file));
  EXPECT_TRUE(std::filesystem::remove_all(file));

  // Remove directory and its content
  EXPECT_TRUE(std::filesystem::exists(dir));
  EXPECT_TRUE(std::filesystem::is_directory(dir));

  auto num_files = 10u;
  for (auto i = 0u; i < num_files; ++i) {
    std::string file_name = std::string("test_file") + std::to_string(i) + ".txt";
    auto file = dir / file_name;
    {
      std::ofstream output_buffer{file.string()};
      output_buffer << "test" << i;
    }
  }

  // remove shall fail given that directory is not empty
  ASSERT_FALSE(std::filesystem::remove(dir, ec));

  EXPECT_TRUE(std::filesystem::remove_all(dir, ec));

  for (auto i = 0u; i < num_files; ++i) {
    std::string file_name = std::string("test_file") + std::to_string(i) + ".txt";
    auto file = dir / file_name;
    ASSERT_FALSE(std::filesystem::exists(file));
  }
  ASSERT_FALSE(std::filesystem::exists(dir));

  // Empty path/directory cannot be created
  EXPECT_FALSE(std::filesystem::create_directories(std::filesystem::path(""), ec));
}

TEST(TestFilesystemHelper, remove_extension)
{
  auto p = path("foo.txt");
  p = p.replace_extension();
  EXPECT_EQ("foo", p.string());
}

TEST(TestFilesystemHelper, remove_extensions)
{
  auto p = path("foo.txt.compress");
  p = p.replace_extension().replace_extension();
  EXPECT_EQ("foo", p.string());
}

TEST(TestFilesystemHelper, remove_extensions_overcount)
{
  auto p = path("foo.txt.compress");
  p = p.replace_extension().replace_extension().replace_extension().replace_extension();
  EXPECT_EQ("foo", p.string());
}

TEST(TestFilesystemHelper, remove_extension_no_extension)
{
  auto p = path("foo");
  p = p.replace_extension();
  EXPECT_EQ("foo", p.string());
}

TEST(TestFilesystemHelper, get_cwd)
{
  std::string expected_dir = rcpputils::get_env_var("EXPECTED_WORKING_DIRECTORY");
  auto p = std::filesystem::current_path();
  EXPECT_EQ(expected_dir, p.string());
}

TEST(TestFilesystemHelper, parent_absolute_path)
{
  std::filesystem::path path("/home/foo/bar/baz");
  ASSERT_EQ(path.string(), "/home/foo/bar/baz");

  std::filesystem::path parent = path.parent_path();
  ASSERT_EQ(parent.string(), "/home/foo/bar");

  std::filesystem::path grandparent = parent.parent_path();
  ASSERT_EQ(grandparent.string(), "/home/foo");

  if (is_win32) {
    std::filesystem::path win_drive_letter("C:\\home\\foo\\bar");
    ASSERT_EQ(win_drive_letter.string(), "C:\\home\\foo\\bar");
    std::filesystem::path win_parent = win_drive_letter.parent_path();
    ASSERT_EQ(win_parent.string(), "C:\\home\\foo");
    std::filesystem::path win_grandparent = win_parent.parent_path();
    ASSERT_EQ(win_grandparent.string(), "C:\\home");
  }
}

TEST(TestFilesystemHelper, create_temporary_directory)
{
  // basic usage
  {
    const std::string basename = "test_base_name";

    const auto tmpdir1 = rcpputils::fs::create_temporary_directory(basename);
    EXPECT_TRUE(std::filesystem::exists(tmpdir1));
    EXPECT_TRUE(std::filesystem::is_directory(tmpdir1));

    auto tmp_file = tmpdir1 / "test_file.txt";
    {
      std::ofstream output_buffer{tmp_file.string()};
      output_buffer << "test";
    }
    EXPECT_TRUE(std::filesystem::exists(tmp_file));
    EXPECT_TRUE(std::filesystem::is_regular_file(tmp_file));

    const auto tmpdir2 = rcpputils::fs::create_temporary_directory(basename);
    EXPECT_TRUE(std::filesystem::exists(tmpdir2));
    EXPECT_TRUE(std::filesystem::is_directory(tmpdir2));

    EXPECT_NE(tmpdir1.string(), tmpdir2.string());

    EXPECT_TRUE(std::filesystem::remove_all(tmpdir1));
    EXPECT_TRUE(std::filesystem::remove_all(tmpdir2));
  }

  // bad names
  {
    if (is_win32) {
      EXPECT_THROW(rcpputils::fs::create_temporary_directory("illegalchar?"), std::system_error);
    } else {
      EXPECT_THROW(rcpputils::fs::create_temporary_directory("base/name"), std::invalid_argument);
    }
  }

  // newly created paths
  {
    const auto new_relative = std::filesystem::current_path() / "child1" / "child2";
    const auto tmpdir = rcpputils::fs::create_temporary_directory("base_name", new_relative);
    EXPECT_TRUE(std::filesystem::exists(tmpdir));
    EXPECT_TRUE(std::filesystem::is_directory(tmpdir));
    EXPECT_TRUE(std::filesystem::remove_all(tmpdir));
  }

  // edge case inputs
  {
    // Provided no base name we should still get a path with the 6 unique template chars
    const auto tmpdir_emptybase = rcpputils::fs::create_temporary_directory("");
    EXPECT_EQ(tmpdir_emptybase.filename().string().size(), 6u);

    // Empty path doesn't exist and cannot be created
    auto path1 = rcpputils::fs::create_temporary_directory("basename", path());
    EXPECT_TRUE(std::filesystem::exists(path1));

    // With the template string XXXXXX already in the name, it will still be there, the unique
    // portion is appended to the end.
    const auto tmpdir_template_in_name = rcpputils::fs::create_temporary_directory("base_XXXXXX");
    EXPECT_TRUE(std::filesystem::exists(tmpdir_template_in_name));
    EXPECT_TRUE(std::filesystem::is_directory(tmpdir_template_in_name));
    // On Linux, it will not replace the base_name Xs, only the final 6 that the function appends.
    // On OSX, it will replace _all_ trailing Xs.
    // Either way, the result is unique, the exact value doesn't matter.
    EXPECT_EQ(tmpdir_template_in_name.filename().string().rfind("base_", 0), 0u);
  }
}

TEST(TestFilesystemHelper, equal_operators)
{
  path a{"foo"};
  EXPECT_EQ(a, a);
  path b{"bar"};
  EXPECT_NE(a, b);

  path c = a / b;
  path d = path("foo") / "bar";
  EXPECT_EQ(c, d);
}