diff options
author | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-18 08:38:16 +0000 |
---|---|---|
committer | aa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-12-18 08:38:16 +0000 |
commit | 406810e402674f6ef1f8248c12a6b4f4a0223a8d (patch) | |
tree | 22d715c0e61f34f4679fa54bb5889010e07fde7e /base | |
parent | 5940ca44ad8aedc988f87e879089e45ac266db2a (diff) | |
download | chromium_src-406810e402674f6ef1f8248c12a6b4f4a0223a8d.zip chromium_src-406810e402674f6ef1f8248c12a6b4f4a0223a8d.tar.gz chromium_src-406810e402674f6ef1f8248c12a6b4f4a0223a8d.tar.bz2 |
Rather than create a TODO that will never be done, I went ahead and implemented FilePath::Contains().
Review URL: http://codereview.chromium.org/14827
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7208 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_path.cc | 27 | ||||
-rw-r--r-- | base/file_path.h | 5 | ||||
-rw-r--r-- | base/file_path_unittest.cc | 70 |
3 files changed, 96 insertions, 6 deletions
diff --git a/base/file_path.cc b/base/file_path.cc index a938ca6..45f6e327 100644 --- a/base/file_path.cc +++ b/base/file_path.cc @@ -3,11 +3,13 @@ // found in the LICENSE file. #include "base/file_path.h" +#include "base/file_util.h" #include "base/logging.h" // These includes are just for the *Hack functions, and should be removed // when those functions are removed. #include "base/string_piece.h" +#include "base/string_util.h" #include "base/sys_string_conversions.h" #if defined(FILE_PATH_USES_WIN_SEPARATORS) @@ -178,6 +180,31 @@ bool FilePath::IsAbsolute() const { return IsPathAbsolute(path_); } +bool FilePath::Contains(const FilePath &other) const { + FilePath parent = FilePath(*this); + FilePath child = FilePath(other); + + if (!file_util::AbsolutePath(&parent) || !file_util::AbsolutePath(&child)) + return false; + +#if defined(OS_WIN) + // file_util::AbsolutePath() does not flatten case on Windows, so we must do + // a case-insensitive compare. + if (!StartsWith(child.value(), parent.value(), false)) +#else + if (!StartsWithASCII(child.value(), parent.value(), true)) +#endif + return false; + + // file_util::AbsolutePath() normalizes '/' to '\' on Windows, so we only need + // to check kSeparators[0]. + if (child.value().length() <= parent.value().length() || + child.value()[parent.value().length()] != kSeparators[0]) + return false; + + return true; +} + #if defined(OS_POSIX) // See file_path.h for a discussion of the encoding of paths on POSIX // platforms. These *Hack() functions are not quite correct, but they're diff --git a/base/file_path.h b/base/file_path.h index 8513592..cadf2cf 100644 --- a/base/file_path.h +++ b/base/file_path.h @@ -163,6 +163,11 @@ class FilePath { // platforms, an absolute path begins with a separator character. bool IsAbsolute() const; + // Returns true if this FilePath represents a parent dir of |other|. Both + // paths are normalized before doing the comparison, but neither |this| nor + // |other| are modified. + bool Contains(const FilePath& other) const; + // Returns a copy of this FilePath that does not end with a trailing // separator. FilePath StripTrailingSeparators() const; diff --git a/base/file_path_unittest.cc b/base/file_path_unittest.cc index ef6239f..df5c48c 100644 --- a/base/file_path_unittest.cc +++ b/base/file_path_unittest.cc @@ -2,10 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "base/file_path.h" - #include "base/basictypes.h" +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/path_service.h" +#include "chrome/common/chrome_paths.h" #include "testing/gtest/include/gtest/gtest.h" +#include "testing/platform_test.h" // This macro helps avoid wrapped lines in the test structs. #define FPL(x) FILE_PATH_LITERAL(x) @@ -25,7 +28,19 @@ struct BinaryTestData { const FilePath::CharType* expected; }; -TEST(FilePathTest, DirName) { +// file_util winds up using autoreleased objects on the Mac, so this needs +// to be a PlatformTest +class FilePathTest : public PlatformTest { + protected: + virtual void SetUp() { + PlatformTest::SetUp(); + } + virtual void TearDown() { + PlatformTest::TearDown(); + } +}; + +TEST_F(FilePathTest, DirName) { const struct UnaryTestData cases[] = { { FPL(""), FPL(".") }, { FPL("aa"), FPL(".") }, @@ -114,7 +129,7 @@ TEST(FilePathTest, DirName) { } } -TEST(FilePathTest, BaseName) { +TEST_F(FilePathTest, BaseName) { const struct UnaryTestData cases[] = { { FPL(""), FPL("") }, { FPL("aa"), FPL("aa") }, @@ -201,7 +216,7 @@ TEST(FilePathTest, BaseName) { } } -TEST(FilePathTest, Append) { +TEST_F(FilePathTest, Append) { const struct BinaryTestData cases[] = { { { FPL(""), FPL("cc") }, FPL("cc") }, { { FPL("."), FPL("ff") }, FPL("ff") }, @@ -282,7 +297,7 @@ TEST(FilePathTest, Append) { } } -TEST(FilePathTest, IsAbsolute) { +TEST_F(FilePathTest, IsAbsolute) { const struct UnaryBooleanTestData cases[] = { { FPL(""), false }, { FPL("a"), false }, @@ -349,3 +364,46 @@ TEST(FilePathTest, IsAbsolute) { "i: " << i << ", input: " << input.value(); } } + +TEST_F(FilePathTest, Contains) { + FilePath data_dir; + ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &data_dir)); + data_dir = data_dir.Append(FILE_PATH_LITERAL("FilePathTest")); + + // Create a fresh, empty copy of this directory. + file_util::Delete(data_dir, true); + file_util::CreateDirectory(data_dir); + + FilePath foo(data_dir.Append(FILE_PATH_LITERAL("foo"))); + FilePath bar(foo.Append(FILE_PATH_LITERAL("bar.txt"))); + FilePath baz(data_dir.Append(FILE_PATH_LITERAL("baz.txt"))); + FilePath foobar(data_dir.Append(FILE_PATH_LITERAL("foobar.txt"))); + + // Annoyingly, the directories must actually exist in order for realpath(), + // which Contains() relies on in posix, to work. + file_util::CreateDirectory(foo); + std::string data("hello"); + file_util::WriteFile(bar.ToWStringHack(), data.c_str(), data.length()); + file_util::WriteFile(baz.ToWStringHack(), data.c_str(), data.length()); + file_util::WriteFile(foobar.ToWStringHack(), data.c_str(), data.length()); + + EXPECT_TRUE(foo.Contains(bar)); + EXPECT_FALSE(foo.Contains(baz)); + EXPECT_FALSE(foo.Contains(foobar)); + EXPECT_FALSE(foo.Contains(foo)); + +// Platform-specific concerns + FilePath foo_caps(data_dir.Append(FILE_PATH_LITERAL("FOO"))); +#if defined(OS_WIN) + EXPECT_TRUE(foo.Contains(foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); + EXPECT_TRUE(foo.Contains( + FilePath(foo.value() + FILE_PATH_LITERAL("/bar.txt")))); +#elif defined(OS_LINUX) + EXPECT_FALSE(foo.Contains(foo_caps.Append(FILE_PATH_LITERAL("bar.txt")))); +#else + // We can't really do this test on osx since the case-sensitivity of the + // filesystem is configurable. +#endif + + // Note: whether +} |