1 // Copyright (c) 2013 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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/err.h"
7 #include "tools/gn/source_dir.h"
8 #include "tools/gn/source_file.h"
9 #include "tools/gn/value.h"
11 TEST(SourceDir
, ResolveRelativeFile
) {
13 SourceDir
base("//base/");
15 base::StringPiece
source_root("C:/source/root");
17 base::StringPiece
source_root("/source/root");
20 // Empty input is an error.
21 EXPECT_TRUE(base
.ResolveRelativeFile(
22 Value(nullptr, std::string()), &err
, source_root
) == SourceFile());
23 EXPECT_TRUE(err
.has_error());
25 // These things are directories, so should be an error.
27 EXPECT_TRUE(base
.ResolveRelativeFile(
28 Value(nullptr, "//foo/bar/"), &err
, source_root
) == SourceFile());
29 EXPECT_TRUE(err
.has_error());
32 EXPECT_TRUE(base
.ResolveRelativeFile(
33 Value(nullptr, "bar/"), &err
, source_root
) == SourceFile());
34 EXPECT_TRUE(err
.has_error());
36 // Absolute paths should be passed unchanged.
38 EXPECT_TRUE(base
.ResolveRelativeFile(
39 Value(nullptr, "//foo"), &err
, source_root
) == SourceFile("//foo"));
40 EXPECT_FALSE(err
.has_error());
42 EXPECT_TRUE(base
.ResolveRelativeFile(
43 Value(nullptr, "/foo"), &err
, source_root
) == SourceFile("/foo"));
44 EXPECT_FALSE(err
.has_error());
46 // Basic relative stuff.
47 EXPECT_TRUE(base
.ResolveRelativeFile(
48 Value(nullptr, "foo"), &err
, source_root
) == SourceFile("//base/foo"));
49 EXPECT_FALSE(err
.has_error());
50 EXPECT_TRUE(base
.ResolveRelativeFile(
51 Value(nullptr, "./foo"), &err
, source_root
) == SourceFile("//base/foo"));
52 EXPECT_FALSE(err
.has_error());
53 EXPECT_TRUE(base
.ResolveRelativeFile(
54 Value(nullptr, "../foo"), &err
, source_root
) == SourceFile("//foo"));
55 EXPECT_FALSE(err
.has_error());
57 // If the given relative path points outside the source root, we
58 // expect an absolute path.
60 EXPECT_TRUE(base
.ResolveRelativeFile(
61 Value(nullptr, "../../foo"), &err
, source_root
) ==
62 SourceFile("/C:/source/foo"));
63 EXPECT_FALSE(err
.has_error());
65 EXPECT_TRUE(base
.ResolveRelativeFile(
66 Value(nullptr, "../../foo"), &err
, source_root
) ==
67 SourceFile("/source/foo"));
68 EXPECT_FALSE(err
.has_error());
72 // Note that we don't canonicalize the backslashes to forward slashes.
73 // This could potentially be changed in the future which would mean we should
74 // just change the expected result.
75 EXPECT_TRUE(base
.ResolveRelativeFile(
76 Value(nullptr, "C:\\foo\\bar.txt"), &err
, source_root
) ==
77 SourceFile("/C:/foo/bar.txt"));
78 EXPECT_FALSE(err
.has_error());
82 TEST(SourceDir
, ResolveRelativeDir
) {
84 SourceDir
base("//base/");
86 base::StringPiece
source_root("C:/source/root");
88 base::StringPiece
source_root("/source/root");
91 // Empty input is an error.
92 EXPECT_TRUE(base
.ResolveRelativeDir(
93 Value(nullptr, std::string()), &err
, source_root
) == SourceDir());
94 EXPECT_TRUE(err
.has_error());
96 // Absolute paths should be passed unchanged.
98 EXPECT_TRUE(base
.ResolveRelativeDir(
99 Value(nullptr, "//foo"), &err
, source_root
) == SourceDir("//foo/"));
100 EXPECT_FALSE(err
.has_error());
101 EXPECT_TRUE(base
.ResolveRelativeDir(
102 Value(nullptr, "/foo"), &err
, source_root
) == SourceDir("/foo/"));
103 EXPECT_FALSE(err
.has_error());
105 // Basic relative stuff.
106 EXPECT_TRUE(base
.ResolveRelativeDir(
107 Value(nullptr, "foo"), &err
, source_root
) == SourceDir("//base/foo/"));
108 EXPECT_FALSE(err
.has_error());
109 EXPECT_TRUE(base
.ResolveRelativeDir(
110 Value(nullptr, "./foo"), &err
, source_root
) == SourceDir("//base/foo/"));
111 EXPECT_FALSE(err
.has_error());
112 EXPECT_TRUE(base
.ResolveRelativeDir(
113 Value(nullptr, "../foo"), &err
, source_root
) == SourceDir("//foo/"));
114 EXPECT_FALSE(err
.has_error());
116 // If the given relative path points outside the source root, we
117 // expect an absolute path.
119 EXPECT_TRUE(base
.ResolveRelativeDir(
120 Value(nullptr, "../../foo"), &err
, source_root
) ==
121 SourceDir("/C:/source/foo/"));
122 EXPECT_FALSE(err
.has_error());
124 EXPECT_TRUE(base
.ResolveRelativeDir(
125 Value(nullptr, "../../foo"), &err
, source_root
) ==
126 SourceDir("/source/foo/"));
127 EXPECT_FALSE(err
.has_error());
131 // Canonicalize the existing backslashes to forward slashes and add a
132 // leading slash if necessary.
133 EXPECT_TRUE(base
.ResolveRelativeDir(
134 Value(nullptr, "\\C:\\foo"), &err
) == SourceDir("/C:/foo/"));
135 EXPECT_FALSE(err
.has_error());
136 EXPECT_TRUE(base
.ResolveRelativeDir(
137 Value(nullptr, "C:\\foo"), &err
) == SourceDir("/C:/foo/"));
138 EXPECT_FALSE(err
.has_error());