Debian Codegraph @

tests/src/cli2/test_CLI.cpp

raw
1 #include <pbcopper/cli2/CLI.h>
2
3 #include <filesystem>
4 #include <iostream>
5 #include <sstream>
6 #include <string>
7 #include <tuple>
8 #include <vector>
9
10 #include <gtest/gtest.h>
11
12 #include <pbcopper/cli2/Option.h>
13 #include <pbcopper/cli2/PositionalArgument.h>
14 #include <pbcopper/cli2/Results.h>
15 #include <pbcopper/cli2/internal/HelpMetrics.h>
16 #include <pbcopper/utility/Alarm.h>
17 #include <pbcopper/utility/OStreamRedirect.h>
18
19 #include "../../../src/cli2/PbBoilerplateDisclaimer.h"
20 #include "PbcopperTestData.h"
21
22 // clang-format off
23
24 namespace CLI_v2_CLITests {
25
26 const PacBio::CLI_v2::Option RefineTestOption
27 {
28 R"({
29 "names" : ["test"],
30 "description" : "Should see this entry",
31 "type" : "boolean"
32 })"
33 };
34
35 const PacBio::CLI_v2::Option RefineCountOption
36 {
37 R"({
38 "names" : ["count"],
39 "description" : "Should see this entry too",
40 "type" : "integer",
41 "default" : 5
42 })"
43 };
44
45 const PacBio::CLI_v2::PositionalArgument Source
46 {
47 R"({
48 "name" : "source",
49 "description" : "Source file to copy.",
50 "syntax" : "FILE",
51 "required" : false
52 })"
53 };
54
55
56 PacBio::CLI_v2::Interface MakeRefineInterface()
57 {
58 PacBio::CLI_v2::Interface refine{"refine", "Remove concatemers and optionally poly-A tails (FL to FLNC)"};
59 refine.AddOptions({
60 RefineTestOption,
61 RefineCountOption});
62 refine.Example("isoseq3 refine movie.consensusreadset.xml primers.barcodeset.xml movie.flnc.bam");
63 return refine;
64 }
65
66 int RefineRunner(const PacBio::CLI_v2::Results& results)
67 {
68 // "$ isoseq3 refine --test --count 10"
69
70 const bool test = results[RefineTestOption];
71 const int count = results[RefineCountOption];
72 EXPECT_TRUE(test);
73 EXPECT_EQ(10, count);
74
75 return EXIT_SUCCESS;
76 }
77
78 PacBio::CLI_v2::Interface MakeClusterInterface()
79 {
80 PacBio::CLI_v2::Interface cluster{"cluster", "Cluster FLNC reads and generate unpolished transcripts (FLNC to UNPOLISHED"};
81 cluster.Example("isoseq3 cluster movie.flnc.bam unpolished.bam");
82 return cluster;
83 }
84
85 int ClusterRunner(const PacBio::CLI_v2::Results&)
86 {
87 return EXIT_SUCCESS;
88 }
89
90 PacBio::CLI_v2::Interface MakePolishInterface()
91 {
92 PacBio::CLI_v2::Interface polish{"polish", "Polish transcripts using subreads (UNPOLISHED to POLISHED)"};
93 polish.Example("isoseq3 polish unpolished.bam movie.subreadset.xml polished.bam");
94 return polish;
95 }
96
97 int PolishRunner(const PacBio::CLI_v2::Results&)
98 {
99 return EXIT_SUCCESS;
100 }
101
102 PacBio::CLI_v2::Interface MakeSummarizeInterface()
103 {
104 PacBio::CLI_v2::Interface summarize{"summarize", "Create barcode overview from transcripts (POLISHED to CSV)"};
105 summarize.Example("isoseq3 summarize polished.bam summary.csv");
106 return summarize;
107 }
108
109 int SummarizeRunner(const PacBio::CLI_v2::Results&)
110 {
111 return EXIT_SUCCESS;
112 }
113
114 } // namespace CLI_v2_CLITests
115
116 TEST(CLI2_CLI, can_print_version)
117 {
118 PacBio::CLI_v2::internal::HelpMetrics::TestingFixedWidth = 80;
119
120 const std::string expectedText{"frobber v3.1\n"};
121 const std::vector<std::string> args {
122 "frobber", "--version"
123 };
124 auto runner = [](const PacBio::CLI_v2::Results&)
125 {
126 return EXIT_SUCCESS;
127 };
128
129 std::ostringstream s;
130 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
131 std::ignore = redirect;
132
133 const PacBio::CLI_v2::Interface i{"frobber", "Frob all the things", "v3.1"};
134 const int result = PacBio::CLI_v2::Run(args, i, runner);
135
136 EXPECT_EQ(EXIT_SUCCESS, result);
137 EXPECT_EQ(expectedText, s.str());
138 }
139
140 TEST(CLI2_CLI, can_print_help)
141 {
142 PacBio::CLI_v2::internal::HelpMetrics::TestingFixedWidth = 80;
143
144 const std::string expectedText{
145 "frobber - Frob all the things.\n"
146 "\n"
147 "Usage:\n"
148 " frobber [options]\n"
149 "\n"
150 "Options:\n"
151 " -h,--help Show this help and exit.\n"
152 " --version Show application version and exit.\n"
153 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0]\n"
154 " --log-level STR Set log level. Valid choices: (TRACE, DEBUG, INFO,\n"
155 " WARN, FATAL). [WARN]\n"
156 " --log-file FILE Log to a file, instead of stderr.\n"
157 "\n"
158 PB_BOILERPLATE_DISCLAIMER
159 };
160
161 const std::vector<std::string> args {
162 "frobber", "-h"
163 };
164 auto runner = [](const PacBio::CLI_v2::Results&)
165 {
166 return EXIT_SUCCESS;
167 };
168
169 std::ostringstream s;
170 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
171 std::ignore = redirect;
172
173 const PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
174 const int result = PacBio::CLI_v2::Run(args, i, runner);
175
176 EXPECT_EQ(EXIT_SUCCESS, result);
177 EXPECT_EQ(expectedText, s.str());
178 }
179
180 TEST(CLI2_CLI, can_print_top_help_for_multitoolinterface)
181 {
182 PacBio::CLI_v2::internal::HelpMetrics::TestingFixedWidth = 80;
183
184 const std::string expectedText{R"(isoseq3 - De Novo Transcript Reconstruction
185
186 Usage:
187 isoseq3 <tool>
188
189 -h,--help Show this help and exit.
190 --version Show application version and exit.
191
192 Tools:
193 refine Remove concatemers and optionally poly-A tails (FL to FLNC)
194 cluster Cluster FLNC reads and generate unpolished transcripts (FLNC to UNPOLISHED
195 polish Polish transcripts using subreads (UNPOLISHED to POLISHED)
196 summarize Create barcode overview from transcripts (POLISHED to CSV)
197
198 Examples:
199 isoseq3 refine movie.consensusreadset.xml primers.barcodeset.xml movie.flnc.bam
200 isoseq3 cluster movie.flnc.bam unpolished.bam
201 isoseq3 polish unpolished.bam movie.subreadset.xml polished.bam
202 isoseq3 summarize polished.bam summary.csv
203
204 Typical workflow:
205 1. Generate consensus sequences from your raw subread data
206 $ ccs movie.subreads.bam movie.ccs.bam --noPolish --minPasses 1
207
208 2. Generate full-length reads by primer removal and demultiplexing
209 $ cat primers.fasta
210 >primer_5p
211 AAGCAGTGGTATCAACGCAGAGTACATGGGG
212 >primer_3p
213 AAGCAGTGGTATCAACGCAGAGTAC
214 $ lima movie.ccs.bam primers.fasta movie.fl.bam --isoseq --no-pbi
215
216 3. Remove noise from FL reads
217 $ isoseq3 refine movie.fl.P5--P3.bam primers.fasta movie.flnc.bam
218
219 4. Cluster consensus sequences to generate unpolished transcripts
220 $ isoseq3 cluster movie.flnc.bam unpolished.bam --verbose
221
222 5. Polish transcripts using subreads
223 $ isoseq3 polish unpolished.bam movie.subreads.bam polished.bam
224
225 )"
226 PB_BOILERPLATE_DISCLAIMER
227 };
228
229 PacBio::CLI_v2::MultiToolInterface i{"isoseq3", "De Novo Transcript Reconstruction", "3.1.2"};
230 i.AddTools({{"refine", CLI_v2_CLITests::MakeRefineInterface(), &CLI_v2_CLITests::RefineRunner},
231 {"cluster", CLI_v2_CLITests::MakeClusterInterface(), &CLI_v2_CLITests::ClusterRunner},
232 {"polish", CLI_v2_CLITests::MakePolishInterface(), &CLI_v2_CLITests::PolishRunner},
233 {"summarize", CLI_v2_CLITests::MakeSummarizeInterface(), &CLI_v2_CLITests::SummarizeRunner}});
234
235 i.HelpFooter(R"(Typical workflow:
236 1. Generate consensus sequences from your raw subread data
237 $ ccs movie.subreads.bam movie.ccs.bam --noPolish --minPasses 1
238
239 2. Generate full-length reads by primer removal and demultiplexing
240 $ cat primers.fasta
241 >primer_5p
242 AAGCAGTGGTATCAACGCAGAGTACATGGGG
243 >primer_3p
244 AAGCAGTGGTATCAACGCAGAGTAC
245 $ lima movie.ccs.bam primers.fasta movie.fl.bam --isoseq --no-pbi
246
247 3. Remove noise from FL reads
248 $ isoseq3 refine movie.fl.P5--P3.bam primers.fasta movie.flnc.bam
249
250 4. Cluster consensus sequences to generate unpolished transcripts
251 $ isoseq3 cluster movie.flnc.bam unpolished.bam --verbose
252
253 5. Polish transcripts using subreads
254 $ isoseq3 polish unpolished.bam movie.subreads.bam polished.bam
255 )");
256
257 const std::vector<std::string> args {
258 "isoseq3", "-h"
259 };
260
261 std::ostringstream s;
262 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
263 std::ignore = redirect;
264
265 const int result = PacBio::CLI_v2::Run(args, i);
266 EXPECT_EQ(EXIT_SUCCESS, result);
267
268 EXPECT_EQ(s.str(), expectedText);
269 }
270
271 TEST(CLI2_CLI, can_print_subtool_help_for_multitoolinterface)
272 {
273 PacBio::CLI_v2::internal::HelpMetrics::TestingFixedWidth = 80;
274
275 const std::string expectedText{R"(refine - Remove concatemers and optionally poly-A tails (FL to FLNC)
276
277 Usage:
278 refine [options]
279
280 --test Should see this entry
281 --count INT Should see this entry too [5]
282
283 -h,--help Show this help and exit.
284 --version Show application version and exit.
285 -j,--num-threads INT Number of threads to use, 0 means autodetection. [0]
286 --log-level STR Set log level. Valid choices: (TRACE, DEBUG, INFO,
287 WARN, FATAL). [WARN]
288 --log-file FILE Log to a file, instead of stderr.
289
290 )"
291 PB_BOILERPLATE_DISCLAIMER
292 };
293
294 PacBio::CLI_v2::MultiToolInterface i{"isoseq3", "De Novo Transcript Reconstruction", "3.1.2"};
295 i.AddTools({{"refine", CLI_v2_CLITests::MakeRefineInterface(), &CLI_v2_CLITests::RefineRunner},
296 {"cluster", CLI_v2_CLITests::MakeClusterInterface(), &CLI_v2_CLITests::ClusterRunner},
297 {"polish", CLI_v2_CLITests::MakePolishInterface(), &CLI_v2_CLITests::PolishRunner},
298 {"summarize", CLI_v2_CLITests::MakeSummarizeInterface(), &CLI_v2_CLITests::SummarizeRunner}});
299
300 const std::vector<std::string> args {
301 "isoseq3", "refine", "-h"
302 };
303
304 std::ostringstream s;
305 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
306 std::ignore = redirect;
307
308 const int result = PacBio::CLI_v2::Run(args, i);
309 EXPECT_EQ(EXIT_SUCCESS, result);
310 EXPECT_EQ(expectedText, s.str());
311 }
312
313 TEST(CLI2_CLI, can_run_from_command_line_args)
314 {
315 const PacBio::CLI_v2::Option MaxNumLines
316 {
317 R"({
318 "names" : ["n"],
319 "description" : "Max Number of lines to Copy",
320 "type" : "int",
321 "default" : 10,
322 "tool_contract.option" : {
323 "title" : "Maximum Line Count",
324 "id" : "max_nlines"
325 }
326 })"
327 };
328
329 const std::vector<std::string> args {
330 "frobber", "-n", "27"
331 };
332 auto runner = [&MaxNumLines](const PacBio::CLI_v2::Results& results)
333 {
334 const int expectedNumLines = 27;
335 const int maxNumLines = results[MaxNumLines];
336
337 EXPECT_EQ(expectedNumLines, maxNumLines);
338 return EXIT_SUCCESS;
339 };
340
341 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
342 i.AddOption(MaxNumLines);
343
344 std::ostringstream s;
345 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
346 std::ignore = redirect;
347
348 const int result = PacBio::CLI_v2::Run(args, i, runner);
349 EXPECT_EQ(EXIT_SUCCESS, result);
350 }
351
352 TEST(CLI2_CLI, correctly_interprets_implicit_empty_strings)
353 {
354 const PacBio::CLI_v2::Option TestDefaultEmptyString
355 {
356 R"({
357 "names" : ["test-default-string"],
358 "description" : "Nothing to see here.",
359 "type" : "string"
360 })"
361 };
362
363 const PacBio::CLI_v2::Option TestDefaultEmptyFile
364 {
365 R"({
366 "names" : ["test-default-file"],
367 "description" : "Nothing to see here.",
368 "type" : "file"
369 })"
370 };
371
372 const PacBio::CLI_v2::Option TestDefaultEmptyDir
373 {
374 R"({
375 "names" : ["test-default-dir"],
376 "description" : "Nothing to see here.",
377 "type" : "dir"
378 })"
379 };
380
381 const std::vector<std::string> args {
382 "frobber"
383 };
384 auto runner = [&](const PacBio::CLI_v2::Results& results)
385 {
386 const std::string shouldBeEmptyString = results[TestDefaultEmptyString];
387 const std::string shouldBeEmptyFile = results[TestDefaultEmptyFile];
388 const std::string shouldBeEmptyDir = results[TestDefaultEmptyDir];
389
390 EXPECT_TRUE(shouldBeEmptyString.empty());
391 EXPECT_TRUE(shouldBeEmptyFile.empty());
392 EXPECT_TRUE(shouldBeEmptyDir.empty());
393 return EXIT_SUCCESS;
394 };
395
396 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
397 i.AddOptions({
398 TestDefaultEmptyString,
399 TestDefaultEmptyFile,
400 TestDefaultEmptyDir
401 });
402
403 std::ostringstream s;
404 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
405 std::ignore = redirect;
406
407 const int result = PacBio::CLI_v2::Run(args, i, runner);
408 EXPECT_EQ(EXIT_SUCCESS, result);
409 }
410
411 TEST(CLI2_CLI, can_run_multitoolinterface_subtool_from_command_line_args)
412 {
413 PacBio::CLI_v2::MultiToolInterface i{"isoseq3", "De Novo Transcript Reconstruction", "3.1.2"};
414 i.AddTools({{"refine", CLI_v2_CLITests::MakeRefineInterface(), &CLI_v2_CLITests::RefineRunner},
415 {"cluster", CLI_v2_CLITests::MakeClusterInterface(), &CLI_v2_CLITests::ClusterRunner},
416 {"polish", CLI_v2_CLITests::MakePolishInterface(), &CLI_v2_CLITests::PolishRunner},
417 {"summarize", CLI_v2_CLITests::MakeSummarizeInterface(), &CLI_v2_CLITests::SummarizeRunner}});
418
419 const std::vector<std::string> args {
420 "isoseq3", "refine", "--test", "--count", "10"
421 };
422
423 std::ostringstream s;
424 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
425 std::ignore = redirect;
426
427 const int result = PacBio::CLI_v2::Run(args, i);
428 EXPECT_EQ(EXIT_SUCCESS, result);
429 }
430
431 TEST(CLI2_CLI, can_fetch_default_log_level_and_log_file_from_results)
432 {
433 const PacBio::CLI_v2::Option MaxNumLines
434 {
435 R"({
436 "names" : ["n"],
437 "description" : "Max Number of lines to Copy",
438 "type" : "int",
439 "default" : 10,
440 "tool_contract.option" : {
441 "title" : "Maximum Line Count",
442 "id" : "max_nlines"
443 }
444 })"
445 };
446
447 const std::vector<std::string> args {
448 "frobber", "-n", "27"
449 };
450
451 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
452 i.AddOption(MaxNumLines);
453 const auto defaultLogLevel = i.DefaultLogLevel();
454
455 std::ostringstream s;
456 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
457 std::ignore = redirect;
458
459 auto runner = [&defaultLogLevel](const PacBio::CLI_v2::Results& results)
460 {
461 EXPECT_TRUE(results.LogFile().empty());
462 EXPECT_EQ(defaultLogLevel, results.LogLevel());
463 return EXIT_SUCCESS;
464 };
465
466 const int result = PacBio::CLI_v2::Run(args, i, runner);
467 EXPECT_EQ(EXIT_SUCCESS, result);
468 }
469
470 TEST(CLI2_CLI, can_fetch_overriden_default_log_level_from_results)
471 {
472 const PacBio::CLI_v2::Option MaxNumLines
473 {
474 R"({
475 "names" : ["n"],
476 "description" : "Max Number of lines to Copy",
477 "type" : "int",
478 "default" : 10,
479 "tool_contract.option" : {
480 "title" : "Maximum Line Count",
481 "id" : "max_nlines"
482 }
483 })"
484 };
485
486 const std::vector<std::string> args {
487 "frobber", "-n", "27"
488 };
489
490 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
491 i.AddOption(MaxNumLines);
492 const auto defaultLogLevel = PacBio::Logging::LogLevel::DEBUG;
493 i.DefaultLogLevel(defaultLogLevel);
494
495 std::ostringstream s;
496 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
497 std::ignore = redirect;
498
499 auto runner = [&MaxNumLines, &defaultLogLevel](const PacBio::CLI_v2::Results& results)
500 {
501 const int expectedNumLines = 27;
502 const int maxNumLines = results[MaxNumLines];
503
504 EXPECT_EQ(expectedNumLines, maxNumLines);
505 EXPECT_TRUE(results.LogFile().empty());
506 EXPECT_EQ(defaultLogLevel, results.LogLevel());
507 return EXIT_SUCCESS;
508 };
509
510 const int result = PacBio::CLI_v2::Run(args, i, runner);
511 EXPECT_EQ(EXIT_SUCCESS, result);
512 }
513
514 TEST(CLI2_CLI, can_fetch_log_level_from_results)
515 {
516 const std::vector<std::string> args {
517 "frobber", "--log-level", "DEBUG"
518 };
519 auto runner = [](const PacBio::CLI_v2::Results& results)
520 {
521 EXPECT_TRUE(results.LogFile().empty());
522 EXPECT_EQ(PacBio::Logging::LogLevel::DEBUG, results.LogLevel());
523 return EXIT_SUCCESS;
524 };
525
526 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
527
528 std::ostringstream s;
529 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
530 std::ignore = redirect;
531
532 const int result = PacBio::CLI_v2::Run(args, i, runner);
533 EXPECT_EQ(EXIT_SUCCESS, result);
534 }
535
536 TEST(CLI2_CLI, can_fetch_log_file_from_results)
537 {
538 const std::vector<std::string> args {
539 "frobber", "--log-file", "log.txt"
540 };
541 auto runner = [](const PacBio::CLI_v2::Results& results)
542 {
543 EXPECT_EQ("log.txt", results.LogFile());
544 return EXIT_SUCCESS;
545 };
546
547 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
548
549 std::ostringstream s;
550 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
551 std::ignore = redirect;
552
553 const int result = PacBio::CLI_v2::Run(args, i, runner);
554 EXPECT_EQ(EXIT_SUCCESS, result);
555 }
556
557 TEST(CLI2_CLI, verbosity_is_true_if_enabled_and_user_requested)
558 {
559 const std::vector<std::string> args {
560 "frobber", "-v"
561 };
562 auto runner = [](const PacBio::CLI_v2::Results& results)
563 {
564 EXPECT_TRUE(results.Verbose());
565 return EXIT_SUCCESS;
566 };
567
568 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
569 i.EnableVerboseOption();
570
571 std::ostringstream s;
572 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
573 std::ignore = redirect;
574
575 const int result = PacBio::CLI_v2::Run(args, i, runner);
576 EXPECT_EQ(EXIT_SUCCESS, result);
577 }
578
579 TEST(CLI2_CLI, verbosity_is_false_if_enabled_but_not_user_requested)
580 {
581 const std::vector<std::string> args {
582 "frobber"
583 };
584 auto runner = [](const PacBio::CLI_v2::Results& results)
585 {
586 EXPECT_FALSE(results.Verbose());
587 return EXIT_SUCCESS;
588 };
589
590 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
591 i.EnableVerboseOption();
592
593 std::ostringstream s;
594 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
595 std::ignore = redirect;
596
597 const int result = PacBio::CLI_v2::Run(args, i, runner);
598 EXPECT_EQ(EXIT_SUCCESS, result);
599 }
600
601 TEST(CLI2_CLI, verbosity_is_false_if_not_enabled)
602 {
603 const std::vector<std::string> args {
604 "frobber"
605 };
606 auto runner = [](const PacBio::CLI_v2::Results& results)
607 {
608 EXPECT_FALSE(results.Verbose());
609 return EXIT_SUCCESS;
610 };
611
612 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
613
614 std::ostringstream s;
615 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
616 std::ignore = redirect;
617
618 const int result = PacBio::CLI_v2::Run(args, i, runner);
619 EXPECT_EQ(EXIT_SUCCESS, result);
620 }
621
622 TEST(CLI2_CLI, can_run_tool_with_optional_pos_args)
623 {
624 const std::vector<std::string> args {"frobber"};
625 auto runner = [](const PacBio::CLI_v2::Results& results)
626 {
627 EXPECT_TRUE(results.PositionalArguments().empty());
628 return EXIT_SUCCESS;
629 };
630
631 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
632 i.AddPositionalArgument(CLI_v2_CLITests::Source);
633
634 std::ostringstream s;
635 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
636 std::ignore = redirect;
637
638 const int result = PacBio::CLI_v2::Run(args, i, runner);
639 EXPECT_EQ(EXIT_SUCCESS, result);
640 }
641
642 TEST(CLI2_CLI, no_throw_on_disabled_log_file_option_in_setup)
643 {
644 const std::vector<std::string> args {"frobber"};
645 auto runner = [](const PacBio::CLI_v2::Results&)
646 {
647 return EXIT_SUCCESS;
648 };
649
650 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
651 i.DisableLogFileOption();
652
653 std::ostringstream s;
654 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
655 std::ignore = redirect;
656
657 EXPECT_NO_THROW(PacBio::CLI_v2::Run(args, i, runner););
658 }
659
660 TEST(CLI2_CLI, no_throw_on_disabled_log_level_option_in_setup)
661 {
662 const std::vector<std::string> args {"frobber"};
663 auto runner = [](const PacBio::CLI_v2::Results&)
664 {
665 return EXIT_SUCCESS;
666 };
667
668 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
669 i.DisableLogLevelOption();
670
671 std::ostringstream s;
672 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
673 std::ignore = redirect;
674
675 EXPECT_NO_THROW(PacBio::CLI_v2::Run(args, i, runner););
676 }
677
678 TEST(CLI2_CLI, multitoolinterface_can_override_subtool_log_config)
679 {
680 auto refineRunner = [](const PacBio::CLI_v2::Results& results) {
681 EXPECT_EQ(PacBio::Logging::LogLevel::DEBUG, results.LogLevel());
682 return EXIT_SUCCESS;
683 };
684
685 PacBio::CLI_v2::MultiToolInterface i{"isoseq3", "De Novo Transcript Reconstruction", "3.1.2"};
686 const auto& refineInterface = CLI_v2_CLITests::MakeRefineInterface();
687 i.AddTools({{"refine", refineInterface, refineRunner},
688 {"cluster", CLI_v2_CLITests::MakeClusterInterface(), &CLI_v2_CLITests::ClusterRunner},
689 {"polish", CLI_v2_CLITests::MakePolishInterface(), &CLI_v2_CLITests::PolishRunner},
690 {"summarize", CLI_v2_CLITests::MakeSummarizeInterface(), &CLI_v2_CLITests::SummarizeRunner}});
691
692 PacBio::Logging::LogConfig config{PacBio::Logging::LogLevel::DEBUG};
693 i.LogConfig(config);
694
695 EXPECT_EQ(PacBio::Logging::LogLevel::WARN, refineInterface.DefaultLogLevel());
696 EXPECT_EQ(PacBio::Logging::LogLevel::DEBUG, i.LogConfig()->Level);
697
698 const std::vector<std::string> args {
699 "isoseq3", "refine", "--test", "--count", "10"
700 };
701 const int result = PacBio::CLI_v2::Run(args, i);
702 EXPECT_EQ(EXIT_SUCCESS, result);
703 }
704
705 TEST(CLI2_CLI, command_line_log_level_overrides_interface_configs)
706 {
707 auto refineRunner = [](const PacBio::CLI_v2::Results& results) {
708 EXPECT_EQ(PacBio::Logging::LogLevel::TRACE, results.LogLevel());
709 return EXIT_SUCCESS;
710 };
711
712 PacBio::CLI_v2::MultiToolInterface i{"isoseq3", "De Novo Transcript Reconstruction", "3.1.2"};
713 const auto& refineInterface = CLI_v2_CLITests::MakeRefineInterface();
714 i.AddTools({{"refine", refineInterface, refineRunner},
715 {"cluster", CLI_v2_CLITests::MakeClusterInterface(), &CLI_v2_CLITests::ClusterRunner},
716 {"polish", CLI_v2_CLITests::MakePolishInterface(), &CLI_v2_CLITests::PolishRunner},
717 {"summarize", CLI_v2_CLITests::MakeSummarizeInterface(), &CLI_v2_CLITests::SummarizeRunner}});
718
719 PacBio::Logging::LogConfig config{PacBio::Logging::LogLevel::DEBUG};
720 i.LogConfig(config);
721
722 EXPECT_EQ(PacBio::Logging::LogLevel::WARN, refineInterface.DefaultLogLevel());
723 EXPECT_EQ(PacBio::Logging::LogLevel::DEBUG, i.LogConfig()->Level);
724
725 const std::vector<std::string> args {
726 "isoseq3", "refine", "--test", "--count", "10", "--log-level", "TRACE"
727 };
728 const int result = PacBio::CLI_v2::Run(args, i);
729 EXPECT_EQ(EXIT_SUCCESS, result);
730 }
731
732 TEST(CLI2_CLI, can_use_custom_version_callback)
733 {
734 const std::vector<std::string> args {
735 "frobber", "--version"
736 };
737 auto runner = [](const PacBio::CLI_v2::Results& results)
738 {
739 EXPECT_FALSE(results.Verbose());
740 return EXIT_SUCCESS;
741 };
742 auto versionPrinter = [](const PacBio::CLI_v2::Interface& i)
743 {
744 std::ignore = i;
745 std::cout << "Custom version printer";
746 };
747
748 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
749 i.RegisterVersionPrinter(versionPrinter);
750
751 std::ostringstream s;
752 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
753 std::ignore = redirect;
754
755 const int result = PacBio::CLI_v2::Run(args, i, runner);
756 EXPECT_EQ("Custom version printer", s.str());
757 EXPECT_EQ(EXIT_SUCCESS, result);
758 }
759
760 TEST(CLI2_CLI, creates_alarm_for_all_exceptions_when_given_alarm_file_arg) {
761
762 const std::filesystem::path alarmFilename{PacBio::PbcopperTestsConfig::Generated_Dir / "std_exception_alarm.json"};
763
764 const std::vector<std::string> args {"frobber", "--alarms", alarmFilename};
765 auto runner = [](const PacBio::CLI_v2::Results&)
766 {
767 throw std::runtime_error{"std exception from application"};
768 return 0;
769 };
770
771 std::ostringstream logOutput;
772 PacBio::Utility::CerrRedirect redirect{logOutput.rdbuf()};
773 std::ignore = redirect;
774
775 PacBio::CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
776 const int result = PacBio::CLI_v2::Run(args, i, runner);
777 EXPECT_EQ(EXIT_FAILURE, result);
778
779 // check std::cerr logging
780 EXPECT_TRUE(logOutput.str().find("std exception from application") != std::string::npos);
781
782 // check alarm file
783 std::ifstream alarmFile{alarmFilename};
784 std::ostringstream alarmText;
785 alarmText << alarmFile.rdbuf();
786 EXPECT_TRUE(alarmText.str().find("std exception from application") != std::string::npos);
787 }
788
789 TEST(CLI2_CLI, populates_alarm_owner_field_with_application_name) {
790
791 const std::filesystem::path alarmFilename{PacBio::PbcopperTestsConfig::Generated_Dir / "add_owner_alarm.json"};
792 const std::vector<std::string> args {"owner-check", "--alarms", alarmFilename};
793 auto runner = [](const PacBio::CLI_v2::Results&)
794 {
795 throw PB_ALARM("alarm type", "alarm message");
796 return 0;
797 };
798
799 std::ostringstream logOutput;
800 PacBio::Utility::CerrRedirect redirect{logOutput.rdbuf()};
801 std::ignore = redirect;
802
803 PacBio::CLI_v2::Interface i{"owner-check", "Check owner in alarm.", "v3.1"};
804 const int result = PacBio::CLI_v2::Run(args, i, runner);
805 EXPECT_EQ(EXIT_FAILURE, result);
806
807 // check alarm file
808 std::ifstream alarmFile{alarmFilename};
809 std::ostringstream alarmText;
810 alarmText << alarmFile.rdbuf();
811 EXPECT_TRUE(alarmText.str().find("\"owner\": \"owner-check\"") != std::string::npos);
812 EXPECT_TRUE(alarmText.str().find("\"message\": \"alarm message\"") != std::string::npos);
813 EXPECT_TRUE(alarmText.str().find("\"name\": \"alarm type\"") != std::string::npos);
814 }
815
816 TEST(CLI2_CLI, show_all_help_displays_hidden_options)
817 {
818 using namespace PacBio;
819
820 const CLI_v2::Option HiddenOption
821 {
822 R"({
823 "names" : ["hidden-mode"],
824 "description" : "Hidden mode",
825 "type" : "string",
826 "hidden" : true
827 })"
828 };
829
830 const auto runner = [](const CLI_v2::Results&) { return EXIT_SUCCESS; };
831
832 CLI_v2::Interface i{"frobber", "Frob all the things.", "v3.1"};
833 i.AddOption(HiddenOption);
834
835 // --all-help
836 {
837 const std::vector<std::string> args {
838 "frobber", "--all-help"
839 };
840
841 std::ostringstream s;
842 Utility::CoutRedirect redirect{s.rdbuf()};
843 std::ignore = redirect;
844 const int result = CLI_v2::Run(args, i, runner);
845 EXPECT_EQ(EXIT_SUCCESS, result);
846 EXPECT_NE(std::string::npos, s.str().find("--hidden-mode"));
847 }
848
849 // --show-all-help
850 {
851 const std::vector<std::string> args {
852 "frobber", "--show-all-help"
853 };
854
855 std::ostringstream s;
856 Utility::CoutRedirect redirect{s.rdbuf()};
857 std::ignore = redirect;
858 const int result = CLI_v2::Run(args, i, runner);
859 EXPECT_EQ(EXIT_SUCCESS, result);
860 EXPECT_NE(std::string::npos, s.str().find("--hidden-mode"));
861 }
862 }
863
864 TEST(CLI2_CLI, show_all_help_displays_hidden_subtools)
865 {
866 using namespace PacBio;
867
868 CLI_v2::MultiToolInterface i{"isoseq3", "De Novo Transcript Reconstruction", "3.1.2"};
869 i.AddTools({{"refine", CLI_v2_CLITests::MakeRefineInterface(), &CLI_v2_CLITests::RefineRunner, CLI_v2::ToolVisibility::HIDDEN},
870 {"cluster", CLI_v2_CLITests::MakeClusterInterface(), &CLI_v2_CLITests::ClusterRunner},
871 {"polish", CLI_v2_CLITests::MakePolishInterface(), &CLI_v2_CLITests::PolishRunner},
872 {"summarize", CLI_v2_CLITests::MakeSummarizeInterface(), &CLI_v2_CLITests::SummarizeRunner}});
873
874 // --all-help
875 {
876 const std::vector<std::string> args {
877 "isoseq3", "--all-help"
878 };
879
880 std::ostringstream s;
881 Utility::CoutRedirect redirect{s.rdbuf()};
882 std::ignore = redirect;
883 const int result = CLI_v2::Run(args, i);
884 EXPECT_EQ(EXIT_SUCCESS, result);
885 EXPECT_NE(std::string::npos, s.str().find("refine"));
886 }
887
888
889 // --show-all-help
890 {
891 const std::vector<std::string> args {
892 "isoseq3", "--show-all-help"
893 };
894
895 std::ostringstream s;
896 Utility::CoutRedirect redirect{s.rdbuf()};
897 std::ignore = redirect;
898 const int result = CLI_v2::Run(args, i);
899 EXPECT_EQ(EXIT_SUCCESS, result);
900 EXPECT_NE(std::string::npos, s.str().find("refine"));
901 }
902 }
903
904 TEST(CLI2_CLI, print_rather_than_terminate_on_CLI_parsing_error)
905 {
906 std::ostringstream s;
907 PacBio::Utility::CerrRedirect redirect{s.rdbuf()};
908 std::ignore = redirect;
909
910 PacBio::CLI_v2::Interface i{"myapp", "Frob all the things.", "v3.1"};
911 auto runner = [](const PacBio::CLI_v2::Results&) { return EXIT_SUCCESS; };
912
913 const std::vector<std::string> args{"myapp", "--blah"};
914 const int result = PacBio::CLI_v2::Run(args, i, runner);
915 EXPECT_EQ(result, EXIT_FAILURE);
916 EXPECT_EQ(s.str().find("terminate"), std::string::npos);
917 }
918
919 TEST(CLI2_CLI, print_help_on_missing_required_pos_arg)
920 {
921 const PacBio::CLI_v2::PositionalArgument Arg {
922 R"({
923 "name" : "source",
924 "description" : "Source file to copy.",
925 "syntax" : "FILE",
926 "required" : true
927 })"};
928
929 std::ostringstream s;
930 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
931
932 PacBio::CLI_v2::Interface i{"myapp", "Frob all the things.", "v3.1"};
933 i.AddPositionalArgument(Arg);
934 auto runner = [](const PacBio::CLI_v2::Results&) {
935 // should not get run
936 return EXIT_FAILURE;
937 };
938
939 const std::vector<std::string> args{"myapp"};
940 const int result = PacBio::CLI_v2::Run(args, i, runner);
941 EXPECT_EQ(result, EXIT_SUCCESS);
942 EXPECT_NE(s.str().find("Usage"), std::string::npos);
943 }
944
945 TEST(CLI2_CLI, print_help_on_missing_required_options)
946 {
947 const PacBio::CLI_v2::Option Opt {
948 R"({
949 "names" : ["source"],
950 "description" : "Source file to copy.",
951 "syntax" : "FILE",
952 "required" : true
953 })"};
954
955 std::ostringstream s;
956 PacBio::Utility::CoutRedirect redirect{s.rdbuf()};
957
958 PacBio::CLI_v2::Interface i{"myapp", "Frob all the things.", "v3.1"};
959 i.AddOption(Opt);
960 auto runner = [](const PacBio::CLI_v2::Results&) {
961 // should not get run
962 return EXIT_FAILURE;
963 };
964
965 const std::vector<std::string> args{"myapp"};
966 const int result = PacBio::CLI_v2::Run(args, i, runner);
967 EXPECT_EQ(result, EXIT_SUCCESS);
968 EXPECT_NE(s.str().find("Usage"), std::string::npos);
969 }
970
971 // clang-format on