Skip to content

Commit

Permalink
Fix #38 Make backward compatible with Rails 4.2.
Browse files Browse the repository at this point in the history
Increase test coverage.
  • Loading branch information
reidmorrison committed May 10, 2017
1 parent 6d25a89 commit ca16392
Show file tree
Hide file tree
Showing 38 changed files with 391 additions and 218 deletions.
15 changes: 10 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
.DS_Store
.bundle/
*.log
pkg/
test/dummy/tmp/
test/dummy/.sass-cache
*.gem
.idea/*
/.idea
tags
*.DS_Store
Gemfile.lock
._test
*.log
/gemfiles/*.lock

*.lock
*.sqlite3
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ source 'https://rubygems.org'
gemspec

gem 'rake'
gem 'minitest'
gem 'minitest', '~> 5.10', '!= 5.10.2'
gem 'minitest-rails'
gem 'minitest-reporters'
gem 'minitest-stub_any_instance'
gem 'awesome_print'
Expand All @@ -14,4 +15,5 @@ gem 'jdbc-sqlite3', platform: :jruby
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
gem 'appraisal'

gem 'rails', '~> 5.1.0'
gem 'rails', '~> 4.2.0'
gem 'puma'
26 changes: 26 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Installation

Install all needed gems to run the tests:

appraisal install

The gems are installed into the global gem list.
The Gemfiles in the `gemfiles` folder are also re-generated.

## Run Tests

For all supported Rails/ActiveRecord versions:

rake

Or for specific rails version:

appraisal rails_4.2 rake

Or for one particular test file:

appraisal rails_5.0 ruby test/controllers/articles_controller_test.rb

Or down to one test case:

appraisal rails_5.0 ruby test/controllers/articles_controller_test.rb -n "/shows new article/"
4 changes: 3 additions & 1 deletion gemfiles/rails_4.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
source "https://rubygems.org"

gem "rake"
gem "minitest"
gem "minitest", "~> 5.10", "!= 5.10.2"
gem "minitest-rails"
gem "minitest-reporters"
gem "minitest-stub_any_instance"
gem "awesome_print"
Expand All @@ -13,5 +14,6 @@ gem "jdbc-sqlite3", platform: :jruby
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "appraisal"
gem "rails", "~> 4.2.0"
gem "puma"

gemspec path: "../"
4 changes: 3 additions & 1 deletion gemfiles/rails_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
source "https://rubygems.org"

gem "rake"
gem "minitest"
gem "minitest", "~> 5.10", "!= 5.10.2"
gem "minitest-rails"
gem "minitest-reporters"
gem "minitest-stub_any_instance"
gem "awesome_print"
Expand All @@ -13,5 +14,6 @@ gem "jdbc-sqlite3", platform: :jruby
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "appraisal"
gem "rails", "~> 5.0"
gem "puma"

gemspec path: "../"
4 changes: 3 additions & 1 deletion gemfiles/rails_5.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
source "https://rubygems.org"

gem "rake"
gem "minitest"
gem "minitest", "~> 5.10", "!= 5.10.2"
gem "minitest-rails"
gem "minitest-reporters"
gem "minitest-stub_any_instance"
gem "awesome_print"
Expand All @@ -13,5 +14,6 @@ gem "jdbc-sqlite3", platform: :jruby
gem "activerecord-jdbcsqlite3-adapter", platform: :jruby
gem "appraisal"
gem "rails", "~> 5.1.0"
gem "puma"

gemspec path: "../"
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ def start_processing(event)

def process_action(event)
controller_logger(event).info do
payload = event.payload.dup
payload[:params] = payload[:params].to_unsafe_h.except(*INTERNAL_PARAMS)
payload = event.payload.dup
# According to PR https://github.com/rocketjob/rails_semantic_logger/pull/37/files
# payload[:params] is not always a Hash.
payload[:params] = payload[:params].to_unsafe_h unless payload[:params].is_a?(Hash)
payload[:params].except!(*INTERNAL_PARAMS)
payload.delete(:params) if payload[:params].empty?

format = payload[:format]
Expand Down Expand Up @@ -43,19 +46,19 @@ def process_action(event)
end

def halted_callback(event)
controller_logger(event).info { "Filter chain halted as #{event.payload[:filter].inspect} rendered or redirected" }
controller_logger(event).info {"Filter chain halted as #{event.payload[:filter].inspect} rendered or redirected"}
end

def send_file(event)
controller_logger(event).info('Sent file') { {path: event.payload[:path], duration: event.duration} }
controller_logger(event).info('Sent file') {{path: event.payload[:path], duration: event.duration}}
end

def redirect_to(event)
controller_logger(event).info('Redirected to') { {location: event.payload[:location]} }
controller_logger(event).info('Redirected to') {{location: event.payload[:location]}}
end

def send_data(event)
controller_logger(event).info('Sent data') { {file_name: event.payload[:filename], duration: event.duration} }
controller_logger(event).info('Sent data') {{file_name: event.payload[:filename], duration: event.duration}}
end

def unpermitted_parameters(event)
Expand All @@ -69,7 +72,8 @@ def unpermitted_parameters(event)
expire_fragment expire_page write_page).each do |method|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{method}(event)
return unless ActionController::Base.enable_fragment_cache_logging
# enable_fragment_cache_logging as of Rails 5
return if ActionController::Base.respond_to?(:enable_fragment_cache_logging) && !ActionController::Base.enable_fragment_cache_logging
controller_logger(event).info do
key_or_path = event.payload[:key] || event.payload[:path]
{message: "#{method.to_s.humanize} \#{key_or_path}", duration: event.duration}
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_semantic_logger/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RailsSemanticLogger #:nodoc
VERSION = '4.1.0'
VERSION = '4.1.1'
end
75 changes: 75 additions & 0 deletions test/controllers/articles_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require_relative "../test_helper"

class ArticlesControllerTest < ActionController::TestCase
describe ArticlesController do
before do
# Use a mock logger that just keeps the last logged entry in an instance variable
SemanticLogger.default_level = :trace
SemanticLogger.backtrace_level = nil
@mock_logger = MockLogger.new
@appender = SemanticLogger.add_appender(logger: @mock_logger, formatter: :raw)
@logger = SemanticLogger['Test']
@hash = {session_id: 'HSSKLEU@JDK767', tracking_number: 12345}

assert_equal [], SemanticLogger.tags
assert_equal 65535, SemanticLogger.backtrace_level_index
end

after do
SemanticLogger.remove_appender(@appender)
end

describe 'GET #new' do
it 'shows new article' do
get :new
assert_response :success
end
end

describe 'POST #create' do
describe 'with valid parameters' do
let(:params) do
{
article: {
text: 'Text1',
title: 'Title1'
}
}
end

before do
params = {params: self.params} if Rails.version.to_i >= 5
post :create, params
end

it 'has no errors' do
assert_response :success
end

it 'successfully logs message' do
SemanticLogger.flush
actual = @mock_logger.message

assert_equal 'Completed #create', actual[:message]
assert_equal 'ArticlesController', actual[:name]

assert payload = actual[:payload], actual
assert_equal 'create', payload[:action], payload
assert_equal 'ArticlesController', payload[:controller], payload
assert_equal 'HTML', payload[:format], payload
assert_equal 'POST', payload[:method], payload
# Only Rails 5 passes the arguments through
if Rails.version.to_i >= 5
assert_equal({"article" => {"text" => "Text1", "title" => "Title1"}}, payload[:params], payload)
end
assert_equal '/articles', payload[:path], payload
assert_equal 200, payload[:status], payload
assert_equal 'OK', payload[:status_message], payload
assert (payload[:view_runtime] >= 0.0), payload
assert (payload[:db_runtime] >= 0.0), payload
end

end
end
end
end
13 changes: 13 additions & 0 deletions test/controllers/welcome_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative '../test_helper'

class WelcomeControllerTest < ActionController::TestCase
describe WelcomeController do
before do
get :index
end

it 'should get index' do
assert_response :success
end
end
end
6 changes: 3 additions & 3 deletions test/dummy/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
//# require jquery
//# require jquery_ujs
//# require_tree .
2 changes: 2 additions & 0 deletions test/dummy/app/assets/javascripts/articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
4 changes: 4 additions & 0 deletions test/dummy/app/assets/stylesheets/articles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
4 changes: 4 additions & 0 deletions test/dummy/app/assets/stylesheets/welcome.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/
2 changes: 1 addition & 1 deletion test/dummy/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class ApplicationController < ActionController::Base
protect_from_forgery
protect_from_forgery with: :exception
end
8 changes: 8 additions & 0 deletions test/dummy/app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ArticlesController < ApplicationController
def new
end

def create
render plain: params[:article].inspect
end
end
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class WelcomeController < ApplicationController
def index
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/helpers/articles_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ArticlesHelper
end
2 changes: 2 additions & 0 deletions test/dummy/app/helpers/welcome_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module WelcomeHelper
end
17 changes: 17 additions & 0 deletions test/dummy/app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>New Article</h1>

<%= form_for :article, url: articles_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>

<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>

<p>
<%= f.submit %>
</p>
<% end %>
2 changes: 2 additions & 0 deletions test/dummy/app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>
3 changes: 3 additions & 0 deletions test/dummy/bin/bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
load Gem.bin_path('bundler', 'bundle')
15 changes: 15 additions & 0 deletions test/dummy/bin/puma
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'puma' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('puma', 'puma')
4 changes: 4 additions & 0 deletions test/dummy/bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
4 changes: 4 additions & 0 deletions test/dummy/bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require_relative '../config/boot'
require 'rake'
Rake.application.run
26 changes: 26 additions & 0 deletions test/dummy/bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby
require 'pathname'

# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)

Dir.chdir APP_ROOT do
# This script is a starting point to setup your application.
# Add necessary setup steps to this file:

puts "== Installing dependencies =="
system "gem install bundler --conservative"
system "bundle check || bundle install"

# puts "\n== Copying sample files =="
# unless File.exist?("config/database.yml")
# system "cp config/database.yml.sample config/database.yml"
# end

puts "\n== Removing old logs and tempfiles =="
system "rm -f log/*"
system "rm -rf tmp/cache"

puts "\n== Restarting application server =="
system "touch tmp/restart.txt"
end

0 comments on commit ca16392

Please sign in to comment.