gtest-filepath_test.cc 20.9 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
41
42
43
44
45
46
47
48
// 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!

#include <gtest/internal/gtest-filepath.h>
#include <gtest/gtest.h>

// 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

zhanyong.wan's avatar
zhanyong.wan committed
53
#if GTEST_OS_WINDOWS
54
#ifdef _WIN32_WCE
55
#include <windows.h>  // NOLINT
56
#else
57
#include <direct.h>  // NOLINT
58
#endif  // _WIN32_WCE
zhanyong.wan's avatar
zhanyong.wan committed
59
#define GTEST_PATH_SEP_ "\\"
shiqian's avatar
shiqian committed
60
#else
zhanyong.wan's avatar
zhanyong.wan committed
61
#define GTEST_PATH_SEP_ "/"
shiqian's avatar
shiqian committed
62
63
64
65
66
67
#endif  // GTEST_OS_WINDOWS

namespace testing {
namespace internal {
namespace {

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifdef _WIN32_WCE
// 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;
}

#endif  // _WIN32_WCE

88
89
90
91
92
#ifndef _WIN32_WCE

TEST(GetCurrentDirTest, ReturnsCurrentDir) {
  EXPECT_FALSE(FilePath::GetCurrentDir().IsEmpty());

zhanyong.wan's avatar
zhanyong.wan committed
93
94
#if GTEST_OS_WINDOWS
  _chdir(GTEST_PATH_SEP_);
95
96
97
98
  const FilePath cwd = FilePath::GetCurrentDir();
  // 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
#else
zhanyong.wan's avatar
zhanyong.wan committed
101
102
  chdir(GTEST_PATH_SEP_);
  EXPECT_STREQ(GTEST_PATH_SEP_, FilePath::GetCurrentDir().c_str());
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#endif
}

#endif  // _WIN32_WCE

TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
  EXPECT_TRUE(FilePath("").IsEmpty());
  EXPECT_TRUE(FilePath(NULL).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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// RemoveDirectoryName "" -> ""
TEST(RemoveDirectoryNameTest, WhenEmptyName) {
  EXPECT_STREQ("", FilePath("").RemoveDirectoryName().c_str());
}

// RemoveDirectoryName "afile" -> "afile"
TEST(RemoveDirectoryNameTest, ButNoDirectory) {
  EXPECT_STREQ("afile",
      FilePath("afile").RemoveDirectoryName().c_str());
}

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

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

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

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


// RemoveFileName "" -> "./"
TEST(RemoveFileNameTest, EmptyName) {
159
160
#ifdef _WIN32_WCE
  // On Windows CE, we use the root as the current directory.
zhanyong.wan's avatar
zhanyong.wan committed
161
  EXPECT_STREQ(GTEST_PATH_SEP_,
162
163
      FilePath("").RemoveFileName().c_str());
#else
zhanyong.wan's avatar
zhanyong.wan committed
164
  EXPECT_STREQ("." GTEST_PATH_SEP_,
shiqian's avatar
shiqian committed
165
      FilePath("").RemoveFileName().c_str());
166
#endif
shiqian's avatar
shiqian committed
167
168
169
170
}

// RemoveFileName "adir/" -> "adir/"
TEST(RemoveFileNameTest, ButNoFile) {
zhanyong.wan's avatar
zhanyong.wan committed
171
172
  EXPECT_STREQ("adir" GTEST_PATH_SEP_,
      FilePath("adir" GTEST_PATH_SEP_).RemoveFileName().c_str());
shiqian's avatar
shiqian committed
173
174
175
176
}

// RemoveFileName "adir/afile" -> "adir/"
TEST(RemoveFileNameTest, GivesDirName) {
zhanyong.wan's avatar
zhanyong.wan committed
177
178
  EXPECT_STREQ("adir" GTEST_PATH_SEP_,
      FilePath("adir" GTEST_PATH_SEP_ "afile")
shiqian's avatar
shiqian committed
179
180
181
182
183
      .RemoveFileName().c_str());
}

// RemoveFileName "adir/subdir/afile" -> "adir/subdir/"
TEST(RemoveFileNameTest, GivesDirAndSubDirName) {
zhanyong.wan's avatar
zhanyong.wan committed
184
185
  EXPECT_STREQ("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_,
      FilePath("adir" GTEST_PATH_SEP_ "subdir" GTEST_PATH_SEP_ "afile")
shiqian's avatar
shiqian committed
186
187
188
189
190
      .RemoveFileName().c_str());
}

// RemoveFileName "/afile" -> "/"
TEST(RemoveFileNameTest, GivesRootDir) {
zhanyong.wan's avatar
zhanyong.wan committed
191
192
  EXPECT_STREQ(GTEST_PATH_SEP_,
      FilePath(GTEST_PATH_SEP_ "afile").RemoveFileName().c_str());
shiqian's avatar
shiqian committed
193
194
195
196
197
198
}


TEST(MakeFileNameTest, GenerateWhenNumberIsZero) {
  FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
      0, "xml");
zhanyong.wan's avatar
zhanyong.wan committed
199
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
shiqian's avatar
shiqian committed
200
201
202
203
204
}

TEST(MakeFileNameTest, GenerateFileNameNumberGtZero) {
  FilePath actual = FilePath::MakeFileName(FilePath("foo"), FilePath("bar"),
      12, "xml");
zhanyong.wan's avatar
zhanyong.wan committed
205
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str());
shiqian's avatar
shiqian committed
206
207
208
}

TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberIsZero) {
zhanyong.wan's avatar
zhanyong.wan committed
209
  FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
shiqian's avatar
shiqian committed
210
      FilePath("bar"), 0, "xml");
