gtest-filepath_test.cc 23.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
35
36
37
38
39
40
// 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.
//
// Authors: keith.ray@gmail.com (Keith Ray)
//
// Google Test filepath utilities
//
// This file tests classes and functions used internally by
// Google Test.  They are subject to change without notice.
//
// This file is #included from gtest_unittest.cc, to avoid changing
// build or make-files for some existing Google Test clients. Do not
// #include this file anywhere else!

41
42
#include "gtest/internal/gtest-filepath.h"
#include "gtest/gtest.h"
shiqian's avatar
shiqian committed
43
44
45
46
47
48

// Indicates that this translation unit is part of Google Test's
// implementation.  It must come before gtest-internal-inl.h is
// included, or there will be a compiler error.  This trick is to
// prevent a user from accidentally including gtest-internal-inl.h in
// his code.
zhanyong.wan's avatar
zhanyong.wan committed
49
#define GTEST_IMPLEMENTATION_ 1
shiqian's avatar
shiqian committed
50
#include "src/gtest-internal-inl.h"
zhanyong.wan's avatar
zhanyong.wan committed
51
#undef GTEST_IMPLEMENTATION_
shiqian's avatar
shiqian committed
52

53
#if GTEST_OS_WINDOWS_MOBILE
54
# include <windows.h>  // NOLINT
55
#elif GTEST_OS_WINDOWS
56
# include <direct.h>  // NOLINT
57
#endif  // GTEST_OS_WINDOWS_MOBILE
shiqian's avatar
shiqian committed
58
59
60
61
62

