Merge pull request #358 from olleolleolle/feature/parserfile-allow-comments

ParserFile: Allow comments in settings file
This commit is contained in:
Petr Korolev 2016-03-21 13:50:07 +02:00
commit 6d9fcf202e
2 changed files with 20 additions and 5 deletions

View File

@ -7,11 +7,13 @@ module GitHubChangelogGenerator
# given Hash. # given Hash.
# #
# In your project's root, you can put a file named # In your project's root, you can put a file named
# <tt>.github_changelog_generator</tt> to override defaults: # <tt>.github_changelog_generator</tt> to override defaults.
# #
# Example: # Example:
# header_label=# My Super Changelog # header_label=# My Super Changelog
# ; Comments are allowed
# future-release=5.0.0 # future-release=5.0.0
# # Ruby-style comments, too
# since-tag=1.0.0 # since-tag=1.0.0
# #
# The configuration format is <tt>some-key=value</tt> or <tt>some_key=value</tt>. # The configuration format is <tt>some-key=value</tt> or <tt>some_key=value</tt>.
@ -41,12 +43,18 @@ module GitHubChangelogGenerator
end end
def parse_line!(line, line_number) def parse_line!(line, line_number)
return if non_configuration_line?(line)
option_name, value = extract_pair(line) option_name, value = extract_pair(line)
@options[option_key_for(option_name)] = convert_value(value, option_name) @options[option_key_for(option_name)] = convert_value(value, option_name)
rescue rescue
raise ParserError, "Failed on line ##{line_number}: \"#{line.gsub(/[\n\r]+/, '')}\"" raise ParserError, "Failed on line ##{line_number}: \"#{line.gsub(/[\n\r]+/, '')}\""
end end
# Returns true if the line starts with a pound sign or a semi-colon.
def non_configuration_line?(line)
line =~ /^[\#;]/ || line =~ /^[\s]+$/
end
# Returns a the option name as a symbol and its string value sans newlines. # Returns a the option name as a symbol and its string value sans newlines.
# #
# @param line [String] unparsed line from config file # @param line [String] unparsed line from config file

View File

@ -1,14 +1,14 @@
describe GitHubChangelogGenerator::ParserFile do describe GitHubChangelogGenerator::ParserFile do
describe ".github_changelog_generator" do describe ".github_changelog_generator" do
context "when no has file" do
let(:options) { {} } let(:options) { {} }
context "when the well-known default file does not exist" do
let(:parser) { GitHubChangelogGenerator::ParserFile.new(options) } let(:parser) { GitHubChangelogGenerator::ParserFile.new(options) }
subject { parser.parse! } subject { parser.parse! }
it { is_expected.to be_nil } it { is_expected.to be_nil }
end end
context "when file is empty" do context "when file is empty" do
let(:options) { {} }
let(:parser) { GitHubChangelogGenerator::ParserFile.new(options, StringIO.new("")) } let(:parser) { GitHubChangelogGenerator::ParserFile.new(options, StringIO.new("")) }
it "does not change the options" do it "does not change the options" do
@ -17,7 +17,6 @@ describe GitHubChangelogGenerator::ParserFile do
end end
context "when file is incorrect" do context "when file is incorrect" do
let(:options) { {} }
let(:options_before_change) { options.dup } let(:options_before_change) { options.dup }
let(:file) { StringIO.new("unreleased_label=staging\nunreleased: false") } let(:file) { StringIO.new("unreleased_label=staging\nunreleased: false") }
let(:parser) do let(:parser) do
@ -26,6 +25,14 @@ describe GitHubChangelogGenerator::ParserFile do
it { expect { parser.parse! }.to raise_error(/line #2/) } it { expect { parser.parse! }.to raise_error(/line #2/) }
end end
context "allows empty lines and comments with semi-colon or pound sign" do
let(:file) { StringIO.new("\n \n# Comment on first line\nunreleased_label=staging\n; Comment on third line\nunreleased=false") }
let(:parser) do
GitHubChangelogGenerator::ParserFile.new(options, file)
end
it { expect { parser.parse! }.not_to raise_error }
end
context "when override default values" do context "when override default values" do
let(:default_options) { GitHubChangelogGenerator::Parser.default_options } let(:default_options) { GitHubChangelogGenerator::Parser.default_options }
let(:options) { {}.merge(default_options) } let(:options) { {}.merge(default_options) }