zhanyong.wan's avatar
zhanyong.wan committed
211
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
shiqian's avatar
shiqian committed
212
213
214
}

TEST(MakeFileNameTest, GenerateFileNameWithSlashNumberGtZero) {
zhanyong.wan's avatar
zhanyong.wan committed
215
  FilePath actual = FilePath::MakeFileName(FilePath("foo" GTEST_PATH_SEP_),
shiqian's avatar
shiqian committed
216
      FilePath("bar"), 12, "xml");
zhanyong.wan's avatar
zhanyong.wan committed
217
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar_12.xml", actual.c_str());
shiqian's avatar
shiqian committed
218
219
}

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
TEST(MakeFileNameTest, GenerateWhenNumberIsZeroAndDirIsEmpty) {
  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
      0, "xml");
  EXPECT_STREQ("bar.xml", actual.c_str());
}

TEST(MakeFileNameTest, GenerateWhenNumberIsNotZeroAndDirIsEmpty) {
  FilePath actual = FilePath::MakeFileName(FilePath(""), FilePath("bar"),
      14, "xml");
  EXPECT_STREQ("bar_14.xml", actual.c_str());
}

TEST(ConcatPathsTest, WorksWhenDirDoesNotEndWithPathSep) {
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
                                          FilePath("bar.xml"));
zhanyong.wan's avatar
zhanyong.wan committed
235
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
236
237
238
}

TEST(ConcatPathsTest, WorksWhenPath1EndsWithPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
239
  FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_),
240
                                          FilePath("bar.xml"));
zhanyong.wan's avatar
zhanyong.wan committed
241
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar.xml", actual.c_str());
242
243
244
245
246
247
248
249
250
251
252
}

TEST(ConcatPathsTest, Path1BeingEmpty) {
  FilePath actual = FilePath::ConcatPaths(FilePath(""),
                                          FilePath("bar.xml"));
  EXPECT_STREQ("bar.xml", actual.c_str());
}

TEST(ConcatPathsTest, Path2BeingEmpty) {
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
                                          FilePath(""));
zhanyong.wan's avatar
zhanyong.wan committed
253
  EXPECT_STREQ("foo" GTEST_PATH_SEP_, actual.c_str());
254
255
256
257
258
259
260
261
262
}

TEST(ConcatPathsTest, BothPathBeingEmpty) {
  FilePath actual = FilePath::ConcatPaths(FilePath(""),
                                          FilePath(""));
  EXPECT_STREQ("", actual.c_str());
}

TEST(ConcatPathsTest, Path1ContainsPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
263
  FilePath actual = FilePath::ConcatPaths(FilePath("foo" GTEST_PATH_SEP_ "bar"),
264
                                          FilePath("foobar.xml"));
zhanyong.wan's avatar
zhanyong.wan committed
265
266
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "foobar.xml",
               actual.c_str());
267
268
269
}

TEST(ConcatPathsTest, Path2ContainsPathSep) {
zhanyong.wan's avatar
zhanyong.wan committed
270
271
272
273
274
  FilePath actual = FilePath::ConcatPaths(
      FilePath("foo" GTEST_PATH_SEP_),
      FilePath("bar" GTEST_PATH_SEP_ "bar.xml"));
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_ "bar.xml",
               actual.c_str());
