ParserFile: use a File instead of a filename
- spec: can use StringIOs instead of loose files
This commit is contained in:
parent
f9ad19285e
commit
93df2d5646
|
@ -6,21 +6,32 @@ module GitHubChangelogGenerator
|
||||||
class ParserFile
|
class ParserFile
|
||||||
FILENAME = ".github_changelog_generator"
|
FILENAME = ".github_changelog_generator"
|
||||||
|
|
||||||
def initialize(options)
|
attr_reader :file
|
||||||
|
|
||||||
|
# @param options [Hash]
|
||||||
|
# @param file [nil,IO]
|
||||||
|
def initialize(options, file = read_default_file)
|
||||||
@options = options
|
@options = options
|
||||||
|
@file = file
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_default_file
|
||||||
|
File.open(path) if path.exist?
|
||||||
|
end
|
||||||
|
|
||||||
|
def path
|
||||||
|
@path ||= Pathname(File.expand_path(FILENAME))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Destructively change @options using data in configured options file.
|
# Destructively change @options using data in configured options file.
|
||||||
def parse!
|
def parse!
|
||||||
file.each_line { |line| parse_line!(line) } if file.exist?
|
return unless file
|
||||||
|
file.each_line { |line| parse_line!(line) }
|
||||||
|
file.close
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def file
|
|
||||||
@file ||= Pathname(File.expand_path(@options[:params_file] || FILENAME))
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_line!(line)
|
def parse_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)
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
exclude-labels=73a91042-da6f-11e5-9335-1040f38d7f90,7adf83b4-da6f-11e5-ae18-1040f38d7f90
|
|
||||||
header_label=# My changelog
|
|
|
@ -1,2 +0,0 @@
|
||||||
unreleased_label: staging
|
|
||||||
unreleased: false
|
|
|
@ -1,3 +0,0 @@
|
||||||
unreleased_label=staging
|
|
||||||
unreleased=false
|
|
||||||
header==== Changelog ===
|
|
|
@ -8,8 +8,8 @@ describe GitHubChangelogGenerator::ParserFile do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when file is empty" do
|
context "when file is empty" do
|
||||||
let(:options) { { params_file: "spec/files/github_changelog_params_empty" } }
|
let(:options) { {} }
|
||||||
let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) }
|
let(:parse) { GitHubChangelogGenerator::ParserFile.new(options, StringIO.new("")) }
|
||||||
|
|
||||||
it "does not change the options" do
|
it "does not change the options" do
|
||||||
expect { parse.parse! }.to_not change { options }
|
expect { parse.parse! }.to_not change { options }
|
||||||
|
@ -17,17 +17,21 @@ describe GitHubChangelogGenerator::ParserFile do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when file is incorrect" do
|
context "when file is incorrect" do
|
||||||
let(:options) { { params_file: "spec/files/github_changelog_params_incorrect" } }
|
let(:options) { {} }
|
||||||
let(:options_before_change) { options.dup }
|
let(:options_before_change) { options.dup }
|
||||||
let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) }
|
let(:file) { StringIO.new("unreleased_label: staging\nunreleased: false") }
|
||||||
|
let(:parse) do
|
||||||
|
GitHubChangelogGenerator::ParserFile.new(options, file)
|
||||||
|
end
|
||||||
it { expect { parse.parse! }.to raise_error(GitHubChangelogGenerator::ParserError) }
|
it { expect { parse.parse! }.to raise_error(GitHubChangelogGenerator::ParserError) }
|
||||||
end
|
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) { { params_file: "spec/files/github_changelog_params_override" }.merge(default_options) }
|
let(:options) { {}.merge(default_options) }
|
||||||
let(:options_before_change) { options.dup }
|
let(:options_before_change) { options.dup }
|
||||||
let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) }
|
let(:file) { StringIO.new("unreleased_label=staging\nunreleased=false\nheader==== Changelog ===") }
|
||||||
|
let(:parse) { GitHubChangelogGenerator::ParserFile.new(options, file) }
|
||||||
|
|
||||||
it "changes the options" do
|
it "changes the options" do
|
||||||
expect { parse.parse! }.to change { options }
|
expect { parse.parse! }.to change { options }
|
||||||
|
@ -38,21 +42,22 @@ describe GitHubChangelogGenerator::ParserFile do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "turns exclude-labels into an Array", bug: '#327' do
|
context "turns exclude-labels into an Array", bug: '#327' do
|
||||||
let(:options) do
|
let(:file) do
|
||||||
{
|
StringIO.new(<<EOF
|
||||||
params_file: "spec/files/github_changelog_params_327"
|
exclude-labels=73a91042-da6f-11e5-9335-1040f38d7f90,7adf83b4-da6f-11e5-ae18-1040f38d7f90
|
||||||
}
|
header_label=# My changelog
|
||||||
|
EOF
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "reads exclude_labels into an Array" do
|
it "reads exclude_labels into an Array" do
|
||||||
expect { parse.parse! }.to change { options[:exclude_labels] }
|
expect { parse.parse! }.to change { options[:exclude_labels] }
|
||||||
.from(nil)
|
.from(default_options[:exclude_labels])
|
||||||
.to(["73a91042-da6f-11e5-9335-1040f38d7f90", "7adf83b4-da6f-11e5-ae18-1040f38d7f90"])
|
.to(["73a91042-da6f-11e5-9335-1040f38d7f90", "7adf83b4-da6f-11e5-ae18-1040f38d7f90"])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "translates given header_label into the :header option" do
|
it "translates given header_label into the :header option" do
|
||||||
expect { parse.parse! }.to change { options[:header] }
|
expect { parse.parse! }.to change { options[:header] }
|
||||||
.from(nil)
|
.from(default_options[:header])
|
||||||
.to("# My changelog")
|
.to("# My changelog")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user