namespace testing {
namespace internal {
namespace {

63
#if GTEST_OS_WINDOWS_MOBILE
64
65
66
// TODO(wan@google.com): Move these to the POSIX adapter section in
// gtest-port.h.

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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;
}

84
#else
85
86

TEST(GetCurrentDirTest, ReturnsCurrentDir) {
87
88
  const FilePath original_dir = FilePath::GetCurrentDir();
  EXPECT_FALSE(original_dir.IsEmpty());
89

90
  posix::ChDir(GTEST_PATH_SEP_);
91
  const FilePath cwd = FilePath::GetCurrentDir();
92
  posix::ChDir(original_dir.c_str());
93

94
95
# if GTEST_OS_WINDOWS

96
97
98
  // 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
99
  EXPECT_STREQ(GTEST_PATH_SEP_, cwd_without_drive + 1);
100
101
102

# else

103
  EXPECT_EQ(GTEST_PATH_SEP_, cwd.string());
104
105

# endif
106
107
}

108
#endif  // GTEST_OS_WINDOWS_MOBILE
109
110
111
112
113
114
115
116
117
118
119
120

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
121
122
// RemoveDirectoryName "" -> ""
TEST(RemoveDirectoryNameTest, WhenEmptyName) {
123
  EXPECT_EQ("", FilePath("").RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
124
125
126
127
}

// RemoveDirectoryName "afile" -> "afile"
TEST(RemoveDirectoryNameTest, ButNoDirectory) {
128
129
  EXPECT_EQ("afile",
      FilePath("afile").RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
130
131
132
133
}

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

// RemoveDirectoryName "adir/" -> ""
TEST(RemoveDirectoryNameTest, WhereThereIsNoFileName) {
140
141
  EXPECT_EQ("",
      FilePath("adir" GTEST_PATH_SEP_).RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
142
143
144
145
}

// RemoveDirectoryName "adir/afile" -> "afile"
TEST(RemoveDirectoryNameTest, ShouldGiveFileName) {
146
147
  EXPECT_EQ("afile",
      FilePath("adir" GTEST_PATH_SEP_ "afile").RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
148
149
150
151
}

// RemoveDirectoryName "adir/subdir/afile" -> "afile"
TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) {
152
  EXPECT_EQ("afile",
zhanyong.wan's avatar
zhanyong.wan committed
153
      FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
154
      .RemoveDirectoryName().string());
shiqian's avatar
shiqian committed
155
156
}

157
158
#if GTEST_HAS_ALT_PATH_SEP_

159
160
// Tests that RemoveDirectoryName() works with the alternate separator
// on Windows.
161

162
// RemoveDirectoryName("/afile") -> "afile"
163
TEST(RemoveDirectoryNameTest, RootFileShouldGiveFileNameForAlternateSeparator) {
164
  EXPECT_EQ("afile", FilePath("/afile").RemoveDirectoryName().string());
165
166
}

167
// RemoveDirectoryName("adir/") -> ""
168
TEST(RemoveDirectoryNameTest, WhereThereIsNoFileNameForAlternateSeparator) {
169
  EXPECT_EQ("", FilePath("adir/").RemoveDirectoryName().string());
170
171
}

172
// RemoveDirectoryName("adir/afile") -> "afile"
173
TEST(RemoveDirectoryNameTest, ShouldGiveFileNameForAlternateSeparator) {
174
  EXPECT_EQ("afile", FilePath("adir/afile").RemoveDirectoryName().string());
175
176
}

177
// RemoveDirectoryName("adir/subdir/afile") -> "afile"
178
TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileNameForAlternateSeparator) {
179
180
  EXPECT_EQ("afile",
            FilePath("adir/subdir/afile").RemoveDirectoryName().string());
181
182
183
}

#endif
shiqian's avatar
shiqian committed
184
185
186

// RemoveFileName "" -> "./"
TEST(RemoveFileNameTest, EmptyName) {
187
#if GTEST_OS_WINDOWS_MOBILE
188
  // On Windows CE, we use the root as the current directory.
189
  EXPECT_EQ(GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
190
#else
191
  EXPECT_EQ("." GTEST_PATH_SEP_, FilePath("").RemoveFileName().string());
192
#endif
shiqian's avatar
shiqian committed
193
194
195
196
}

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

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

// RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
209
  EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
zhanyong.wan's avatar
zhanyong.wan committed
210
      FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
211
      .RemoveFileName().string());
shiqian's avatar
shiqian committed
212
213
214
215
}

// RemoveFileName "/afile" -> "/"
TEST(RemoveFileNameTest, GivesRootDir) {
216
217
  EXPECT_EQ(GTEST_PATH_SEP_,
      FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().string());
shiqian's avatar
shiqian committed
218
219
}

220
221
#if GTEST_HAS_ALT_PATH_SEP_

222
223
// Tests that RemoveFileName() works with the alternate separator on
// Windows.
224

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

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

237
// RemoveFileName("adir/subdir/afile") -> "adir/subdir/"
238
TEST(RemoveFileNameTest, GivesDirAndSubDirNameForAlternateSeparator) {
239
240
  EXPECT_EQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
            FilePath("adir/subdir/afile").RemoveFileName().string());
241
242
}

243
// RemoveFileName("/afile") -> "\"
244
TEST(RemoveFileNameTest, GivesRootDirForAlternateSeparator) {
245
  EXPECT_EQ(GTEST_PATH_SEP_, FilePath("/afile").RemoveFileName().string());
246
247
248
}

#endif
shiqian's avatar
shiqian committed
249
250
251
252

TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
  FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
      0, "xml");
253
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
shiqian's avatar
shiqian committed
254
255
256
257
258
}

TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
  FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
      12, "xml");
259
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
shiqian's avatar
shiqian committed
260
261
262
}

TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
zhanyong.wan's avatar
zhanyong.wan committed
263
  FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
shiqian's avatar
shiqian committed
264
      FilePath("bar"), 0, "xml");
265
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
shiqian's avatar
shiqian committed
266
267
268
}

TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
zhanyong.wan's avatar
zhanyong.wan committed
269
  FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
shiqian's avatar
shiqian committed
270
      FilePath("bar"), 12, "xml");
271
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.string());
shiqian's avatar
shiqian committed
272
273
}

274
275
276
TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
      0, "xml");
277
  EXPECT_EQ("bar.xml", actual.string());
