"cupy_backends/stub/cupy_cusolver.h" did not exist on "93bf084b3332e0d58c118590cc1722af6c810a8e"
googletest-filepath-test.cc 22.1 KB
Newer Older
shiqian's avatar
shiqian committed
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
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Google Test filepath utilities
//
// This file tests classes and functions used internally by
// Google Test.  They are subject to change without notice.
//
35
36
// This file is #included from gtest-internal.h.
// Do not #include this file anywhere else!
shiqian's avatar
shiqian committed
37

38
39
#include "gtest/internal/gtest-filepath.h"
#include "gtest/gtest.h"
shiqian's avatar
shiqian committed
40
41
#include "src/gtest-internal-inl.h"

42
#if GTEST_OS_WINDOWS_MOBILE
43
# include <windows.h>  // NOLINT
44
#elif GTEST_OS_WINDOWS
45
# include <direct.h>  // NOLINT
46
#endif  // GTEST_OS_WINDOWS_MOBILE
shiqian's avatar
shiqian committed
47
48
49
50
51

namespace testing {
namespace internal {
namespace {

52
#if GTEST_OS_WINDOWS_MOBILE
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Windows CE doesn't have the remove C function.
int remove(const char* path) {
  LPCWSTR wpath = String::AnsiToUtf16(path);
  int ret = DeleteFile(wpath) ? 0 : -1;
  delete [] wpath;
  return ret;
}
// Windows CE doesn't have the _rmdir C function.
int _rmdir(const char* path) {
  FilePath filepath(path);
  LPCWSTR wpath = String::AnsiToUtf16(
      filepath.RemoveTrailingPathSeparator().c_str());
  int ret = RemoveDirectory(wpath) ? 0 : -1;
  delete [] wpath;
  return ret;
}

71
#else
72
73

TEST(GetCurrentDirTest, ReturnsCurrentDir) {
74
75
  const FilePath original_dir = FilePath::GetCurrentDir();
  EXPECT_FALSE(original_dir.IsEmpty());
76

77
  posix::ChDir(GTEST_PATH_SEP_);
78
  const FilePath cwd = FilePath::GetCurrentDir();
79
  posix::ChDir(original_dir.c_str());
80

81
# if GTEST_OS_WINDOWS || GTEST_OS_OS2
82

83
84
85
  // Skips the ":".
  const char* const cwd_without_drive = strchr(cwd.c_str(), ':');
  ASSERT_TRUE(cwd_without_drive != NULL);
zhanyong.wan's avatar
zhanyong.wan committed
86
  EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
87
88
89

# else

90
  EXPECT_EQ(GTEST_PATH_SEP_, cwd.string());
91
92

# endif
93
94
}

95
#endif  // GTEST_OS_WINDOWS_MOBILE
96
97
98
99
100
101
102
103
104
105
106
107

TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
  EXPECT_TRUE(FilePath("").IsEmpty());
}

TEST(IsEmptyTest, ReturnsFalseForNonEmptyPath) {
  EXPECT_FALSE(FilePath("a").IsEmpty());
  EXPECT_FALSE(FilePath(".").IsEmpty());
  EXPECT_FALSE(FilePath("a/b").IsEmpty());
  EXPECT_FALSE(FilePath("a\\b\\").IsEmpty());
}

shiqian's avatar
shiqian committed
108
109
// RemoveDirectoryName "" -> ""
TEST(RemoveDirectoryNameTest, WhenEmptyName) {
110
  EXPECT_EQ("", FilePath("").RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
111
112
113
114
}

// RemoveDirectoryName "afile" -> "afile"
TEST(RemoveDirectoryNameTest, ButNoDirectory) {
115
116
  EXPECT_EQ("afile",
      FilePath("afile").RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
117
118
119
120
}

// RemoveDirectoryName "/afile" -> "afile"
TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileName) {
121
122
  EXPECT_EQ("afile",
      FilePath(GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
123
124
125
126
}

// RemoveDirectoryName "adir/" -> ""
TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
127
128
  EXPECT_EQ("",
      FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
129
130
131
132
}

// RemoveDirectoryName "adir/afile" -> "afile"
TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
133
134
  EXPECT_EQ("afile",
      FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
135
136
137
138
}

// RemoveDirectoryName "adir/subdir/afile" -> "afile"
TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) {
139
  EXPECT_EQ("afile",
zhanyong.wan's avatar
zhanyong.wan committed
140
      FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
141
      .RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
142
143
}

144
145
#if GTEST_HAS_ALT_PATH_SEP_

146
147
// Tests that RemoveDirectoryName() works with the alternate separator
// on Windows.
148

149
// RemoveDirectoryName("/afile") -> "afile"
150
TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) {
151
  EXPECT_EQ("afile", FilePath("/afile").RemoveDirectoryName().string());
152
153
}

154
// RemoveDirectoryName("adir/") -> ""
155
TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) {
156
  EXPECT_EQ("", FilePath("adir/").RemoveDirectoryName().string());
157
158
}

159
// RemoveDirectoryName("adir/afile") -> "afile"
160
TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) {
161
  EXPECT_EQ("afile", FilePath("adir/afile").RemoveDirectoryName().string());
162
163
}

164
// RemoveDirectoryName("adir/subdir/afile") -> "afile"
165
TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) {
166
167
  EXPECT_EQ("afile",
            FilePath("adir/subdir/afile").RemoveDirectoryName().string());
168
169
170
}

#endif
shiqian's avatar
shiqian committed
171
172
173

// RemoveFileName "" -> "./"
TEST(RemoveFileNameTest, EmptyName) {
174
#if GTEST_OS_WINDOWS_MOBILE
175
  // On Windows CE, we use the root as the current directory.
176
  EXPECT_EQ(GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
177
#else
178
  EXPECT_EQ("." GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
179
#endif
shiqian's avatar
shiqian committed
180
181
182
183
}

// RemoveFileName "adir/" -> "adir/"
TEST(RemoveFileNameTest, ButNoFile) {
184
185
  EXPECT_EQ("adir" GTEST_PATH_SEP_,
      FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().string());
shiqian's avatar
shiqian committed
186
187
188
189
}

// RemoveFileName "adir/afile" -> "adir/"
TEST(RemoveFileNameTest, GivesDirName) {
190
191
  EXPECT_EQ("adir" GTEST_PATH_SEP_,
            FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveFileName().string());
shiqian's avatar
shiqian committed
192
193
194
195
}

// RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
196
  EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
zhanyong.wan's avatar
zhanyong.wan committed
197
      FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
198
      .RemoveFileName().string());
shiqian's avatar
shiqian committed
199
200
201
202
}

// RemoveFileName "/afile" -> "/"
TEST(RemoveFileNameTest, GivesRootDir) {
203
204
  EXPECT_EQ(GTEST_PATH_SEP_,
      FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().string());
shiqian's avatar
shiqian committed
205
206
}

207
208
#if GTEST_HAS_ALT_PATH_SEP_

209
210
// Tests that RemoveFileName() works with the alternate separator on
// Windows.
211

212
// RemoveFileName("adir/") -> "adir/"
213
TEST(RemoveFileNameTest, ButNoFileForAlternateSeparator) {
214
215
  EXPECT_EQ("adir" GTEST_PATH_SEP_,
            FilePath("adir/").RemoveFileName().string());
216
217
}

218
// RemoveFileName("adir/afile") -> "adir/"
219
TEST(RemoveFileNameTest, GivesDirNameForAlternateSeparator) {
220
221
  EXPECT_EQ("adir" GTEST_PATH_SEP_,
            FilePath("adir/afile").RemoveFileName().string());
222
223
}

224
// RemoveFileName("adir/subdir/afile") -> "adir/subdir/"
225
TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) {
226
227
  EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
            FilePath("adir/subdir/afile").RemoveFileName().string());
