Commit 7ba1e84b105a3d74ee83f5328120ba8c21ec98dc

Authored by Henry Fredrick Schreiner
1 parent 3061782d

Adding exists check, verify nothrow

include/CLI.hpp
@@ -115,11 +115,8 @@ bool _ExistingDirectory(std::string filename) { @@ -115,11 +115,8 @@ bool _ExistingDirectory(std::string filename) {
115 } 115 }
116 116
117 bool _NonexistentPath(std::string filename) { 117 bool _NonexistentPath(std::string filename) {
118 - std::cout << "Validating: " << filename << std::endl;  
119 struct stat buffer; 118 struct stat buffer;
120 - bool out = stat(filename.c_str(), &buffer) != 0;  
121 - std::cout << (out ? "Passed" : "Failed") << std::endl;  
122 - return out; 119 + return stat(filename.c_str(), &buffer) != 0;
123 } 120 }
124 121
125 struct Error : public std::runtime_error { 122 struct Error : public std::runtime_error {
tests/CLITest.cpp
@@ -39,7 +39,7 @@ struct TApp : public ::testing::Test { @@ -39,7 +39,7 @@ struct TApp : public ::testing::Test {
39 TEST_F(TApp, OneFlagShort) { 39 TEST_F(TApp, OneFlagShort) {
40 app.add_flag("c,count"); 40 app.add_flag("c,count");
41 args = {"-c"}; 41 args = {"-c"};
42 - run(); 42 + EXPECT_NO_THROW(run());
43 EXPECT_EQ(1, app.count("c")); 43 EXPECT_EQ(1, app.count("c"));
44 EXPECT_EQ(1, app.count("count")); 44 EXPECT_EQ(1, app.count("count"));
45 } 45 }
@@ -47,7 +47,7 @@ TEST_F(TApp, OneFlagShort) { @@ -47,7 +47,7 @@ TEST_F(TApp, OneFlagShort) {
47 TEST_F(TApp, OneFlagLong) { 47 TEST_F(TApp, OneFlagLong) {
48 app.add_flag("c,count"); 48 app.add_flag("c,count");
49 args = {"--count"}; 49 args = {"--count"};
50 - run(); 50 + EXPECT_NO_THROW(run());
51 EXPECT_EQ(1, app.count("c")); 51 EXPECT_EQ(1, app.count("c"));
52 EXPECT_EQ(1, app.count("count")); 52 EXPECT_EQ(1, app.count("count"));
53 } 53 }
@@ -56,7 +56,7 @@ TEST_F(TApp, OneFlagRef) { @@ -56,7 +56,7 @@ TEST_F(TApp, OneFlagRef) {
56 int ref; 56 int ref;
57 app.add_flag("c,count", ref); 57 app.add_flag("c,count", ref);
58 args = {"--count"}; 58 args = {"--count"};
59 - run(); 59 + EXPECT_NO_THROW(run());
60 EXPECT_EQ(1, app.count("c")); 60 EXPECT_EQ(1, app.count("c"));
61 EXPECT_EQ(1, app.count("count")); 61 EXPECT_EQ(1, app.count("count"));
62 EXPECT_EQ(1, ref); 62 EXPECT_EQ(1, ref);
@@ -66,7 +66,7 @@ TEST_F(TApp, OneString) { @@ -66,7 +66,7 @@ TEST_F(TApp, OneString) {
66 std::string str; 66 std::string str;
67 app.add_option("s,string", str); 67 app.add_option("s,string", str);
68 args = {"--string", "mystring"}; 68 args = {"--string", "mystring"};
69 - run(); 69 + EXPECT_NO_THROW(run());
70 EXPECT_EQ(1, app.count("s")); 70 EXPECT_EQ(1, app.count("s"));
71 EXPECT_EQ(1, app.count("string")); 71 EXPECT_EQ(1, app.count("string"));
72 EXPECT_EQ(str, "mystring"); 72 EXPECT_EQ(str, "mystring");
@@ -77,7 +77,7 @@ TEST_F(TApp, TogetherInt) { @@ -77,7 +77,7 @@ TEST_F(TApp, TogetherInt) {
77 int i; 77 int i;
78 app.add_option("i,int", i); 78 app.add_option("i,int", i);
79 args = {"-i4"}; 79 args = {"-i4"};
80 - run(); 80 + EXPECT_NO_THROW(run());
81 EXPECT_EQ(1, app.count("int")); 81 EXPECT_EQ(1, app.count("int"));
82 EXPECT_EQ(1, app.count("i")); 82 EXPECT_EQ(1, app.count("i"));
83 EXPECT_EQ(i, 4); 83 EXPECT_EQ(i, 4);
@@ -87,7 +87,7 @@ TEST_F(TApp, SepInt) { @@ -87,7 +87,7 @@ TEST_F(TApp, SepInt) {
87 int i; 87 int i;
88 app.add_option("i,int", i); 88 app.add_option("i,int", i);
89 args = {"-i","4"}; 89 args = {"-i","4"};
90 - run(); 90 + EXPECT_NO_THROW(run());
91 EXPECT_EQ(1, app.count("int")); 91 EXPECT_EQ(1, app.count("int"));
92 EXPECT_EQ(1, app.count("i")); 92 EXPECT_EQ(1, app.count("i"));
93 EXPECT_EQ(i, 4); 93 EXPECT_EQ(i, 4);
@@ -97,7 +97,7 @@ TEST_F(TApp, OneStringAgain) { @@ -97,7 +97,7 @@ TEST_F(TApp, OneStringAgain) {
97 std::string str; 97 std::string str;
98 app.add_option("s,string", str); 98 app.add_option("s,string", str);
99 args = {"--string", "mystring"}; 99 args = {"--string", "mystring"};
100 - run(); 100 + EXPECT_NO_THROW(run());
101 EXPECT_EQ(1, app.count("s")); 101 EXPECT_EQ(1, app.count("s"));
102 EXPECT_EQ(1, app.count("string")); 102 EXPECT_EQ(1, app.count("string"));
103 EXPECT_EQ(str, "mystring"); 103 EXPECT_EQ(str, "mystring");
@@ -107,7 +107,7 @@ TEST_F(TApp, OneStringAgain) { @@ -107,7 +107,7 @@ TEST_F(TApp, OneStringAgain) {
107 TEST_F(TApp, DefaultStringAgain) { 107 TEST_F(TApp, DefaultStringAgain) {
108 std::string str = "previous"; 108 std::string str = "previous";
109 app.add_option("s,string", str); 109 app.add_option("s,string", str);
110 - run(); 110 + EXPECT_NO_THROW(run());
111 EXPECT_EQ(0, app.count("s")); 111 EXPECT_EQ(0, app.count("s"));
112 EXPECT_EQ(0, app.count("string")); 112 EXPECT_EQ(0, app.count("string"));
113 EXPECT_EQ(str, "previous"); 113 EXPECT_EQ(str, "previous");
@@ -120,7 +120,7 @@ TEST_F(TApp, LotsOfFlags) { @@ -120,7 +120,7 @@ TEST_F(TApp, LotsOfFlags) {
120 app.add_flag("b"); 120 app.add_flag("b");
121 121
122 args = {"-a","-b","-aA"}; 122 args = {"-a","-b","-aA"};
123 - run(); 123 + EXPECT_NO_THROW(run());
124 EXPECT_EQ(2, app.count("a")); 124 EXPECT_EQ(2, app.count("a"));
125 EXPECT_EQ(1, app.count("b")); 125 EXPECT_EQ(1, app.count("b"));
126 EXPECT_EQ(1, app.count("A")); 126 EXPECT_EQ(1, app.count("A"));
@@ -135,7 +135,7 @@ TEST_F(TApp, ShortOpts) { @@ -135,7 +135,7 @@ TEST_F(TApp, ShortOpts) {
135 135
136 args = {"-zzyzyz",}; 136 args = {"-zzyzyz",};
137 137
138 - run(); 138 + EXPECT_NO_THROW(run());
139 139
140 EXPECT_EQ(2, app.count("z")); 140 EXPECT_EQ(2, app.count("z"));
141 EXPECT_EQ(1, app.count("y")); 141 EXPECT_EQ(1, app.count("y"));
@@ -152,7 +152,7 @@ TEST_F(TApp, Positionals) { @@ -152,7 +152,7 @@ TEST_F(TApp, Positionals) {
152 152
153 args = {"thing1","thing2"}; 153 args = {"thing1","thing2"};
154 154
155 - run(); 155 + EXPECT_NO_THROW(run());
156 156
157 EXPECT_EQ(1, app.count("posit1")); 157 EXPECT_EQ(1, app.count("posit1"));
158 EXPECT_EQ(1, app.count("posit2")); 158 EXPECT_EQ(1, app.count("posit2"));
@@ -169,7 +169,7 @@ TEST_F(TApp, MixedPositionals) { @@ -169,7 +169,7 @@ TEST_F(TApp, MixedPositionals) {
169 169
170 args = {"--posit2","thing2","7"}; 170 args = {"--posit2","thing2","7"};
171 171
172 - run(); 172 + EXPECT_NO_THROW(run());
173 173
174 EXPECT_EQ(1, app.count("posit2")); 174 EXPECT_EQ(1, app.count("posit2"));
175 EXPECT_EQ(1, app.count("posit1")); 175 EXPECT_EQ(1, app.count("posit1"));
@@ -185,7 +185,7 @@ TEST_F(TApp, Reset) { @@ -185,7 +185,7 @@ TEST_F(TApp, Reset) {
185 185
186 args = {"--simple", "--double", "1.2"}; 186 args = {"--simple", "--double", "1.2"};
187 187
188 - run(); 188 + EXPECT_NO_THROW(run());
189 189
190 EXPECT_EQ(1, app.count("simple")); 190 EXPECT_EQ(1, app.count("simple"));
191 EXPECT_EQ(1, app.count("d")); 191 EXPECT_EQ(1, app.count("d"));
@@ -196,7 +196,7 @@ TEST_F(TApp, Reset) { @@ -196,7 +196,7 @@ TEST_F(TApp, Reset) {
196 EXPECT_EQ(0, app.count("simple")); 196 EXPECT_EQ(0, app.count("simple"));
197 EXPECT_EQ(0, app.count("d")); 197 EXPECT_EQ(0, app.count("d"));
198 198
199 - run(); 199 + EXPECT_NO_THROW(run());
200 200
201 EXPECT_EQ(1, app.count("simple")); 201 EXPECT_EQ(1, app.count("simple"));
202 EXPECT_EQ(1, app.count("d")); 202 EXPECT_EQ(1, app.count("d"));
@@ -213,7 +213,7 @@ TEST_F(TApp, FileNotExists) { @@ -213,7 +213,7 @@ TEST_F(TApp, FileNotExists) {
213 app.add_option("file", filename, "", CLI::NonexistentPath); 213 app.add_option("file", filename, "", CLI::NonexistentPath);
214 args = {"--file", myfile}; 214 args = {"--file", myfile};
215 215
216 - run(); 216 + EXPECT_NO_THROW(run());
217 EXPECT_EQ(myfile, filename); 217 EXPECT_EQ(myfile, filename);
218 218
219 app.reset(); 219 app.reset();
@@ -227,23 +227,44 @@ TEST_F(TApp, FileNotExists) { @@ -227,23 +227,44 @@ TEST_F(TApp, FileNotExists) {
227 EXPECT_FALSE(CLI::_ExistingFile(myfile)); 227 EXPECT_FALSE(CLI::_ExistingFile(myfile));
228 } 228 }
229 229
  230 +TEST_F(TApp, FileExists) {
  231 + std::string myfile{"TestNonFileNotUsed.txt"};
  232 + EXPECT_FALSE(CLI::_ExistingFile(myfile));
  233 +
  234 + std::string filename = "Failed";
  235 + app.add_option("file", filename, "", CLI::ExistingFile);
  236 + args = {"--file", myfile};
  237 +
  238 + EXPECT_THROW(run(), CLI::ParseError);
  239 + EXPECT_EQ("Failed", filename);
  240 +
  241 + app.reset();
  242 +
  243 + bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file
  244 + EXPECT_TRUE(ok);
  245 + EXPECT_NO_THROW(run());
  246 + EXPECT_EQ(myfile, filename);
  247 +
  248 + std::remove(myfile.c_str());
  249 + EXPECT_FALSE(CLI::_ExistingFile(myfile));
  250 +}
230 TEST_F(TApp, Basic) { 251 TEST_F(TApp, Basic) {
231 auto sub1 = app.add_subcommand("sub1"); 252 auto sub1 = app.add_subcommand("sub1");
232 auto sub2 = app.add_subcommand("sub2"); 253 auto sub2 = app.add_subcommand("sub2");
233 254
234 - run(); 255 + EXPECT_NO_THROW(run());
235 EXPECT_EQ(nullptr, app.get_subcommand()); 256 EXPECT_EQ(nullptr, app.get_subcommand());
236 257
237 app.reset(); 258 app.reset();
238 args = {"sub1"}; 259 args = {"sub1"};
239 - run(); 260 + EXPECT_NO_THROW(run());
240 EXPECT_EQ(sub1, app.get_subcommand()); 261 EXPECT_EQ(sub1, app.get_subcommand());
241 262
242 app.reset(); 263 app.reset();
243 EXPECT_EQ(nullptr, app.get_subcommand()); 264 EXPECT_EQ(nullptr, app.get_subcommand());
244 265
245 args = {"sub2"}; 266 args = {"sub2"};
246 - run(); 267 + EXPECT_NO_THROW(run());
247 EXPECT_EQ(sub2, app.get_subcommand()); 268 EXPECT_EQ(sub2, app.get_subcommand());
248 } 269 }
249 270
@@ -270,7 +291,7 @@ struct SubcommandProgram : public TApp { @@ -270,7 +291,7 @@ struct SubcommandProgram : public TApp {
270 TEST_F(SubcommandProgram, Working) { 291 TEST_F(SubcommandProgram, Working) {
271 args = {"-d", "start", "-ffilename"}; 292 args = {"-d", "start", "-ffilename"};
272 293
273 - run(); 294 + EXPECT_NO_THROW(run());
274 295
275 EXPECT_EQ(1, dummy); 296 EXPECT_EQ(1, dummy);
276 EXPECT_EQ(start, app.get_subcommand()); 297 EXPECT_EQ(start, app.get_subcommand());