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.
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/gn/header_checker.h"
9 #include "tools/gn/scheduler.h"
10 #include "tools/gn/target.h"
11 #include "tools/gn/test_with_scope.h"
15 class HeaderCheckerTest
: public testing::Test
{
18 : a_(setup_
.settings(), Label(SourceDir("//a/"), "a")),
19 b_(setup_
.settings(), Label(SourceDir("//b/"), "a")),
20 c_(setup_
.settings(), Label(SourceDir("//c/"), "c")) {
21 a_
.deps().push_back(LabelTargetPair(&b_
));
22 b_
.deps().push_back(LabelTargetPair(&c_
));
24 // Start with all public visibility.
25 a_
.visibility().SetPublic();
26 b_
.visibility().SetPublic();
27 c_
.visibility().SetPublic();
29 targets_
.push_back(&a_
);
30 targets_
.push_back(&b_
);
31 targets_
.push_back(&c_
);
39 // Some headers that are automatically set up with a dependency chain.
45 std::vector
<const Target
*> targets_
;
50 TEST_F(HeaderCheckerTest
, IsDependencyOf
) {
51 scoped_refptr
<HeaderChecker
> checker(
52 new HeaderChecker(setup_
.build_settings(), targets_
));
54 EXPECT_FALSE(checker
->IsDependencyOf(&a_
, &a_
));
55 EXPECT_TRUE(checker
->IsDependencyOf(&b_
, &a_
));
56 EXPECT_TRUE(checker
->IsDependencyOf(&c_
, &a_
));
57 EXPECT_FALSE(checker
->IsDependencyOf(&a_
, &c_
));
60 TEST_F(HeaderCheckerTest
, CheckInclude
) {
61 InputFile
input_file(SourceFile("//some_file.cc"));
62 input_file
.SetContents(std::string());
63 LocationRange range
; // Dummy value.
65 // Add a disconnected target d with a header to check that you have to have
66 // to depend on a target listing a header.
67 Target
d(setup_
.settings(), Label(SourceDir("//"), "d"));
68 SourceFile
d_header("//d_header.h");
69 d
.sources().push_back(SourceFile(d_header
));
71 // Add a header on B and say everything in B is public.
72 SourceFile
b_public("//b_public.h");
73 b_
.sources().push_back(b_public
);
74 c_
.set_all_headers_public(true);
76 // Add a public and private header on C.
77 SourceFile
c_public("//c_public.h");
78 SourceFile
c_private("//c_private.h");
79 c_
.sources().push_back(c_private
);
80 c_
.public_headers().push_back(c_public
);
81 c_
.set_all_headers_public(false);
83 targets_
.push_back(&d
);
84 scoped_refptr
<HeaderChecker
> checker(
85 new HeaderChecker(setup_
.build_settings(), targets_
));
87 // A file in target A can't include a header from D because A has no
90 EXPECT_FALSE(checker
->CheckInclude(&a_
, input_file
, d_header
, range
, &err
));
91 EXPECT_TRUE(err
.has_error());
93 // A can include the public header in B.
95 EXPECT_TRUE(checker
->CheckInclude(&a_
, input_file
, b_public
, range
, &err
));
96 EXPECT_FALSE(err
.has_error());
98 // Check A depending on the public and private headers in C.
100 EXPECT_TRUE(checker
->CheckInclude(&a_
, input_file
, c_public
, range
, &err
));
101 EXPECT_FALSE(err
.has_error());
102 EXPECT_FALSE(checker
->CheckInclude(&a_
, input_file
, c_private
, range
, &err
));
103 EXPECT_TRUE(err
.has_error());
105 // A can depend on a random file unknown to the build.
107 EXPECT_TRUE(checker
->CheckInclude(&a_
, input_file
, SourceFile("//random.h"),
109 EXPECT_FALSE(err
.has_error());
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());
115 EXPECT_FALSE(checker
->CheckInclude(&a_
, input_file
, c_public
, range
, &err
));
116 EXPECT_TRUE(err
.has_error());