228
229
}

230
// RemoveFileName("/afile") -> "\"
231
TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) {
232
  EXPECT_EQ(GTEST_PATH_SEP_, FilePath("/afile").RemoveFileName().string());
233
234
235
}

#endif
shiqian's avatar
shiqian committed
236
237
238
239

TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
  FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
      0, "xml");
240
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
shiqian's avatar
shiqian committed
241
242
243
244
245
}

TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
  FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
      12, "xml");
246
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
shiqian's avatar
shiqian committed
247
248
249
}

TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
zhanyong.wan's avatar
zhanyong.wan committed
250
  FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
shiqian's avatar
shiqian committed
251
      FilePath("bar"), 0, "xml");
252
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
shiqian's avatar
shiqian committed
253
254
255
}

TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
zhanyong.wan's avatar
zhanyong.wan committed
256
  FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
shiqian's avatar
shiqian committed
257
      FilePath("bar"), 12, "xml");
258
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
shiqian's avatar
shiqian committed
259
260
}

261
262
263
TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
      0, "xml");
264
  EXPECT_EQ("bar.xml", actual.string());
265
266
267
268
269
}

TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
      14, "xml");
270
  EXPECT_EQ("bar_14.xml", actual.string());
271
272
273
274
275
}

TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
                                          FilePath("bar.xml"));
