remove eof whitespace lines
[folly.git] / folly / experimental / io / test / FsUtilTest.cpp
1 /*
2  * Copyright 2014 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 #include <gtest/gtest.h>
21
22 using namespace folly;
23 using namespace folly::fs;
24
25 namespace {
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
29 // recursion.
30 void expectPathEq(const path& a, const path& b) {
31   EXPECT_TRUE(a == b) << "expected path=" << a << "\nactual path=" << b;
32 }
33 }  // namespace
34
35 TEST(Simple, Path) {
36   path root("/");
37   path abs1("/hello/world");
38   path rel1("meow");
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);
43
44   path abs2("/hello");
45   path abs3("/hello/");
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);
59 }
60
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");
66
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));
75 }