Merge pull request #368 from olleolleolle/fix/issue-with-detect-tags

Avoid nil bug in detect_since_tag
This commit is contained in:
Olle Jonsson 2016-04-18 17:10:48 +02:00
commit 1a19286ea0
2 changed files with 39 additions and 42 deletions

View File

@ -54,13 +54,14 @@ module GitHubChangelogGenerator
# @return [Object] try to find newest tag using #Reader and :base option if specified otherwise returns nil # @return [Object] try to find newest tag using #Reader and :base option if specified otherwise returns nil
def detect_since_tag def detect_since_tag
@since_tag ||= @options[:since_tag] @since_tag ||= @options.fetch(:since_tag) { version_of_first_item }
if @since_tag.nil? && @options[:base] && File.file?(@options[:base]) end
reader = GitHubChangelogGenerator::Reader.new
content = reader.read(@options[:base]) def version_of_first_item
@since_tag = content[0]["version"] if content.count && content return unless File.file?(@options[:base].to_s)
end
@since_tag sections = GitHubChangelogGenerator::Reader.new.read(@options[:base])
sections.first["version"] if sections && sections.any?
end end
# Return tags after filtering tags in lists provided by option: --between-tags & --exclude-tags # Return tags after filtering tags in lists provided by option: --between-tags & --exclude-tags

View File

@ -1,19 +1,14 @@
def tag_mash_with_name(tag)
mash_tag = Hashie::Mash.new
mash_tag.name = tag
mash_tag
end
def tags_mash_from_strings(tags_strings)
mash_array = []
tags_strings.each do |tag|
mash_tag = tag_mash_with_name(tag)
mash_array << mash_tag
end
mash_array
end
describe GitHubChangelogGenerator::Generator do describe GitHubChangelogGenerator::Generator do
def tag_mash_with_name(tag)
Hashie::Mash.new.tap { |mash_tag| mash_tag.name = tag }
end
def tags_mash_from_strings(tags_strings)
tags_strings.map do |tag|
tag_mash_with_name(tag)
end
end
describe "#filter_between_tags" do describe "#filter_between_tags" do
context "when between_tags nil" do context "when between_tags nil" do
before do before do
@ -105,13 +100,13 @@ describe GitHubChangelogGenerator::Generator do
subject { generator.filter_excluded_tags(tags_mash_from_strings(%w(1 2 3))) } subject { generator.filter_excluded_tags(tags_mash_from_strings(%w(1 2 3))) }
context "with matching regex" do context "with matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: '[23]') } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: "[23]") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1))) } it { is_expected.to match_array(tags_mash_from_strings(%w(1))) }
end end
context "with non-matching regex" do context "with non-matching regex" do
let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: '[45]') } let(:generator) { GitHubChangelogGenerator::Generator.new(exclude_tags_regex: "[45]") }
it { is_expected.to be_a Array } it { is_expected.to be_a Array }
it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) } it { is_expected.to match_array(tags_mash_from_strings(%w(1 2 3))) }
end end
@ -206,7 +201,7 @@ describe GitHubChangelogGenerator::Generator do
@generator.instance_variable_set :@fetcher, mock @generator.instance_variable_set :@fetcher, mock
end end
subject do subject do
of_tag = @generator.get_time_of_tag tag_mash_with_name("valid_tag") of_tag = @generator.get_time_of_tag(tag_mash_with_name("valid_tag"))
of_tag of_tag
end end
it { is_expected.to be_a_kind_of(Time) } it { is_expected.to be_a_kind_of(Time) }
@ -215,31 +210,32 @@ describe GitHubChangelogGenerator::Generator do
end end
describe "#sort_tags_by_date" do describe "#sort_tags_by_date" do
time1 = Time.now let(:time1) { Time.now }
time2 = Time.now let(:time2) { Time.now }
time3 = Time.now let(:time3) { Time.now }
before(:all) do before(:all) do
@generator = GitHubChangelogGenerator::Generator.new @generator = GitHubChangelogGenerator::Generator.new
end end
before do
@generator.instance_variable_set(:@tag_times_hash, "valid_tag1" => time1,
"valid_tag2" => time2,
"valid_tag3" => time3)
end
subject do
@generator.sort_tags_by_date(tags)
end
context "sort unsorted tags" do context "sort unsorted tags" do
tags = tags_mash_from_strings %w(valid_tag1 valid_tag2 valid_tag3) let(:tags) { tags_mash_from_strings %w(valid_tag1 valid_tag2 valid_tag3) }
before do
@generator.instance_variable_set :@tag_times_hash, "valid_tag1" => time1, "valid_tag2" => time2, "valid_tag3" => time3
end
subject do
@generator.sort_tags_by_date tags
end
it { is_expected.to be_a_kind_of(Array) } it { is_expected.to be_a_kind_of(Array) }
it { is_expected.to match_array(tags.reverse!) } it { is_expected.to match_array(tags.reverse!) }
end end
context "sort sorted tags" do context "sort sorted tags" do
tags = tags_mash_from_strings %w(valid_tag3 valid_tag2 valid_tag1) let(:tags) { tags_mash_from_strings %w(valid_tag3 valid_tag2 valid_tag1) }
before do
@generator.instance_variable_set :@tag_times_hash, "valid_tag1" => time1, "valid_tag2" => time2, "valid_tag3" => time3
end
subject do
@generator.sort_tags_by_date tags
end
it { is_expected.to be_a_kind_of(Array) } it { is_expected.to be_a_kind_of(Array) }
it { is_expected.to match_array(tags) } it { is_expected.to match_array(tags) }
end end