Use the GTest portability headers
[folly.git] / folly / experimental / io / test / FsUtilTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/experimental/io/FsUtil.h>
18
19 #include <glog/logging.h>
20
21 #include <folly/portability/GTest.h>
22
23 using namespace folly;
24 using namespace folly::fs;
25
26 namespace {
27 // We cannot use EXPECT_EQ(a, b) due to a bug in gtest 1.6.0: gtest wants
28 // to print path as a container even though it has operator<< defined,
29 // and as path is a container of path, this leads to infinite
30 // recursion.
31 void expectPathEq(const path& a, const path& b) {
32   EXPECT_TRUE(a == b) << "expected path=" << a << "\nactual path=" << b;
33 }
34 }  // namespace
35
36 TEST(Simple, Path) {
37   path root("/");
38   path abs1("/hello/world");
39   path rel1("meow");
40   EXPECT_TRUE(starts_with(abs1, root));
41   EXPECT_FALSE(starts_with(rel1, root));
42   expectPathEq(path("hello/world"), remove_prefix(abs1, root));
43   EXPECT_THROW({remove_prefix(rel1, root);}, filesystem_error);
44
45   path abs2("/hello");
46   path abs3("/hello/");
47   path abs4("/hello/world");
48   path abs5("/hello/world/");
49   path abs6("/hello/wo");
50   EXPECT_TRUE(starts_with(abs1, abs2));
51   EXPECT_TRUE(starts_with(abs1, abs3));
52   EXPECT_TRUE(starts_with(abs1, abs4));
53   EXPECT_FALSE(starts_with(abs1, abs5));
54   EXPECT_FALSE(starts_with(abs1, abs6));
55   expectPathEq(path("world"), remove_prefix(abs1, abs2));
56   expectPathEq(path("world"), remove_prefix(abs1, abs3));
57   expectPathEq(path(), remove_prefix(abs1, abs4));
58   EXPECT_THROW({remove_prefix(abs1, abs5);}, filesystem_error);
59   EXPECT_THROW({remove_prefix(abs1, abs6);}, filesystem_error);
60 }
61
62 TEST(Simple, CanonicalizeParent) {
63   path a("/usr/bin/tail");
64   path b("/usr/lib/../bin/tail");
65   path c("/usr/bin/DOES_NOT_EXIST_ASDF");
66   path d("/usr/lib/../bin/DOES_NOT_EXIST_ASDF");
67
68   expectPathEq(a, canonical(a));
69   expectPathEq(a, canonical_parent(b));
70   expectPathEq(a, canonical(b));
71   expectPathEq(a, canonical_parent(b));
72   EXPECT_THROW({canonical(c);}, filesystem_error);
73   EXPECT_THROW({canonical(d);}, filesystem_error);
74   expectPathEq(c, canonical_parent(c));
75   expectPathEq(c, canonical_parent(d));
76 }