275
276
277
278
}

TEST(ConcatPathsTest, Path2EndsWithPathSep) {
  FilePath actual = FilePath::ConcatPaths(FilePath("foo"),
zhanyong.wan's avatar
zhanyong.wan committed
279
280
                                          FilePath("bar" GTEST_PATH_SEP_));
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_, actual.c_str());
281
}
shiqian's avatar
shiqian committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

// RemoveTrailingPathSeparator "" -> ""
TEST(RemoveTrailingPathSeparatorTest, EmptyString) {
  EXPECT_STREQ("",
      FilePath("").RemoveTrailingPathSeparator().c_str());
}

// RemoveTrailingPathSeparator "foo" -> "foo"
TEST(RemoveTrailingPathSeparatorTest, FileNoSlashString) {
  EXPECT_STREQ("foo",
      FilePath("foo").RemoveTrailingPathSeparator().c_str());
}

// RemoveTrailingPathSeparator "foo/" -> "foo"
TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveTrailingSeparator) {
zhanyong.wan's avatar
zhanyong.wan committed
297
298
299
  EXPECT_STREQ(
      "foo",
      FilePath("foo" GTEST_PATH_SEP_).RemoveTrailingPathSeparator().c_str());
shiqian's avatar
shiqian committed
300
301
302
303
}

// RemoveTrailingPathSeparator "foo/bar/" -> "foo/bar/"
TEST(RemoveTrailingPathSeparatorTest, ShouldRemoveLastSeparator) {
zhanyong.wan's avatar
zhanyong.wan committed
304
305
306
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
               FilePath("foo" GTEST_PATH_SEP_ "bar" GTEST_PATH_SEP_)
               .RemoveTrailingPathSeparator().c_str());
shiqian's avatar
shiqian committed
307
308
309
310
}

// RemoveTrailingPathSeparator "foo/bar" -> "foo/bar"
TEST(RemoveTrailingPathSeparatorTest, ShouldReturnUnmodified) {
zhanyong.wan's avatar
zhanyong.wan committed
311
312
313
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
               FilePath("foo" GTEST_PATH_SEP_ "bar")
               .RemoveTrailingPathSeparator().c_str());
shiqian's avatar
shiqian committed
314
315
}

shiqian's avatar
shiqian committed
316
TEST(DirectoryTest, RootDirectoryExists) {
zhanyong.wan's avatar
zhanyong.wan committed
317
#if GTEST_OS_WINDOWS  // We are on Windows.
318
  char current_drive[_MAX_PATH];  // NOLINT
319
  current_drive[0] = static_cast<char>(_getdrive() + 'A' - 1);
shiqian's avatar
shiqian committed
320
321
322
323
324
325
326
327
328
  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
329
#if GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
330
331
332
333
334
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) {
335
      char non_drive[_MAX_PATH];  // NOLINT
shiqian's avatar
shiqian committed
336
337
338
339
340
341
342
343
344
      non_drive[0] = drive;
      non_drive[1] = ':';
      non_drive[2] = '\\';
      non_drive[3] = '\0';
      EXPECT_FALSE(FilePath(non_drive).DirectoryExists());
      break;
    }
  _chdrive(saved_drive_);
}
345
#endif  // GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
346

shiqian's avatar
shiqian committed
347
348
#ifndef _WIN32_WCE
// Windows CE _does_ consider an empty directory to exist.
shiqian's avatar
shiqian committed
349
350
351
TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
  EXPECT_FALSE(FilePath("").DirectoryExists());
}
352
#endif  // ! _WIN32_WCE
shiqian's avatar
shiqian committed
353
354

TEST(DirectoryTest, CurrentDirectoryExists) {
zhanyong.wan's avatar
zhanyong.wan committed
355
#if GTEST_OS_WINDOWS  // We are on Windows.
shiqian's avatar
shiqian committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#ifndef _WIN32_CE  // Windows CE doesn't have a current directory.
  EXPECT_TRUE(FilePath(".").DirectoryExists());
  EXPECT_TRUE(FilePath(".\\").DirectoryExists());
#endif  // _WIN32_CE
#else
  EXPECT_TRUE(FilePath(".").DirectoryExists());
  EXPECT_TRUE(FilePath("./").DirectoryExists());
#endif  // GTEST_OS_WINDOWS
}

