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