Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
path_unittest.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "filesystem/path.h"
6#include "filesystem/directory.h"
7#include "filesystem/scoped_temp_dir.h"
8#include "gtest/gtest.h"
10
11namespace filesystem {
12
13void ExpectPlatformPath(std::string expected, std::string actual) {
14#if defined(OS_WIN)
15 std::replace(expected.begin(), expected.end(), '/', '\\');
16#endif
17 EXPECT_EQ(expected, actual);
18}
19
24 ExpectPlatformPath("...", SimplifyPath("..."));
25
29 ExpectPlatformPath("/...", SimplifyPath("/..."));
30
31 ExpectPlatformPath("foo", SimplifyPath("foo"));
32 ExpectPlatformPath("foo", SimplifyPath("foo/"));
33 ExpectPlatformPath("foo", SimplifyPath("foo/."));
34 ExpectPlatformPath("foo", SimplifyPath("foo/./"));
35 ExpectPlatformPath(".", SimplifyPath("foo/.."));
36 ExpectPlatformPath(".", SimplifyPath("foo/../"));
37 ExpectPlatformPath("foo/...", SimplifyPath("foo/..."));
38 ExpectPlatformPath("foo/...", SimplifyPath("foo/.../"));
39 ExpectPlatformPath("foo/.b", SimplifyPath("foo/.b"));
40 ExpectPlatformPath("foo/.b", SimplifyPath("foo/.b/"));
41
42 ExpectPlatformPath("/foo", SimplifyPath("/foo"));
43 ExpectPlatformPath("/foo", SimplifyPath("/foo/"));
44 ExpectPlatformPath("/foo", SimplifyPath("/foo/."));
45 ExpectPlatformPath("/foo", SimplifyPath("/foo/./"));
46 ExpectPlatformPath("/", SimplifyPath("/foo/.."));
47 ExpectPlatformPath("/", SimplifyPath("/foo/../"));
48 ExpectPlatformPath("/foo/...", SimplifyPath("/foo/..."));
49 ExpectPlatformPath("/foo/...", SimplifyPath("/foo/.../"));
50 ExpectPlatformPath("/foo/.b", SimplifyPath("/foo/.b"));
51 ExpectPlatformPath("/foo/.b", SimplifyPath("/foo/.b/"));
52
53 ExpectPlatformPath("foo/bar", SimplifyPath("foo/bar"));
54 ExpectPlatformPath("foo/bar", SimplifyPath("foo/bar/"));
55 ExpectPlatformPath("foo/bar", SimplifyPath("foo/./bar"));
56 ExpectPlatformPath("foo/bar", SimplifyPath("foo/./bar/"));
57 ExpectPlatformPath("bar", SimplifyPath("foo/../bar"));
58 ExpectPlatformPath("bar", SimplifyPath("foo/baz/../../bar"));
59 ExpectPlatformPath("bar", SimplifyPath("foo/../bar/"));
60 ExpectPlatformPath("foo/.../bar", SimplifyPath("foo/.../bar"));
61 ExpectPlatformPath("foo/.../bar", SimplifyPath("foo/.../bar/"));
62 ExpectPlatformPath("foo/.b/bar", SimplifyPath("foo/.b/bar"));
63 ExpectPlatformPath("foo/.b/bar", SimplifyPath("foo/.b/bar/"));
64
65 ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/bar"));
66 ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/bar/"));
67 ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/./bar"));
68 ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/./bar/"));
69 ExpectPlatformPath("/bar", SimplifyPath("/foo/../bar"));
70 ExpectPlatformPath("/bar", SimplifyPath("/foo/../bar/"));
71 ExpectPlatformPath("/foo/.../bar", SimplifyPath("/foo/.../bar"));
72 ExpectPlatformPath("/foo/.../bar", SimplifyPath("/foo/.../bar/"));
73 ExpectPlatformPath("/foo/.b/bar", SimplifyPath("/foo/.b/bar"));
74 ExpectPlatformPath("/foo/.b/bar", SimplifyPath("/foo/.b/bar/"));
75
76 ExpectPlatformPath("../foo", SimplifyPath("../foo"));
77 ExpectPlatformPath("../../bar", SimplifyPath("../foo/../../bar"));
78 ExpectPlatformPath("/bar", SimplifyPath("/foo/../../bar"));
79
80 // Already clean
82 ExpectPlatformPath("abc", SimplifyPath("abc"));
83 ExpectPlatformPath("abc/def", SimplifyPath("abc/def"));
84 ExpectPlatformPath("a/b/c", SimplifyPath("a/b/c"));
87 ExpectPlatformPath("../..", SimplifyPath("../.."));
88 ExpectPlatformPath("../../abc", SimplifyPath("../../abc"));
89 ExpectPlatformPath("/abc", SimplifyPath("/abc"));
91
92 // Remove trailing slash
93 ExpectPlatformPath("abc", SimplifyPath("abc/"));
94 ExpectPlatformPath("abc/def", SimplifyPath("abc/def/"));
95 ExpectPlatformPath("a/b/c", SimplifyPath("a/b/c/"));
97 ExpectPlatformPath("..", SimplifyPath("../"));
98 ExpectPlatformPath("../..", SimplifyPath("../../"));
99 ExpectPlatformPath("/abc", SimplifyPath("/abc/"));
100
101 // Remove doubled slash
102 ExpectPlatformPath("abc/def/ghi", SimplifyPath("abc//def//ghi"));
103 ExpectPlatformPath("/abc", SimplifyPath("//abc"));
104 ExpectPlatformPath("/abc", SimplifyPath("///abc"));
105 ExpectPlatformPath("/abc", SimplifyPath("//abc//"));
106 ExpectPlatformPath("abc", SimplifyPath("abc//"));
107
108 // Remove . elements
109 ExpectPlatformPath("abc/def", SimplifyPath("abc/./def"));
110 ExpectPlatformPath("/abc/def", SimplifyPath("/./abc/def"));
111 ExpectPlatformPath("abc", SimplifyPath("abc/."));
112
113 // Remove .. elements
114 ExpectPlatformPath("abc/def/jkl", SimplifyPath("abc/def/ghi/../jkl"));
115 ExpectPlatformPath("abc/jkl", SimplifyPath("abc/def/../ghi/../jkl"));
116 ExpectPlatformPath("abc", SimplifyPath("abc/def/.."));
117 ExpectPlatformPath(".", SimplifyPath("abc/def/../.."));
118 ExpectPlatformPath("/", SimplifyPath("/abc/def/../.."));
119 ExpectPlatformPath("..", SimplifyPath("abc/def/../../.."));
120 ExpectPlatformPath("/", SimplifyPath("/abc/def/../../.."));
121 ExpectPlatformPath("../../mno",
122 SimplifyPath("abc/def/../../../ghi/jkl/../../../mno"));
123 ExpectPlatformPath("/mno", SimplifyPath("/../mno"));
124
125 // Combinations
126 ExpectPlatformPath("def", SimplifyPath("abc/./../def"));
127 ExpectPlatformPath("def", SimplifyPath("abc//./../def"));
128 ExpectPlatformPath("../../def", SimplifyPath("abc/../../././../def"));
129
130#if defined(OS_WIN)
131 ExpectPlatformPath("a\\c", SimplifyPath("a\\b\\..\\c"));
132 ExpectPlatformPath("X:\\a\\c", SimplifyPath("X:/a/b/../c"));
133 ExpectPlatformPath("X:\\a\\b\\c", SimplifyPath("X:/a/b/./c"));
134 ExpectPlatformPath("X:\\c", SimplifyPath("X:/../../c"));
135#endif
136}
137
139#if defined(OS_WIN)
140 // We cut out the drive letter as it can be different on every system.
141 EXPECT_EQ(":\\foo\\bar", AbsolutePath("\\foo\\bar").substr(1));
142 EXPECT_EQ(":\\foo\\bar", AbsolutePath("/foo/bar").substr(1));
143 EXPECT_EQ(":\\foo\\bar\\", AbsolutePath("\\foo\\bar\\").substr(1));
144 EXPECT_EQ(":\\foo\\bar\\", AbsolutePath("/foo/bar/").substr(1));
145 EXPECT_EQ("C:\\foo\\bar\\", AbsolutePath("C:\\foo\\bar\\"));
146 EXPECT_EQ(GetCurrentDirectory() + "\\foo", AbsolutePath("foo"));
147#else
148 EXPECT_EQ("/foo/bar", AbsolutePath("/foo/bar"));
149 EXPECT_EQ("/foo/bar/", AbsolutePath("/foo/bar/"));
150 EXPECT_EQ(GetCurrentDirectory() + "/foo", AbsolutePath("foo"));
151#endif
152 EXPECT_EQ(GetCurrentDirectory(), AbsolutePath(""));
153}
154
156 EXPECT_EQ("foo", GetDirectoryName("foo/"));
157 EXPECT_EQ("foo/bar", GetDirectoryName("foo/bar/"));
158 EXPECT_EQ("foo", GetDirectoryName("foo/bar"));
159 EXPECT_EQ("foo/bar", GetDirectoryName("foo/bar/.."));
160 EXPECT_EQ("foo/bar/..", GetDirectoryName("foo/bar/../.."));
161 EXPECT_EQ("", GetDirectoryName("foo"));
162 EXPECT_EQ("/", GetDirectoryName("/"));
163 EXPECT_EQ("", GetDirectoryName("a"));
164 EXPECT_EQ("/", GetDirectoryName("/a"));
165 EXPECT_EQ("/a", GetDirectoryName("/a/"));
166 EXPECT_EQ("a", GetDirectoryName("a/"));
167#if defined(OS_WIN)
168 EXPECT_EQ("C:\\", GetDirectoryName("C:\\"));
169 EXPECT_EQ("C:\\foo", GetDirectoryName("C:\\foo\\"));
170 EXPECT_EQ("C:\\foo", GetDirectoryName("C:\\foo\\bar"));
171 EXPECT_EQ("foo\\bar", GetDirectoryName("foo\\bar\\"));
172 EXPECT_EQ("foo", GetDirectoryName("foo\\bar"));
173 EXPECT_EQ("\\", GetDirectoryName("\\"));
174 EXPECT_EQ("\\", GetDirectoryName("\\a"));
175#endif
176}
177
179 EXPECT_EQ("", GetBaseName("foo/"));
180 EXPECT_EQ("", GetBaseName("foo/bar/"));
181 EXPECT_EQ("bar", GetBaseName("foo/bar"));
182 EXPECT_EQ("..", GetBaseName("foo/bar/.."));
183 EXPECT_EQ("..", GetBaseName("foo/bar/../.."));
184 EXPECT_EQ("foo", GetBaseName("foo"));
185 EXPECT_EQ("", GetBaseName("/"));
186 EXPECT_EQ("a", GetBaseName("a"));
187 EXPECT_EQ("a", GetBaseName("/a"));
188 EXPECT_EQ("", GetBaseName("/a/"));
189 EXPECT_EQ("", GetBaseName("a/"));
190#if defined(OS_WIN)
191 EXPECT_EQ("", GetBaseName("C:\\"));
192 EXPECT_EQ("", GetBaseName("C:\\foo\\"));
193 EXPECT_EQ("bar", GetBaseName("C:\\foo\\bar"));
194 EXPECT_EQ("", GetBaseName("foo\\bar\\"));
195 EXPECT_EQ("bar", GetBaseName("foo\\bar"));
196 EXPECT_EQ("", GetBaseName("\\"));
197 EXPECT_EQ("a", GetBaseName("\\a"));
198#endif
199}
200
201TEST(Path, DeletePath) {
202 ScopedTempDir dir;
203
204 std::string sub_dir = dir.path() + "/dir";
205 CreateDirectory(sub_dir);
206 EXPECT_TRUE(IsDirectory(sub_dir));
207 EXPECT_TRUE(DeletePath(sub_dir, false));
208 EXPECT_FALSE(IsDirectory(sub_dir));
209}
210
211TEST(Path, DeletePathRecursively) {
212 ScopedTempDir dir;
213
214 std::string sub_dir = dir.path() + "/dir";
215 CreateDirectory(sub_dir);
216 EXPECT_TRUE(IsDirectory(sub_dir));
217
218 std::string sub_sub_dir1 = sub_dir + "/dir1";
219 CreateDirectory(sub_sub_dir1);
220 EXPECT_TRUE(IsDirectory(sub_sub_dir1));
221 std::string sub_sub_dir2 = sub_dir + "/dir2";
222 CreateDirectory(sub_sub_dir2);
223 EXPECT_TRUE(IsDirectory(sub_sub_dir2));
224
225 EXPECT_FALSE(DeletePath(sub_dir, false));
226 EXPECT_TRUE(IsDirectory(sub_dir));
227 EXPECT_TRUE(IsDirectory(sub_sub_dir1));
228
229 EXPECT_TRUE(DeletePath(sub_dir, true));
230 EXPECT_FALSE(IsDirectory(sub_dir));
231 EXPECT_FALSE(IsDirectory(sub_sub_dir1));
232}
233
234} // namespace filesystem
#define TEST(S, s, D, expected)
const SkPath & path() const
Definition path.h:117
std::string GetBaseName(const std::string &path)
void ExpectPlatformPath(std::string expected, std::string actual)
std::string AbsolutePath(const std::string &path)
std::string GetDirectoryName(const std::string &path)
std::string SimplifyPath(std::string path)
Definition path_posix.cc:48
#define EXPECT_TRUE(handle)
Definition unit_test.h:685
#define CreateDirectory
#define GetCurrentDirectory