2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/experimental/io/FsUtil.h>
19 #include <glog/logging.h>
20 #include <gtest/gtest.h>
22 using namespace folly;
23 using namespace folly::fs;
26 // We cannot use EXPECT_EQ(a, b) due to a bug in gtest 1.6.0: gtest wants
27 // to print path as a container even though it has operator<< defined,
28 // and as path is a container of path, this leads to infinite
30 void expectPathEq(const path& a, const path& b) {
31 EXPECT_TRUE(a == b) << "expected path=" << a << "\nactual path=" << b;
37 path abs1("/hello/world");
39 EXPECT_TRUE(starts_with(abs1, root));
40 EXPECT_FALSE(starts_with(rel1, root));
41 expectPathEq(path("hello/world"), remove_prefix(abs1, root));
42 EXPECT_THROW({remove_prefix(rel1, root);}, filesystem_error);
46 path abs4("/hello/world");
47 path abs5("/hello/world/");
48 path abs6("/hello/wo");
49 EXPECT_TRUE(starts_with(abs1, abs2));
50 EXPECT_TRUE(starts_with(abs1, abs3));
51 EXPECT_TRUE(starts_with(abs1, abs4));
52 EXPECT_FALSE(starts_with(abs1, abs5));
53 EXPECT_FALSE(starts_with(abs1, abs6));
54 expectPathEq(path("world"), remove_prefix(abs1, abs2));
55 expectPathEq(path("world"), remove_prefix(abs1, abs3));
56 expectPathEq(path(), remove_prefix(abs1, abs4));
57 EXPECT_THROW({remove_prefix(abs1, abs5);}, filesystem_error);
58 EXPECT_THROW({remove_prefix(abs1, abs6);}, filesystem_error);
61 TEST(Simple, CanonicalizeParent) {
62 path a("/usr/bin/tail");
63 path b("/usr/lib/../bin/tail");
64 path c("/usr/bin/DOES_NOT_EXIST_ASDF");
65 path d("/usr/lib/../bin/DOES_NOT_EXIST_ASDF");
67 expectPathEq(a, canonical(a));
68 expectPathEq(a, canonical_parent(b));
69 expectPathEq(a, canonical(b));
70 expectPathEq(a, canonical_parent(b));
71 EXPECT_THROW({canonical(c);}, filesystem_error);
72 EXPECT_THROW({canonical(d);}, filesystem_error);
73 expectPathEq(c, canonical_parent(c));
74 expectPathEq(c, canonical_parent(d));