1 module cucumber.commandline;
2 
3 import std.getopt;
4 
5 import cucumber.formatter;
6 import cucumber.result : RunResult;
7 import cucumber.runner : CucumberRunner;
8 import gherkin : GherkinDocument, Parser, getFeatureFiles;
9 
10 /**
11  * CucumberCommandLine
12  */
13 class CucumberCommandline
14 {
15     /**
16      * run
17      *
18      * Returns:
19      *   exit code
20      */
21     int run(ModuleNames...)(string[] args)
22     {
23         string require;
24         string format;
25         bool dryRun;
26         bool noColor;
27         bool quiet;
28         bool noSnippets;
29         bool noSource;
30         bool strict;
31         // dfmt off
32         auto opts = getopt(args,
33                 std.getopt.config.caseSensitive,
34                 "r|require", "Implemented for compatibility. Do nothing.",
35                 &require,
36                 "d|dry-run", "Invokes formatters without executing the steps.",
37                 &dryRun,
38                 "f|format", "How to format features (Default: pretty). Available formats:
39                    json        : Prints the feature as JSON
40                    json_pretty : Prints the feature as prettified JSON
41                    pretty      : Prints the feature as is - in colours.
42                    progress    : Prints one character per scenario.",
43                 &format,
44                 "no-color", "Whether or not to use ANSI color in the output.",
45                 &noColor,
46                 "i|no-snippets", "Don't print snippets for pending steps.",
47                 &noSnippets,
48                 "s|no-source", "Don't print the file and line of the step definition with the steps.",
49                 &noSource,
50                 "q|quiet", "Alias for --no-snippets --no-source.",
51                 &quiet,
52                 "S|strict", "Fail if there are any strict affected results.",
53                 &strict,
54                 std.getopt.config.passThrough);
55         // dfmt on
56         noSnippets |= quiet;
57         noSource |= quiet;
58 
59         if (opts.helpWanted)
60         {
61             defaultGetoptPrinter("Usage: cucumber [options]", opts.options);
62             return 0;
63         }
64 
65         Formatter formatter;
66         switch (format)
67         {
68         case "json":
69             formatter = new Json(noColor, noSnippets, noSource);
70             break;
71         case "json_pretty":
72             formatter = new Json(noColor, noSnippets, noSource, true);
73             break;
74         case "progress":
75             formatter = new Progress(noColor, noSnippets, noSource);
76             break;
77         case "pretty":
78         default:
79             formatter = new Pretty(noColor, noSnippets, noSource);
80         }
81         auto runner = new CucumberRunner(formatter, dryRun);
82         auto featureFiles = getFeatureFiles(args);
83         RunResult result;
84 
85         foreach (featureFile; featureFiles)
86         {
87             // TODO: Throw No such file error if file does not exit
88             auto gherkinDocument = Parser.parseFromFile(featureFile);
89 
90             result += runner.runFeature!ModuleNames(gherkinDocument);
91         }
92 
93         formatter.summarizeResult(result);
94 
95         immutable auto scenarioResultSummary = result.resultSummaries["scenario"];
96         if (strict)
97         {
98             return scenarioResultSummary.passed == scenarioResultSummary.total ? 0 : 1;
99         }
100         return scenarioResultSummary.failed == 0 ? 0 : 1;
101     }
102 }