From 93df2d56465fb658305e4b31e721fe5bfa10783f Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Sat, 19 Mar 2016 13:59:58 +0100 Subject: [PATCH] ParserFile: use a File instead of a filename - spec: can use StringIOs instead of loose files --- lib/github_changelog_generator/parser_file.rb | 23 ++++++++++---- spec/files/github_changelog_params_327 | 2 -- spec/files/github_changelog_params_empty | 0 spec/files/github_changelog_params_incorrect | 2 -- spec/files/github_changelog_params_override | 3 -- spec/unit/parse_file_spec.rb | 31 +++++++++++-------- 6 files changed, 35 insertions(+), 26 deletions(-) delete mode 100644 spec/files/github_changelog_params_327 delete mode 100644 spec/files/github_changelog_params_empty delete mode 100644 spec/files/github_changelog_params_incorrect delete mode 100644 spec/files/github_changelog_params_override diff --git a/lib/github_changelog_generator/parser_file.rb b/lib/github_changelog_generator/parser_file.rb index 40c2498..a85d969 100644 --- a/lib/github_changelog_generator/parser_file.rb +++ b/lib/github_changelog_generator/parser_file.rb @@ -6,21 +6,32 @@ module GitHubChangelogGenerator class ParserFile 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 + @file = file + end + + def read_default_file + File.open(path) if path.exist? + end + + def path + @path ||= Pathname(File.expand_path(FILENAME)) end # Destructively change @options using data in configured options file. 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 private - def file - @file ||= Pathname(File.expand_path(@options[:params_file] || FILENAME)) - end - def parse_line!(line) option_name, value = extract_pair(line) @options[option_key_for(option_name)] = convert_value(value, option_name) diff --git a/spec/files/github_changelog_params_327 b/spec/files/github_changelog_params_327 deleted file mode 100644 index 2506a61..0000000 --- a/spec/files/github_changelog_params_327 +++ /dev/null @@ -1,2 +0,0 @@ -exclude-labels=73a91042-da6f-11e5-9335-1040f38d7f90,7adf83b4-da6f-11e5-ae18-1040f38d7f90 -header_label=# My changelog diff --git a/spec/files/github_changelog_params_empty b/spec/files/github_changelog_params_empty deleted file mode 100644 index e69de29..0000000 diff --git a/spec/files/github_changelog_params_incorrect b/spec/files/github_changelog_params_incorrect deleted file mode 100644 index 35fa3d8..0000000 --- a/spec/files/github_changelog_params_incorrect +++ /dev/null @@ -1,2 +0,0 @@ -unreleased_label: staging -unreleased: false diff --git a/spec/files/github_changelog_params_override b/spec/files/github_changelog_params_override deleted file mode 100644 index c31e411..0000000 --- a/spec/files/github_changelog_params_override +++ /dev/null @@ -1,3 +0,0 @@ -unreleased_label=staging -unreleased=false -header==== Changelog === diff --git a/spec/unit/parse_file_spec.rb b/spec/unit/parse_file_spec.rb index 2e4f1a6..24171fc 100644 --- a/spec/unit/parse_file_spec.rb +++ b/spec/unit/parse_file_spec.rb @@ -8,8 +8,8 @@ describe GitHubChangelogGenerator::ParserFile do end context "when file is empty" do - let(:options) { { params_file: "spec/files/github_changelog_params_empty" } } - let(:parse) { GitHubChangelogGenerator::ParserFile.new(options) } + let(:options) { {} } + let(:parse) { GitHubChangelogGenerator::ParserFile.new(options, StringIO.new("")) } it "does not change the options" do expect { parse.parse! }.to_not change { options } @@ -17,17 +17,21 @@ describe GitHubChangelogGenerator::ParserFile do end 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(: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) } end context "when override default values" do 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(: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 expect { parse.parse! }.to change { options } @@ -38,21 +42,22 @@ describe GitHubChangelogGenerator::ParserFile do end context "turns exclude-labels into an Array", bug: '#327' do - let(:options) do - { - params_file: "spec/files/github_changelog_params_327" - } + let(:file) do + StringIO.new(<