1 : // Copyright 2011 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Unittests for core::file_util.h.
16 :
17 : #include "syzygy/core/file_util.h"
18 :
19 : #include "base/command_line.h"
20 : #include "base/file_util.h"
21 : #include "base/files/scoped_temp_dir.h"
22 : #include "gtest/gtest.h"
23 : #include "syzygy/core/unittest_util.h"
24 :
25 : namespace core {
26 :
27 : namespace {
28 :
29 : // For FilePath pretty-printing.
30 : std::ostream& operator<<(std::ostream& ostream, const base::FilePath& path) {
31 : ostream << "base::FilePath(" << path.value().c_str() << ")";
32 : return ostream;
33 : }
34 :
35 : class CompareFilePathsTest : public testing::Test {
36 : public:
37 E : virtual void SetUp() {
38 : // Initialize the temp directory for the first test.
39 E : if (temp_dir_.get() == NULL) {
40 E : temp_dir_.reset(new base::ScopedTempDir());
41 E : ASSERT_TRUE(temp_dir_->CreateUniqueTempDir());
42 : }
43 :
44 E : existing_path_ = testing::GetSrcRelativePath(L"syzygy\\core\\file_util.h");
45 : alternate_existing_path_ = testing::GetSrcRelativePath(
46 E : L"syzygy\\core\\..\\..\\syzygy\\core\\file_util.h");
47 : another_existing_path_ =
48 E : testing::GetSrcRelativePath(L"syzygy\\core\\file_util.cc");
49 :
50 E : ASSERT_TRUE(file_util::PathExists(existing_path_));
51 E : ASSERT_TRUE(file_util::PathExists(alternate_existing_path_));
52 E : ASSERT_NE(existing_path_, alternate_existing_path_);
53 E : ASSERT_TRUE(file_util::PathExists(another_existing_path_));
54 :
55 E : nonexisting_path_ = temp_dir_->path().Append(L"does\\not\\exist.txt");
56 : alternate_nonexisting_path_ = temp_dir_->path().Append(
57 E : L"does\\not\\..\\not\\exist.txt");
58 : another_nonexisting_path_ = temp_dir_->path().Append(
59 E : L"nonexisting.txt");
60 :
61 E : ASSERT_FALSE(file_util::PathExists(nonexisting_path_));
62 E : ASSERT_FALSE(file_util::PathExists(alternate_nonexisting_path_));
63 E : ASSERT_NE(nonexisting_path_, alternate_nonexisting_path_);
64 E : ASSERT_FALSE(file_util::PathExists(another_nonexisting_path_));
65 E : }
66 :
67 : base::FilePath existing_path_;
68 : base::FilePath alternate_existing_path_;
69 : base::FilePath another_existing_path_;
70 :
71 : base::FilePath nonexisting_path_;
72 : base::FilePath alternate_nonexisting_path_;
73 : base::FilePath another_nonexisting_path_;
74 :
75 : // This is static so that it is only initialized once for this whole group
76 : // of tests.
77 : static scoped_ptr<base::ScopedTempDir> temp_dir_;
78 : };
79 :
80 E : scoped_ptr<base::ScopedTempDir> CompareFilePathsTest::temp_dir_;
81 :
82 : } // namespace
83 :
84 E : TEST_F(CompareFilePathsTest, NeitherExistsDistinctPaths) {
85 : EXPECT_EQ(kUnableToCompareFilePaths,
86 : CompareFilePaths(nonexisting_path_,
87 E : another_nonexisting_path_));
88 E : }
89 :
90 E : TEST_F(CompareFilePathsTest, NeitherExistsIdenticalPaths) {
91 : EXPECT_EQ(kEquivalentFilePaths,
92 : CompareFilePaths(nonexisting_path_,
93 E : nonexisting_path_));
94 E : }
95 :
96 E : TEST_F(CompareFilePathsTest, NeitherExistsEquivalentPaths) {
97 : EXPECT_EQ(kEquivalentFilePaths,
98 : CompareFilePaths(nonexisting_path_,
99 E : alternate_nonexisting_path_));
100 E : }
101 :
102 E : TEST_F(CompareFilePathsTest, OnlyPath1Exists) {
103 : EXPECT_EQ(kDistinctFilePaths,
104 : CompareFilePaths(existing_path_,
105 E : nonexisting_path_));
106 E : }
107 :
108 E : TEST_F(CompareFilePathsTest, OnlyPath2Exists) {
109 : EXPECT_EQ(kDistinctFilePaths,
110 : CompareFilePaths(nonexisting_path_,
111 E : existing_path_));
112 E : }
113 :
114 E : TEST_F(CompareFilePathsTest, BothExistDistinctPaths) {
115 : EXPECT_EQ(kDistinctFilePaths,
116 : CompareFilePaths(existing_path_,
117 E : another_existing_path_));
118 E : }
119 :
120 E : TEST_F(CompareFilePathsTest, BothExistSamePath) {
121 : EXPECT_EQ(kEquivalentFilePaths,
122 : CompareFilePaths(existing_path_,
123 E : existing_path_));
124 E : }
125 :
126 E : TEST_F(CompareFilePathsTest, BothExistEquivalentPath) {
127 : EXPECT_EQ(kEquivalentFilePaths,
128 : CompareFilePaths(existing_path_,
129 E : alternate_existing_path_));
130 E : }
131 :
132 : } // namespace core
|