TEST(NormalizeTest, NullStringsEqualEmptyDirectory) {
  EXPECT_STREQ("", FilePath(NULL).c_str());
  EXPECT_STREQ("", FilePath(String(NULL)).c_str());
}

// "foo/bar" == foo//bar" == "foo///bar"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsInMidstring) {
zhanyong.wan's avatar
zhanyong.wan committed
373
374
375
376
377
378
379
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
               FilePath("foo" GTEST_PATH_SEP_ "bar").c_str());
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
               FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
  EXPECT_STREQ("foo" GTEST_PATH_SEP_ "bar",
               FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_
                        GTEST_PATH_SEP_ "bar").c_str());
shiqian's avatar
shiqian committed
380
381
382
383
}

// "/bar" == //bar" == "///bar"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringStart) {
zhanyong.wan's avatar
zhanyong.wan committed
384
385
386
387
388
389
  EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
    FilePath(GTEST_PATH_SEP_ "bar").c_str());
  EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
    FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
  EXPECT_STREQ(GTEST_PATH_SEP_ "bar",
    FilePath(GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_ "bar").c_str());
shiqian's avatar
shiqian committed
390
391
392
393
}

// "foo/" == foo//" == "foo///"
TEST(NormalizeTest, MultipleConsecutiveSepaparatorsAtStringEnd) {
zhanyong.wan's avatar
zhanyong.wan committed
394
395
396
397
398
399
  EXPECT_STREQ("foo" GTEST_PATH_SEP_,
    FilePath("foo" GTEST_PATH_SEP_).c_str());
  EXPECT_STREQ("foo" GTEST_PATH_SEP_,
    FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str());
  EXPECT_STREQ("foo" GTEST_PATH_SEP_,
    FilePath("foo" GTEST_PATH_SEP_ GTEST_PATH_SEP_ GTEST_PATH_SEP_).c_str());
shiqian's avatar
shiqian committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
}

TEST(AssignmentOperatorTest, DefaultAssignedToNonDefault) {
  FilePath default_path;
  FilePath non_default_path("path");
  non_default_path = default_path;
  EXPECT_STREQ("", non_default_path.c_str());
  EXPECT_STREQ("", default_path.c_str());  // RHS var is unchanged.
}

TEST(AssignmentOperatorTest, NonDefaultAssignedToDefault) {
  FilePath non_default_path("path");
  FilePath default_path;
  default_path = non_default_path;
  EXPECT_STREQ("path", default_path.c_str());
  EXPECT_STREQ("path", non_default_path.c_str());  // RHS var is unchanged.
}

TEST(AssignmentOperatorTest, ConstAssignedToNonConst) {
  const FilePath const_default_path("const_path");
  FilePath non_default_path("path");
  non_default_path = const_default_path;
  EXPECT_STREQ("const_path", non_default_path.c_str());
}
shiqian's avatar
shiqian committed
424
425
426
427
428
429

class DirectoryCreationTest : public Test {
 protected:
  virtual void SetUp() {
    testdata_path_.Set(FilePath(String::Format("%s%s%s",
        TempDir().c_str(), GetCurrentExecutableName().c_str(),
zhanyong.wan's avatar
zhanyong.wan committed
430
        "_directory_creation" GTEST_PATH_SEP_ "test" GTEST_PATH_SEP_)));
shiqian's avatar
shiqian committed
431
432
433
434
435
436
437
438
439
440
    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());
zhanyong.wan's avatar
zhanyong.wan committed
441
#if GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
442
443
444
445
446
447
448
449
450
451
    _rmdir(testdata_path_.c_str());
#else
    rmdir(testdata_path_.c_str());
#endif  // GTEST_OS_WINDOWS
  }

  virtual void TearDown() {
    remove(testdata_file_.c_str());
    remove(unique_file0_.c_str());
    remove(unique_file1_.c_str());
zhanyong.wan's avatar
zhanyong.wan committed
452
#if GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
453
454
455
456
457
458
459
460
461
462
    _rmdir(testdata_path_.c_str());
#else
    rmdir(testdata_path_.c_str());
#endif  // GTEST_OS_WINDOWS
  }

  String TempDir() const {
#ifdef _WIN32_WCE
    return String("\\temp\\");

zhanyong.wan's avatar
zhanyong.wan committed
463
#elif GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    // MSVC 8 deprecates getenv(), so we want to suppress warning 4996
    // (deprecated function) there.
#pragma warning(push)          // Saves the current warning state.
#pragma warning(disable:4996)  // Temporarily disables warning 4996.
    const char* temp_dir = getenv("TEMP");
#pragma warning(pop)           // Restores the warning state.

    if (temp_dir == NULL || temp_dir[0] == '\0')
      return String("\\temp\\");
    else if (String(temp_dir).EndsWith("\\"))
      return String(temp_dir);
    else
      return String::Format("%s\\", temp_dir);
#else
    return String("/tmp/");
#endif
  }

  void CreateTextFile(const char* filename) {
zhanyong.wan's avatar
zhanyong.wan committed
483
#if GTEST_OS_WINDOWS
shiqian's avatar
shiqian committed
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
    // MSVC 8 deprecates fopen(), so we want to suppress warning 4996
    // (deprecated function) there.#pragma warning(push)
#pragma warning(push)          // Saves the current warning state.
#pragma warning(disable:4996)  // Temporarily disables warning 4996.
    FILE* f = fopen(filename, "w");
#pragma warning(pop)           // Restores the warning state.
#else  // We are on Linux or Mac OS.
    FILE* f = fopen(filename, "w");
#endif  // GTEST_OS_WINDOWS
    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) {
  EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str();
  EXPECT_TRUE(testdata_path_.CreateDirectoriesRecursively());
  EXPECT_TRUE(testdata_path_.DirectoryExists());
}

