Permalink
Browse files

Fixed CLI file lists

After last config file changes, when giving files via the CLI, the
:default file set file list would still get used (in addition to the
CLI file).  Also refactored out some cyclomatic complexity.
  • Loading branch information...
1 parent e2bae97 commit d3bcda6a8389212f81ec6b63fd23c5541949b9e8 @turboladen committed May 6, 2012
Showing with 228 additions and 96 deletions.
  1. +41 −31 lib/tailor/configuration.rb
  2. +5 −1 lib/tailor/configuration/file_set.rb
  3. +182 −64 spec/integration/configuration_spec.rb
View
72 lib/tailor/configuration.rb
@@ -13,6 +13,10 @@ class Tailor
#
# It then basically represents a list of "file sets" and the rulers that
# should be applied against each file set.
+ #
+ # If a file list is given from the CLI _and_ a configuration file is
+ # given/found, tailor uses the style settings for the default file set and
+ # only checks the default file set.
class Configuration
include Tailor::Logger::Mixin
@@ -35,7 +39,6 @@ def self.default
def initialize(runtime_file_list=nil, options=nil)
@formatters = ['text']
@file_sets = {}
-
@runtime_file_list = runtime_file_list
log "Got runtime file list: #{@runtime_file_list}"
@@ -49,17 +52,17 @@ def initialize(runtime_file_list=nil, options=nil)
# Call this to load settings from the config file and from CLI options.
def load!
- # Get config file settings
if config_file
load_from_config_file(config_file)
- if @rc_file_config
+ if @config_from_file
get_formatters_from_config_file
+ #get_file_sets_from_config_file unless @runtime_file_list
get_file_sets_from_config_file
end
else
log "Creating default file set..."
- @file_sets = { default: FileSet.new }
+ @file_sets = { default: FileSet.new(@runtime_file_list) }
end
get_formatters_from_cli_opts
@@ -78,9 +81,9 @@ def load_from_config_file(config_file)
log "Loading config from file: #{user_config_file}"
begin
- @rc_file_config = instance_eval(File.read(user_config_file), user_config_file)
+ @config_from_file = instance_eval(File.read(user_config_file), user_config_file)
log "Got new config from file: #{user_config_file}"
- log @rc_file_config.inspect
+ log @config_from_file.inspect
rescue LoadError => ex
raise Tailor::RuntimeError,
"Couldn't load config file: #{user_config_file}"
@@ -104,30 +107,30 @@ def config_file
end
def get_file_sets_from_config_file
- unless @rc_file_config.file_sets.empty?
- @rc_file_config.file_sets.each do |label, file_set|
- log "label: #{label}"
- log "file set file list: #{file_set[:file_list]}"
- log "file set style: #{file_set[:style]}"
-
- if @file_sets[label]
- log "label already exists. Updating..."
- @file_sets[label].update_file_list(file_set[:file_list])
- @file_sets[label].update_style(file_set[:style])
- else
- log "Creating new label..."
- @file_sets[label] =
- FileSet.new(file_set[:style], file_set[:file_list])
- end
+ return if @config_from_file.file_sets.empty?
+
+ @config_from_file.file_sets.each do |label, file_set|
+ log "label: #{label}"
+ log "file set file list: #{file_set[:file_list]}"
+ log "file set style: #{file_set[:style]}"
+
+ if @file_sets[label]
+ log "label already exists. Updating..."
+ @file_sets[label].update_file_list(file_set[:file_list])
+ @file_sets[label].update_style(file_set[:style])
+ else
+ log "Creating new label..."
+ @file_sets[label] =
+ FileSet.new(file_set[:file_list], file_set[:style])
end
end
end
def get_formatters_from_config_file
- unless @rc_file_config.formatters.empty?
- @formatters = @rc_file_config.formatters
- log "@formatters is now #{@formatters}"
- end
+ return if @config_from_file.formatters.empty?
+
+ @formatters = @config_from_file.formatters
+ log "@formatters is now #{@formatters}"
end
def get_style_from_cli_opts
@@ -144,12 +147,19 @@ def get_style_from_cli_opts
end
end
+ # If any files are given from the CLI, this gets that list of files and
+ # replaces those in any :default file set.
def get_file_sets_from_cli_opts
- unless @runtime_file_list.nil? || @runtime_file_list.empty?
- # Only use options set for the :default file set because the user gave
- # a different set of files to measure.
- @file_sets.delete_if { |k, v| k != :default }
- @file_sets[:default].update_file_list(@runtime_file_list)
+ return if @runtime_file_list.nil? || @runtime_file_list.empty?
+
+ # Only use options set for the :default file set because the user gave
+ # a different set of files to measure.
+ @file_sets.delete_if { |k, v| k != :default }
+
+ if @file_sets.include? :default
+ @file_sets[:default].file_list = @runtime_file_list
+ else
+ @file_sets = { default: FileSet.new(@runtime_file_list) }
end
end
@@ -179,7 +189,7 @@ def file_set(file_expression='lib/**/*.rb', label=:default)
yield new_style if block_given?
- @file_sets[label] = FileSet.new(new_style, file_expression)
+ @file_sets[label] = FileSet.new(file_expression, new_style)
log "file sets after: #{@file_sets}"
end
View
6 lib/tailor/configuration/file_set.rb
@@ -15,7 +15,7 @@ class FileSet < Hash
# @param [Hash] style Style options to merge into the default Style
# settings.
# @param [String,Array] file_expression
- def initialize(style=nil, file_expression=nil)
+ def initialize(file_expression=nil, style=nil)
@style = if style
Style.new.to_hash.merge(style)
else
@@ -48,6 +48,10 @@ def [](key)
end
end
+ def file_list=(file_expression)
+ @file_list = build_file_list(file_expression)
+ end
+
private
# The list of the files in the project to check.
View
246 spec/integration/configuration_spec.rb
@@ -7,41 +7,75 @@
FakeFS.deactivate!
end
- let(:config) do
- config = Tailor::Configuration.new
- config.load!
+ context "files aren't given at runtime" do
+ let(:config) do
+ config = Tailor::Configuration.new
+ config.load!
- config
- end
-
- context ".tailor does not exist" do
- before do
- Tailor::Configuration.any_instance.stub(:config_file).and_return false
+ config
end
- it "sets formatters to 'text'" do
- config.formatters.should == %w(text)
- end
+ context ".tailor does not exist" do
+ before do
+ Tailor::Configuration.any_instance.stub(:config_file).and_return false
+ end
- it "sets file_sets[:default].style to the default style" do
- config.file_sets[:default].style.should_not be_nil
- config.file_sets[:default].style.should == Tailor::Configuration::Style.new.to_hash
- end
+ it "sets formatters to 'text'" do
+ config.formatters.should == %w(text)
+ end
- it "sets file_sets[:default].file_list to the files in lib/**/*.rb" do
- config.file_sets[:default].file_list.all? do |path|
- path =~ /tailor\/lib/
- end.should be_true
+ it "sets file_sets[:default].style to the default style" do
+ config.file_sets[:default].style.should_not be_nil
+ config.file_sets[:default].style.should == Tailor::Configuration::Style.new.to_hash
+ end
+
+ it "sets file_sets[:default].file_list to the files in lib/**/*.rb" do
+ config.file_sets[:default].file_list.all? do |path|
+ path =~ /tailor\/lib/
+ end.should be_true
+ end
end
+
+ context ".tailor defines the default file set" do
+ context "and another file set" do
+ let(:config_file) do
+ <<-CONFIG
+Tailor.config do |config|
+ config.file_set 'lib/**/*.rb'
+
+ config.file_set 'features/**/*.rb', :features do |style|
+ style.max_line_length 90, level: :warn
end
+end
+ CONFIG
+ end
+
+ before do
+ File.should_receive(:read).and_return config_file
+ end
+
+ it "creates the default file set" do
+ config.file_sets[:default].style.should == Tailor::Configuration::Style.new.to_hash
+ config.file_sets[:default].file_list.all? do |path|
+ path =~ /tailor\/lib/
+ end.should be_true
+ end
+
+ it "creates the :features file set" do
+ style = Tailor::Configuration::Style.new
+ style.max_line_length(90, level: :warn)
+ config.file_sets[:features].style.should == style.to_hash
+ config.file_sets[:features].file_list.all? do |path|
+ path =~ /features/
+ end.should be_true
+ end
+ end
+ end
- context ".tailor defines the default file set" do
- context "and another file set" do
+ context ".tailor defines NO default file set" do
let(:config_file) do
<<-CONFIG
Tailor.config do |config|
- config.file_set 'lib/**/*.rb'
-
config.file_set 'features/**/*.rb', :features do |style|
style.max_line_length 90, level: :warn
end
@@ -53,71 +87,155 @@
File.should_receive(:read).and_return config_file
end
- it "creates the default file set" do
- config.file_sets[:default].style.should == Tailor::Configuration::Style.new.to_hash
- config.file_sets[:default].file_list.all? do |path|
- path =~ /tailor\/lib/
- end.should be_true
+ it "does not create a :default file set" do
+ config.file_sets.should_not include :default
end
- it "creates the :features file set" do
- style = Tailor::Configuration::Style.new
- style.max_line_length(90, level: :warn)
- config.file_sets[:features].style.should == style.to_hash
- config.file_sets[:features].file_list.all? do |path|
- path =~ /features/
- end.should be_true
+ it "creates the non-default file set" do
+ config.file_sets.should include :features
end
end
- end
- context ".tailor defines NO default file set" do
- let(:config_file) do
- <<-CONFIG
+ context '.tailor defines a single recursive file set' do
+ let(:config_file) do
+ <<-CONFIG
Tailor.config do |config|
- config.file_set 'features/**/*.rb', :features do |style|
+ config.recursive_file_set '*spec.rb' do |style|
style.max_line_length 90, level: :warn
end
end
- CONFIG
- end
+ CONFIG
+ end
- before do
- File.should_receive(:read).and_return config_file
+ before do
+ File.should_receive(:read).and_return config_file
+ end
+
+ it 'creates a :default file set' do
+ config.file_sets.keys.should == [:default]
+ end
+
+ it 'has files in the file list levels deep' do
+ config.file_sets[:default].file_list.all? do |file|
+ file =~ /spec\.rb$/
+ end.should be_true
+ end
end
+ end
- it "does not create a :default file set" do
- config.file_sets.should_not include :default
+ context 'files are given at runtime' do
+ let(:config) do
+ config = Tailor::Configuration.new('lib/tailor.rb')
+ config.load!
+
+ config
end
- it "creates the non-default file set" do
- config.file_sets.should include :features
+ context ".tailor does not exist" do
+ before do
+ Tailor::Configuration.any_instance.stub(:config_file).and_return false
+ end
+
+ it "sets formatters to 'text'" do
+ config.formatters.should == %w(text)
+ end
+
+ it "sets file_sets[:default].style to the default style" do
+ config.file_sets[:default].style.should_not be_nil
+ config.file_sets[:default].style.should == Tailor::Configuration::Style.new.to_hash
+ end
+
+ it "sets file_sets[:default].file_list to the runtime files" do
+ config.file_sets[:default].file_list.size.should be 1
+ config.file_sets[:default].file_list.first.match /lib\/tailor\.rb$/
+ end
end
- end
- context '.tailor defines a single recursive file set' do
- let(:config_file) do
- <<-CONFIG
+ context ".tailor defines the default file set" do
+ context "and another file set" do
+ let(:config_file) do
+ <<-CONFIG
Tailor.config do |config|
- config.recursive_file_set '*spec.rb' do |style|
+ config.file_set 'lib/**/*.rb' do |style|
+ style.max_line_length 85
+ end
+
+ config.file_set 'features/**/*.rb', :features do |style|
style.max_line_length 90, level: :warn
end
end
- CONFIG
+ CONFIG
+ end
+
+ before do
+ File.should_receive(:read).and_return config_file
+ end
+
+ it "creates the default file set using the runtime files" do
+ style = Tailor::Configuration::Style.new
+ style.max_line_length 85
+ config.file_sets[:default].style.should == style.to_hash
+ config.file_sets[:default].file_list.size.should be 1
+ config.file_sets[:default].file_list.first.match /lib\/tailor\.rb$/
+ end
+
+ it "does not create the :features file set" do
+ config.file_sets.should_not include :features
+ end
+ end
end
- before do
- File.should_receive(:read).and_return config_file
- end
+ context ".tailor defines NO default file set" do
+ let(:config_file) do
+ <<-CONFIG
+Tailor.config do |config|
+ config.file_set 'features/**/*.rb', :features do |style|
+ style.max_line_length 90, level: :warn
+ end
+end
+ CONFIG
+ end
- it 'creates a :default file set' do
- config.file_sets.keys.should == [:default]
+ before do
+ File.should_receive(:read).and_return config_file
+ end
+
+ it "creates a :default file set with the runtime file and default style" do
+ config.file_sets[:default].style.should == Tailor::Configuration::Style.new.to_hash
+ config.file_sets[:default].file_list.size.should be 1
+ config.file_sets[:default].file_list.first.match /lib\/tailor\.rb$/
+ end
+
+ it "does not create the non-default file set" do
+ config.file_sets.should_not include :features
+ end
end
- it 'has files in the file list levels deep' do
- config.file_sets[:default].file_list.all? do |file|
- file =~ /spec\.rb$/
- end.should be_true
+ context '.tailor defines a single recursive file set' do
+ let(:config_file) do
+ <<-CONFIG
+Tailor.config do |config|
+ config.recursive_file_set '*_spec.rb' do |style|
+ style.max_line_length 90, level: :warn
+ end
+end
+ CONFIG
+ end
+
+ before do
+ File.should_receive(:read).and_return config_file
+ end
+
+ it 'creates a :default file set' do
+ config.file_sets.keys.should == [:default]
+ end
+
+ it "creates a :default file set with the runtime file and default style" do
+ config.file_sets[:default].style.should == Tailor::Configuration::Style.new.to_hash
+ config.file_sets[:default].file_list.size.should be 1
+ config.file_sets[:default].file_list.first.match /lib\/tailor\.rb$/
+ end
end
end
end
+

0 comments on commit d3bcda6

Please sign in to comment.