1 //
2 // Copyright 2019 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "absl/flags/usage_config.h"
17
18 #include <string>
19
20 #include "gtest/gtest.h"
21 #include "absl/flags/internal/path_util.h"
22 #include "absl/flags/internal/program_name.h"
23 #include "absl/strings/match.h"
24 #include "absl/strings/string_view.h"
25
26 namespace {
27
28 class FlagsUsageConfigTest : public testing::Test {
29 protected:
SetUp()30 void SetUp() override {
31 // Install Default config for the use on this unit test.
32 // Binary may install a custom config before tests are run.
33 absl::FlagsUsageConfig default_config;
34 absl::SetFlagsUsageConfig(default_config);
35 }
36 };
37
38 namespace flags = absl::flags_internal;
39
TstContainsHelpshortFlags(absl::string_view f)40 bool TstContainsHelpshortFlags(absl::string_view f) {
41 return absl::StartsWith(flags::Basename(f), "progname.");
42 }
43
TstContainsHelppackageFlags(absl::string_view f)44 bool TstContainsHelppackageFlags(absl::string_view f) {
45 return absl::EndsWith(flags::Package(f), "aaa/");
46 }
47
TstContainsHelpFlags(absl::string_view f)48 bool TstContainsHelpFlags(absl::string_view f) {
49 return absl::EndsWith(flags::Package(f), "zzz/");
50 }
51
TstVersionString()52 std::string TstVersionString() { return "program 1.0.0"; }
53
TstNormalizeFilename(absl::string_view filename)54 std::string TstNormalizeFilename(absl::string_view filename) {
55 return std::string(filename.substr(2));
56 }
57
TstReportUsageMessage(absl::string_view msg)58 void TstReportUsageMessage(absl::string_view msg) {}
59
60 // --------------------------------------------------------------------
61
TEST_F(FlagsUsageConfigTest,TestGetSetFlagsUsageConfig)62 TEST_F(FlagsUsageConfigTest, TestGetSetFlagsUsageConfig) {
63 EXPECT_TRUE(flags::GetUsageConfig().contains_helpshort_flags);
64 EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags);
65 EXPECT_TRUE(flags::GetUsageConfig().contains_helppackage_flags);
66 EXPECT_TRUE(flags::GetUsageConfig().version_string);
67 EXPECT_TRUE(flags::GetUsageConfig().normalize_filename);
68
69 absl::FlagsUsageConfig empty_config;
70 empty_config.contains_helpshort_flags = &TstContainsHelpshortFlags;
71 empty_config.contains_help_flags = &TstContainsHelpFlags;
72 empty_config.contains_helppackage_flags = &TstContainsHelppackageFlags;
73 empty_config.version_string = &TstVersionString;
74 empty_config.normalize_filename = &TstNormalizeFilename;
75 absl::SetFlagsUsageConfig(empty_config);
76
77 EXPECT_TRUE(flags::GetUsageConfig().contains_helpshort_flags);
78 EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags);
79 EXPECT_TRUE(flags::GetUsageConfig().contains_helppackage_flags);
80 EXPECT_TRUE(flags::GetUsageConfig().version_string);
81 EXPECT_TRUE(flags::GetUsageConfig().normalize_filename);
82 }
83
84 // --------------------------------------------------------------------
85
TEST_F(FlagsUsageConfigTest,TestContainsHelpshortFlags)86 TEST_F(FlagsUsageConfigTest, TestContainsHelpshortFlags) {
87 flags::SetProgramInvocationName("usage_config_test");
88
89 auto config = flags::GetUsageConfig();
90 EXPECT_TRUE(config.contains_helpshort_flags("adir/cd/usage_config_test.cc"));
91 EXPECT_TRUE(
92 config.contains_helpshort_flags("aaaa/usage_config_test-main.cc"));
93 EXPECT_TRUE(config.contains_helpshort_flags("abc/usage_config_test_main.cc"));
94 EXPECT_FALSE(config.contains_helpshort_flags("usage_config_main.cc"));
95
96 absl::FlagsUsageConfig empty_config;
97 empty_config.contains_helpshort_flags = &TstContainsHelpshortFlags;
98 absl::SetFlagsUsageConfig(empty_config);
99
100 EXPECT_TRUE(
101 flags::GetUsageConfig().contains_helpshort_flags("aaa/progname.cpp"));
102 EXPECT_FALSE(
103 flags::GetUsageConfig().contains_helpshort_flags("aaa/progmane.cpp"));
104 }
105
106 // --------------------------------------------------------------------
107
TEST_F(FlagsUsageConfigTest,TestContainsHelpFlags)108 TEST_F(FlagsUsageConfigTest, TestContainsHelpFlags) {
109 flags::SetProgramInvocationName("usage_config_test");
110
111 auto config = flags::GetUsageConfig();
112 EXPECT_TRUE(config.contains_help_flags("zzz/usage_config_test.cc"));
113 EXPECT_TRUE(
114 config.contains_help_flags("bdir/a/zzz/usage_config_test-main.cc"));
115 EXPECT_TRUE(
116 config.contains_help_flags("//aqse/zzz/usage_config_test_main.cc"));
117 EXPECT_FALSE(config.contains_help_flags("zzz/aa/usage_config_main.cc"));
118
119 absl::FlagsUsageConfig empty_config;
120 empty_config.contains_help_flags = &TstContainsHelpFlags;
121 absl::SetFlagsUsageConfig(empty_config);
122
123 EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags("zzz/main-body.c"));
124 EXPECT_FALSE(
125 flags::GetUsageConfig().contains_help_flags("zzz/dir/main-body.c"));
126 }
127
128 // --------------------------------------------------------------------
129
TEST_F(FlagsUsageConfigTest,TestContainsHelppackageFlags)130 TEST_F(FlagsUsageConfigTest, TestContainsHelppackageFlags) {
131 flags::SetProgramInvocationName("usage_config_test");
132
133 auto config = flags::GetUsageConfig();
134 EXPECT_TRUE(config.contains_helppackage_flags("aaa/usage_config_test.cc"));
135 EXPECT_TRUE(
136 config.contains_helppackage_flags("bbdir/aaa/usage_config_test-main.cc"));
137 EXPECT_TRUE(config.contains_helppackage_flags(
138 "//aqswde/aaa/usage_config_test_main.cc"));
139 EXPECT_FALSE(config.contains_helppackage_flags("aadir/usage_config_main.cc"));
140
141 absl::FlagsUsageConfig empty_config;
142 empty_config.contains_helppackage_flags = &TstContainsHelppackageFlags;
143 absl::SetFlagsUsageConfig(empty_config);
144
145 EXPECT_TRUE(
146 flags::GetUsageConfig().contains_helppackage_flags("aaa/main-body.c"));
147 EXPECT_FALSE(
148 flags::GetUsageConfig().contains_helppackage_flags("aadir/main-body.c"));
149 }
150
151 // --------------------------------------------------------------------
152
TEST_F(FlagsUsageConfigTest,TestVersionString)153 TEST_F(FlagsUsageConfigTest, TestVersionString) {
154 flags::SetProgramInvocationName("usage_config_test");
155
156 #ifdef NDEBUG
157 std::string expected_output = "usage_config_test\n";
158 #else
159 std::string expected_output =
160 "usage_config_test\nDebug build (NDEBUG not #defined)\n";
161 #endif
162
163 EXPECT_EQ(flags::GetUsageConfig().version_string(), expected_output);
164
165 absl::FlagsUsageConfig empty_config;
166 empty_config.version_string = &TstVersionString;
167 absl::SetFlagsUsageConfig(empty_config);
168
169 EXPECT_EQ(flags::GetUsageConfig().version_string(), "program 1.0.0");
170 }
171
172 // --------------------------------------------------------------------
173
TEST_F(FlagsUsageConfigTest,TestNormalizeFilename)174 TEST_F(FlagsUsageConfigTest, TestNormalizeFilename) {
175 // This tests the default implementation.
176 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a/a.cc");
177 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/a/a.cc"), "a/a.cc");
178 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("///a/a.cc"), "a/a.cc");
179 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/"), "");
180
181 // This tests that the custom implementation is called.
182 absl::FlagsUsageConfig empty_config;
183 empty_config.normalize_filename = &TstNormalizeFilename;
184 absl::SetFlagsUsageConfig(empty_config);
185
186 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a.cc");
187 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("aaa/a.cc"), "a/a.cc");
188
189 // This tests that the default implementation is called.
190 empty_config.normalize_filename = nullptr;
191 absl::SetFlagsUsageConfig(empty_config);
192
193 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a/a.cc");
194 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/a/a.cc"), "a/a.cc");
195 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("///a/a.cc"), "a/a.cc");
196 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("\\a\\a.cc"), "a\\a.cc");
197 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("//"), "");
198 EXPECT_EQ(flags::GetUsageConfig().normalize_filename("\\\\"), "");
199 }
200
201 } // namespace
202