276
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
277
278
279
}

TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
280
  FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
281
                                          FilePath("bar.xml"));
282
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
283
284
285
286
287
}

TEST(ConcatPathsTest, Path1BeingEmpty) {
  FilePath actual = FilePath::ConcatPaths(FilePath(""),
                                          FilePath("bar.xml"));
288
  EXPECT_EQ("bar.xml", actual.string());
289
290
291
}

TEST(ConcatPathsTest, Path2BeingEmpty) {
292
293
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath(""));
  EXPECT_EQ("foo" GTEST_PATH_SEP_, actual.string());
294
295
296
297
298
}

TEST(ConcatPathsTest, BothPathBeingEmpty) {
  FilePath actual = FilePath::ConcatPaths(FilePath(""),
                                          FilePath(""));
299
  EXPECT_EQ("", actual.string());
300
301
302
}

TEST(ConcatPathsTest, Path1ContainsPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
303
  FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
304
                                          FilePath("foobar.xml"));
305
306
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
            actual.string());
307
308
309
}

TEST(ConcatPathsTest, Path2ContainsPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
310
311
312
  FilePath actual = FilePath::ConcatPaths(
      FilePath("foo" GTEST_PATH_SEP_),
      FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
313
314
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
            actual.string());
315
316
317
318
}

TEST(ConcatPathsTest, Path2EndsWithPathSep) {
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
zhanyong.wan's avatar
zhanyong.wan committed
319
                                          FilePath("bar" GTEST_PATH_SEP_));
320
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.string());
321
}
shiqian's avatar
shiqian committed
322
323
324

// RemoveTrailingPathSeparator "" -> ""
TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
325
  EXPECT_EQ("", FilePath("").RemoveTrailingPathSeparator().string());
shiqian's avatar
shiqian committed
326
327
328
329
}

// RemoveTrailingPathSeparator "foo" -> "foo"
TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
330
  EXPECT_EQ("foo", FilePath("foo").RemoveTrailingPathSeparator().string());
shiqian's avatar
shiqian committed
331
332
333
334
}

// RemoveTrailingPathSeparator "foo/" -> "foo"
TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
335
336
  EXPECT_EQ("foo",
      FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().string());
337
#if GTEST_HAS_ALT_PATH_SEP_
338
  EXPECT_EQ("foo", FilePath("foo/").RemoveTrailingPathSeparator().string());
339
#endif
shiqian's avatar
shiqian committed
340
341
342
343
}

// RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
344
345
346
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
            FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
                .RemoveTrailingPathSeparator().string());
shiqian's avatar
shiqian committed
347
348
349
350
}

// RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
351
352
353
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
            FilePath("foo" GTEST_PATH_SEP_ "bar")
                .RemoveTrailingPathSeparator().string());
shiqian's avatar
shiqian committed
354
355
}

shiqian's avatar
shiqian committed
356
TEST(DirectoryTest, RootDirectoryExists) {
zhanyong.wan's avatar
zhanyong.wan committed
357
#if GTEST_OS_WINDOWS  // We are on Windows.
358
  char current_drive[_MAX_PATH];  // NOLINT
359
  current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
shiqian's avatar
shiqian committed
360
361
362
363
364
365
366
367
368
  current_drive[1] = ':';
  current_drive[2] = '\\';
  current_drive[3] = '\0';
  EXPECT_TRUE(FilePath(current_drive).DirectoryExists());
#else
  EXPECT_TRUE(FilePath("/").DirectoryExists());
#endif  // GTEST_OS_WINDOWS
}

zhanyong.wan's avatar
zhanyong.wan committed
369
#if GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
370
371
372
373
374
TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
  const int saved_drive_ = _getdrive();
  // Find a drive that doesn't exist. Start with 'Z' to avoid common ones.
  for (char drive = 'Z'; drive >= 'A'; drive--)
    if (_chdrive(drive - 'A' + 1) == -1) {
375
      char non_drive[_MAX_PATH];  // NOLINT
shiqian's avatar
shiqian committed
376
377
378
379
380
381
382
383
384
      non_drive[0] = drive;
      non_drive[1] = ':';
      non_drive[2] = '\\';
      non_drive[3] = '\0';
      EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
      break;
    }
  _chdrive(saved_drive_);
}
385
#endif  // GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
386

