• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(_WIN32)
88   flags::SetProgramInvocationName("usage_config_test.exe");
89 #else
90   flags::SetProgramInvocationName("usage_config_test");
91 #endif
92 
93   auto config = flags::GetUsageConfig();
94   EXPECT_TRUE(config.contains_helpshort_flags("adir/cd/usage_config_test.cc"));
95   EXPECT_TRUE(
96       config.contains_helpshort_flags("aaaa/usage_config_test-main.cc"));
97   EXPECT_TRUE(config.contains_helpshort_flags("abc/usage_config_test_main.cc"));
98   EXPECT_FALSE(config.contains_helpshort_flags("usage_config_main.cc"));
99 
100   absl::FlagsUsageConfig empty_config;
101   empty_config.contains_helpshort_flags = &TstContainsHelpshortFlags;
102   absl::SetFlagsUsageConfig(empty_config);
103 
104   EXPECT_TRUE(
105       flags::GetUsageConfig().contains_helpshort_flags("aaa/progname.cpp"));
106   EXPECT_FALSE(
107       flags::GetUsageConfig().contains_helpshort_flags("aaa/progmane.cpp"));
108 }
109 
110 // --------------------------------------------------------------------
111 
TEST_F(FlagsUsageConfigTest,TestContainsHelpFlags)112 TEST_F(FlagsUsageConfigTest, TestContainsHelpFlags) {
113   flags::SetProgramInvocationName("usage_config_test");
114 
115   auto config = flags::GetUsageConfig();
116   EXPECT_TRUE(config.contains_help_flags("zzz/usage_config_test.cc"));
117   EXPECT_TRUE(
118       config.contains_help_flags("bdir/a/zzz/usage_config_test-main.cc"));
119   EXPECT_TRUE(
120       config.contains_help_flags("//aqse/zzz/usage_config_test_main.cc"));
121   EXPECT_FALSE(config.contains_help_flags("zzz/aa/usage_config_main.cc"));
122 
123   absl::FlagsUsageConfig empty_config;
124   empty_config.contains_help_flags = &TstContainsHelpFlags;
125   absl::SetFlagsUsageConfig(empty_config);
126 
127   EXPECT_TRUE(flags::GetUsageConfig().contains_help_flags("zzz/main-body.c"));
128   EXPECT_FALSE(
129       flags::GetUsageConfig().contains_help_flags("zzz/dir/main-body.c"));
130 }
131 
132 // --------------------------------------------------------------------
133 
TEST_F(FlagsUsageConfigTest,TestContainsHelppackageFlags)134 TEST_F(FlagsUsageConfigTest, TestContainsHelppackageFlags) {
135   flags::SetProgramInvocationName("usage_config_test");
136 
137   auto config = flags::GetUsageConfig();
138   EXPECT_TRUE(config.contains_helppackage_flags("aaa/usage_config_test.cc"));
139   EXPECT_TRUE(
140       config.contains_helppackage_flags("bbdir/aaa/usage_config_test-main.cc"));
141   EXPECT_TRUE(config.contains_helppackage_flags(
142       "//aqswde/aaa/usage_config_test_main.cc"));
143   EXPECT_FALSE(config.contains_helppackage_flags("aadir/usage_config_main.cc"));
144 
145   absl::FlagsUsageConfig empty_config;
146   empty_config.contains_helppackage_flags = &TstContainsHelppackageFlags;
147   absl::SetFlagsUsageConfig(empty_config);
148 
149   EXPECT_TRUE(
150       flags::GetUsageConfig().contains_helppackage_flags("aaa/main-body.c"));
151   EXPECT_FALSE(
152       flags::GetUsageConfig().contains_helppackage_flags("aadir/main-body.c"));
153 }
154 
155 // --------------------------------------------------------------------
156 
TEST_F(FlagsUsageConfigTest,TestVersionString)157 TEST_F(FlagsUsageConfigTest, TestVersionString) {
158   flags::SetProgramInvocationName("usage_config_test");
159 
160 #ifdef NDEBUG
161   std::string expected_output = "usage_config_test\n";
162 #else
163   std::string expected_output =
164       "usage_config_test\nDebug build (NDEBUG not #defined)\n";
165 #endif
166 
167   EXPECT_EQ(flags::GetUsageConfig().version_string(), expected_output);
168 
169   absl::FlagsUsageConfig empty_config;
170   empty_config.version_string = &TstVersionString;
171   absl::SetFlagsUsageConfig(empty_config);
172 
173   EXPECT_EQ(flags::GetUsageConfig().version_string(), "program 1.0.0");
174 }
175 
176 // --------------------------------------------------------------------
177 
TEST_F(FlagsUsageConfigTest,TestNormalizeFilename)178 TEST_F(FlagsUsageConfigTest, TestNormalizeFilename) {
179   // This tests the default implementation.
180   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a/a.cc");
181   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/a/a.cc"), "a/a.cc");
182   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("///a/a.cc"), "a/a.cc");
183   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/"), "");
184 
185   // This tests that the custom implementation is called.
186   absl::FlagsUsageConfig empty_config;
187   empty_config.normalize_filename = &TstNormalizeFilename;
188   absl::SetFlagsUsageConfig(empty_config);
189 
190   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a.cc");
191   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("aaa/a.cc"), "a/a.cc");
192 
193   // This tests that the default implementation is called.
194   empty_config.normalize_filename = nullptr;
195   absl::SetFlagsUsageConfig(empty_config);
196 
197   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("a/a.cc"), "a/a.cc");
198   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("/a/a.cc"), "a/a.cc");
199   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("///a/a.cc"), "a/a.cc");
200   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("\\a\\a.cc"), "a\\a.cc");
201   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("//"), "");
202   EXPECT_EQ(flags::GetUsageConfig().normalize_filename("\\\\"), "");
203 }
204 
205 }  // namespace
206