278
279
280
281
282
}

TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
      14, "xml");
283
  EXPECT_EQ("bar_14.xml", actual.string());
284
285
286
287
288
}

TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
                                          FilePath("bar.xml"));
289
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
290
291
292
}

TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
293
  FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
294
                                          FilePath("bar.xml"));
295
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.string());
296
297
298
299
300
}

TEST(ConcatPathsTest, Path1BeingEmpty) {
  FilePath actual = FilePath::ConcatPaths(FilePath(""),
                                          FilePath("bar.xml"));
301
  EXPECT_EQ("bar.xml", actual.string());
302
303
304
}

TEST(ConcatPathsTest, Path2BeingEmpty) {
305
306
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"), FilePath(""));
  EXPECT_EQ("foo" GTEST_PATH_SEP_, actual.string());
307
308
309
310
311
}

TEST(ConcatPathsTest, BothPathBeingEmpty) {
  FilePath actual = FilePath::ConcatPaths(FilePath(""),
                                          FilePath(""));
312
  EXPECT_EQ("", actual.string());
313
314
315
}

TEST(ConcatPathsTest, Path1ContainsPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
316
  FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
317
                                          FilePath("foobar.xml"));
318
319
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
            actual.string());
320
321
322
}

TEST(ConcatPathsTest, Path2ContainsPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
323
324
325
  FilePath actual = FilePath::ConcatPaths(
      FilePath("foo" GTEST_PATH_SEP_),
      FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
326
327
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
            actual.string());
328
329
330
331
}

TEST(ConcatPathsTest, Path2EndsWithPathSep) {
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
zhanyong.wan's avatar
zhanyong.wan committed
332
                                          FilePath("bar" GTEST_PATH_SEP_));
333
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.string());
334
}
shiqian's avatar
shiqian committed
335
336
337

// RemoveTrailingPathSeparator "" -> ""
TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
338
  EXPECT_EQ("", FilePath("").RemoveTrailingPathSeparator().string());
shiqian's avatar
shiqian committed
339
340
341
342
}

// RemoveTrailingPathSeparator "foo" -> "foo"
TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
343
  EXPECT_EQ("foo", FilePath("foo").RemoveTrailingPathSeparator().string());
shiqian's avatar
shiqian committed
344
345
346
347
}

// RemoveTrailingPathSeparator "foo/" -> "foo"
TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
348
349
  EXPECT_EQ("foo",
      FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().string());
350
#if GTEST_HAS_ALT_PATH_SEP_
351
  EXPECT_EQ("foo", FilePath("foo/").RemoveTrailingPathSeparator().string());
352
#endif
shiqian's avatar
shiqian committed
353
354
355
356
}

// RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
357
358
359
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
            FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
                .RemoveTrailingPathSeparator().string());
shiqian's avatar
shiqian committed
360
361
362
363
}

// RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
364
365
366
  EXPECT_EQ("foo" GTEST_PATH_SEP_ "bar",
            FilePath("foo" GTEST_PATH_SEP_ "bar")
                .RemoveTrailingPathSeparator().string());
shiqian's avatar
shiqian committed
367
368
}

shiqian's avatar
shiqian committed
369
TEST(DirectoryTest, RootDirectoryExists) {
zhanyong.wan's avatar
zhanyong.wan committed
370
#if GTEST_OS_WINDOWS  // We are on Windows.
371
  char current_drive[_MAX_PATH];  // NOLINT
372
  current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
shiqian's avatar
shiqian committed
373
374
375
376
377
378
379
380
381
  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
382
#if GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
383
384
385
386
387
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) {
388
      char non_drive[_MAX_PATH];  // NOLINT
shiqian's avatar
shiqian committed
389
390
391
392
393
394
395
396
397
      non_drive[0] = drive;
      non_drive[1] = ':';
      non_drive[2] = '\\';
      non_drive[3] = '\0';
      EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
      break;
    }
  _chdrive(saved_drive_);
}
398
#endif  // GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
399