TEST_F(DirectoryCreationTest, CreateDirectoriesForAlreadyExistingPath) {
  EXPECT_FALSE(testdata_path_.DirectoryExists()) << testdata_path_.c_str();
  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"));
  EXPECT_STREQ(unique_file0_.c_str(), file_path.c_str());
  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"));
  EXPECT_STREQ(unique_file1_.c_str(), file_path2.c_str());
  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;
  EXPECT_STREQ("", fp.c_str());
}

TEST(FilePathTest, CharAndCopyConstructors) {
  const FilePath fp("spicy");
  EXPECT_STREQ("spicy", fp.c_str());

  const FilePath fp_copy(fp);
  EXPECT_STREQ("spicy", fp_copy.c_str());
}

TEST(FilePathTest, StringConstructor) {
  const FilePath fp(String("cider"));
  EXPECT_STREQ("cider", fp.c_str());
}

TEST(FilePathTest, Set) {
  const FilePath apple("apple");
  FilePath mac("mac");
  mac.Set(apple);  // Implement Set() since overloading operator= is forbidden.
  EXPECT_STREQ("apple", mac.c_str());
  EXPECT_STREQ("apple", apple.c_str());
}

TEST(FilePathTest, ToString) {
  const FilePath file("drink");
  String str(file.ToString());
  EXPECT_STREQ("drink", str.c_str());
}

TEST(FilePathTest, RemoveExtension) {
  EXPECT_STREQ("app", FilePath("app.exe").RemoveExtension("exe").c_str());
  EXPECT_STREQ("APP", FilePath("APP.EXE").RemoveExtension("exe").c_str());
}

TEST(FilePathTest, RemoveExtensionWhenThereIsNoExtension) {
  EXPECT_STREQ("app", FilePath("app").RemoveExtension("exe").c_str());
}

TEST(FilePathTest, IsDirectory) {
  EXPECT_FALSE(FilePath("cola").IsDirectory());
zhanyong.wan's avatar
zhanyong.wan committed
594
  EXPECT_TRUE(FilePath("koala" GTEST_PATH_SEP_).IsDirectory());
shiqian's avatar
shiqian committed
595
596
}

597
TEST(FilePathTest, IsAbsolutePath) {
zhanyong.wan's avatar
zhanyong.wan committed
598
  EXPECT_FALSE(FilePath("is" GTEST_PATH_SEP_ "relative").IsAbsolutePath());
599
  EXPECT_FALSE(FilePath("").IsAbsolutePath());
zhanyong.wan's avatar
zhanyong.wan committed
600
601
602
603
#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());
604
#else
zhanyong.wan's avatar
zhanyong.wan committed
605
  EXPECT_TRUE(FilePath(GTEST_PATH_SEP_ "is_not" GTEST_PATH_SEP_ "relative")
606
607
608
609
              .IsAbsolutePath());
#endif  // GTEST_OS_WINDOWS
}

shiqian's avatar
shiqian committed
610
611
612
613
}  // namespace
}  // namespace internal
}  // namespace testing

zhanyong.wan's avatar
zhanyong.wan committed
614
#undef GTEST_PATH_SEP_