| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "tools/gn/err.h" |
| 6 #include "tools/gn/source_dir.h" | 7 #include "tools/gn/source_dir.h" |
| 7 #include "tools/gn/source_file.h" | 8 #include "tools/gn/source_file.h" |
| 9 #include "tools/gn/value.h" |
| 8 | 10 |
| 9 TEST(SourceDir, ResolveRelativeFile) { | 11 TEST(SourceDir, ResolveRelativeFile) { |
| 12 Err err; |
| 10 SourceDir base("//base/"); | 13 SourceDir base("//base/"); |
| 11 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
| 12 base::StringPiece source_root("C:/source/root"); | 15 base::StringPiece source_root("C:/source/root"); |
| 13 #else | 16 #else |
| 14 base::StringPiece source_root("/source/root"); | 17 base::StringPiece source_root("/source/root"); |
| 15 #endif | 18 #endif |
| 16 | 19 |
| 17 // Empty input is an error. | 20 // Empty input is an error. |
| 18 EXPECT_TRUE(base.ResolveRelativeFile("", source_root) == SourceFile()); | 21 EXPECT_TRUE(base.ResolveRelativeFile( |
| 22 Value(nullptr, std::string()), &err, source_root) == SourceFile()); |
| 23 EXPECT_TRUE(err.has_error()); |
| 19 | 24 |
| 20 // These things are directories, so should be an error. | 25 // These things are directories, so should be an error. |
| 21 EXPECT_TRUE(base.ResolveRelativeFile("//foo/bar/", source_root) == | 26 err = Err(); |
| 22 SourceFile()); | 27 EXPECT_TRUE(base.ResolveRelativeFile( |
| 23 EXPECT_TRUE(base.ResolveRelativeFile("bar/", source_root) == | 28 Value(nullptr, "//foo/bar/"), &err, source_root) == SourceFile()); |
| 24 SourceFile()); | 29 EXPECT_TRUE(err.has_error()); |
| 30 |
| 31 err = Err(); |
| 32 EXPECT_TRUE(base.ResolveRelativeFile( |
| 33 Value(nullptr, "bar/"), &err, source_root) == SourceFile()); |
| 34 EXPECT_TRUE(err.has_error()); |
| 25 | 35 |
| 26 // Absolute paths should be passed unchanged. | 36 // Absolute paths should be passed unchanged. |
| 27 EXPECT_TRUE(base.ResolveRelativeFile("//foo",source_root) == | 37 err = Err(); |
| 28 SourceFile("//foo")); | 38 EXPECT_TRUE(base.ResolveRelativeFile( |
| 29 EXPECT_TRUE(base.ResolveRelativeFile("/foo", source_root) == | 39 Value(nullptr, "//foo"), &err, source_root) == SourceFile("//foo")); |
| 30 SourceFile("/foo")); | 40 EXPECT_FALSE(err.has_error()); |
| 41 |
| 42 EXPECT_TRUE(base.ResolveRelativeFile( |
| 43 Value(nullptr, "/foo"), &err, source_root) == SourceFile("/foo")); |
| 44 EXPECT_FALSE(err.has_error()); |
| 31 | 45 |
| 32 // Basic relative stuff. | 46 // Basic relative stuff. |
| 33 EXPECT_TRUE(base.ResolveRelativeFile("foo", source_root) == | 47 EXPECT_TRUE(base.ResolveRelativeFile( |
| 34 SourceFile("//base/foo")); | 48 Value(nullptr, "foo"), &err, source_root) == SourceFile("//base/foo")); |
| 35 EXPECT_TRUE(base.ResolveRelativeFile("./foo", source_root) == | 49 EXPECT_FALSE(err.has_error()); |
| 36 SourceFile("//base/foo")); | 50 EXPECT_TRUE(base.ResolveRelativeFile( |
| 37 EXPECT_TRUE(base.ResolveRelativeFile("../foo", source_root) == | 51 Value(nullptr, "./foo"), &err, source_root) == SourceFile("//base/foo")); |
| 38 SourceFile("//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()); |
| 39 | 56 |
| 40 // If the given relative path points outside the source root, we | 57 // If the given relative path points outside the source root, we |
| 41 // expect an absolute path. | 58 // expect an absolute path. |
| 42 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
| 43 EXPECT_TRUE(base.ResolveRelativeFile("../../foo", source_root) == | 60 EXPECT_TRUE(base.ResolveRelativeFile( |
| 44 SourceFile("/C:/source/foo")); | 61 Value(nullptr, "../../foo"), &err, source_root) == |
| 62 SourceFile("/C:/source/foo")); |
| 63 EXPECT_FALSE(err.has_error()); |
| 45 #else | 64 #else |
| 46 EXPECT_TRUE(base.ResolveRelativeFile("../../foo", source_root) == | 65 EXPECT_TRUE(base.ResolveRelativeFile( |
| 47 SourceFile("/source/foo")); | 66 Value(nullptr, "../../foo"), &err, source_root) == |
| 67 SourceFile("/source/foo")); |
| 68 EXPECT_FALSE(err.has_error()); |
| 48 #endif | 69 #endif |
| 49 | 70 |
| 50 #if defined(OS_WIN) | 71 #if defined(OS_WIN) |
| 51 // Note that we don't canonicalize the backslashes to forward slashes. | 72 // Note that we don't canonicalize the backslashes to forward slashes. |
| 52 // This could potentially be changed in the future which would mean we should | 73 // This could potentially be changed in the future which would mean we should |
| 53 // just change the expected result. | 74 // just change the expected result. |
| 54 EXPECT_TRUE(base.ResolveRelativeFile("C:\\foo\\bar.txt", source_root) == | 75 EXPECT_TRUE(base.ResolveRelativeFile( |
| 55 SourceFile("/C:/foo/bar.txt")); | 76 Value(nullptr, "C:\\foo\\bar.txt"), &err, source_root) == |
| 77 SourceFile("/C:/foo/bar.txt")); |
| 78 EXPECT_FALSE(err.has_error()); |
| 56 #endif | 79 #endif |
| 57 } | 80 } |
| 58 | 81 |
| 59 TEST(SourceDir, ResolveRelativeDir) { | 82 TEST(SourceDir, ResolveRelativeDir) { |
| 83 Err err; |
| 60 SourceDir base("//base/"); | 84 SourceDir base("//base/"); |
| 61 #if defined(OS_WIN) | 85 #if defined(OS_WIN) |
| 62 base::StringPiece source_root("C:/source/root"); | 86 base::StringPiece source_root("C:/source/root"); |
| 63 #else | 87 #else |
| 64 base::StringPiece source_root("/source/root"); | 88 base::StringPiece source_root("/source/root"); |
| 65 #endif | 89 #endif |
| 66 | 90 |
| 67 // Empty input is an error. | 91 // Empty input is an error. |
| 68 EXPECT_TRUE(base.ResolveRelativeDir("", source_root) == SourceDir()); | 92 EXPECT_TRUE(base.ResolveRelativeDir( |
| 93 Value(nullptr, std::string()), &err, source_root) == SourceDir()); |
| 94 EXPECT_TRUE(err.has_error()); |
| 69 | 95 |
| 70 // Absolute paths should be passed unchanged. | 96 // Absolute paths should be passed unchanged. |
| 71 EXPECT_TRUE(base.ResolveRelativeDir("//foo", source_root) == | 97 err = Err(); |
| 72 SourceDir("//foo/")); | 98 EXPECT_TRUE(base.ResolveRelativeDir( |
| 73 EXPECT_TRUE(base.ResolveRelativeDir("/foo", source_root) == | 99 Value(nullptr, "//foo"), &err, source_root) == SourceDir("//foo/")); |
| 74 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()); |
| 75 | 104 |
| 76 // Basic relative stuff. | 105 // Basic relative stuff. |
| 77 EXPECT_TRUE(base.ResolveRelativeDir("foo", source_root) == | 106 EXPECT_TRUE(base.ResolveRelativeDir( |
| 78 SourceDir("//base/foo/")); | 107 Value(nullptr, "foo"), &err, source_root) == SourceDir("//base/foo/")); |
| 79 EXPECT_TRUE(base.ResolveRelativeDir("./foo", source_root) == | 108 EXPECT_FALSE(err.has_error()); |
| 80 SourceDir("//base/foo/")); | 109 EXPECT_TRUE(base.ResolveRelativeDir( |
| 81 EXPECT_TRUE(base.ResolveRelativeDir("../foo", source_root) == | 110 Value(nullptr, "./foo"), &err, source_root) == SourceDir("//base/foo/")); |
| 82 SourceDir("//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()); |
| 83 | 115 |
| 84 // If the given relative path points outside the source root, we | 116 // If the given relative path points outside the source root, we |
| 85 // expect an absolute path. | 117 // expect an absolute path. |
| 86 #if defined(OS_WIN) | 118 #if defined(OS_WIN) |
| 87 EXPECT_TRUE(base.ResolveRelativeDir("../../foo", source_root) == | 119 EXPECT_TRUE(base.ResolveRelativeDir( |
| 88 SourceDir("/C:/source/foo/")); | 120 Value(nullptr, "../../foo"), &err, source_root) == |
| 121 SourceDir("/C:/source/foo/")); |
| 122 EXPECT_FALSE(err.has_error()); |
| 89 #else | 123 #else |
| 90 EXPECT_TRUE(base.ResolveRelativeDir("../../foo", source_root) == | 124 EXPECT_TRUE(base.ResolveRelativeDir( |
| 91 SourceDir("/source/foo/")); | 125 Value(nullptr, "../../foo"), &err, source_root) == |
| 126 SourceDir("/source/foo/")); |
| 127 EXPECT_FALSE(err.has_error()); |
| 92 #endif | 128 #endif |
| 93 | 129 |
| 94 #if defined(OS_WIN) | 130 #if defined(OS_WIN) |
| 95 // Canonicalize the existing backslashes to forward slashes and add a | 131 // Canonicalize the existing backslashes to forward slashes and add a |
| 96 // leading slash if necessary. | 132 // leading slash if necessary. |
| 97 EXPECT_TRUE(base.ResolveRelativeDir("\\C:\\foo") == SourceDir("/C:/foo/")); | 133 EXPECT_TRUE(base.ResolveRelativeDir( |
| 98 EXPECT_TRUE(base.ResolveRelativeDir("C:\\foo") == SourceDir("/C:/foo/")); | 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()); |
| 99 #endif | 139 #endif |
| 100 } | 140 } |
| OLD | NEW |