387
#if !GTEST_OS_WINDOWS_MOBILE
shiqian's avatar
shiqian committed
388
// Windows CE _does_ consider an empty directory to exist.
shiqian's avatar
shiqian committed
389
390
391
TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
  EXPECT_FALSE(FilePath("").DirectoryExists());
}
392
#endif  // !GTEST_OS_WINDOWS_MOBILE
shiqian's avatar
shiqian committed
393
394

TEST(DirectoryTest, CurrentDirectoryExists) {
zhanyong.wan's avatar
zhanyong.wan committed
395
#if GTEST_OS_WINDOWS  // We are on Windows.
396
397
# ifndef _WIN32_CE  // Windows CE doesn't have a current directory.

shiqian's avatar
shiqian committed
398
399
  EXPECT_TRUE(FilePath(".").DirectoryExists());
  EXPECT_TRUE(FilePath(".\\").DirectoryExists());
400
401

# endif  // _WIN32_CE
shiqian's avatar
shiqian committed
402
403
404
405
406
407
408
409
#else
  EXPECT_TRUE(FilePath(".").DirectoryExists());
  EXPECT_TRUE(FilePath("./").DirectoryExists());
#endif  // GTEST_OS_WINDOWS
}

// "foo/bar" == foo//bar" == "foo///bar"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
410
411
412
413
414
415
416
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
            FilePath("foo" GTEST_PATH_SEP_ "bar").string());
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
            FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
            FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_
                     GTEST_PATH_SEP_ "bar").string());
shiqian's avatar
shiqian committed
417
418
419
420
}

// "/bar" == //bar" == "///bar"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
421
422
423
424
425
426
  EXPECT_EQ(GTEST_PATH_SEP_ "bar",
    FilePath(GTEST_PATH_SEP_ "bar").string());
  EXPECT_EQ(GTEST_PATH_SEP_ "bar",
    FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
  EXPECT_EQ(GTEST_PATH_SEP_ "bar",
    FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").string());
shiqian's avatar
shiqian committed
427
428
429
430
}

// "foo/" == foo//" == "foo///"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
431
432
433
434
435
436
  EXPECT_EQ("foo" GTEST_PATH_SEP_,
    FilePath("foo" GTEST_PATH_SEP_).string());
  EXPECT_EQ("foo" GTEST_PATH_SEP_,
    FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
  EXPECT_EQ("foo" GTEST_PATH_SEP_,
    FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).string());
shiqian's avatar
shiqian committed
437
438
}

439
440
#if GTEST_HAS_ALT_PATH_SEP_

441
442
443
// Tests that separators at the end of the string are normalized
// regardless of their combination (e.g. "foo\" =="foo/\" ==
// "foo\\/").
444
TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) {
445
446
447
448
449
450
  EXPECT_EQ("foo" GTEST_PATH_SEP_,
            FilePath("foo/").string());
  EXPECT_EQ("foo" GTEST_PATH_SEP_,
            FilePath("foo" GTEST_PATH_SEP_ "/").string());
  EXPECT_EQ("foo" GTEST_PATH_SEP_,
            FilePath("foo//" GTEST_PATH_SEP_).string());
451
452
453
454
}

#endif

shiqian's avatar
shiqian committed
455
456
457
458
TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
  FilePath default_path;
  FilePath non_default_path("path");
  non_default_path = default_path;
459
460
  EXPECT_EQ("", non_default_path.string());
  EXPECT_EQ("", default_path.string());  // RHS var is unchanged.
shiqian's avatar
shiqian committed
461
462
463
464
465
466
}

TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
  FilePath non_default_path("path");
  FilePath default_path;
  default_path = non_default_path;
467
468
  EXPECT_EQ("path", default_path.string());
  EXPECT_EQ("path", non_default_path.string());  // RHS var is unchanged.
shiqian's avatar
shiqian committed
469
470
471
472
473
474
}

TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
  const FilePath const_default_path("const_path");
  FilePath non_default_path("path");
  non_default_path = const_default_path;
475
  EXPECT_EQ("const_path", non_default_path.string());
shiqian's avatar
shiqian committed
476
}
shiqian's avatar
shiqian committed
477
478
479

