Add cmd specs

This commit is contained in:
Vitalii Elenhaupt 2017-12-08 19:40:11 +02:00 committed by V. Elenhaupt
parent e1fa8677b0
commit b515faff87
2 changed files with 73 additions and 4 deletions

View file

@ -0,0 +1,69 @@
require "../../spec_helper"
require "../../../src/ameba/cli/cmd"
module Ameba::Cli
describe "Cmd" do
it "has a list of available formatters" do
AVAILABLE_FORMATTERS.should_not be_nil
end
describe ".run" do
it "runs ameba" do
r = Cli.run %w(-f silent file.cr)
r.should be_nil
end
end
describe ".parse_args" do
%w(-s --silent).each do |f|
it "accepts #{f} flag" do
c = Cli.parse_args [f]
c.formatter.should eq :silent
end
end
%w(-c --config).each do |f|
it "accepts #{f} flag" do
c = Cli.parse_args [f, "config.yml"]
c.config.should eq "config.yml"
end
end
%w(-f --format).each do |f|
it "accepts #{f} flag" do
c = Cli.parse_args [f, "my-formatter"]
c.formatter.should eq "my-formatter"
end
end
it "accepts --only flag" do
c = Cli.parse_args ["--only", "RULE1,RULE2"]
c.only.should eq %w(RULE1 RULE2)
end
it "accepts --except flag" do
c = Cli.parse_args ["--except", "RULE1,RULE2"]
c.except.should eq %w(RULE1 RULE2)
end
it "accepts --gen-config flag" do
c = Cli.parse_args %w(--gen-config)
c.formatter.should eq :todo
end
it "accepts unknown args as files" do
c = Cli.parse_args %w(source1.cr source2.cr)
c.files.should eq %w(source1.cr source2.cr)
end
it "allows args to be blank" do
c = Cli.parse_args [] of String
c.formatter.should eq :progress
c.files.should be_nil
c.only.should be_nil
c.except.should be_nil
c.config.should eq Config::PATH
end
end
end
end

View file

@ -14,7 +14,7 @@ module Ameba::Cli
def run(args) def run(args)
opts = parse_args args opts = parse_args args
config = Ameba::Config.load opts.config config = Config.load opts.config
config.files = opts.files config.files = opts.files
configure_formatter(config, opts) configure_formatter(config, opts)
@ -27,7 +27,7 @@ module Ameba::Cli
end end
private class Opts private class Opts
property config = Ameba::Config::PATH property config = Config::PATH
property formatter : String | Symbol = :progress property formatter : String | Symbol = :progress
property files : Array(String)? property files : Array(String)?
property only : Array(String)? property only : Array(String)?
@ -65,7 +65,7 @@ module Ameba::Cli
parser.on("--gen-config", parser.on("--gen-config",
"Generate a configuration file acting as a TODO list") do "Generate a configuration file acting as a TODO list") do
opts.formatter = "todo" opts.formatter = :todo
end end
end end
@ -98,7 +98,7 @@ module Ameba::Cli
end end
private def print_version private def print_version
puts Ameba::VERSION puts VERSION
exit 0 exit 0
end end