Skip to content

Commit

Permalink
Handle empty changelog files
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Jun 9, 2024
1 parent 02e6ffc commit afb024d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Reissue.finalize returns the version and value as an array
- Documentation on the refined redo method in Gem::Version
- Limit major numbers to Integers
- Handle empty changelog files

## [0.1.2] - 2024-06-08

Expand Down
6 changes: 3 additions & 3 deletions lib/reissue/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def parse

def parse_title(scanner)
scanner.scan(/# ([\w\s]+)$/)
title = scanner[1].strip
title = scanner[1].to_s.strip
{"title" => title}
end

def parse_preamble(scanner)
preamble = scanner.scan_until(VERSION_MATCH)
preamble = preamble.gsub(VERSION_BREAK, "").strip
scanner.unscan
preamble = preamble.to_s.gsub(VERSION_BREAK, "").strip
scanner.unscan unless preamble.empty?
{"preamble" => preamble.strip}
end

Expand Down
12 changes: 9 additions & 3 deletions lib/reissue/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Reissue
class Printer
def initialize(changelog)
@changelog = changelog
@title = @changelog["title"]
@preamble = @changelog["preamble"]
@title = @changelog["title"] || "Changelog"
@preamble = @changelog["preamble"] || "All project changes are documented in this file."
@versions = versions
end

Expand All @@ -29,7 +29,13 @@ def versions
format_section(section, changes)
end.join("\n\n")
[version_string, changes_string].filter_map { |str| str unless str.empty? }.join("\n\n")
end.join("\n\n")
end.then do |data|
if data.empty?
"## [0.0.0] - Unreleased"
else
data.join("\n\n")
end
end
end

def format_section(section, changes)
Expand Down
7 changes: 7 additions & 0 deletions test/test_changelog_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class TestChangelogUpdater < Minitest::Spec
assert_match(/\[1.1\.0\]/, contents)
refute_match(/\[1.0\.0\]/, contents)
end

it "handles empty changelog files" do
@changelog_updater = Reissue::ChangelogUpdater.new(@tempfile.path)
@changelog_updater.call("1.0.0", changelog_file: @tempfile.path)
contents = @changelog_updater.to_s
assert_match(/\[1.0\.0\]/, contents)
end
end

describe "update" do
Expand Down
14 changes: 14 additions & 0 deletions test/test_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,19 @@ class TestPrinter < Minitest::Spec
- New feature
MARKDOWN
end

it "handles empty changelogs" do
changelog = {
"versions" => []
}
printer = Reissue::Printer.new(changelog)
assert_equal(<<~MARKDOWN, printer.to_s)
# Changelog
All project changes are documented in this file.
## [0.0.0] - Unreleased
MARKDOWN
end
end
end

0 comments on commit afb024d

Please sign in to comment.