class DirectoryCreationTest : public Test {
 protected:
Abseil Team's avatar
Abseil Team committed
480
  void SetUp() override {
481
482
483
    testdata_path_.Set(FilePath(
        TempDir() + GetCurrentExecutableName().string() +
        "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_));
shiqian's avatar
shiqian committed
484
485
486
487
488
489
490
491
492
493
    testdata_file_.Set(testdata_path_.RemoveTrailingPathSeparator());

    unique_file0_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
        0, "txt"));
    unique_file1_.Set(FilePath::MakeFileName(testdata_path_, FilePath("unique"),
        1, "txt"));

    remove(testdata_file_.c_str());
    remove(unique_file0_.c_str());
    remove(unique_file1_.c_str());
494
    posix::RmDir(testdata_path_.c_str());
shiqian's avatar
shiqian committed
495
496
  }

Abseil Team's avatar
Abseil Team committed
497
  void TearDown() override {
shiqian's avatar
shiqian committed
498
499
500
    remove(testdata_file_.c_str());
    remove(unique_file0_.c_str());
    remove(unique_file1_.c_str());
501
    posix::RmDir(testdata_path_.c_str());
shiqian's avatar
shiqian committed
502
503
504
  }

  void CreateTextFile(const char* filename) {
505
    FILE* f = posix::FOpen(filename, "w");
shiqian's avatar
shiqian committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
    fprintf(f, "text\n");
    fclose(f);
  }

  // Strings representing a directory and a file, with identical paths
  // except for the trailing separator character that distinquishes
  // a directory named 'test' from a file named 'test'. Example names:
  FilePath testdata_path_;  // "/tmp/directory_creation/test/"
  FilePath testdata_file_;  // "/tmp/directory_creation/test"
  FilePath unique_file0_;  // "/tmp/directory_creation/test/unique.txt"
  FilePath unique_file1_;  // "/tmp/directory_creation/test/unique_1.txt"
};

TEST_F(DirectoryCreationTest, CreateDirectoriesRecursively) {
520
  EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
shiqian's avatar
shiqian committed
521
522
523
524
525
  EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  EXPECT_TRUE(testdata_path_.DirectoryExists());
}

TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
526
  EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
shiqian's avatar
shiqian committed
527
528
529
530
531
532
533
534
  EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  // Call 'create' again... should still succeed.
  EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
}

TEST_F(DirectoryCreationTest, CreateDirectoriesAndUniqueFilename) {
  FilePath file_path(FilePath::GenerateUniqueFileName(testdata_path_,
      FilePath("unique"), "txt"));
535
  EXPECT_EQ(unique_file0_.string(), file_path.string());
shiqian's avatar
shiqian committed
536
537
538
539
540
541
542
543
544
  EXPECT_FALSE(file_path.FileOrDirectoryExists());  // file not there

  testdata_path_.CreateDirectoriesRecursively();
  EXPECT_FALSE(file_path.FileOrDirectoryExists());  // file still not there
  CreateTextFile(file_path.c_str());
  EXPECT_TRUE(file_path.FileOrDirectoryExists());

  FilePath file_path2(FilePath::GenerateUniqueFileName(testdata_path_,
      FilePath("unique"), "txt"));
545
  EXPECT_EQ(unique_file1_.string(), file_path2.string());
shiqian's avatar
shiqian committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
  EXPECT_FALSE(file_path2.FileOrDirectoryExists());  // file not there
  CreateTextFile(file_path2.c_str());
  EXPECT_TRUE(file_path2.FileOrDirectoryExists());
}

TEST_F(DirectoryCreationTest, CreateDirectoriesFail) {
  // force a failure by putting a file where we will try to create a directory.
  CreateTextFile(testdata_file_.c_str());
  EXPECT_TRUE(testdata_file_.FileOrDirectoryExists());
  EXPECT_FALSE(testdata_file_.DirectoryExists());
  EXPECT_FALSE(testdata_file_.CreateDirectoriesRecursively());
}

TEST(NoDirectoryCreationTest, CreateNoDirectoriesForDefaultXmlFile) {
  const FilePath test_detail_xml("test_detail.xml");
  EXPECT_FALSE(test_detail_xml.CreateDirectoriesRecursively());
}

TEST(FilePathTest, DefaultConstructor) {
  FilePath fp;
566
  EXPECT_EQ("", fp.string());
shiqian's avatar
shiqian committed
567
568
569
570
}

