Basic command line handling

This commit is contained in:
Emmanuel Briot 2014-02-06 16:48:11 +01:00
parent e52dd57fc4
commit 04f5d7825d
5 changed files with 246 additions and 21 deletions

View File

@ -109,4 +109,36 @@ package body BDD.Formatters is
New_Line;
end Scenario_Completed;
------------------------
-- Scenario_Completed --
------------------------
overriding procedure Scenario_Completed
(Self : Formatter_Dots;
Feature : BDD.Features.Feature'Class;
Scenario : BDD.Features.Scenario'Class)
is
pragma Unreferenced (Self, Feature, Scenario);
begin
Put (".");
end Scenario_Completed;
----------------------
-- Create_Formatter --
----------------------
function Create_Formatter return not null access Formatter'Class is
begin
case BDD.Output is
when Output_Hide_Passed =>
return new Formatter_Hide_Passed;
when Output_Quiet =>
return new Formatter_Quiet;
when Output_Dots =>
return new Formatter_Dots;
when Output_Full =>
return new Formatter_Full;
end case;
end Create_Formatter;
end BDD.Formatters;

View File

@ -54,6 +54,13 @@ package BDD.Formatters is
Scenario : BDD.Features.Scenario'Class) is null;
-- Called when a scenario has completed
function Create_Formatter return not null access Formatter'Class;
-- Create the formatter to use, depending on BDD.Output
----------
-- Full --
----------
type Formatter_Full is new Formatter with private;
-- A formatter that displays all the features, scenarios and steps that
-- are executed
@ -70,11 +77,37 @@ package BDD.Formatters is
Feature : BDD.Features.Feature'Class;
Scenario : BDD.Features.Scenario'Class);
----------
-- Dots --
----------
type Formatter_Dots is new Formatter with private;
overriding procedure Scenario_Completed
(Self : Formatter_Dots;
Feature : BDD.Features.Feature'Class;
Scenario : BDD.Features.Scenario'Class);
-----------
-- Quiet --
-----------
type Formatter_Quiet is new Formatter with private;
-----------------
-- Hide_Passed --
-----------------
type Formatter_Hide_Passed is new Formatter with private;
private
type Formatter is abstract tagged record
Term : GNATCOLL.Terminal.Terminal_Info_Access;
end record;
type Formatter_Full is new Formatter with null record;
type Formatter_Full is new Formatter with null record;
type Formatter_Dots is new Formatter with null record;
type Formatter_Quiet is new Formatter with null record;
type Formatter_Hide_Passed is new Formatter with null record;
end BDD.Formatters;

159
src/bdd.adb Normal file
View File

@ -0,0 +1,159 @@
-----------------------------------------------------------------------------
-- g N A T C O L L --
-- --
-- Copyright (C) 2005-2014, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
------------------------------------------------------------------------------
with BDD.Formatters; use BDD.Formatters;
with BDD.Parser; use BDD.Parser;
with BDD.Runner; use BDD.Runner;
with GNAT.Command_Line; use GNAT.Command_Line;
with GNATCOLL.Terminal; use GNATCOLL.Terminal;
with GNATCOLL.Traces; use GNATCOLL.Traces;
package body BDD is
---------------------------
-- Command_Line_Switches --
---------------------------
procedure Command_Line_Switches is
procedure Callback (Switch, Parameter, Section : String);
-- Called for each switch on the command line
procedure Callback (Switch, Parameter, Section : String) is
pragma Unreferenced (Section);
begin
if Switch = "--features" then
BDD.Features_Directory := Create_From_Base (+Parameter);
elsif Switch = "--tags" then
null; -- ??? not handled yet
elsif Switch = "--color" then
if Parameter = "yes" then
BDD.Colors := GNATCOLL.Terminal.Yes;
elsif Parameter = "no" then
BDD.Colors := GNATCOLL.Terminal.No;
else
BDD.Colors := GNATCOLL.Terminal.Auto;
end if;
elsif Switch = "--output" then
if Parameter = "quiet" then
BDD.Output := Output_Quiet;
elsif Parameter = "hide_passed" then
BDD.Output := Output_Hide_Passed;
elsif Parameter = "full" then
BDD.Output := Output_Full;
else
BDD.Output := Output_Dots;
end if;
elsif Switch = "--log" then
null; -- ??? not handled yet
end if;
end Callback;
Config : Command_Line_Configuration;
begin
Set_Usage
(Config,
Usage => "[switches] [file:line1:line2] [file#num1#num2]",
Help =>
"Run each of the scenario defined in features files.");
Define_Switch
(Config,
Long_Switch => "--features=",
Argument => "DIR",
Help => "Directory in which to recursively look for features files"
& " (default 'features')");
Define_Switch
(Config,
Output => BDD.Features_File_Ext'Access,
Long_Switch => "--ext=",
Argument => ".EXT",
Help => "Extension for features file (default '.feature')");
Define_Switch
(Config,
Long_Switch => "--tags=",
Argument => "EXPR",
Help => "Defines which scenarios to run. EXPR can be one of"
& ASCII.LF
& " @tag1,@tag2 (run scenarios with either @tag1 or @tag2)"
& ASCII.LF
& " ~@tag1,@tag2 (run scenarios with neither @tag1 nor @tag2)"
& ASCII.LF
& " Use multiple --tags switches to AND multiple tags");
Define_Switch
(Config,
Long_Switch => "--color=",
Argument => "YESNO",
Help => "Whether to use colors in the output (AUTO|yes|no)");
Define_Switch
(Config,
Long_Switch => "--output=",
Argument => "format",
Help => "Controls the output format (quiet|DOTS|hide_passed|full)");
Define_Switch
(Config,
Long_Switch => "--log=",
Argument => "DIR",
Help => "Directory to store the log files (default is 'logs')");
Getopt (Config, Callback'Unrestricted_Access);
end Command_Line_Switches;
----------
-- Main --
----------
procedure Main is
Features : BDD.Runner.Feature_Runner;
Parser : BDD.Parser.Feature_Parser;
Format : access BDD.Formatters.Formatter'Class;
Term : constant Terminal_Info_Access := new Terminal_Info;
begin
GNATCOLL.Traces.Parse_Config_File;
BDD.Command_Line_Switches;
Features.Discover (+BDD.Features_File_Ext.all, BDD.Features_Directory);
Term.Init_For_Stdout (Colors => BDD.Colors);
Format := BDD.Formatters.Create_Formatter;
Format.Init (Term);
Features.Run (Format, Parser);
exception
when GNAT.Command_Line.Exit_From_Command_Line
| GNAT.Command_Line.Invalid_Switch
| GNAT.Command_Line.Invalid_Parameter
=>
null;
end Main;
end BDD;

View File

@ -22,6 +22,7 @@
------------------------------------------------------------------------------
with GNAT.Strings; use GNAT.Strings;
with GNATCOLL.Terminal;
with GNATCOLL.VFS; use GNATCOLL.VFS;
package BDD is
@ -43,7 +44,25 @@ package BDD is
Create_From_Base ("features");
-- The parent directory for all features file.
Features_File_Ext : GNAT.Strings.String_Access := new String'(".feature");
Features_File_Ext : aliased GNAT.Strings.String_Access :=
new String'(".feature");
-- Extension for the features file
Colors : GNATCOLL.Terminal.Supports_Color := GNATCOLL.Terminal.Auto;
-- Whether we should use colors in the output
type Output_Type is
(Output_Quiet,
Output_Dots,
Output_Hide_Passed,
Output_Full);
Output : Output_Type := Output_Dots;
-- The kind of output the user expects
procedure Command_Line_Switches;
-- Handles the command line switches
procedure Main;
-- The main loop, which discovers and then runs all the tests
end BDD;

View File

@ -25,26 +25,8 @@
-- Such code would be automatically generated by gnattdd
with BDD; use BDD;
with BDD.Formatters; use BDD.Formatters;
with BDD.Parser; use BDD.Parser;
with BDD.Runner; use BDD.Runner;
with GNATCOLL.Terminal; use GNATCOLL.Terminal;
with GNATCOLL.Traces; use GNATCOLL.Traces;
with GNATCOLL.VFS; use GNATCOLL.VFS;
procedure Driver is
Features : BDD.Runner.Feature_Runner;
Parser : BDD.Parser.Feature_Parser;
Format : constant access BDD.Formatters.Formatter'Class :=
new BDD.Formatters.Formatter_Full;
Term : constant Terminal_Info_Access := new Terminal_Info;
begin
GNATCOLL.Traces.Parse_Config_File;
Features.Discover (+BDD.Features_File_Ext.all, BDD.Features_Directory);
Term.Init_For_Stdout (Colors => Auto);
Format.Init (Term);
Features.Run (Format, Parser);
BDD.Main;
end Driver;