[email protected] | ff800495 | 2014-08-08 01:03:51 | [diff] [blame] | 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 | |
Michael Giuffrida | 26c5026 | 2017-06-07 17:54:50 | [diff] [blame] | 5 | #include "extensions/browser/path_util.h" |
[email protected] | ff800495 | 2014-08-08 01:03:51 | [diff] [blame] | 6 | |
[email protected] | ff800495 | 2014-08-08 01:03:51 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
David Bertoni | a38320b | 2018-04-02 20:38:07 | [diff] [blame] | 9 | #include "base/path_service.h" |
| 10 | #include "build/build_config.h" |
[email protected] | ff800495 | 2014-08-08 01:03:51 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | using base::FilePath; |
| 14 | |
| 15 | namespace extensions { |
| 16 | |
Michael Giuffrida | 26c5026 | 2017-06-07 17:54:50 | [diff] [blame] | 17 | // Basic unittest for path_util::PrettifyPath. |
[email protected] | ff800495 | 2014-08-08 01:03:51 | [diff] [blame] | 18 | // For legacy reasons, it's tested more in |
| 19 | // FileSystemApiTest.FileSystemApiGetDisplayPathPrettify. |
| 20 | TEST(ExtensionPathUtilTest, BasicPrettifyPathTest) { |
| 21 | const FilePath::CharType kHomeShortcut[] = FILE_PATH_LITERAL("~"); |
| 22 | |
| 23 | // Test prettifying empty path. |
| 24 | FilePath unprettified; |
| 25 | FilePath prettified = path_util::PrettifyPath(unprettified); |
| 26 | EXPECT_EQ(unprettified, prettified); |
| 27 | |
| 28 | // Test home directory ("~"). |
| 29 | unprettified = base::GetHomeDir(); |
| 30 | prettified = path_util::PrettifyPath(unprettified); |
| 31 | EXPECT_NE(unprettified, prettified); |
| 32 | EXPECT_EQ(FilePath(kHomeShortcut), prettified); |
| 33 | |
| 34 | // Test with one layer ("~/foo"). |
| 35 | unprettified = unprettified.AppendASCII("foo"); |
| 36 | prettified = path_util::PrettifyPath(unprettified); |
| 37 | EXPECT_NE(unprettified, prettified); |
| 38 | EXPECT_EQ(FilePath(kHomeShortcut).AppendASCII("foo"), prettified); |
| 39 | |
| 40 | // Test with two layers ("~/foo/bar"). |
| 41 | unprettified = unprettified.AppendASCII("bar"); |
| 42 | prettified = path_util::PrettifyPath(unprettified); |
| 43 | EXPECT_NE(unprettified, prettified); |
| 44 | EXPECT_EQ( |
| 45 | FilePath(kHomeShortcut).AppendASCII("foo").AppendASCII("bar"), |
| 46 | prettified); |
| 47 | } |
| 48 | |
David Bertoni | a38320b | 2018-04-02 20:38:07 | [diff] [blame] | 49 | TEST(ExtensionPathUtilTest, ResolveHomeDirTest) { |
| 50 | FilePath home_dir; |
Avi Drissman | 210441b7 | 2018-05-01 15:51:00 | [diff] [blame] | 51 | ASSERT_TRUE(base::PathService::Get(base::DIR_HOME, &home_dir)); |
David Bertoni | a38320b | 2018-04-02 20:38:07 | [diff] [blame] | 52 | const FilePath abs_path(FILE_PATH_LITERAL("/foo/bar/baz")); |
| 53 | const FilePath rel_path(FILE_PATH_LITERAL("foo/bar/baz")); |
| 54 | const FilePath rel_path_with_tilde(FILE_PATH_LITERAL("~/foo/bar")); |
| 55 | const FilePath rel_path_with_tilde_no_separator(FILE_PATH_LITERAL("~foobar")); |
| 56 | |
| 57 | // This function is a no-op on Windows. |
| 58 | #if defined(OS_WIN) |
| 59 | EXPECT_EQ(rel_path_with_tilde, |
| 60 | path_util::ResolveHomeDirectory(rel_path_with_tilde)); |
| 61 | #else |
| 62 | EXPECT_EQ(home_dir.Append("foo/bar"), |
| 63 | path_util::ResolveHomeDirectory(rel_path_with_tilde)); |
| 64 | // Make sure tilde without any relative path works as expected. |
| 65 | EXPECT_EQ(home_dir, |
| 66 | path_util::ResolveHomeDirectory(FilePath(FILE_PATH_LITERAL("~")))); |
| 67 | EXPECT_EQ(home_dir, |
| 68 | path_util::ResolveHomeDirectory(FilePath(FILE_PATH_LITERAL("~/")))); |
| 69 | #endif |
| 70 | |
| 71 | // An absolute path without a ~ should be untouched. |
| 72 | EXPECT_EQ(abs_path, path_util::ResolveHomeDirectory(abs_path)); |
| 73 | // A relative path without a ~ should be untouched. |
| 74 | EXPECT_EQ(rel_path, path_util::ResolveHomeDirectory(rel_path)); |
| 75 | // A tilde, followed by a non-separator character should not |
| 76 | // expand. |
| 77 | EXPECT_EQ(rel_path_with_tilde_no_separator, |
| 78 | path_util::ResolveHomeDirectory(rel_path_with_tilde_no_separator)); |
| 79 | } |
| 80 | |
[email protected] | ff800495 | 2014-08-08 01:03:51 | [diff] [blame] | 81 | } // namespace extensions |