400
#if !GTEST_OS_WINDOWS_MOBILE
shiqian's avatar
shiqian committed
401
// Windows CE _does_ consider an empty directory to exist.
shiqian's avatar
shiqian committed
402
403
404
TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
  EXPECT_FALSE(FilePath("").DirectoryExists());
}
405
#endif  // !GTEST_OS_WINDOWS_MOBILE
shiqian's avatar
shiqian committed
406
407

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

shiqian's avatar
shiqian committed
411
412
  EXPECT_TRUE(FilePath(".").DirectoryExists());
  EXPECT_TRUE(FilePath(".\\").DirectoryExists());
413
414

# endif  // _WIN32_CE
shiqian's avatar
shiqian committed
415
416
417
418
419
420
421
422
#else
  EXPECT_TRUE(FilePath(".").DirectoryExists());
  EXPECT_TRUE(FilePath("./").DirectoryExists());
#endif  // GTEST_OS_WINDOWS
}

// "foo/bar" == foo//bar" == "foo///bar"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
423
424
425
426
427
428
429
  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
430
431
432
433
}

// "/bar" == //bar" == "///bar"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
434
435
436
437
438
439
  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
440
441
442
443
}

// "foo/" == foo//" == "foo///"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
444
445
446
447
448
449
  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
450
451
}

452
453
#if GTEST_HAS_ALT_PATH_SEP_

454
455
456
// Tests that separators at the end of the string are normalized
// regardless of their combination (e.g. "foo\" =="foo/\" ==
// "foo\\/").
457
TEST(NormalizeTest, MixAlternateSeparatorAtStringEnd) {
458
459
460
461
462
463
  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());
464
465
466
467
}

#endif

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

TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
  FilePath non_default_path("path");
  FilePath default_path;
  default_path = non_default_path;
480
481
  EXPECT_EQ("path", default_path.string());
  EXPECT_EQ("path", non_default_path.string());  // RHS var is unchanged.
shiqian's avatar
shiqian committed
482
483
484
485
486
487
}

TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
  const FilePath const_default_path("const_path");
  FilePath non_default_path("path");
  non_default_path = const_default_path;
488
  EXPECT_EQ("const_path", non_default_path.string());
shiqian's avatar
shiqian committed
489
}
shiqian's avatar
shiqian committed
490
491
492
493

