Commit aaeab039 by Cuong Tran

Merge pull request #324 from ryanfox1985/develop

Add ruby head to travis and integrate with rubocop
parents 477b641b 9ba5d511
inherit_from: ./.rubocop_todo.yml
AllCops:
Include:
- '**/Rakefile'
- '**/config.ru'
Exclude:
- 'bin/**/*'
...@@ -6,5 +6,12 @@ rvm: ...@@ -6,5 +6,12 @@ rvm:
- 2.1 - 2.1
- 2.2 - 2.2
- 2.3.0 - 2.3.0
- ruby-head
matrix:
allow_failures:
- rvm: ruby-head
before_install: before_install:
- rvm @global do gem install bundler - rvm @global do gem install bundler
script:
- bundle exec rspec
- bundle exec rubocop
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'rake', '>= 10.4.2', :require => false gem 'rake', '>= 10.4.2', require: false
gem 'activerecord', '>= 4.2.5', :require => false gem 'activerecord', '>= 4.2.5', require: false
group :development do group :development do
gem 'mg', :require => false gem 'mg', require: false
gem 'bump' gem 'bump'
platforms :mri do platforms :mri do
gem 'yard', :require => false gem 'yard', require: false
end end
end end
group :development, :test do group :development, :test do
gem 'rspec', :require => false gem 'rspec', require: false
gem 'guard-rspec', :require => false gem 'guard-rspec', require: false
gem 'terminal-notifier-guard', :require => false gem 'terminal-notifier-guard', require: false
gem 'simplecov', require: false
gem 'rubocop', require: false unless RUBY_VERSION =~ /^1.8/
gem 'coveralls'
gem 'codeclimate-test-reporter'
platforms :mri do platforms :mri do
gem 'pry', :require => false gem 'pry', require: false
gem 'pry-coolline', :require => false gem 'pry-coolline', require: false
end end
end end
group :test do group :test do
gem 'wrong', :require => false gem 'wrong', require: false
gem 'files', :require => false gem 'files', require: false
end end
== Annotate (aka AnnotateModels) == Annotate (aka AnnotateModels)
{<img src="https://badge.fury.io/rb/annotate.svg" alt="Gem Version" />}[http://badge.fury.io/rb/annotate] {<img src="https://badge.fury.io/rb/annotate.svg" alt="Gem Version" />}[http://badge.fury.io/rb/annotate]
{<img src="https://img.shields.io/gem/dt/annotate_models.svg?style=flat" />}[https://rubygems.org/gems/annotate_models]
{<img src="https://travis-ci.org/ctran/annotate_models.png" />}[https://travis-ci.org/ctran/annotate_models] {<img src="https://travis-ci.org/ctran/annotate_models.png" />}[https://travis-ci.org/ctran/annotate_models]
{<img src="https://codeclimate.com/github/ctran/annotate_models/badges/gpa.svg" />}[https://codeclimate.com/github/ctran/annotate_models]
{<img src="https://gemnasium.com/ctran/annotate_models.png" />}[https://gemnasium.com/ctran/annotate_models] {<img src="https://gemnasium.com/ctran/annotate_models.png" />}[https://gemnasium.com/ctran/annotate_models]
Add a comment summarizing the current schema to the top or bottom of each of Add a comment summarizing the current schema to the top or bottom of each of
......
def exit_exception(e)
$stderr.puts e.message
exit e.status_code
end
# Note : this causes annoying psych warnings under Ruby 1.9.2-p180; to fix, upgrade to 1.9.3 # Note : this causes annoying psych warnings under Ruby 1.9.2-p180; to fix, upgrade to 1.9.3
begin begin
require 'bundler' require 'bundler'
Bundler.setup(:default, :development) Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e rescue Bundler::BundlerError => e
$stderr.puts e.message $stderr.puts 'Run `bundle install` to install missing gems'
$stderr.puts "Run `bundle install` to install missing gems" exit_exception(e)
exit e.status_code
end end
using_dsl = false using_dsl = false
...@@ -14,18 +18,16 @@ begin ...@@ -14,18 +18,16 @@ begin
using_dsl = true using_dsl = true
rescue Exception => e rescue Exception => e
# We might just be on an old version of Rake... # We might just be on an old version of Rake...
exit_exception(e)
end end
require 'rake' require 'rake'
if(using_dsl) include Rake::DSL if using_dsl
include Rake::DSL
end
require "./lib/annotate" require './lib/annotate'
require 'mg'
require "mg"
begin begin
MG.new("annotate.gemspec") MG.new("annotate.gemspec")
rescue Exception => e rescue Exception
STDERR.puts("WARNING: Couldn't read gemspec. As such, a number of tasks may be unavailable to you until you run 'rake gem:gemspec' to correct the issue.") STDERR.puts("WARNING: Couldn't read gemspec. As such, a number of tasks may be unavailable to you until you run 'rake gem:gemspec' to correct the issue.")
# Gemspec is probably in a broken state, so let's give ourselves a chance to # Gemspec is probably in a broken state, so let's give ourselves a chance to
# build a new one... # build a new one...
...@@ -88,7 +90,7 @@ namespace :jeweler do ...@@ -88,7 +90,7 @@ namespace :jeweler do
FileUtils.rm_f("pkg") FileUtils.rm_f("pkg")
end end
end end
task :clobber => :'jeweler:clobber' task clobber: :'jeweler:clobber'
require "rspec/core/rake_task" # RSpec 2.0 require "rspec/core/rake_task" # RSpec 2.0
RSpec::Core::RakeTask.new(:spec) do |t| RSpec::Core::RakeTask.new(:spec) do |t|
...@@ -111,7 +113,7 @@ namespace :gemsets do ...@@ -111,7 +113,7 @@ namespace :gemsets do
end end
end end
end end
task :clobber => :'gemsets:empty' task clobber: :'gemsets:empty'
namespace :integration do namespace :integration do
desc "Remove any cruft generated by manual debugging runs which is .gitignore'd." desc "Remove any cruft generated by manual debugging runs which is .gitignore'd."
...@@ -120,19 +122,19 @@ namespace :integration do ...@@ -120,19 +122,19 @@ namespace :integration do
end end
desc "Reset any changed files, and remove any untracked files in spec/integration/*/, plus run integration:clean." desc "Reset any changed files, and remove any untracked files in spec/integration/*/, plus run integration:clean."
task :clobber => [:integration_environment, :'integration:clean'] do task clobber: [:integration_environment, :'integration:clean'] do
Annotate::Integration.reset_dirty_files Annotate::Integration.reset_dirty_files
Annotate::Integration.clear_untracked_files Annotate::Integration.clear_untracked_files
end end
task :symlink => [:integration_environment] do task symlink: [:integration_environment] do
require 'digest/md5' require 'digest/md5'
integration_dir = File.expand_path(File.join(File.dirname(__FILE__), 'spec', 'integration')) integration_dir = File.expand_path(File.join(File.dirname(__FILE__), 'spec', 'integration'))
fixture_dir = File.expand_path(File.join(File.dirname(__FILE__), 'spec', 'fixtures')) # fixture_dir = File.expand_path(File.join(File.dirname(__FILE__), 'spec', 'fixtures'))
target_dir = File.expand_path(ENV['TARGET']) if(ENV['TARGET']) target_dir = File.expand_path(ENV['TARGET']) if(ENV['TARGET'])
raise "Must specify TARGET=x, where x is an integration test scenario!" unless(target_dir && Dir.exist?(target_dir)) raise "Must specify TARGET=x, where x is an integration test scenario!" unless target_dir && Dir.exist?(target_dir)
raise "TARGET directory must be within spec/integration/!" unless(target_dir.start_with?(integration_dir)) raise "TARGET directory must be within spec/integration/!" unless target_dir.start_with?(integration_dir)
candidates = {} candidates = {}
FileList[ FileList[
"#{target_dir}/.rvmrc", "#{target_dir}/.rvmrc",
...@@ -156,7 +158,7 @@ namespace :integration do ...@@ -156,7 +158,7 @@ namespace :integration do
candidates[digest] << fname candidates[digest] << fname
end end
fixtures = {} fixtures = {}
FileList["spec/fixtures/**/*"].each do |fname| FileList['spec/fixtures/**/*'].each do |fname|
fixtures[Digest::MD5.hexdigest(File.read(fname))] = File.expand_path(fname) fixtures[Digest::MD5.hexdigest(File.read(fname))] = File.expand_path(fname)
end end
...@@ -190,7 +192,7 @@ namespace :yard do ...@@ -190,7 +192,7 @@ namespace :yard do
FileUtils.rm_f("doc") FileUtils.rm_f("doc")
end end
end end
task :clobber => :'yard:clobber' task clobber: :'yard:clobber'
namespace :rubinius do namespace :rubinius do
task :clobber do task :clobber do
...@@ -198,7 +200,7 @@ namespace :rubinius do ...@@ -198,7 +200,7 @@ namespace :rubinius do
FileList[".rbx/**/*"].each { |fname| FileUtils.rm_f(fname) } FileList[".rbx/**/*"].each { |fname| FileUtils.rm_f(fname) }
end end
end end
task :clobber => :'rubinius:clobber' task clobber: :'rubinius:clobber'
# want other tests/tasks run by default? Add them to the list # want other tests/tasks run by default? Add them to the list
task :default => [:spec] task default: [:spec]
#!/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!
......
...@@ -158,22 +158,23 @@ module Annotate ...@@ -158,22 +158,23 @@ module Annotate
def self.bootstrap_rake def self.bootstrap_rake
begin begin
require 'rake/dsl_definition' require 'rake/dsl_definition'
rescue Exception rescue Exception => e
# We might just be on an old version of Rake... # We might just be on an old version of Rake...
puts e.message
exit e.status_code
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)) unless 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'
require 'active_support/core_ext/class/subclasses' require 'active_support/core_ext/class/subclasses'
require 'active_support/core_ext/string/inflections' require 'active_support/core_ext/string/inflections'
end end
self.load_tasks self.load_tasks
Rake::Task[:set_annotation_options].invoke Rake::Task[:set_annotation_options].invoke
end end
......
...@@ -375,12 +375,14 @@ module AnnotateModels ...@@ -375,12 +375,14 @@ module AnnotateModels
old_content.sub!(magic_comment_matcher, '') old_content.sub!(magic_comment_matcher, '')
old_content.sub!(PATTERN, '') old_content.sub!(PATTERN, '')
new_content = %w(after bottom).include?(options[position].to_s) ? if %w(after bottom).include?(options[position].to_s)
(magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block)) : new_content = magic_comments.join + (old_content.rstrip + "\n\n" + wrapped_info_block)
(magic_comments.join + wrapped_info_block + "\n" + old_content) else
new_content = magic_comments.join + wrapped_info_block + "\n" + old_content
end
end end
File.open(file_name, "wb") { |f| f.puts new_content } File.open(file_name, 'wb') { |f| f.puts new_content }
return true return true
end end
else else
...@@ -391,14 +393,13 @@ module AnnotateModels ...@@ -391,14 +393,13 @@ module AnnotateModels
def remove_annotation_of_file(file_name) def remove_annotation_of_file(file_name)
if File.exist?(file_name) if File.exist?(file_name)
content = File.read(file_name) content = File.read(file_name)
content.sub!(PATTERN, '') content.sub!(PATTERN, '')
File.open(file_name, "wb") { |f| f.puts content } File.open(file_name, 'wb') { |f| f.puts content }
return true true
else else
return false false
end end
end end
...@@ -628,12 +629,12 @@ module AnnotateModels ...@@ -628,12 +629,12 @@ module AnnotateModels
associations = [] associations = []
id = nil id = nil
cols = cols.each do |c| cols.each do |c|
if c.name.eql?("id") if c.name.eql?('id')
id = c id = c
elsif (c.name.eql?("created_at") || c.name.eql?("updated_at")) elsif (c.name.eql?('created_at') || c.name.eql?('updated_at'))
timestamps << c timestamps << c
elsif c.name[-3,3].eql?("_id") elsif c.name[-3,3].eql?('_id')
associations << c associations << c
else else
rest_cols << c rest_cols << c
......
...@@ -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
......
...@@ -57,10 +57,10 @@ module Annotate ...@@ -57,10 +57,10 @@ module Annotate
def self.apply_annotation(test_rig, fname, annotation, place_before=true) def self.apply_annotation(test_rig, fname, annotation, place_before=true)
corpus = File.read(File.join(test_rig, fname)) corpus = File.read(File.join(test_rig, fname))
if(place_before) if place_before
corpus = annotation + "\n" + corpus annotation + "\n" + corpus
else else
corpus = corpus + "\n" + annotation corpus + "\n" + annotation
end end
end end
end end
......
...@@ -16,25 +16,23 @@ ENV['rvm_pretty_print_flag'] = '0' ...@@ -16,25 +16,23 @@ ENV['rvm_pretty_print_flag'] = '0'
ENV['BUNDLE_GEMFILE'] = './Gemfile' ENV['BUNDLE_GEMFILE'] = './Gemfile'
describe "annotate inside Rails, using #{CURRENT_RUBY}" do describe "annotate inside Rails, using #{CURRENT_RUBY}" do
here = File.expand_path('..', __FILE__)
chosen_scenario = nil chosen_scenario = nil
if(!ENV['SCENARIO'].blank?) if !ENV['SCENARIO'].blank?
chosen_scenario = File.expand_path(ENV['SCENARIO']) chosen_scenario = File.expand_path(ENV['SCENARIO'])
raise "Can't find specified scenario '#{chosen_scenario}'!" unless(File.directory?(chosen_scenario)) raise "Can't find specified scenario '#{chosen_scenario}'!" unless(File.directory?(chosen_scenario))
end end
Annotate::Integration::SCENARIOS.each do |test_rig, base_dir, test_name| Annotate::Integration::SCENARIOS.each do |test_rig, base_dir, test_name|
next if(chosen_scenario && chosen_scenario != test_rig) next if(chosen_scenario && chosen_scenario != test_rig)
it "works under #{test_name}" do it "works under #{test_name}" do
if(!USING_RVM) if !USING_RVM
skip "Must have RVM installed." skip 'Must have RVM installed.'
next next
end end
# Don't proceed if the working copy is dirty! # Don't proceed if the working copy is dirty!
expect(Annotate::Integration.is_clean?(test_rig)).to eq(true) expect(Annotate::Integration.is_clean?(test_rig)).to eq(true)
skip "temporarily ignored until Travis can run them" skip 'temporarily ignored until Travis can run them'
Bundler.with_clean_env do Bundler.with_clean_env do
dir base_dir do dir base_dir do
......
require 'coveralls'
require 'codeclimate-test-reporter'
require 'simplecov'
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
Coveralls::SimpleCov::Formatter,
SimpleCov::Formatter::HTMLFormatter,
CodeClimate::TestReporter::Formatter
]
SimpleCov.start
require 'rubygems' require 'rubygems'
require 'bundler' require 'bundler'
Bundler.setup Bundler.setup
...@@ -18,10 +30,7 @@ module Annotate ...@@ -18,10 +30,7 @@ module Annotate
module Integration module Integration
ABSOLUTE_GEM_ROOT=File.expand_path('../../', __FILE__) ABSOLUTE_GEM_ROOT=File.expand_path('../../', __FILE__)
CRUFT_PATTERNS=[ CRUFT_PATTERNS= %w(%SCENARIO%/bin/* %SCENARIO%/log/* %SCENARIO%/tmp/* %SCENARIO%/.bundle)
"%SCENARIO%/bin/*", "%SCENARIO%/log/*", "%SCENARIO%/tmp/*",
"%SCENARIO%/.bundle"
]
SCENARIO_HOME=File.join(File.dirname(__FILE__), 'integration') SCENARIO_HOME=File.join(File.dirname(__FILE__), 'integration')
SCENARIOS=Dir.glob("#{SCENARIO_HOME}/*"). SCENARIOS=Dir.glob("#{SCENARIO_HOME}/*").
select { |candidate| File.directory?(candidate) }. select { |candidate| File.directory?(candidate) }.
......
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