Browse files

Add configuration tests

  • Loading branch information...
1 parent 5b2b91a commit b5069b8ea9e3eea7fbd09e8459786fd9e7cf1410 @jt committed Jun 28, 2011
Showing with 123 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +9 −0 Rakefile
  3. +1 −0 itslog.gemspec
  4. +13 −5 lib/itslog/railtie.rb
  5. +3 −0 test/fixtures/article.rb
  6. +2 −0 test/fixtures/log.txt
  7. +6 −0 test/fixtures/schema.rb
  8. +28 −0 test/helper.rb
  9. +60 −0 test/itslog_test.rb
View
1 .gitignore
@@ -2,4 +2,5 @@
.bundle
Gemfile.lock
pkg/*
+*.sqlite3
.rvmrc
View
9 Rakefile
@@ -1,2 +1,11 @@
require 'bundler'
+require 'rake/testtask'
Bundler::GemHelper.install_tasks
+
+task :default => :test
+Rake::TestTask.new do |t|
+ t.libs << 'lib'
+ t.ruby_opts << '-rubygems'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
View
1 itslog.gemspec
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.files = %w( README.md Rakefile LICENSE )
s.files += Dir.glob("lib/**/*")
+ s.files += Dir.glob('test/**/*')
s.require_paths = ['lib']
end
View
18 lib/itslog/railtie.rb
@@ -1,7 +1,15 @@
-class Railtie < Rails::Railtie
- config.before_initialize do
- ActiveSupport::BufferedLogger.send(:include, Itslog::BufferedLoggerExtension)
- ActiveSupport::LogSubscriber.send(:include, Itslog::LogSubscriberExtension)
- ActiveSupport::LogSubscriber.colorize_logging = false
+module Itslog
+ class Railtie < Rails::Railtie
+ config.before_initialize do
+ Itslog::Railtie.insert
+ ActiveSupport::LogSubscriber.colorize_logging = false
+ end
+ end
+
+ class Railtie
+ def self.insert
+ ActiveSupport::BufferedLogger.send(:include, Itslog::BufferedLoggerExtension)
+ ActiveSupport::LogSubscriber.send(:include, Itslog::LogSubscriberExtension)
+ end
end
end
View
3 test/fixtures/article.rb
@@ -0,0 +1,3 @@
+class Article < ActiveRecord::Base
+ validates_presence_of :name
+end
View
2 test/fixtures/log.txt
@@ -0,0 +1,2 @@
+
+01:01:01 test
View
6 test/fixtures/schema.rb
@@ -0,0 +1,6 @@
+ActiveRecord::Schema.define do
+ self.verbose = false
+ create_table :articles, :force => true do |t|
+ t.string :name
+ end
+end
View
28 test/helper.rb
@@ -0,0 +1,28 @@
+require 'active_record'
+require 'active_support'
+require 'test/unit'
+require 'timecop'
+require 'redgreen'
+require 'itslog'
+
+ActiveRecord::Base.establish_connection \
+ :adapter => 'sqlite3',
+ :database => File.dirname(__FILE__) + '/fixtures/itslog.sqlite3'
+load File.dirname(__FILE__) + '/fixtures/schema.rb'
+load File.dirname(__FILE__) + '/fixtures/article.rb'
+
+Itslog::Railtie.insert
+
+def assert_log(log, msg, severity=0, namespace=nil)
+ level = [:debug, :info, :warn, :error, :fatal, :unknown]
+ Timecop.freeze(Time.parse('01:01:01'))
+ file_name = File.dirname(__FILE__) + '/fixtures/log.txt'
+ File.delete(file_name)
+ Rails.logger = ActiveSupport::BufferedLogger.new(file_name)
+ Rails.logger.namespace = namespace if namespace.present?
+ Rails.logger.send(level[severity], msg)
+ assert_equal log, File.read(file_name)
+ Timecop.return
+end
+
+class Test::Unit::TestCase; end
View
60 test/itslog_test.rb
@@ -0,0 +1,60 @@
+$LOAD_PATH.unshift File.dirname(__FILE__)
+require 'helper'
+
+class ItslogTest < Test::Unit::TestCase
+
+ def test_default_severity_color
+ Itslog::Configure.reset
+ assert_log "\n\e[37m01:01:01 \e[37mtest\n", 'test', 0
+ assert_log "\n\e[37m01:01:01 \e[37mtest\n", 'test', 1
+ assert_log "\n\e[33m01:01:01 \e[37mtest\n", 'test', 2
+ assert_log "\n\e[31m01:01:01 \e[37mtest\n", 'test', 3
+ assert_log "\n\e[31m01:01:01 \e[37mtest\n", 'test', 4
+ assert_log "\n\e[37m01:01:01 \e[37mtest\n", 'test', 5
+ end
+
+ def test_custom_severity_color
+ Itslog::Configure.reset
+ Itslog.configure do |config|
+ config.severity_colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo']
+ config.color_by = :severity
+ end
+ assert_log "\nred01:01:01 \e[37mtest\n" , 'test', 0
+ assert_log "\norange01:01:01 \e[37mtest\n", 'test', 1
+ assert_log "\nyellow01:01:01 \e[37mtest\n", 'test', 2
+ assert_log "\ngreen01:01:01 \e[37mtest\n" , 'test', 3
+ assert_log "\nblue01:01:01 \e[37mtest\n" , 'test', 4
+ assert_log "\nindigo01:01:01 \e[37mtest\n", 'test', 5
+ end
+
+ def test_default_namespaces
+ Itslog::Configure.reset
+ assert_log "\e[32m01:01:01 action_controller \e[37mtest\n", 'test', 0, 'action_controller'
+ assert_log "\e[36m01:01:01 action_view \e[37mtest\n" , 'test', 0, 'action_view'
+ assert_log "\e[94m01:01:01 active_record \e[37mtest\n" , 'test', 0, 'active_record'
+ assert_log "\e[94m01:01:01 mongo \e[37mtest\n" , 'test', 0, 'mongo'
+ end
+
+ def test_custom_namespace_color
+ Itslog::Configure.reset
+ Itslog.configure do |config|
+ config.format = '%n %m'
+ config.namespace_colors = {
+ 'action_controller' => 'red',
+ 'action_view' => 'orange',
+ 'active_record' => 'yellow',
+ 'mongo' => 'green'}
+ end
+ assert_log "redaction_controller \e[37mtest\n", 'test', 0, 'action_controller'
+ assert_log "orangeaction_view \e[37mtest\n" , 'test', 0, 'action_view'
+ assert_log "yellowactive_record \e[37mtest\n" , 'test', 0, 'active_record'
+ assert_log "greenmongo \e[37mtest\n" , 'test', 0, 'mongo'
+ end
+
+ def test_custom_format
+ Itslog::Configure.reset
+ Itslog.configure { |config| config.format = '[%n] %m' }
+ assert_log "\n\e[37m[] \e[37mtest\n", 'test', 0
+ assert_log "\e[32m[action_controller] \e[37mtest\n", 'test', 0, 'action_controller'
+ end
+end

0 comments on commit b5069b8

Please sign in to comment.