blob: bdac4ffe4d9c524859431052d163303564c13a8b [file] [log] [blame]
[email protected]ff8004952014-08-08 01:03:511// 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 Giuffrida26c50262017-06-07 17:54:505#include "extensions/browser/path_util.h"
[email protected]ff8004952014-08-08 01:03:516
[email protected]ff8004952014-08-08 01:03:517#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
David Bertonia38320b2018-04-02 20:38:079#include "base/path_service.h"
10#include "build/build_config.h"
[email protected]ff8004952014-08-08 01:03:5111#include "testing/gtest/include/gtest/gtest.h"
12
13using base::FilePath;
14
15namespace extensions {
16
Michael Giuffrida26c50262017-06-07 17:54:5017// Basic unittest for path_util::PrettifyPath.
[email protected]ff8004952014-08-08 01:03:5118// For legacy reasons, it's tested more in
19// FileSystemApiTest.FileSystemApiGetDisplayPathPrettify.
20TEST(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 Bertonia38320b2018-04-02 20:38:0749TEST(ExtensionPathUtilTest, ResolveHomeDirTest) {
50 FilePath home_dir;
Avi Drissman210441b72018-05-01 15:51:0051 ASSERT_TRUE(base::PathService::Get(base::DIR_HOME, &home_dir));
David Bertonia38320b2018-04-02 20:38:0752 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]ff8004952014-08-08 01:03:5181} // namespace extensions