class DirectoryCreationTest : public Test {
 protected:
  virtual void SetUp() {
494
495
496
    testdata_path_.Set(FilePath(
        TempDir() + GetCurrentExecutableName().string() +
        "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_));
shiqian's avatar
shiqian committed
497
498
499
500
501
502
503
504
505
506
    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());
507
    posix::RmDir(testdata_path_.c_str());
shiqian's avatar
shiqian committed
508
509
510
511
512
513
  }

  virtual void TearDown() {
    remove(testdata_file_.c_str());
    remove(unique_file0_.c_str());
    remove(unique_file1_.c_str());
514
    posix::RmDir(testdata_path_.c_str());
shiqian's avatar
shiqian committed
515
516
  }

517
  std::string TempDir() const {
518
#if GTEST_OS_WINDOWS_MOBILE
519
    return "\\temp\\";
zhanyong.wan's avatar
zhanyong.wan committed
520
#elif GTEST_OS_WINDOWS
521
    const char* temp_dir = posix::GetEnv("TEMP");
shiqian's avatar
shiqian committed
522
    if (temp_dir == NULL || temp_dir[0] == '\0')
523
524
525
      return "\\temp\\";
    else if (temp_dir[strlen(temp_dir) - 1] == '\\')
      return temp_dir;
shiqian's avatar
shiqian committed
526
    else
527
      return std::string(temp_dir) + "\\";
528
#elif GTEST_OS_LINUX_ANDROID
529
    return "/sdcard/";
shiqian's avatar
shiqian committed
530
#else
531
    return "/tmp/";
532
#endif  // GTEST_OS_WINDOWS_MOBILE
shiqian's avatar
shiqian committed
533
534
535
  }

  void CreateTextFile(const char* filename) {
536
    FILE* f = posix::FOpen(filename, "w");
shiqian's avatar
shiqian committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
    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) {
551
  EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
shiqian's avatar
shiqian committed
552
553
554
555
556
  EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  EXPECT_TRUE(testdata_path_.DirectoryExists());
}

TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
557
  EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.string();
shiqian's avatar
shiqian committed
558
559
560
561
562
563
564
565
  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"));
566
  EXPECT_EQ(unique_file0_.string(), file_path.string());
shiqian's avatar
shiqian committed
567
568
569
570
571
572
573
574
575
  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"));
576
  EXPECT_EQ(unique_file1_.string(), file_path2.string());
shiqian's avatar
shiqian committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
  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;
597
  EXPECT_EQ("", fp.string());
shiqian's avatar
shiqian committed
598
599
600
601
}

TEST(FilePathTest, CharAndCopyConstructors) {
  const FilePath fp("spicy");
602
  EXPECT_EQ("spicy", fp.string());
shiqian's avatar
shiqian committed
603
604

  const FilePath fp_copy(fp);
605
  EXPECT_EQ("spicy", fp_copy.string());
shiqian's avatar
shiqian committed
606
607
608
}

TEST(FilePathTest, StringConstructor) {
609
610
  const FilePath fp(std::string("cider"));
  EXPECT_EQ("cider", fp.string());
shiqian's avatar
shiqian committed
611
612
613
614
615
616
}

TEST(FilePathTest, Set) {
  const FilePath apple("apple");
  FilePath mac("mac");
  mac.Set(apple);  // Implement Set() since overloading operator= is forbidden.
617
618
  EXPECT_EQ("apple", mac.string());
  EXPECT_EQ("apple", apple.string());
shiqian's avatar
shiqian committed
619
620
621
622
}

TEST(FilePathTest, ToString) {
  const FilePath file("drink");
623
  EXPECT_EQ("drink", file.string());
shiqian's avatar
shiqian committed
624
625
626
}

TEST(FilePathTest, RemoveExtension) {
627
628
629
  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
630
631
632
}

TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
633
  EXPECT_EQ("app", FilePath("app").RemoveExtension("exe").string());
shiqian's avatar
shiqian committed
634
635
636
637
}

TEST(FilePathTest, IsDirectory) {
  EXPECT_FALSE(FilePath("cola").IsDirectory());
zhanyong.wan's avatar
zhanyong.wan committed
638
  EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
639
640
641
#if GTEST_HAS_ALT_PATH_SEP_
  EXPECT_TRUE(FilePath("koala/").IsDirectory());
#endif
shiqian's avatar
shiqian committed
642
643
}

644
TEST(FilePathTest, IsAbsolutePath) {
zhanyong.wan's avatar
zhanyong.wan committed
645
  EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
646
  EXPECT_FALSE(FilePath("").IsAbsolutePath());
zhanyong.wan's avatar
zhanyong.wan committed
647
648
649
650
#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());
651
652
  EXPECT_TRUE(FilePath("c:/" GTEST_PATH_SEP_ "is_not"
                       GTEST_PATH_SEP_ "relative").IsAbsolutePath());
653
#else
zhanyong.wan's avatar
zhanyong.wan committed
654
  EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
655
656
657
658
              .IsAbsolutePath());
#endif  // GTEST_OS_WINDOWS
}

659
660
661
662
663
664
665
666
667
668
669
670
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());
671
  EXPECT_TRUE(FilePath("//").IsRootDirectory());
672
673
674
675
676
677
  EXPECT_FALSE(FilePath("").IsRootDirectory());
  EXPECT_FALSE(FilePath("\\").IsRootDirectory());
  EXPECT_FALSE(FilePath("/x").IsRootDirectory());
#endif
}

shiqian's avatar
shiqian committed
678
679
680
}  // namespace
}  // namespace internal
}  // namespace testing