Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: tools/gn/header_checker_unittest.cc

Issue 231813002: Improve GN public header file checking (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/gn/header_checker.cc ('k') | tools/gn/input_conversion.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 <vector> 5 #include <vector>
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/gn/header_checker.h" 8 #include "tools/gn/header_checker.h"
9 #include "tools/gn/scheduler.h" 9 #include "tools/gn/scheduler.h"
10 #include "tools/gn/target.h" 10 #include "tools/gn/target.h"
11 #include "tools/gn/test_with_scope.h" 11 #include "tools/gn/test_with_scope.h"
12 12
13 namespace { 13 namespace {
14 14
15 class HeaderCheckerTest : public testing::Test { 15 class HeaderCheckerTest : public testing::Test {
16 public: 16 public:
17 HeaderCheckerTest() 17 HeaderCheckerTest()
18 : a_(setup_.settings(), Label(SourceDir("//"), "a")), 18 : a_(setup_.settings(), Label(SourceDir("//a/"), "a")),
19 b_(setup_.settings(), Label(SourceDir("//"), "a")), 19 b_(setup_.settings(), Label(SourceDir("//b/"), "a")),
20 c_(setup_.settings(), Label(SourceDir("//"), "c")) { 20 c_(setup_.settings(), Label(SourceDir("//c/"), "c")) {
21 a_.deps().push_back(LabelTargetPair(&b_)); 21 a_.deps().push_back(LabelTargetPair(&b_));
22 b_.deps().push_back(LabelTargetPair(&c_)); 22 b_.deps().push_back(LabelTargetPair(&c_));
23 23
24 // Start with all public visibility.
25 a_.visibility().SetPublic();
26 b_.visibility().SetPublic();
27 c_.visibility().SetPublic();
28
24 targets_.push_back(&a_); 29 targets_.push_back(&a_);
25 targets_.push_back(&b_); 30 targets_.push_back(&b_);
26 targets_.push_back(&c_); 31 targets_.push_back(&c_);
27 } 32 }
28 33
29 protected: 34 protected:
30 Scheduler scheduler_; 35 Scheduler scheduler_;
31 36
32 TestWithScope setup_; 37 TestWithScope setup_;
33 38
(...skipping 12 matching lines...) Expand all
46 scoped_refptr<HeaderChecker> checker( 51 scoped_refptr<HeaderChecker> checker(
47 new HeaderChecker(setup_.build_settings(), targets_)); 52 new HeaderChecker(setup_.build_settings(), targets_));
48 53
49 EXPECT_FALSE(checker->IsDependencyOf(&a_, &a_)); 54 EXPECT_FALSE(checker->IsDependencyOf(&a_, &a_));
50 EXPECT_TRUE(checker->IsDependencyOf(&b_, &a_)); 55 EXPECT_TRUE(checker->IsDependencyOf(&b_, &a_));
51 EXPECT_TRUE(checker->IsDependencyOf(&c_, &a_)); 56 EXPECT_TRUE(checker->IsDependencyOf(&c_, &a_));
52 EXPECT_FALSE(checker->IsDependencyOf(&a_, &c_)); 57 EXPECT_FALSE(checker->IsDependencyOf(&a_, &c_));
53 } 58 }
54 59
55 TEST_F(HeaderCheckerTest, CheckInclude) { 60 TEST_F(HeaderCheckerTest, CheckInclude) {
61 InputFile input_file(SourceFile("//some_file.cc"));
62 input_file.SetContents(std::string());
63 LocationRange range; // Dummy value.
64
56 // Add a disconnected target d with a header to check that you have to have 65 // Add a disconnected target d with a header to check that you have to have
57 // to depend on a target listing a header. 66 // to depend on a target listing a header.
58 Target d(setup_.settings(), Label(SourceDir("//"), "d")); 67 Target d(setup_.settings(), Label(SourceDir("//"), "d"));
59 SourceFile d_header("//d_header.h"); 68 SourceFile d_header("//d_header.h");
60 d.sources().push_back(SourceFile(d_header)); 69 d.sources().push_back(SourceFile(d_header));
61 70
62 // Add a header on B and say everything in B is public. 71 // Add a header on B and say everything in B is public.
63 SourceFile b_public("//b_public.h"); 72 SourceFile b_public("//b_public.h");
64 b_.sources().push_back(b_public); 73 b_.sources().push_back(b_public);
65 c_.set_all_headers_public(true); 74 c_.set_all_headers_public(true);
66 75
67 // Add a public and private header on C. 76 // Add a public and private header on C.
68 SourceFile c_public("//c_public.h"); 77 SourceFile c_public("//c_public.h");
69 SourceFile c_private("//c_private.h"); 78 SourceFile c_private("//c_private.h");
70 c_.sources().push_back(c_private); 79 c_.sources().push_back(c_private);
71 c_.public_headers().push_back(c_public); 80 c_.public_headers().push_back(c_public);
72 c_.set_all_headers_public(false); 81 c_.set_all_headers_public(false);
73 82
74 targets_.push_back(&d); 83 targets_.push_back(&d);
75 scoped_refptr<HeaderChecker> checker( 84 scoped_refptr<HeaderChecker> checker(
76 new HeaderChecker(setup_.build_settings(), targets_)); 85 new HeaderChecker(setup_.build_settings(), targets_));
77 86
78 // A file in target A can't include a header from D because A has no 87 // A file in target A can't include a header from D because A has no
79 // dependency on D. 88 // dependency on D.
80 Err err; 89 Err err;
81 SourceFile source_file("//some_file.cc"); 90 EXPECT_FALSE(checker->CheckInclude(&a_, input_file, d_header, range, &err));
82 EXPECT_FALSE(checker->CheckInclude(&a_, source_file, d_header, &err));
83 EXPECT_TRUE(err.has_error()); 91 EXPECT_TRUE(err.has_error());
84 92
85 // A can include the public header in B. 93 // A can include the public header in B.
86 err = Err(); 94 err = Err();
87 EXPECT_TRUE(checker->CheckInclude(&a_, source_file, b_public, &err)); 95 EXPECT_TRUE(checker->CheckInclude(&a_, input_file, b_public, range, &err));
88 EXPECT_FALSE(err.has_error()); 96 EXPECT_FALSE(err.has_error());
89 97
90 // Check A depending on the public and private headers in C. 98 // Check A depending on the public and private headers in C.
91 err = Err(); 99 err = Err();
92 EXPECT_TRUE(checker->CheckInclude(&a_, source_file, c_public, &err)); 100 EXPECT_TRUE(checker->CheckInclude(&a_, input_file, c_public, range, &err));
93 EXPECT_FALSE(err.has_error()); 101 EXPECT_FALSE(err.has_error());
94 EXPECT_FALSE(checker->CheckInclude(&a_, source_file, c_private, &err)); 102 EXPECT_FALSE(checker->CheckInclude(&a_, input_file, c_private, range, &err));
95 EXPECT_TRUE(err.has_error()); 103 EXPECT_TRUE(err.has_error());
96 104
97 // A can depend on a random file unknown to the build. 105 // A can depend on a random file unknown to the build.
98 err = Err(); 106 err = Err();
99 EXPECT_TRUE(checker->CheckInclude(&a_, source_file, SourceFile("//random.h"), 107 EXPECT_TRUE(checker->CheckInclude(&a_, input_file, SourceFile("//random.h"),
100 &err)); 108 range, &err));
101 EXPECT_FALSE(err.has_error()); 109 EXPECT_FALSE(err.has_error());
110
111 // If C is not visible from A, A can't include public headers even if there
112 // is a dependency path.
113 c_.visibility().SetPrivate(c_.label().dir());
114 err = Err();
115 EXPECT_FALSE(checker->CheckInclude(&a_, input_file, c_public, range, &err));
116 EXPECT_TRUE(err.has_error());
102 } 117 }
OLDNEW
« no previous file with comments | « tools/gn/header_checker.cc ('k') | tools/gn/input_conversion.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698