Fixed some offenses.

parent d3aebd58
# This configuration was generated by # This configuration was generated by
# `rubocop --auto-gen-config` # `rubocop --auto-gen-config`
# on 2016-01-18 16:32:43 +0100 using RuboCop version 0.36.0. # on 2016-01-19 22:54:29 +0100 using RuboCop version 0.36.0.
# The point is for the user to remove these configuration records # The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base. # one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new # Note that changes in the inspected code, or installation of new
...@@ -19,11 +19,10 @@ Lint/Debugger: ...@@ -19,11 +19,10 @@ Lint/Debugger:
Exclude: Exclude:
- 'spec/integration/common_validation.rb' - 'spec/integration/common_validation.rb'
# Offense count: 8 # Offense count: 7
# Cop supports --auto-correct. # Cop supports --auto-correct.
Lint/DeprecatedClassMethods: Lint/DeprecatedClassMethods:
Exclude: Exclude:
- 'lib/annotate.rb'
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes.rb'
- 'spec/fixtures/rails32_boot.rb' - 'spec/fixtures/rails32_boot.rb'
- 'spec/integration/rails_3.2.2/config/boot.rb' - 'spec/integration/rails_3.2.2/config/boot.rb'
...@@ -379,7 +378,7 @@ Style/IfInsideElse: ...@@ -379,7 +378,7 @@ Style/IfInsideElse:
Exclude: Exclude:
- 'lib/annotate/annotate_routes.rb' - 'lib/annotate/annotate_routes.rb'
# Offense count: 7 # Offense count: 6
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: MaxLineLength. # Configuration parameters: MaxLineLength.
Style/IfUnlessModifier: Style/IfUnlessModifier:
...@@ -635,7 +634,7 @@ Style/SpaceAfterComma: ...@@ -635,7 +634,7 @@ Style/SpaceAfterComma:
Exclude: Exclude:
- 'lib/annotate/annotate_models.rb' - 'lib/annotate/annotate_models.rb'
# Offense count: 58 # Offense count: 50
# Cop supports --auto-correct. # Cop supports --auto-correct.
Style/SpaceAfterControlKeyword: Style/SpaceAfterControlKeyword:
Exclude: Exclude:
...@@ -720,14 +719,14 @@ Style/SpaceInsideStringInterpolation: ...@@ -720,14 +719,14 @@ Style/SpaceInsideStringInterpolation:
Style/SpecialGlobalVars: Style/SpecialGlobalVars:
EnforcedStyle: use_perl_names EnforcedStyle: use_perl_names
# Offense count: 403 # Offense count: 373
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline. # Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes # SupportedStyles: single_quotes, double_quotes
Style/StringLiterals: Style/StringLiterals:
Enabled: false Enabled: false
# Offense count: 3 # Offense count: 2
# Cop supports --auto-correct. # Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles. # Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: single_quotes, double_quotes # SupportedStyles: single_quotes, double_quotes
......
#!/usr/bin/env ruby #!/usr/bin/env ruby
unless File.exists?('./Rakefile') || File.exists?('./Gemfile') unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
abort "Please run annotate from the root of the project." abort 'Please run annotate from the root of the project.'
end end
require 'rubygems' require 'rubygems'
...@@ -20,58 +20,55 @@ Annotate.bootstrap_rake ...@@ -20,58 +20,55 @@ Annotate.bootstrap_rake
has_set_position = {} has_set_position = {}
target_action = :do_annotations target_action = :do_annotations
positions = %w(before top after bottom)
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "Usage: annotate [options] [model_file]*" opts.banner = 'Usage: annotate [options] [model_file]*'
opts.on('-d', '--delete',
"Remove annotations from all model files or the routes.rb file") do
opts.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
target_action = :remove_annotations target_action = :remove_annotations
end end
opts.on('-p', '--position [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'], opts.on('-p', '--position [before|top|after|bottom]', positions,
"Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)") do |p| 'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
ENV['position'] = p ENV['position'] = p
[ %w(position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer).each do |key|
'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes', 'position_in_serializer'
].each do |key|
ENV[key] = p unless(has_set_position[key]) ENV[key] = p unless(has_set_position[key])
end end
end end
opts.on('--pc', '--position-in-class [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'], opts.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
"Place the annotations at the top (before) or the bottom (after) of the model file") do |p| 'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
ENV['position_in_class'] = p ENV['position_in_class'] = p
has_set_position['position_in_class'] = true has_set_position['position_in_class'] = true
end end
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'], opts.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
"Place the annotations at the top (before) or the bottom (after) of any factory files") do |p| 'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
ENV['position_in_factory'] = p ENV['position_in_factory'] = p
has_set_position['position_in_factory'] = true has_set_position['position_in_factory'] = true
end end
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'], opts.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
"Place the annotations at the top (before) or the bottom (after) of any fixture files") do |p| 'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
ENV['position_in_fixture'] = p ENV['position_in_fixture'] = p
has_set_position['position_in_fixture'] = true has_set_position['position_in_fixture'] = true
end end
opts.on('--pt', '--position-in-test [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'], opts.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
"Place the annotations at the top (before) or the bottom (after) of any test files") do |p| 'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
ENV['position_in_test'] = p ENV['position_in_test'] = p
has_set_position['position_in_test'] = true has_set_position['position_in_test'] = true
end end
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'], opts.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
"Place the annotations at the top (before) or the bottom (after) of the routes.rb file") do |p| 'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
ENV['position_in_routes'] = p ENV['position_in_routes'] = p
has_set_position['position_in_routes'] = true has_set_position['position_in_routes'] = true
end end
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'], opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
"Place the annotations at the top (before) or the bottom (after) of the serializer files") do |p| 'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
ENV['position_in_serializer'] = p ENV['position_in_serializer'] = p
has_set_position['position_in_serializer'] = true has_set_position['position_in_serializer'] = true
end end
...@@ -89,34 +86,32 @@ OptionParser.new do |opts| ...@@ -89,34 +86,32 @@ OptionParser.new do |opts|
ENV['wrapper_close'] = p ENV['wrapper_close'] = p
end end
opts.on('-r', '--routes', opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
"Annotate routes.rb with the output of 'rake routes'") do
ENV['routes'] = 'true' ENV['routes'] = 'true'
end end
opts.on('-v', '--version', opts.on('-v', '--version',
"Show the current version of this gem") do 'Show the current version of this gem') do
puts "annotate v#{Annotate.version}"; exit puts "annotate v#{Annotate.version}"; exit
end end
opts.on('-m', '--show-migration', opts.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
"Include the migration version number in the annotation") do ENV['include_version'] = 'yes'
ENV['include_version'] = "yes"
end end
opts.on('-k', '--show-foreign-keys', opts.on('-k', '--show-foreign-keys',
"List the table's foreign key constraints in the annotation") do "List the table's foreign key constraints in the annotation") do
ENV['show_foreign_keys'] = "yes" ENV['show_foreign_keys'] = 'yes'
end end
opts.on('-i', '--show-indexes', opts.on('-i', '--show-indexes',
"List the table's database indexes in the annotation") do "List the table's database indexes in the annotation") do
ENV['show_indexes'] = "yes" ENV['show_indexes'] = 'yes'
end end
opts.on('-s', '--simple-indexes', opts.on('-s', '--simple-indexes',
"Concat the column's related indexes in the annotation") do "Concat the column's related indexes in the annotation") do
ENV['simple_indexes'] = "yes" ENV['simple_indexes'] = 'yes'
end end
opts.on('--model-dir dir', opts.on('--model-dir dir',
...@@ -131,17 +126,17 @@ OptionParser.new do |opts| ...@@ -131,17 +126,17 @@ OptionParser.new do |opts|
opts.on('--ignore-model-subdirects', opts.on('--ignore-model-subdirects',
"Ignore subdirectories of the models directory") do |dir| "Ignore subdirectories of the models directory") do |dir|
ENV['ignore_model_sub_dir'] = "yes" ENV['ignore_model_sub_dir'] = 'yes'
end end
opts.on('--sort', opts.on('--sort',
"Sort columns alphabetically, rather than in creation order") do |dir| "Sort columns alphabetically, rather than in creation order") do |dir|
ENV['sort'] = "yes" ENV['sort'] = 'yes'
end end
opts.on('--classified-sort', opts.on('--classified-sort',
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |dir| "Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |dir|
ENV['classified_sort'] = "yes" ENV['classified_sort'] = 'yes'
end end
opts.on('-R', '--require path', opts.on('-R', '--require path',
...@@ -155,10 +150,10 @@ OptionParser.new do |opts| ...@@ -155,10 +150,10 @@ OptionParser.new do |opts|
opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions| opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
exclusions ||= %w(tests fixtures factories) exclusions ||= %w(tests fixtures factories)
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" } exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = 'yes' }
end end
opts.on('-f', '--format [bare|rdoc|markdown]', ['bare', 'rdoc', 'markdown'], 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt| opts.on('-f', '--format [bare|rdoc|markdown]', %w(bare rdoc markdown), 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
ENV["format_#{fmt}"] = 'yes' ENV["format_#{fmt}"] = 'yes'
end end
...@@ -186,8 +181,8 @@ OptionParser.new do |opts| ...@@ -186,8 +181,8 @@ OptionParser.new do |opts|
ENV['hide_limit_column_types'] = "#{values}" ENV['hide_limit_column_types'] = "#{values}"
end end
opts.on('--ignore-unknown-models', "don't display warnings for bad model files" ) do |values| opts.on('--ignore-unknown-models', "don't display warnings for bad model files" ) do |_values|
ENV['ignore_unknown_models'] = "true" ENV['ignore_unknown_models'] = 'true'
end end
end.parse! end.parse!
......
...@@ -163,11 +163,9 @@ module Annotate ...@@ -163,11 +163,9 @@ module Annotate
end end
require 'rake' require 'rake'
if File.exists?('./Rakefile') load './Rakefile' if File.exist?('./Rakefile')
load './Rakefile'
end
Rake::Task[:environment].invoke rescue nil Rake::Task[:environment].invoke rescue nil
if(!defined?(Rails)) if !defined?(Rails)
# Not in a Rails project, so time to load up the parts of # Not in a Rails project, so time to load up the parts of
# ActiveSupport we need. # ActiveSupport we need.
require 'active_support' require 'active_support'
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
# Released under the same license as Ruby. No Support. No Warranty. # Released under the same license as Ruby. No Support. No Warranty.
# #
module AnnotateRoutes module AnnotateRoutes
PREFIX = "# == Route Map" PREFIX = '# == Route Map'
def self.do_annotations(options={}) def self.do_annotations(options={})
return unless(routes_exists?) return unless(routes_exists?)
...@@ -37,8 +37,8 @@ module AnnotateRoutes ...@@ -37,8 +37,8 @@ module AnnotateRoutes
routes_map.reject!{|line| line.match(/#{options[:ignore_routes]}/)} if options[:ignore_routes] routes_map.reject!{|line| line.match(/#{options[:ignore_routes]}/)} if options[:ignore_routes]
header = [ header = [
"#{PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime("%Y-%m-%d %H:%M")})" : ""), "#{PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : ''),
"#" '#'
] + routes_map.map { |line| "# #{line}".rstrip } ] + routes_map.map { |line| "# #{line}".rstrip }
existing_text = File.read(routes_file) existing_text = File.read(routes_file)
...@@ -49,7 +49,7 @@ module AnnotateRoutes ...@@ -49,7 +49,7 @@ module AnnotateRoutes
# middle of the file. If the number is # middle of the file. If the number is
# zero, no annotation was found. # zero, no annotation was found.
if(position_after) if position_after
# Ensure we have adequate trailing newlines at the end of the file to # Ensure we have adequate trailing newlines at the end of the file to
# ensure a blank line separating the content from the annotation. # ensure a blank line separating the content from the annotation.
content << '' if(content.last != '') content << '' if(content.last != '')
...@@ -89,12 +89,12 @@ module AnnotateRoutes ...@@ -89,12 +89,12 @@ module AnnotateRoutes
protected protected
def self.routes_file def self.routes_file
@routes_rb ||= File.join("config", "routes.rb") @routes_rb ||= File.join('config', 'routes.rb')
end end
def self.routes_exists? def self.routes_exists?
routes_exists = File.exists?(routes_file) routes_exists = File.exists?(routes_file)
puts "Can`t find routes.rb" if(!routes_exists) puts "Can't find routes.rb" if(!routes_exists)
return routes_exists return routes_exists
end end
...@@ -105,7 +105,7 @@ protected ...@@ -105,7 +105,7 @@ protected
return false if existing_text == new_text return false if existing_text == new_text
File.open(routes_file, "wb") { |f| f.puts(new_text) } File.open(routes_file, 'wb') { |f| f.puts(new_text) }
return true return true
end end
...@@ -117,13 +117,13 @@ protected ...@@ -117,13 +117,13 @@ protected
content.split(/\n/, -1).each do |line| content.split(/\n/, -1).each do |line|
line_number += 1 line_number += 1
begin begin
if(mode == :header) if mode == :header
if(line !~ /\s*#/) if line !~ /\s*#/
mode = :content mode = :content
raise unless (line == '') raise unless (line == '')
end end
elsif(mode == :content) elsif mode == :content
if(line =~ /^\s*#\s*== Route.*$/) if line =~ /^\s*#\s*== Route.*$/
header_found_at = line_number header_found_at = line_number
mode = :header mode = :header
else else
...@@ -147,9 +147,9 @@ protected ...@@ -147,9 +147,9 @@ protected
end end
def self.strip_on_removal(content, where_header_found) def self.strip_on_removal(content, where_header_found)
if(where_header_found == :before) if where_header_found == :before
content.shift while(content.first == '') content.shift while(content.first == '')
elsif(where_header_found == :after) elsif where_header_found == :after
content.pop while(content.last == '') content.pop while(content.last == '')
end end
# TODO: If the user buried it in the middle, we should probably see about # TODO: If the user buried it in the middle, we should probably see about
......
...@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper.rb' ...@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper.rb'
require 'annotate/annotate_routes' require 'annotate/annotate_routes'
describe AnnotateRoutes do describe AnnotateRoutes do
ROUTE_FILE = "config/routes.rb" ROUTE_FILE = 'config/routes.rb'
ANNOTATION_ADDED = "#{ROUTE_FILE} annotated." ANNOTATION_ADDED = "#{ROUTE_FILE} annotated."
ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}." ANNOTATION_REMOVED = "Removed annotations from #{ROUTE_FILE}."
FILE_UNCHANGED = "#{ROUTE_FILE} unchanged." FILE_UNCHANGED = "#{ROUTE_FILE} unchanged."
...@@ -11,28 +11,28 @@ describe AnnotateRoutes do ...@@ -11,28 +11,28 @@ describe AnnotateRoutes do
@mock_file ||= double(File, stubs) @mock_file ||= double(File, stubs)
end end
it "should check if routes.rb exists" do it 'should check if routes.rb exists' do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(false) expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(false)
expect(AnnotateRoutes).to receive(:puts).with("Can`t find routes.rb") expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb")
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
describe "When adding" do describe 'When adding' do
before(:each) do before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("") expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return('')
end end
it "should insert annotations if file does not contain annotations" do it 'should insert annotations if file does not contain annotations' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("") expect(File).to receive(:read).with(ROUTE_FILE).and_return("")
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(@mock_file).to receive(:puts).with("\n# == Route Map\n#\n") expect(@mock_file).to receive(:puts).with("\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
it "should skip annotations if file does already contain annotation" do it 'should skip annotations if file does already contain annotation' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n# == Route Map\n#\n") expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n# == Route Map\n#\n")
expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED) expect(AnnotateRoutes).to receive(:puts).with(FILE_UNCHANGED)
...@@ -41,22 +41,22 @@ describe AnnotateRoutes do ...@@ -41,22 +41,22 @@ describe AnnotateRoutes do
end end
describe "When adding with older Rake versions" do describe 'When adding with older Rake versions' do
before(:each) do before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("(in /bad/line)\ngood line") expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
end end
it "should annotate and add a newline!" do it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/) expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
it "should not add a newline if there are empty lines" do it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/) expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# good line\n/)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
...@@ -64,29 +64,29 @@ describe AnnotateRoutes do ...@@ -64,29 +64,29 @@ describe AnnotateRoutes do
end end
describe "When adding with newer Rake versions" do describe 'When adding with newer Rake versions' do
before(:each) do before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("another good line\ngood line") expect(AnnotateRoutes).to receive(:`).with("rake routes").and_return("another good line\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
end end
it "should annotate and add a newline!" do it 'should annotate and add a newline!' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/) expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
it "should not add a newline if there are empty lines" do it 'should not add a newline if there are empty lines' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/) expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations AnnotateRoutes.do_annotations
end end
it "should add a timestamp when :timestamp is passed" do it 'should add a timestamp when :timestamp is passed' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo") expect(File).to receive(:read).with(ROUTE_FILE).and_return("ActionController::Routing...\nfoo")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/) expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n# == Route Map \(Updated \d{4}-\d{2}-\d{2} \d{2}:\d{2}\)\n#\n# another good line\n# good line\n/)
AnnotateRoutes.do_annotations :timestamp => true AnnotateRoutes.do_annotations :timestamp => true
...@@ -94,21 +94,21 @@ describe AnnotateRoutes do ...@@ -94,21 +94,21 @@ describe AnnotateRoutes do
end end
describe "When removing" do describe 'When removing' do
before(:each) do before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true) expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(File).to receive(:open).with(ROUTE_FILE, "wb").and_yield(mock_file) expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED) expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED)
end end
it "should remove trailing annotation and trim trailing newlines, but leave leading newlines alone" do it 'should remove trailing annotation and trim trailing newlines, but leave leading newlines alone' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n# == Route Map\n#\n# another good line\n# good line\n") expect(File).to receive(:read).with(ROUTE_FILE).and_return("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n# == Route Map\n#\n# another good line\n# good line\n")
expect(@mock_file).to receive(:puts).with(/\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n/) expect(@mock_file).to receive(:puts).with(/\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n/)
AnnotateRoutes.remove_annotations AnnotateRoutes.remove_annotations
end end
it "should remove prepended annotation and trim leading newlines, but leave trailing newlines alone" do it 'should remove prepended annotation and trim leading newlines, but leave trailing newlines alone' do
expect(File).to receive(:read).with(ROUTE_FILE).and_return("# == Route Map\n#\n# another good line\n# good line\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n") expect(File).to receive(:read).with(ROUTE_FILE).and_return("# == Route Map\n#\n# another good line\n# good line\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n")
expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n/) expect(@mock_file).to receive(:puts).with(/ActionController::Routing...\nfoo\n\n\n\n\n\n\n\n\n\n\n/)
AnnotateRoutes.remove_annotations AnnotateRoutes.remove_annotations
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment