1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Improves spec runner harness, switches to better env detection.

Does not run rails specs when running mongoid specs

WIP Start of remove BRE to check tests

Remove old helper of active record

Use RSpec skip to skip AR specs

Run ActionView only when needed

Run ActiveSupport only when needed

Run Mongoid only when needed

Create ExtVerifier.require_dependencies

Run MongoMapper only when needed

Always load nokogiri specs

Always load ostruct specs

Run Ripple only when needed

Remove :: when check for defined constants

Require spec_helper directly

Remove 1.8.6 old monkey patch

Add some RSpec configs
This commit is contained in:
Mauro George 2014-12-31 18:59:32 -02:00 committed by James Cox
parent 69815f01b2
commit 526f907629
24 changed files with 490 additions and 526 deletions

View file

@ -15,17 +15,14 @@ appraise 'rails-4.2' do
end
appraise 'mongoid-3.0' do
gem 'rails', '~> 3.2.0'
gem 'mongoid', '~> 3.0.0'
end
appraise 'mongoid-3.1' do
gem 'rails', '~> 3.2.0'
gem 'mongoid', '~> 3.1.0'
end
appraise 'mongoid-4.0' do
gem 'rails', '~> 4.2.0'
gem 'mongoid', '~> 4.0.0'
end

View file

@ -2,7 +2,6 @@
source "https://rubygems.org"
gem "rails", "~> 3.2.0"
gem "mongoid", "~> 3.0.0"
gemspec :path => "../"

View file

@ -2,7 +2,6 @@
source "https://rubygems.org"
gem "rails", "~> 3.2.0"
gem "mongoid", "~> 3.1.0"
gemspec :path => "../"

View file

@ -2,7 +2,6 @@
source "https://rubygems.org"
gem "rails", "~> 4.2.0"
gem "mongoid", "~> 4.0.0"
gemspec :path => "../"

View file

@ -1,34 +1,24 @@
require 'active_record'
# Required to use the column support
module Rails
if ExtVerifier.has_rails?
# Required to use the column support
module Rails
def self.env
{}
end
end
end
# Establish connection to in-memory SQLite DB
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
# Establish connection to in-memory SQLite DB
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
# Create the users table
ActiveRecord::Migration.verbose = false
ActiveRecord::Migration.create_table :users do |t|
# Create the users table
ActiveRecord::Migration.verbose = false
ActiveRecord::Migration.create_table :users do |t|
t.string :name
t.integer :rank
t.boolean :admin
t.datetime :created_at
end
# Create models
class User < ActiveRecord::Base; end
class SubUser < User; end
end
# Create models
class User < ActiveRecord::Base; end
class SubUser < User; end
# Helper methods
# ##############
# we only work with ActiveRecord 2+
def is_usable_activerecord?
defined?(ActiveRecord::VERSION::MAJOR) && ActiveRecord::VERSION::MAJOR >= 2
end

View file

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'spec_helper'
describe "AwesomePrint" do
RSpec.describe "AwesomePrint" do
def stub_tty!(output = true, stream = STDOUT)
if output
stream.instance_eval { def tty?; true; end }

View file

@ -1,10 +1,10 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
require 'logger'
require 'awesome_print/core_ext/logger'
describe "AwesomePrint logging extensions" do
RSpec.describe "AwesomePrint logging extensions" do
before(:all) do
@logger = Logger.new('/dev/null') rescue Logger.new('nul')
end

View file

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
describe "String extensions" do
RSpec.describe "String extensions" do
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
it "should have #{color} color" do
expect(color.to_s.send(color)).to eq("\e[1;#{30+i}m#{color}\e[0m")

View file

@ -1,10 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
begin
require 'action_view'
require 'awesome_print/ext/action_view'
RSpec.describe "AwesomePrint ActionView extensions", skip: ->{ !ExtVerifier.has_rails? }.call do
describe "AwesomePrint ActionView extensions" do
before do
@view = ActionView::Base.new
end
@ -18,8 +15,4 @@ begin
markup = ' &<hello>'
expect(@view.ap(markup)).to eq('<pre class="debug_dump"><kbd style="color:brown">&quot; &amp;&lt;hello&gt;&quot;</kbd></pre>')
end
end
rescue LoadError => error
puts "Skipping ActionView specs: #{error}"
end

View file

@ -1,12 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
require 'active_record_helper'
begin
require 'awesome_print/ext/active_record'
require File.expand_path(File.dirname(__FILE__) + '/../active_record_helper')
RSpec.describe "AwesomePrint/ActiveRecord", skip: ->{ !ExtVerifier.has_rails? }.call do
if is_usable_activerecord?
describe "AwesomePrint/ActiveRecord" do
before do
stub_dotfile!
end
@ -30,7 +26,7 @@ begin
:name => "Diana",
:rank => 1
}
EOS
EOS
if RUBY_VERSION < '1.9'
str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
else
@ -58,7 +54,7 @@ EOS
:rank => 2
}
]
EOS
EOS
if RUBY_VERSION < '1.9'
str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
@ -207,7 +203,7 @@ EOS
"rank" => nil
}
>
EOS
EOS
elsif activerecord_4_1?
str = <<-EOS.strip
#<User:0x01234567
@ -307,7 +303,7 @@ EOS
"rank" => nil
}
>
EOS
EOS
elsif activerecord_4_0?
str = <<-EOS.strip
#<User:0x01234567
@ -408,7 +404,7 @@ EOS
"rank" => nil
}
>
EOS
EOS
elsif activerecord_3_2?
str = <<-EOS.strip
#<User:0x01234567
@ -435,7 +431,7 @@ EOS
"rank" => nil
}
>
EOS
EOS
end
str.sub!('?', '1992-10-10 12:30:00')
expect(out.gsub(/0x([a-f\d]+)/, "0x01234567")).to eq(str)
@ -694,7 +690,7 @@ EOS
}
>
]
EOS
EOS
elsif activerecord_4_1?
str = <<-EOS.strip
[
@ -893,7 +889,7 @@ EOS
}
>
]
EOS
EOS
elsif activerecord_4_0?
str = <<-EOS.strip
[
@ -1094,7 +1090,7 @@ EOS
}
>
]
EOS
EOS
elsif activerecord_3_2?
str = <<-EOS.strip
[
@ -1147,7 +1143,7 @@ EOS
}
>
]
EOS
EOS
end
str.sub!('?', '1992-10-10 12:30:00')
str.sub!('?', '2003-05-26 14:15:00')
@ -1170,7 +1166,7 @@ class User < ActiveRecord::Base {
:admin => :boolean,
:created_at => :datetime
}
EOS
EOS
end
it "should print the class for non-direct subclasses of ActiveRecord::Base" do
@ -1183,7 +1179,7 @@ class SubUser < User {
:admin => :boolean,
:created_at => :datetime
}
EOS
EOS
end
it "should print ActiveRecord::Base objects (ex. ancestors)" do
@ -1226,8 +1222,4 @@ EOS
end
end
end
end
rescue LoadError => error
puts "Skipping ActiveRecord specs: #{error}"
end

View file

@ -1,10 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
begin
require 'active_support/all'
require 'awesome_print/ext/active_support'
describe "AwesomePrint::ActiveSupport" do
RSpec.describe "AwesomePrint::ActiveSupport", skip: ->{ !ExtVerifier.has_rails? }.call do
before do
stub_dotfile!
@ap = AwesomePrint::Inspector.new
@ -28,8 +24,4 @@ begin
expect(date.ai(:plain => true)).to eq("Mon, 26 May 2003")
expect(date.ai).to eq("\e[0;32mMon, 26 May 2003\e[0m")
end
end
rescue LoadError => error
puts "Skipping ActiveSupport specs: #{error}"
end

View file

@ -1,10 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
begin
require "mongo_mapper"
require "awesome_print/ext/mongo_mapper"
RSpec.describe "AwesomePrint/MongoMapper", skip: ->{ !ExtVerifier.has_mongo_mapper? }.call do
describe "AwesomePrint/MongoMapper" do
if ExtVerifier.has_mongo_mapper?
before :all do
class MongoUser
include MongoMapper::Document
@ -18,6 +16,7 @@ begin
Object.instance_eval{ remove_const :MongoUser }
Object.instance_eval{ remove_const :Chamelion }
end
end
before do
stub_dotfile!
@ -111,7 +110,7 @@ begin
"last_name" => nil
}
>
EOS
EOS
else
str = <<-EOS.strip
#<MongoUser:0x01234567
@ -125,7 +124,7 @@ EOS
attr_reader :first_name_before_type_cast = "Al",
attr_reader :last_name_before_type_cast = "Capone"
>
EOS
EOS
end
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
@ -138,7 +137,7 @@ class MongoUser < Object {
"first_name" => :string,
"last_name" => :string
}
EOS
EOS
end
it "should print the class when type is undefined" do
@ -152,11 +151,13 @@ class Chamelion < Object {
"_id" => :object_id,
"last_attribute" => :undefined
}
EOS
EOS
end
end
describe "with associations" do
if ExtVerifier.has_mongo_mapper?
before :all do
class Child
include MongoMapper::EmbeddedDocument
@ -176,6 +177,7 @@ EOS
one :sibling
end
end
end
describe "with show associations turned off (default)" do
it "should render the class as normal" do
@ -184,7 +186,7 @@ class Parent < Object {
"_id" => :object_id,
"name" => :undefined
}
EOS
EOS
end
it "should render an instance as normal" do
@ -195,7 +197,7 @@ EOS
"_id" => BSON::ObjectId('4d9183739a546f6806000001'),
"name" => "test"
}
EOS
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
@ -215,7 +217,7 @@ class Parent < Object {
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
EOS
end
it "should render an instance with associations shown" do
@ -228,7 +230,7 @@ EOS
"child" => embeds one Child,
"sibling" => one Sibling
}
EOS
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
@ -258,15 +260,11 @@ EOS
},
"sibling" => one Sibling
}
EOS
EOS
out.gsub!(/'([\w]+){23}'/, "'4d9183739a546f6806000001'")
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
end
end
end
end
rescue LoadError => error
puts "Skipping MongoMapper specs: #{error}"
end

View file

@ -1,10 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
begin
require "mongoid"
require "awesome_print/ext/mongoid"
RSpec.describe "AwesomePrint/Mongoid", skip: ->{ !ExtVerifier.has_mongoid? }.call do
describe "AwesomePrint/Mongoid" do
if ExtVerifier.has_mongoid?
before :all do
class MongoUser
include Mongoid::Document
@ -18,6 +16,7 @@ begin
Object.instance_eval{ remove_const :MongoUser }
Object.instance_eval{ remove_const :Chamelion }
end
end
before do
stub_dotfile!
@ -35,7 +34,7 @@ begin
:first_name => "Al",
:last_name => "Capone"
}
EOS
EOS
out.gsub!(/0x([a-f\d]+)/, "0x01234567")
expect(out).to eq(str)
end
@ -84,28 +83,24 @@ class Chamelion < Object {
:_type => :string,
:last_attribute => :object
}
EOS
EOS
elsif mongoid_3_1?
<<-EOS.strip
class Chamelion < Object {
:_id => :"moped/bson/object_id",
:last_attribute => :object
}
EOS
EOS
elsif mongoid_4_0?
<<-EOS.strip
class Chamelion < Object {
:_id => :"bson/object_id",
:last_attribute => :object
}
EOS
EOS
end
expect(@ap.send(:awesome, Chamelion)).to eq class_spec
end
end
rescue LoadError => error
puts "Skipping Mongoid specs: #{error}"
end

View file

@ -1,10 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
begin
require "nokogiri"
require "awesome_print/ext/nokogiri"
describe "AwesomePrint/Nokogiri" do
RSpec.describe "AwesomePrint/Nokogiri" do
before do
stub_dotfile!
end
@ -18,7 +14,7 @@ begin
\e[0m<\e[1;36mh1\e[0m/>\e[1;32m
\e[0m<\e[1;36m/body\e[0m>\e[1;32m
\e[0m<\e[1;36m/html\e[0m>
EOS
EOS
end
it "should colorize contents" do
@ -30,7 +26,7 @@ EOS
\e[0m<\e[1;36mh1\e[0m>\e[1;32mHello\e[0m<\e[1;36m/h1\e[0m>\e[1;32m
\e[0m<\e[1;36m/body\e[0m>\e[1;32m
\e[0m<\e[1;36m/html\e[0m>
EOS
EOS
end
it "should colorize class and id" do
@ -44,10 +40,6 @@ EOS
\e[0m<\e[1;36m/h1\e[0m>\e[1;32m
\e[0m<\e[1;36m/body\e[0m>\e[1;32m
\e[0m<\e[1;36m/html\e[0m>
EOS
EOS
end
end
rescue LoadError => error
puts "Skipping Nokogiri specs: #{error}"
end

View file

@ -1,10 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
begin
require 'ostruct'
require 'awesome_print/ext/ostruct'
describe 'AwesomePrint Ostruct extension' do
RSpec.describe 'AwesomePrint Ostruct extension' do
before do
stub_dotfile!
@ap = AwesomePrint::Inspector.new(:plain => true, :sort_keys => true)
@ -22,10 +18,6 @@ OpenStruct {
:address => "Bar",
:name => "Foo"
}
EOS
EOS
end
end
rescue LoadError => error
puts "Skipping OpenStruct specs: #{error}"
end

View file

@ -1,10 +1,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'spec_helper'
begin
require 'ripple'
require 'awesome_print/ext/ripple'
RSpec.describe 'AwesomePrint/Ripple', skip: ->{ !ExtVerifier.has_ripple? }.call do
describe 'AwesomePrint/Ripple' do
if ExtVerifier.has_ripple?
before :all do
class RippleUser
include Ripple::Document
@ -19,6 +17,7 @@ begin
after :all do
Object.instance_eval { remove_const :RippleUser }
end
end
before do
stub_dotfile!
@ -35,7 +34,7 @@ begin
:first_name => "Al",
:last_name => "Capone"
}
EOS
EOS
end
it "should print the class" do
@ -45,10 +44,6 @@ class RippleUser < Object {
:first_name => :string,
:last_name => :string
}
EOS
EOS
end
end
rescue LoadError => error
puts "Skipping Ripple specs: #{error}"
end

View file

@ -1,9 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'spec_helper'
require "bigdecimal"
require "rational"
require "set"
describe "AwesomePrint" do
RSpec.describe "AwesomePrint" do
before do
stub_dotfile!
end

View file

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'spec_helper'
describe "Single method" do
RSpec.describe "Single method" do
before do
stub_dotfile!
end
@ -80,7 +80,7 @@ describe "Single method" do
end
end
describe "Object methods" do
RSpec.describe "Object methods" do
before do
stub_dotfile!
end
@ -202,7 +202,7 @@ describe "Object methods" do
end
end
describe "Class methods" do
RSpec.describe "Class methods" do
before do
stub_dotfile!
end
@ -313,7 +313,7 @@ describe "Class methods" do
end
if RUBY_VERSION >= '1.9.2'
describe "Ruby 1.9.2+ Method#parameters" do
RSpec.describe "Ruby 1.9.2+ Method#parameters" do
before do
stub_dotfile!
end
@ -364,7 +364,7 @@ if RUBY_VERSION >= '1.9.2'
end
end
describe "Methods arrays" do
RSpec.describe "Methods arrays" do
after do
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
Object.instance_eval{ remove_const :World } if defined?(World)

View file

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'spec_helper'
describe "AwesomePrint" do
RSpec.describe "AwesomePrint" do
describe "Misc" do
before do

View file

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'spec_helper'
describe "Objects" do
RSpec.describe "Objects" do
before do
stub_dotfile!
end

View file

@ -16,12 +16,35 @@
#
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'awesome_print'
Dir[File.dirname(__FILE__) + '/support/**/*.rb'].each do |file|
require file
end
ExtVerifier.require_dependencies!(%w{rails active_record action_view
active_support/all mongoid mongo_mapper ripple})
require 'nokogiri'
require 'awesome_print'
RSpec.configure do |config|
config.disable_monkey_patching!
# TODO: Make specs not order dependent
# config.order = :random
Kernel.srand config.seed
config.filter_run focus: true
config.run_all_when_everything_filtered = true
config.expect_with :rspec do |expectations|
expectations.syntax = :expect
end
config.mock_with :rspec do |mocks|
mocks.syntax = :expect
mocks.verify_partial_doubles = true
end
if config.files_to_run.one?
config.default_formatter = 'doc'
end
end
def stub_dotfile!
dotfile = File.join(ENV["HOME"], ".aprc")
expect(File).to receive(:readable?).at_least(:once).with(dotfile).and_return(false)
@ -33,36 +56,3 @@ def capture!
ensure
$stdout = standard
end
# The following is needed for the Infinity Test. It runs tests as subprocesses,
# which sets STDOUT.tty? to false and would otherwise prematurely disallow colors.
### AwesomePrint.force_colors!
# Ruby 1.8.6 only: define missing String methods that are needed for the specs to pass.
if RUBY_VERSION < '1.8.7'
class String
def shellescape # Taken from Ruby 1.9.2 standard library, see lib/shellwords.rb.
return "''" if self.empty?
str = self.dup
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
str.gsub!(/\n/, "'\n'")
str
end
def start_with?(*prefixes)
prefixes.each do |prefix|
prefix = prefix.to_s
return true if prefix == self[0, prefix.size]
end
false
end
def end_with?(*suffixes)
suffixes.each do |suffix|
suffix = suffix.to_s
return true if suffix == self[-suffix.size, suffix.size]
end
false
end
end
end

View file

@ -1,26 +0,0 @@
module ActiveRecordVersions
def activerecord_version
Gem::Version.new(ActiveRecord::VERSION::STRING)
end
def activerecord_4_2?
Gem::Requirement.new('~> 4.2.0').satisfied_by?(activerecord_version)
end
def activerecord_4_1?
Gem::Requirement.new('~> 4.1.0').satisfied_by?(activerecord_version)
end
def activerecord_4_0?
Gem::Requirement.new('~> 4.0.0').satisfied_by?(activerecord_version)
end
def activerecord_3_2?
Gem::Requirement.new('~> 3.2.0').satisfied_by?(activerecord_version)
end
end
RSpec.configure do |config|
config.include(ActiveRecordVersions)
config.extend(ActiveRecordVersions)
end

View file

@ -0,0 +1,37 @@
module ExtVerifier
def require_dependencies!(dependencies)
dependencies.each do |dependency|
begin
require dependency
rescue LoadError
end
end
end
module_function :require_dependencies!
def has_rails?
defined?(Rails)
end
module_function :has_rails?
def has_mongoid?
defined?(Mongoid)
end
module_function :has_mongoid?
def has_mongo_mapper?
defined?(MongoMapper)
end
module_function :has_mongo_mapper?
def has_ripple?
defined?(Ripple)
end
module_function :has_ripple?
end
RSpec.configure do |config|
config.include(ExtVerifier)
config.extend(ExtVerifier)
end

View file

@ -0,0 +1,30 @@
module RailsVersions
def rails_version
Gem::Version.new(Rails::VERSION::STRING)
end
def rails_4_2?
Gem::Requirement.new('~> 4.2.0').satisfied_by?(rails_version)
end
alias_method :activerecord_4_2?, :rails_4_2?
def rails_4_1?
Gem::Requirement.new('~> 4.1.0').satisfied_by?(rails_version)
end
alias_method :activerecord_4_1?, :rails_4_1?
def rails_4_0?
Gem::Requirement.new('~> 4.0.0').satisfied_by?(rails_version)
end
alias_method :activerecord_4_0?, :rails_4_0?
def rails_3_2?
Gem::Requirement.new('~> 3.2.0').satisfied_by?(rails_version)
end
alias_method :activerecord_3_2?, :rails_3_2?
end
RSpec.configure do |config|
config.include(RailsVersions)
config.extend(RailsVersions)
end