• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/err.h"
7 #include "tools/gn/label.h"
8 #include "tools/gn/value.h"
9 #include "tools/gn/visibility.h"
10 
11 namespace {
12 
13 struct VisPatternCase {
14   const char* input;
15   bool success;
16 
17   Visibility::VisPattern::Type type;
18   const char* dir;
19   const char* name;
20 };
21 
22 }  // namespace
23 
TEST(Visibility,PatternParse)24 TEST(Visibility, PatternParse) {
25   SourceDir current_dir("//foo/");
26   VisPatternCase cases[] = {
27     // Missing stuff.
28     { "", false, Visibility::VisPattern::MATCH, "", "" },
29     { ":", false, Visibility::VisPattern::MATCH, "", "" },
30     // Normal things.
31     { ":bar", true, Visibility::VisPattern::MATCH, "//foo/", "bar" },
32     { "//la:bar", true, Visibility::VisPattern::MATCH, "//la/", "bar" },
33     { "*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY, "", "" },
34     { ":*", true, Visibility::VisPattern::DIRECTORY, "//foo/", "" },
35     { "la:*", true, Visibility::VisPattern::DIRECTORY, "//foo/la/", "" },
36     { "la/*:*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY,
37       "//foo/la/", "" },
38     { "//la:*", true, Visibility::VisPattern::DIRECTORY, "//la/", "" },
39     { "./*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY, "//foo/", "" },
40     { "foo/*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY,
41       "//foo/foo/", "" },
42     { "//l/*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY, "//l/", "" },
43     // No toolchains allowed.
44     { "//foo(//bar)", false, Visibility::VisPattern::MATCH, "", "" },
45     // Wildcards in invalid places.
46     { "*foo*:bar", false, Visibility::VisPattern::MATCH, "", "" },
47     { "foo*:*bar", false, Visibility::VisPattern::MATCH, "", "" },
48     { "*foo:bar", false, Visibility::VisPattern::MATCH, "", "" },
49     { "foo:bar*", false, Visibility::VisPattern::MATCH, "", "" },
50     { "*:*", true, Visibility::VisPattern::RECURSIVE_DIRECTORY, "", "" },
51   };
52 
53   for (size_t i = 0; i < arraysize(cases); i++) {
54     const VisPatternCase& cur = cases[i];
55     Err err;
56     Visibility::VisPattern result =
57         Visibility::GetPattern(current_dir, Value(NULL, cur.input), &err);
58 
59     EXPECT_EQ(cur.success, !err.has_error()) << i << " " << cur.input;
60     EXPECT_EQ(cur.type, result.type()) << i << " " << cur.input;
61     EXPECT_EQ(cur.dir, result.dir().value()) << i << " " << cur.input;
62     EXPECT_EQ(cur.name, result.name()) << i << " " << cur.input;
63   }
64 }
65 
TEST(Visibility,CanSeeMe)66 TEST(Visibility, CanSeeMe) {
67   Value list(NULL, Value::LIST);
68   list.list_value().push_back(Value(NULL, "//rec/*"));  // Recursive.
69   list.list_value().push_back(Value(NULL, "//dir:*"));  // One dir.
70   list.list_value().push_back(Value(NULL, "//my:name"));  // Exact match.
71 
72   Err err;
73   Visibility vis;
74   ASSERT_TRUE(vis.Set(SourceDir("//"), list, &err));
75 
76   EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//random/"), "thing")));
77   EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//my/"), "notname")));
78 
79   EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//my/"), "name")));
80   EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//rec/"), "anything")));
81   EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//rec/a/"), "anything")));
82   EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//rec/b/"), "anything")));
83   EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//dir/"), "anything")));
84   EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//dir/a/"), "anything")));
85   EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//directory/"), "anything")));
86 }
87 
TEST(Visibility,Public)88 TEST(Visibility, Public) {
89   Err err;
90   Visibility vis;
91   ASSERT_TRUE(vis.Set(SourceDir("//"), Value(NULL, "*"), &err));
92 
93   EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//random/"), "thing")));
94   EXPECT_TRUE(vis.CanSeeMe(Label(SourceDir("//"), "")));
95 }
96 
TEST(Visibility,Private)97 TEST(Visibility, Private) {
98   Err err;
99   Visibility vis;
100   ASSERT_TRUE(vis.Set(SourceDir("//"), Value(NULL, Value::LIST), &err));
101 
102   EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//random/"), "thing")));
103   EXPECT_FALSE(vis.CanSeeMe(Label(SourceDir("//"), "")));
104 }
105