ParserFile: Allow comments in settings file

- Ruby-style or semi-colon-style accepted
This commit is contained in:
Olle Jonsson 2016-03-19 16:42:36 +01:00
parent c5a92b71b2
commit 4569fad479
2 changed files with 17 additions and 1 deletions

View File

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

View File

@ -26,6 +26,14 @@ describe GitHubChangelogGenerator::ParserFile do
it { expect { parser.parse! }.to raise_error(/line #2/) }
end
context "allows comments with semi-colon or pound sign" do
let(:file) { StringIO.new("# Comment on first line\nunreleased_label=staging\n; Comment on third line\nunreleased=false") }
let(:parser) do
GitHubChangelogGenerator::ParserFile.new({}, file)
end
it { expect { parser.parse! }.not_to raise_error }
end
context "when override default values" do
let(:default_options) { GitHubChangelogGenerator::Parser.default_options }
let(:options) { {}.merge(default_options) }