bovem 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/.travis.yml +8 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +86 -0
- data/README.md +29 -0
- data/Rakefile +18 -0
- data/bovem.gemspec +36 -0
- data/lib/bovem.rb +16 -0
- data/lib/bovem/configuration.rb +69 -0
- data/lib/bovem/errors.rb +18 -0
- data/lib/bovem/logger.rb +90 -0
- data/lib/bovem/version.rb +25 -0
- data/spec/bovem/configuration_spec.rb +46 -0
- data/spec/bovem/logger_spec.rb +100 -0
- data/spec/coverage_helper.rb +19 -0
- data/spec/spec_helper.rb +16 -0
- metadata +216 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
source "http://rubygems.org"
|
8
|
+
|
9
|
+
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
bovem (0.5.0)
|
5
|
+
lazier (~> 1.0)
|
6
|
+
rainbow (~> 1.1.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionpack (3.2.7)
|
12
|
+
activemodel (= 3.2.7)
|
13
|
+
activesupport (= 3.2.7)
|
14
|
+
builder (~> 3.0.0)
|
15
|
+
erubis (~> 2.7.0)
|
16
|
+
journey (~> 1.0.4)
|
17
|
+
rack (~> 1.4.0)
|
18
|
+
rack-cache (~> 1.2)
|
19
|
+
rack-test (~> 0.6.1)
|
20
|
+
sprockets (~> 2.1.3)
|
21
|
+
activemodel (3.2.7)
|
22
|
+
activesupport (= 3.2.7)
|
23
|
+
builder (~> 3.0.0)
|
24
|
+
activesupport (3.2.7)
|
25
|
+
i18n (~> 0.6)
|
26
|
+
multi_json (~> 1.0)
|
27
|
+
builder (3.0.0)
|
28
|
+
coderay (1.0.7)
|
29
|
+
diff-lcs (1.1.3)
|
30
|
+
erubis (2.7.0)
|
31
|
+
github-markup (0.7.2)
|
32
|
+
hike (1.2.1)
|
33
|
+
i18n (0.6.0)
|
34
|
+
journey (1.0.4)
|
35
|
+
json (1.7.4)
|
36
|
+
lazier (1.0.1)
|
37
|
+
actionpack (~> 3.0)
|
38
|
+
json (~> 1.7.0)
|
39
|
+
tzinfo (~> 0.3.0)
|
40
|
+
method_source (0.8)
|
41
|
+
multi_json (1.3.6)
|
42
|
+
pry (0.9.10)
|
43
|
+
coderay (~> 1.0.5)
|
44
|
+
method_source (~> 0.8)
|
45
|
+
slop (~> 3.3.1)
|
46
|
+
rack (1.4.1)
|
47
|
+
rack-cache (1.2)
|
48
|
+
rack (>= 0.4)
|
49
|
+
rack-test (0.6.1)
|
50
|
+
rack (>= 1.0)
|
51
|
+
rainbow (1.1.4)
|
52
|
+
rake (0.9.2.2)
|
53
|
+
redcarpet (2.1.1)
|
54
|
+
rspec (2.11.0)
|
55
|
+
rspec-core (~> 2.11.0)
|
56
|
+
rspec-expectations (~> 2.11.0)
|
57
|
+
rspec-mocks (~> 2.11.0)
|
58
|
+
rspec-core (2.11.0)
|
59
|
+
rspec-expectations (2.11.1)
|
60
|
+
diff-lcs (~> 1.1.3)
|
61
|
+
rspec-mocks (2.11.1)
|
62
|
+
simplecov (0.6.4)
|
63
|
+
multi_json (~> 1.0)
|
64
|
+
simplecov-html (~> 0.5.3)
|
65
|
+
simplecov-html (0.5.3)
|
66
|
+
slop (3.3.2)
|
67
|
+
sprockets (2.1.3)
|
68
|
+
hike (~> 1.2)
|
69
|
+
rack (~> 1.0)
|
70
|
+
tilt (~> 1.1, != 1.3.0)
|
71
|
+
tilt (1.3.3)
|
72
|
+
tzinfo (0.3.33)
|
73
|
+
yard (0.8.2.1)
|
74
|
+
|
75
|
+
PLATFORMS
|
76
|
+
ruby
|
77
|
+
|
78
|
+
DEPENDENCIES
|
79
|
+
bovem!
|
80
|
+
github-markup (~> 0.7.0)
|
81
|
+
pry
|
82
|
+
rake (~> 0.9.0)
|
83
|
+
redcarpet (~> 2.1.0)
|
84
|
+
rspec (~> 2.11.0)
|
85
|
+
simplecov (~> 0.6.0)
|
86
|
+
yard (~> 0.8.0)
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# bovem
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/ShogunPanda/bovem.png?branch=master)](http://travis-ci.org/ShogunPanda/bovem)
|
4
|
+
[![Dependency Status](https://gemnasium.com/ShogunPanda/bovem.png?travis)](https://gemnasium.com/ShogunPanda/bovem)
|
5
|
+
|
6
|
+
A collection of utilities for developers.
|
7
|
+
|
8
|
+
http://github.com/ShogunPanda/bovem
|
9
|
+
|
10
|
+
## Description
|
11
|
+
|
12
|
+
Bovem is a small set of utilities for Ruby developers.
|
13
|
+
|
14
|
+
See the documentation for more informations.
|
15
|
+
|
16
|
+
## Contributing to bovem
|
17
|
+
|
18
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
19
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
20
|
+
* Fork the project.
|
21
|
+
* Start a feature/bugfix branch.
|
22
|
+
* Commit and push until you are happy with your contribution.
|
23
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
24
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
25
|
+
|
26
|
+
## Copyright
|
27
|
+
|
28
|
+
Copyright (C) 2012 and above Shogun <[shogun_panda@me.com](mailto:shogun_panda@me.com)>.
|
29
|
+
Licensed under the MIT license, which can be found at [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the cowtech-extensions gem. Copyright (C) 2011 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "bundler/gem_tasks"
|
8
|
+
require "rspec/core/rake_task"
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new("spec")
|
11
|
+
|
12
|
+
namespace :spec do
|
13
|
+
desc "Run all specs with coverage"
|
14
|
+
task :coverage do
|
15
|
+
ENV["BOVEM_COVERAGE"] = "TRUE"
|
16
|
+
Rake::Task["spec"].invoke
|
17
|
+
end
|
18
|
+
end
|
data/bovem.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "./lib/bovem/version"
|
8
|
+
|
9
|
+
Gem::Specification.new do |gem|
|
10
|
+
gem.name = "bovem"
|
11
|
+
gem.version = Bovem::Version::STRING
|
12
|
+
gem.authors = ["Shogun"]
|
13
|
+
gem.email = ["shogun_panda@me.com"]
|
14
|
+
gem.homepage = "http://github.com/ShogunPanda/bovem"
|
15
|
+
gem.summary = %q{A collection of utilities for developers.}
|
16
|
+
gem.description = %q{A collection of utilities for developers.}
|
17
|
+
|
18
|
+
gem.rubyforge_project = "bovem"
|
19
|
+
gem.files = `git ls-files`.split("\n")
|
20
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
gem.require_paths = ["lib"]
|
23
|
+
|
24
|
+
gem.add_dependency("lazier", "~> 1.0")
|
25
|
+
gem.add_dependency("rainbow", "~> 1.1.0")
|
26
|
+
|
27
|
+
gem.add_development_dependency("rspec", "~> 2.11.0")
|
28
|
+
gem.add_development_dependency("rake", "~> 0.9.0")
|
29
|
+
gem.add_development_dependency("simplecov", "~> 0.6.0")
|
30
|
+
gem.add_development_dependency("pry", ">= 0")
|
31
|
+
gem.add_development_dependency("yard", "~> 0.8.0")
|
32
|
+
gem.add_development_dependency("redcarpet", "~> 2.1.0")
|
33
|
+
gem.add_development_dependency("github-markup", "~> 0.7.0")
|
34
|
+
end
|
35
|
+
|
36
|
+
|
data/lib/bovem.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "rainbow"
|
8
|
+
require "logger"
|
9
|
+
require "lazier"
|
10
|
+
|
11
|
+
#Lazier.load!
|
12
|
+
|
13
|
+
require "bovem/version" if !defined?(Bovem::Version)
|
14
|
+
require "bovem/errors"
|
15
|
+
require "bovem/configuration"
|
16
|
+
require "bovem/logger"
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Bovem
|
8
|
+
# This class holds the configuration of the applicaton.
|
9
|
+
#
|
10
|
+
# Extend this class and add valid properties via `attr_accessor` methods.
|
11
|
+
# Example:
|
12
|
+
# ```ruby
|
13
|
+
# class MyConfiguration << Bovem::Configuration
|
14
|
+
# attr_accessor :property
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # Configuration file
|
18
|
+
# config.property = "VALUE"
|
19
|
+
# ```
|
20
|
+
class Configuration
|
21
|
+
# Creates a new configuration.
|
22
|
+
# A configuration file is a plain Ruby file with a top-level {Configuration config} object.
|
23
|
+
#
|
24
|
+
# @param file [String] The file to read.
|
25
|
+
# @param overrides [Hash] A set of values which override those set in the configuration file.
|
26
|
+
# @param logger [Logger] The logger to use for notifications.
|
27
|
+
# @see parse
|
28
|
+
def initialize(file = nil, overrides = {}, logger = nil)
|
29
|
+
self.parse(file, overrides, logger)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Creates a new configuration.
|
33
|
+
# A configuration file is a plain Ruby file with a top-level {Configuration config} object.
|
34
|
+
#
|
35
|
+
# Example:
|
36
|
+
#
|
37
|
+
# ```ruby
|
38
|
+
# config.property = "VALUE"
|
39
|
+
# ```
|
40
|
+
#
|
41
|
+
# @param file [String] The file to read.
|
42
|
+
# @param logger [Logger] The logger to use for notifications.
|
43
|
+
# @param overrides [Hash] A set of values which override those set in the configuration file.
|
44
|
+
def parse(file = nil, overrides = {}, logger = nil)
|
45
|
+
if file.present? then
|
46
|
+
begin
|
47
|
+
# Open the file
|
48
|
+
path = ::Pathname.new(file).realpath
|
49
|
+
logger.info("Using configuration file #{path}.") if logger
|
50
|
+
self.tap do |config|
|
51
|
+
eval(::File.read(path))
|
52
|
+
end
|
53
|
+
rescue ::Errno::ENOENT, ::LoadError
|
54
|
+
rescue ::Exception => e
|
55
|
+
raise Bovem::Errors::InvalidConfiguration.new("Config file #{file} is not valid.")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Apply overrides
|
60
|
+
if overrides.is_a?(::Hash) then
|
61
|
+
overrides.each_pair do |k, v|
|
62
|
+
self.send("#{k}=", v) if self.respond_to?("#{k}=")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
self
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/bovem/errors.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Bovem
|
8
|
+
# Exceptions for {Bovem Bovem}.
|
9
|
+
module Errors
|
10
|
+
# This exception is raised if a {Configuration Configuration} is invalid.
|
11
|
+
class InvalidConfiguration < ::ArgumentError
|
12
|
+
end
|
13
|
+
|
14
|
+
# This exception is raised if a {Logger Logger} is invalid.
|
15
|
+
class InvalidLogger < ::ArgumentError
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/bovem/logger.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Bovem
|
8
|
+
# A custom logger.
|
9
|
+
class Logger < ::Logger
|
10
|
+
# The start time of first line. This allows to show a `T+0.1234` information into the log.
|
11
|
+
mattr_accessor :start_time
|
12
|
+
|
13
|
+
# The file or device to log messages to.
|
14
|
+
attr_reader :device
|
15
|
+
|
16
|
+
# Creates a new logger
|
17
|
+
# @see http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html
|
18
|
+
#
|
19
|
+
# @param logdev [String|IO] The log device. This is a filename (String) or IO object (typically STDOUT, STDERR, or an open file).
|
20
|
+
# @param shift_age [Fixnum] Number of old log files to keep, or frequency of rotation (daily, weekly or monthly).
|
21
|
+
# @param shift_size [Fixnum] Maximum logfile size (only applies when shift_age is a number).
|
22
|
+
def initialize(logdev, shift_age = 0, shift_size = 1048576)
|
23
|
+
@device = logdev
|
24
|
+
super(logdev, shift_age, shift_size)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Creates a new logger
|
28
|
+
#
|
29
|
+
# @param file [String|IO] The log device. This is a filename (String) or IO object (typically STDOUT, STDERR, or an open file).
|
30
|
+
# @param level [Fixnum] The minimum severity to log. See http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html for valid levels.
|
31
|
+
# @param formatter [Proc] The formatter to use for logging.
|
32
|
+
# @return [Logger] The new logger.
|
33
|
+
def self.create(file = nil, level = Logger::INFO, formatter = nil)
|
34
|
+
file ||= self.default_file
|
35
|
+
|
36
|
+
begin
|
37
|
+
rv = self.new(self.get_real_file(file))
|
38
|
+
rescue
|
39
|
+
raise Bovem::Errors::InvalidLogger
|
40
|
+
end
|
41
|
+
|
42
|
+
rv.level = level.to_i
|
43
|
+
rv.formatter = formatter || self.default_formatter
|
44
|
+
rv
|
45
|
+
end
|
46
|
+
|
47
|
+
# Translates a file to standard input or standard ouput in some special cases.
|
48
|
+
#
|
49
|
+
# @param file [String] The string to translate.
|
50
|
+
# @return [String|IO] The translated file name.
|
51
|
+
def self.get_real_file(file)
|
52
|
+
case file
|
53
|
+
when "STDOUT" then $stdout
|
54
|
+
when "STDERR" then $stderr
|
55
|
+
else file
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# The default file for logging.
|
60
|
+
# @return [String|IO] The default file for logging.
|
61
|
+
def self.default_file
|
62
|
+
@default_file ||= $stdout
|
63
|
+
end
|
64
|
+
|
65
|
+
# The default formatter for logging.
|
66
|
+
# @return [Proc] The default formatter for logging.
|
67
|
+
def self.default_formatter
|
68
|
+
@default_formatter ||= ::Proc.new {|severity, datetime, progname, msg|
|
69
|
+
color = case severity
|
70
|
+
when "DEBUG" then :cyan
|
71
|
+
when "INFO" then :green
|
72
|
+
when "WARN" then :yellow
|
73
|
+
when "ERROR" then :red
|
74
|
+
when "FATAL" then :magenta
|
75
|
+
else :white
|
76
|
+
end
|
77
|
+
|
78
|
+
header = ("[%s T+%0.5f] %s:" %[datetime.strftime("%Y/%b/%d %H:%M:%S"), [datetime.to_f - self.start_time.to_f, 0].max, severity.rjust(5)]).bright
|
79
|
+
header = header.color(color) if color.present?
|
80
|
+
"%s %s\n" % [header, msg]
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
# The log time of the first logger. This allows to show a `T+0.1234` information into the log.
|
85
|
+
# @return [Time] The log time of the first logger.
|
86
|
+
def self.start_time
|
87
|
+
@start_time ||= ::Time.now
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
# A collection of utilities for developers.
|
8
|
+
module Bovem
|
9
|
+
# The current version of bovem, according to semantic versioning.
|
10
|
+
#
|
11
|
+
# @see http://semver.org
|
12
|
+
module Version
|
13
|
+
# The major version.
|
14
|
+
MAJOR = 0
|
15
|
+
|
16
|
+
# The minor version.
|
17
|
+
MINOR = 5
|
18
|
+
|
19
|
+
# The patch version.
|
20
|
+
PATCH = 0
|
21
|
+
|
22
|
+
# The current version number of Bovem.
|
23
|
+
STRING = [MAJOR, MINOR, PATCH].compact.join(".")
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "spec_helper"
|
8
|
+
|
9
|
+
describe Bovem::Configuration do
|
10
|
+
class BaseConfiguration < Bovem::Configuration
|
11
|
+
attr_accessor :property
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:log_file) { "/tmp/bovem-test-log-#{Time.now.strftime("%Y%m%d-%H:%M:%S")}" }
|
15
|
+
let(:test_prefix) { "/tmp/bovem-test-#{Time.now.strftime("%Y%m%d-%H:%M:%S")}" }
|
16
|
+
|
17
|
+
describe "#initialize" do
|
18
|
+
it "reads a valid configuration file" do
|
19
|
+
file = ::File.open("#{test_prefix}", "w") {|f| f.write("config.property = 1234") }
|
20
|
+
|
21
|
+
config = BaseConfiguration.new(test_prefix)
|
22
|
+
expect(config.property).to eq(1234)
|
23
|
+
File.unlink(test_prefix)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "reject an invalid configuration" do
|
27
|
+
file1 = ::File.open("#{test_prefix}-1", "w") {|f| f.write("config.property = ") }
|
28
|
+
file2 = ::File.open("#{test_prefix}-2", "w") {|f| f.write("config.non_property = 1234") }
|
29
|
+
|
30
|
+
expect { config = BaseConfiguration.new("#{test_prefix}-1")}.to raise_error(Bovem::Errors::InvalidConfiguration)
|
31
|
+
expect { config = BaseConfiguration.new("#{test_prefix}-2")}.to raise_error(Bovem::Errors::InvalidConfiguration)
|
32
|
+
|
33
|
+
File.unlink("#{test_prefix}-1")
|
34
|
+
File.unlink("#{test_prefix}-2")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "allows overrides" do
|
38
|
+
file = ::File.open("#{test_prefix}", "w") {|f| f.write("config.property = 1234") }
|
39
|
+
|
40
|
+
config = BaseConfiguration.new(test_prefix, {:property => 5678, :non_property => 1234})
|
41
|
+
expect(config.property).to eq(5678)
|
42
|
+
|
43
|
+
File.unlink(test_prefix)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "spec_helper"
|
8
|
+
|
9
|
+
describe Bovem::Logger do
|
10
|
+
before(:each) do
|
11
|
+
Sickill::Rainbow.enabled = false
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".create" do
|
15
|
+
it("should create a new default logger") do
|
16
|
+
logger = Bovem::Logger.create
|
17
|
+
expect(logger.device).to eq(Bovem::Logger.default_file)
|
18
|
+
expect(logger.level).to eq(::Logger::INFO)
|
19
|
+
expect(logger.formatter).to eq(Bovem::Logger.default_formatter)
|
20
|
+
end
|
21
|
+
|
22
|
+
it("should create a logger with a custom file and level") do
|
23
|
+
logger = Bovem::Logger.create("/dev/null", ::Logger::WARN)
|
24
|
+
expect(logger.device).to eq("/dev/null")
|
25
|
+
expect(logger.level).to eq(::Logger::WARN)
|
26
|
+
expect(logger.formatter).to eq(Bovem::Logger.default_formatter)
|
27
|
+
end
|
28
|
+
|
29
|
+
it("should create a logger with a custom formatter") do
|
30
|
+
formatter = Proc.new {|severity, datetime, progname, msg| msg }
|
31
|
+
logger = Bovem::Logger.create("/dev/null", ::Logger::WARN, formatter)
|
32
|
+
expect(logger.device).to eq("/dev/null")
|
33
|
+
expect(logger.level).to eq(::Logger::WARN)
|
34
|
+
expect(logger.formatter).to eq(formatter)
|
35
|
+
end
|
36
|
+
|
37
|
+
it("should raise exceptions for invalid files") do
|
38
|
+
expect { Bovem::Logger.create("/invalid/file") }.to raise_error(Bovem::Errors::InvalidLogger)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe ".default_formatter" do
|
43
|
+
let(:output) { ::StringIO.new }
|
44
|
+
let(:logger) { Bovem::Logger.create(output, Logger::DEBUG) }
|
45
|
+
|
46
|
+
def get_last_line(buffer)
|
47
|
+
buffer.string.split("\n").last.strip.gsub(/ T\+\d+\.\d+/, "")
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should correctly format a DEBUG message" do
|
51
|
+
logger.debug("Message.")
|
52
|
+
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] DEBUG: Message.")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should correctly format a INFO message" do
|
56
|
+
logger.info("Message.")
|
57
|
+
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] INFO: Message.")
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should correctly format a WARN message" do
|
61
|
+
logger.warn("Message.")
|
62
|
+
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] WARN: Message.")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should correctly format a ERROR message" do
|
66
|
+
logger.error("Message.")
|
67
|
+
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] ERROR: Message.")
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should correctly format a FATAL message" do
|
71
|
+
logger.fatal("Message.")
|
72
|
+
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] FATAL: Message.")
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should correctly format a INVALID message" do
|
76
|
+
logger.log(::Logger::UNKNOWN, "Message.")
|
77
|
+
expect(get_last_line(output)).to eq("[#{::Time.now.strftime("%Y/%b/%d %H:%M:%S")}] ANY: Message.")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe ".get_real_file" do
|
82
|
+
it("should return the standard ouput") do
|
83
|
+
expect(Bovem::Logger.get_real_file("STDOUT")).to eq($stdout )
|
84
|
+
end
|
85
|
+
|
86
|
+
it("should return the standard error") do
|
87
|
+
expect(Bovem::Logger.get_real_file("STDERR")).to eq($stderr )
|
88
|
+
end
|
89
|
+
|
90
|
+
it("should return the file") do
|
91
|
+
expect(Bovem::Logger.get_real_file("/dev/null")).to eq("/dev/null" )
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe ".default_file" do
|
96
|
+
it("should return the standard output") do
|
97
|
+
expect(Bovem::Logger.default_file).to eq($stdout)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "simplecov"
|
8
|
+
require "pathname"
|
9
|
+
|
10
|
+
if ENV["BOVEM_COVERAGE"] == "TRUE" then
|
11
|
+
root = Pathname.new(File.dirname(__FILE__)) + ".."
|
12
|
+
|
13
|
+
SimpleCov.start do
|
14
|
+
add_filter do |src_file|
|
15
|
+
path = Pathname.new(src_file.filename).relative_path_from(root).to_s
|
16
|
+
path !~ /^(bin|lib)/
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "rubygems"
|
8
|
+
require "bundler/setup"
|
9
|
+
require "coverage_helper"
|
10
|
+
require "bovem"
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.expect_with :rspec do |c|
|
14
|
+
c.syntax = :expect
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bovem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Shogun
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: lazier
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rainbow
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.1.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.1.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.11.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.11.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.9.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.9.0
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: simplecov
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 0.6.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 0.6.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: pry
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: yard
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.8.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 0.8.0
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: redcarpet
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ~>
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 2.1.0
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ~>
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: 2.1.0
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: github-markup
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ~>
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: 0.7.0
|
150
|
+
type: :development
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ~>
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: 0.7.0
|
158
|
+
description: A collection of utilities for developers.
|
159
|
+
email:
|
160
|
+
- shogun_panda@me.com
|
161
|
+
executables: []
|
162
|
+
extensions: []
|
163
|
+
extra_rdoc_files: []
|
164
|
+
files:
|
165
|
+
- .gitignore
|
166
|
+
- .travis.yml
|
167
|
+
- Gemfile
|
168
|
+
- Gemfile.lock
|
169
|
+
- README.md
|
170
|
+
- Rakefile
|
171
|
+
- bovem.gemspec
|
172
|
+
- lib/bovem.rb
|
173
|
+
- lib/bovem/configuration.rb
|
174
|
+
- lib/bovem/errors.rb
|
175
|
+
- lib/bovem/logger.rb
|
176
|
+
- lib/bovem/version.rb
|
177
|
+
- spec/bovem/configuration_spec.rb
|
178
|
+
- spec/bovem/logger_spec.rb
|
179
|
+
- spec/coverage_helper.rb
|
180
|
+
- spec/spec_helper.rb
|
181
|
+
homepage: http://github.com/ShogunPanda/bovem
|
182
|
+
licenses: []
|
183
|
+
post_install_message:
|
184
|
+
rdoc_options: []
|
185
|
+
require_paths:
|
186
|
+
- lib
|
187
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
188
|
+
none: false
|
189
|
+
requirements:
|
190
|
+
- - ! '>='
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
segments:
|
194
|
+
- 0
|
195
|
+
hash: -3168302242936700516
|
196
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
|
+
none: false
|
198
|
+
requirements:
|
199
|
+
- - ! '>='
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
segments:
|
203
|
+
- 0
|
204
|
+
hash: -3168302242936700516
|
205
|
+
requirements: []
|
206
|
+
rubyforge_project: bovem
|
207
|
+
rubygems_version: 1.8.24
|
208
|
+
signing_key:
|
209
|
+
specification_version: 3
|
210
|
+
summary: A collection of utilities for developers.
|
211
|
+
test_files:
|
212
|
+
- spec/bovem/configuration_spec.rb
|
213
|
+
- spec/bovem/logger_spec.rb
|
214
|
+
- spec/coverage_helper.rb
|
215
|
+
- spec/spec_helper.rb
|
216
|
+
has_rdoc:
|