TEST(FilePathTest, CharAndCopyConstructors) {
  const FilePath fp("spicy");
571
  EXPECT_EQ("spicy", fp.string());
shiqian's avatar
shiqian committed
572
573

  const FilePath fp_copy(fp);
574
  EXPECT_EQ("spicy", fp_copy.string());
shiqian's avatar
shiqian committed
575
576
577
}

TEST(FilePathTest, StringConstructor) {
578
579
  const FilePath fp(std::string("cider"));
  EXPECT_EQ("cider", fp.string());
shiqian's avatar
shiqian committed
580
581
582
583
584
585
}

TEST(FilePathTest, Set) {
  const FilePath apple("apple");
  FilePath mac("mac");
  mac.Set(apple);  // Implement Set() since overloading operator= is forbidden.
586
587
  EXPECT_EQ("apple", mac.string());
  EXPECT_EQ("apple", apple.string());
shiqian's avatar
shiqian committed
588
589
590
591
}

TEST(FilePathTest, ToString) {
  const FilePath file("drink");
592
  EXPECT_EQ("drink", file.string());
shiqian's avatar
shiqian committed
593
594
595
}

TEST(FilePathTest, RemoveExtension) {
596
597
598
  EXPECT_EQ("app", FilePath("app.cc").RemoveExtension("cc").string());
  EXPECT_EQ("app", FilePath("app.exe").RemoveExtension("exe").string());
  EXPECT_EQ("APP", FilePath("APP.EXE").RemoveExtension("exe").string());
shiqian's avatar
shiqian committed
599
600
601
}

TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
602
  EXPECT_EQ("app", FilePath("app").RemoveExtension("exe").string());
shiqian's avatar
shiqian committed
603
604
605
606
}

TEST(FilePathTest, IsDirectory) {
  EXPECT_FALSE(FilePath("cola").IsDirectory());
zhanyong.wan's avatar
zhanyong.wan committed
607
  EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
608
609
610
#if GTEST_HAS_ALT_PATH_SEP_
  EXPECT_TRUE(FilePath("koala/").IsDirectory());
#endif
shiqian's avatar
shiqian committed
611
612
}

613
TEST(FilePathTest, IsAbsolutePath) {
zhanyong.wan's avatar
zhanyong.wan committed
614
  EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
615
  EXPECT_FALSE(FilePath("").IsAbsolutePath());
zhanyong.wan's avatar
zhanyong.wan committed
616
617
618
619
#if GTEST_OS_WINDOWS
  EXPECT_TRUE(FilePath("c:\\" GTEST_PATH_SEP_ "is_not"
                       GTEST_PATH_SEP_ "relative").IsAbsolutePath());
  EXPECT_FALSE(FilePath("c:foo" GTEST_PATH_SEP_ "bar").IsAbsolutePath());
620
621
  EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not"
                       GTEST_PATH_SEP_ "relative").IsAbsolutePath());
622
#else
zhanyong.wan's avatar
zhanyong.wan committed
623
  EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
624
625
626
627
              .IsAbsolutePath());
#endif  // GTEST_OS_WINDOWS
}

628
629
630
631
632
633
634
635
636
637
638
639
TEST(FilePathTest, IsRootDirectory) {
#if GTEST_OS_WINDOWS
  EXPECT_TRUE(FilePath("a:\\").IsRootDirectory());
  EXPECT_TRUE(FilePath("Z:/").IsRootDirectory());
  EXPECT_TRUE(FilePath("e://").IsRootDirectory());
  EXPECT_FALSE(FilePath("").IsRootDirectory());
  EXPECT_FALSE(FilePath("b:").IsRootDirectory());
  EXPECT_FALSE(FilePath("b:a").IsRootDirectory());
  EXPECT_FALSE(FilePath("8:/").IsRootDirectory());
  EXPECT_FALSE(FilePath("c|/").IsRootDirectory());
#else
  EXPECT_TRUE(FilePath("/").IsRootDirectory());
640
  EXPECT_TRUE(FilePath("//").IsRootDirectory());
641
642
643
644
645
646
  EXPECT_FALSE(FilePath("").IsRootDirectory());
  EXPECT_FALSE(FilePath("\\").IsRootDirectory());
  EXPECT_FALSE(FilePath("/x").IsRootDirectory());
#endif
}

shiqian's avatar
shiqian committed
647
648
649
}  // namespace
}  // namespace internal
}  // namespace testing