gtest-filepath.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Google Test filepath utilities
  30. //
  31. // This header file declares classes and functions used internally by
  32. // Google Test. They are subject to change without notice.
  33. //
  34. // This file is #included in gtest/internal/gtest-internal.h.
  35. // Do not include this header file separately!
  36. // IWYU pragma: private, include "gtest/gtest.h"
  37. // IWYU pragma: friend gtest/.*
  38. // IWYU pragma: friend gmock/.*
  39. #ifndef GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
  40. #define GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_
  41. #include <string>
  42. #include <utility>
  43. #include "gtest/internal/gtest-port.h"
  44. #include "gtest/internal/gtest-string.h"
  45. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
  46. /* class A needs to have dll-interface to be used by clients of class B */)
  47. #if GTEST_HAS_FILE_SYSTEM
  48. namespace testing {
  49. namespace internal {
  50. // FilePath - a class for file and directory pathname manipulation which
  51. // handles platform-specific conventions (like the pathname separator).
  52. // Used for helper functions for naming files in a directory for xml output.
  53. // Except for Set methods, all methods are const or static, which provides an
  54. // "immutable value object" -- useful for peace of mind.
  55. // A FilePath with a value ending in a path separator ("like/this/") represents
  56. // a directory, otherwise it is assumed to represent a file. In either case,
  57. // it may or may not represent an actual file or directory in the file system.
  58. // Names are NOT checked for syntax correctness -- no checking for illegal
  59. // characters, malformed paths, etc.
  60. class GTEST_API_ FilePath {
  61. public:
  62. FilePath() : pathname_("") {}
  63. FilePath(const FilePath& rhs) : pathname_(rhs.pathname_) {}
  64. FilePath(FilePath&& rhs) noexcept : pathname_(std::move(rhs.pathname_)) {}
  65. explicit FilePath(std::string pathname) : pathname_(std::move(pathname)) {
  66. Normalize();
  67. }
  68. FilePath& operator=(const FilePath& rhs) {
  69. Set(rhs);
  70. return *this;
  71. }
  72. FilePath& operator=(FilePath&& rhs) noexcept {
  73. pathname_ = std::move(rhs.pathname_);
  74. return *this;
  75. }
  76. void Set(const FilePath& rhs) { pathname_ = rhs.pathname_; }
  77. const std::string& string() const { return pathname_; }
  78. const char* c_str() const { return pathname_.c_str(); }
  79. // Returns the current working directory, or "" if unsuccessful.
  80. static FilePath GetCurrentDir();
  81. // Given directory = "dir", base_name = "test", number = 0,
  82. // extension = "xml", returns "dir/test.xml". If number is greater
  83. // than zero (e.g., 12), returns "dir/test_12.xml".
  84. // On Windows platform, uses \ as the separator rather than /.
  85. static FilePath MakeFileName(const FilePath& directory,
  86. const FilePath& base_name, int number,
  87. const char* extension);
  88. // Given directory = "dir", relative_path = "test.xml",
  89. // returns "dir/test.xml".
  90. // On Windows, uses \ as the separator rather than /.
  91. static FilePath ConcatPaths(const FilePath& directory,
  92. const FilePath& relative_path);
  93. // Returns a pathname for a file that does not currently exist. The pathname
  94. // will be directory/base_name.extension or
  95. // directory/base_name_<number>.extension if directory/base_name.extension
  96. // already exists. The number will be incremented until a pathname is found
  97. // that does not already exist.
  98. // Examples: 'dir/foo_test.xml' or 'dir/foo_test_1.xml'.
  99. // There could be a race condition if two or more processes are calling this
  100. // function at the same time -- they could both pick the same filename.
  101. static FilePath GenerateUniqueFileName(const FilePath& directory,
  102. const FilePath& base_name,
  103. const char* extension);
  104. // Returns true if and only if the path is "".
  105. bool IsEmpty() const { return pathname_.empty(); }
  106. // If input name has a trailing separator character, removes it and returns
  107. // the name, otherwise return the name string unmodified.
  108. // On Windows platform, uses \ as the separator, other platforms use /.
  109. FilePath RemoveTrailingPathSeparator() const;
  110. // Returns a copy of the FilePath with the directory part removed.
  111. // Example: FilePath("path/to/file").RemoveDirectoryName() returns
  112. // FilePath("file"). If there is no directory part ("just_a_file"), it returns
  113. // the FilePath unmodified. If there is no file part ("just_a_dir/") it
  114. // returns an empty FilePath ("").
  115. // On Windows platform, '\' is the path separator, otherwise it is '/'.
  116. FilePath RemoveDirectoryName() const;
  117. // RemoveFileName returns the directory path with the filename removed.
  118. // Example: FilePath("path/to/file").RemoveFileName() returns "path/to/".
  119. // If the FilePath is "a_file" or "/a_file", RemoveFileName returns
  120. // FilePath("./") or, on Windows, FilePath(".\\"). If the filepath does
  121. // not have a file, like "just/a/dir/", it returns the FilePath unmodified.
  122. // On Windows platform, '\' is the path separator, otherwise it is '/'.
  123. FilePath RemoveFileName() const;
  124. // Returns a copy of the FilePath with the case-insensitive extension removed.
  125. // Example: FilePath("dir/file.exe").RemoveExtension("EXE") returns
  126. // FilePath("dir/file"). If a case-insensitive extension is not
  127. // found, returns a copy of the original FilePath.
  128. FilePath RemoveExtension(const char* extension) const;
  129. // Creates directories so that path exists. Returns true if successful or if
  130. // the directories already exist; returns false if unable to create
  131. // directories for any reason. Will also return false if the FilePath does
  132. // not represent a directory (that is, it doesn't end with a path separator).
  133. bool CreateDirectoriesRecursively() const;
  134. // Create the directory so that path exists. Returns true if successful or
  135. // if the directory already exists; returns false if unable to create the
  136. // directory for any reason, including if the parent directory does not
  137. // exist. Not named "CreateDirectory" because that's a macro on Windows.
  138. bool CreateFolder() const;
  139. // Returns true if FilePath describes something in the file-system,
  140. // either a file, directory, or whatever, and that something exists.
  141. bool FileOrDirectoryExists() const;
  142. // Returns true if pathname describes a directory in the file-system
  143. // that exists.
  144. bool DirectoryExists() const;
  145. // Returns true if FilePath ends with a path separator, which indicates that
  146. // it is intended to represent a directory. Returns false otherwise.
  147. // This does NOT check that a directory (or file) actually exists.
  148. bool IsDirectory() const;
  149. // Returns true if pathname describes a root directory. (Windows has one
  150. // root directory per disk drive.)
  151. bool IsRootDirectory() const;
  152. // Returns true if pathname describes an absolute path.
  153. bool IsAbsolutePath() const;
  154. private:
  155. // Replaces multiple consecutive separators with a single separator.
  156. // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
  157. // redundancies that might be in a pathname involving "." or "..".
  158. //
  159. // A pathname with multiple consecutive separators may occur either through
  160. // user error or as a result of some scripts or APIs that generate a pathname
  161. // with a trailing separator. On other platforms the same API or script
  162. // may NOT generate a pathname with a trailing "/". Then elsewhere that
  163. // pathname may have another "/" and pathname components added to it,
  164. // without checking for the separator already being there.
  165. // The script language and operating system may allow paths like "foo//bar"
  166. // but some of the functions in FilePath will not handle that correctly. In
  167. // particular, RemoveTrailingPathSeparator() only removes one separator, and
  168. // it is called in CreateDirectoriesRecursively() assuming that it will change
  169. // a pathname from directory syntax (trailing separator) to filename syntax.
  170. //
  171. // On Windows this method also replaces the alternate path separator '/' with
  172. // the primary path separator '\\', so that for example "bar\\/\\foo" becomes
  173. // "bar\\foo".
  174. void Normalize();
  175. // Returns a pointer to the last occurrence of a valid path separator in
  176. // the FilePath. On Windows, for example, both '/' and '\' are valid path
  177. // separators. Returns NULL if no path separator was found.
  178. const char* FindLastPathSeparator() const;
  179. // Returns the length of the path root, including the directory separator at
  180. // the end of the prefix. Returns zero by definition if the path is relative.
  181. // Examples:
  182. // - [Windows] "..\Sibling" => 0
  183. // - [Windows] "\Windows" => 1
  184. // - [Windows] "C:/Windows\Notepad.exe" => 3
  185. // - [Windows] "\\Host\Share\C$/Windows" => 13
  186. // - [UNIX] "/bin" => 1
  187. size_t CalculateRootLength() const;
  188. std::string pathname_;
  189. }; // class FilePath
  190. } // namespace internal
  191. } // namespace testing
  192. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  193. #endif // GTEST_HAS_FILE_SYSTEM
  194. #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_FILEPATH_H_