Move logging to separate class, disable logging during test execution.

This commit is contained in:
Petr Korolev 2015-06-10 13:27:27 +03:00
parent a0cf2f54ee
commit ff6115247e
7 changed files with 62 additions and 41 deletions

View File

@ -23,9 +23,10 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_runtime_dependency("github_api", ["~> 0.12"])
spec.add_runtime_dependency("colorize", ["~> 0.7"])
# Development only
spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
end

View File

@ -2,8 +2,6 @@
## [0.0.4](https://github.com/skywinder/changelog_test/tree/0.0.4) (2015-05-22)
[Full Changelog](https://github.com/skywinder/changelog_test/compare/v0.0.3...0.0.4)
**Closed issues:**
- Test issue, that should appear in 0.0.4 [\#3](https://github.com/skywinder/changelog_test/issues/3)
@ -12,24 +10,10 @@
- Add automatically generated change log file. [\#5](https://github.com/skywinder/changelog_test/pull/5) ([skywinder](https://github.com/skywinder))
## [v0.0.3](https://github.com/skywinder/changelog_test/tree/v0.0.3) (2015-03-04)
[Full Changelog](https://github.com/skywinder/changelog_test/compare/v0.0.2...v0.0.3)
**Merged pull requests:**
- fix \#3. hotfix. Should appear in v0.0.3 [\#4](https://github.com/skywinder/changelog_test/pull/4) ([skywinder](https://github.com/skywinder))
## [v0.0.2](https://github.com/skywinder/changelog_test/tree/v0.0.2) (2015-03-04)
[Full Changelog](https://github.com/skywinder/changelog_test/compare/v0.0.1...v0.0.2)
**Merged pull requests:**
- Here is a test hotfix should appear in v.0.0.2 [\#2](https://github.com/skywinder/changelog_test/pull/2) ([skywinder](https://github.com/skywinder))
## [v0.0.1](https://github.com/skywinder/changelog_test/tree/v0.0.1) (2015-03-02)
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*

View File

@ -5,6 +5,7 @@ require "json"
require "colorize"
require "benchmark"
require_relative "github_changelog_generator/helper"
require_relative "github_changelog_generator/parser"
require_relative "github_changelog_generator/generator/generator"
require_relative "github_changelog_generator/version"

View File

@ -1,4 +1,3 @@
require "logger"
module GitHubChangelogGenerator
# A Fetcher responsible for all requests to GitHub and all basic manipulation with related data
@ -6,6 +5,7 @@ module GitHubChangelogGenerator
#
# Example:
# fetcher = GitHubChangelogGenerator::Fetcher.new options
class Fetcher
PER_PAGE_NUMBER = 30
CHANGELOG_GITHUB_TOKEN = "CHANGELOG_GITHUB_TOKEN"
@ -16,12 +16,6 @@ module GitHubChangelogGenerator
def initialize(options = {})
@options = options || {}
@logger = Logger.new(STDOUT)
@logger.formatter = proc do |_severity, _datetime, _progname, msg|
"#{msg}\n"
end
@user = @options[:user]
@project = @options[:project]
@github_token = fetch_github_token
@ -41,7 +35,7 @@ module GitHubChangelogGenerator
def fetch_github_token
env_var = @options[:token] ? @options[:token] : (ENV.fetch CHANGELOG_GITHUB_TOKEN, nil)
@logger.warn NO_TOKEN_PROVIDED.yellow unless env_var
Helper.log.warn NO_TOKEN_PROVIDED.yellow unless env_var
env_var
end
@ -62,11 +56,11 @@ module GitHubChangelogGenerator
begin
value = yield
rescue Github::Error::Unauthorized => e
@logger.error e.body.red
Helper.log.error e.body.red
abort "Error: wrong GitHub token"
rescue Github::Error::Forbidden => e
@logger.warn e.body.red
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
Helper.log.warn e.body.red
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
value
end
@ -83,10 +77,10 @@ module GitHubChangelogGenerator
print_empty_line
if tags.count == 0
@logger.warn "Warning: Can't find any tags in repo.\
Helper.log.warn "Warning: Can't find any tags in repo.\
Make sure, that you push tags to remote repo via 'git push --tags'".yellow
else
@logger.info "Found #{tags.count} tags"
Helper.log.info "Found #{tags.count} tags"
end
end
@ -112,10 +106,10 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
break if @options[:max_issues] && issues.length >= @options[:max_issues]
end
print_empty_line
@logger.info "Received issues: #{issues.count}"
Helper.log.info "Received issues: #{issues.count}"
rescue
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
# separate arrays of issues and pull requests:
@ -140,10 +134,10 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
end
print_empty_line
rescue
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
@logger.info "Fetching merged dates: #{pull_requests.count}"
Helper.log.info "Fetching merged dates: #{pull_requests.count}"
pull_requests
end
@ -170,7 +164,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
repo: @options[:project],
issue_number: issue["number"]
rescue
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
issue[:events] = obj.body
print_in_same_line("Fetching events for issues and PR: #{i + 1}/#{issues.count}")
@ -184,7 +178,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
# to clear line from prev print
print_empty_line
@logger.info "Fetching events for issues and PR: #{i}"
Helper.log.info "Fetching events for issues and PR: #{i}"
end
# Try to find tag date in local hash.
@ -203,7 +197,7 @@ Make sure, that you push tags to remote repo via 'git push --tags'".yellow
@options[:project],
tag_name["commit"]["sha"]
rescue
@logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
Helper.log.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
end
time_string = github_git_data_commits_get["committer"]["date"]
@tag_times_hash[tag_name["name"]] = Time.parse(time_string)

View File

@ -0,0 +1,37 @@
require "logger"
module GitHubChangelogGenerator
module Helper
# @return true if the currently running program is a unit test
def self.test?
defined?SpecHelper
end
if test?
@log ||= Logger.new(nil) # don't show any logs when running tests
else
@log ||= Logger.new(STDOUT)
end
@log.formatter = proc do |severity, _datetime, _progname, msg|
string = "#{msg}\n"
if severity == "DEBUG"
string = string.magenta
elsif severity == "INFO"
string = string.white
elsif severity == "WARN"
string = string.yellow
elsif severity == "ERROR"
string = string.red
elsif severity == "FATAL"
string = string.red.bold
end
string
end
# Logging happens using this method
class << self
attr_reader :log
end
end
end

View File

@ -2,7 +2,7 @@
require "optparse"
require "pp"
require_relative "version"
require_relative "helper"
module GitHubChangelogGenerator
class Parser
# parse options with optparse
@ -21,7 +21,7 @@ module GitHubChangelogGenerator
end
if options[:verbose]
puts "Performing task with options:"
Helper.log.info "Performing task with options:"
pp options
puts ""
end

View File

@ -19,6 +19,10 @@ require "codeclimate-test-reporter"
require "simplecov"
require "coveralls"
# This module is only used to check the environment is currently a testing env
module SpecHelper
end
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
Coveralls::SimpleCov::Formatter,
SimpleCov::Formatter::HTMLFormatter,