path_util_unittest.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "extensions/browser/path_util.h"
  5. #include "base/files/file_path.h"
  6. #include "base/files/file_util.h"
  7. #include "base/path_service.h"
  8. #include "build/build_config.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. using base::FilePath;
  11. namespace extensions {
  12. // Basic unittest for path_util::PrettifyPath.
  13. // For legacy reasons, it's tested more in
  14. // FileSystemApiTest.FileSystemApiGetDisplayPathPrettify.
  15. TEST(ExtensionPathUtilTest, BasicPrettifyPathTest) {
  16. const FilePath::CharType kHomeShortcut[] = FILE_PATH_LITERAL("~");
  17. // Test prettifying empty path.
  18. FilePath unprettified;
  19. FilePath prettified = path_util::PrettifyPath(unprettified);
  20. EXPECT_EQ(unprettified, prettified);
  21. // Test home directory ("~").
  22. unprettified = base::GetHomeDir();
  23. prettified = path_util::PrettifyPath(unprettified);
  24. EXPECT_NE(unprettified, prettified);
  25. EXPECT_EQ(FilePath(kHomeShortcut), prettified);
  26. // Test with one layer ("~/foo").
  27. unprettified = unprettified.AppendASCII("foo");
  28. prettified = path_util::PrettifyPath(unprettified);
  29. EXPECT_NE(unprettified, prettified);
  30. EXPECT_EQ(FilePath(kHomeShortcut).AppendASCII("foo"), prettified);
  31. // Test with two layers ("~/foo/bar").
  32. unprettified = unprettified.AppendASCII("bar");
  33. prettified = path_util::PrettifyPath(unprettified);
  34. EXPECT_NE(unprettified, prettified);
  35. EXPECT_EQ(
  36. FilePath(kHomeShortcut).AppendASCII("foo").AppendASCII("bar"),
  37. prettified);
  38. }
  39. TEST(ExtensionPathUtilTest, ResolveHomeDirTest) {
  40. FilePath home_dir;
  41. ASSERT_TRUE(base::PathService::Get(base::DIR_HOME, &home_dir));
  42. const FilePath abs_path(FILE_PATH_LITERAL("/foo/bar/baz"));
  43. const FilePath rel_path(FILE_PATH_LITERAL("foo/bar/baz"));
  44. const FilePath rel_path_with_tilde(FILE_PATH_LITERAL("~/foo/bar"));
  45. const FilePath rel_path_with_tilde_no_separator(FILE_PATH_LITERAL("~foobar"));
  46. // This function is a no-op on Windows.
  47. #if BUILDFLAG(IS_WIN)
  48. EXPECT_EQ(rel_path_with_tilde,
  49. path_util::ResolveHomeDirectory(rel_path_with_tilde));
  50. #else
  51. EXPECT_EQ(home_dir.Append("foo/bar"),
  52. path_util::ResolveHomeDirectory(rel_path_with_tilde));
  53. // Make sure tilde without any relative path works as expected.
  54. EXPECT_EQ(home_dir,
  55. path_util::ResolveHomeDirectory(FilePath(FILE_PATH_LITERAL("~"))));
  56. EXPECT_EQ(home_dir,
  57. path_util::ResolveHomeDirectory(FilePath(FILE_PATH_LITERAL("~/"))));
  58. #endif
  59. // An absolute path without a ~ should be untouched.
  60. EXPECT_EQ(abs_path, path_util::ResolveHomeDirectory(abs_path));
  61. // A relative path without a ~ should be untouched.
  62. EXPECT_EQ(rel_path, path_util::ResolveHomeDirectory(rel_path));
  63. // A tilde, followed by a non-separator character should not
  64. // expand.
  65. EXPECT_EQ(rel_path_with_tilde_no_separator,
  66. path_util::ResolveHomeDirectory(rel_path_with_tilde_no_separator));
  67. }
  68. } // namespace extensions