diff --git a/lib/github/nippou/settings.rb b/lib/github/nippou/settings.rb index 5a5f2a7..31c31a3 100644 --- a/lib/github/nippou/settings.rb +++ b/lib/github/nippou/settings.rb @@ -39,22 +39,13 @@ def yaml def data @data ||= begin - yaml_data = - case - when ENV['GITHUB_NIPPOU_SETTINGS'] - ENV['GITHUB_NIPPOU_SETTINGS'].chomp - when !`git config github-nippou.settings`.chomp.empty? - `git config github-nippou.settings`.chomp - when !`git config github-nippou.settings-gist-id`.chomp.empty? - gist_id = `git config github-nippou.settings-gist-id`.chomp - gist = client.gist(gist_id) - gist[:files][:'settings.yml'][:content] - end - - if yaml_data + if gist_id.present? + gist = client.gist(gist_id) + yaml_data = gist[:files][:'settings.yml'][:content] YAML.load(yaml_data).deep_symbolize_keys else - YAML.load_file(File.expand_path('../../../config/settings.yml', __dir__)).deep_symbolize_keys + default_yml = File.expand_path('../../../config/settings.yml', __dir__) + YAML.load_file(default_yml).deep_symbolize_keys end rescue Psych::SyntaxError puts <<~MESSAGE @@ -67,6 +58,20 @@ def data end end + # Getting gist id which has settings.yml + # + # @return [String] gist id + def gist_id + @gist_id ||= + begin + ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] || + begin + git_config = `git config github-nippou.settings-gist-id`.chomp + git_config.present? ? git_config : nil + end + end + end + # Cast to OpenStruct # # @param hash [Hash] diff --git a/spec/fixtures/settings-invalid.yml b/spec/fixtures/settings-invalid.yml new file mode 100644 index 0000000..64ac95d --- /dev/null +++ b/spec/fixtures/settings-invalid.yml @@ -0,0 +1,7 @@ +format: + **!!invalid!!** + line: '* [%{title}](%{url}) by %{user} %{status}' +dictionary: + status: + merged: '**merged!**' + closed: '**closed!**' diff --git a/spec/fixtures/settings-valid.yml b/spec/fixtures/settings-valid.yml new file mode 100644 index 0000000..c47e7e1 --- /dev/null +++ b/spec/fixtures/settings-valid.yml @@ -0,0 +1,7 @@ +format: + subject: '### %{subject}' + line: '* [%{title}](%{url}) by %{user} %{status}' +dictionary: + status: + merged: '**merged!**' + closed: '**closed!**' diff --git a/spec/github/nippou/settings_spec.rb b/spec/github/nippou/settings_spec.rb index fb63f3a..3637759 100644 --- a/spec/github/nippou/settings_spec.rb +++ b/spec/github/nippou/settings_spec.rb @@ -2,18 +2,14 @@ let(:client) { Octokit::Client.new(login: 'taro', access_token: '1234abcd') } let(:settings) { described_class.new(client: client) } + before do + ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] = '12345' + allow(client).to receive(:gist).and_return( files: { 'settings.yml': { content: settings_yaml } } ) + end + describe '#format' do context 'given valid settings' do - let(:settings_format) do - { - format: { - subject: '### %{subject}', - line: '* [%{title}](%{url}) by %{user} %{status}', - }, - } - end - - before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format.to_yaml } + let(:settings_yaml) { load_fixture('settings-valid.yml') } it 'is valid `subject`' do expect(settings.format.subject).to eq '### %{subject}' @@ -25,15 +21,7 @@ end context 'given invalid settings' do - let(:settings_format_yaml) do - <<~INVALID_YAML - format: - **!!invalid!!** - line: '* [%{title}](%{url}) by %{user} %{status}' - INVALID_YAML - end - - before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format_yaml } + let(:settings_yaml) { load_fixture('settings-invalid.yml') } it 'outputs YAML syntax error message' do expect { settings.format }.to raise_error Psych::SyntaxError @@ -43,18 +31,7 @@ describe '#dictionary' do context 'given valid settings' do - let(:settings_dictionary) do - { - dictionary: { - status: { - merged: '**merged!**', - closed: '**closed!**', - }, - }, - } - end - - before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_dictionary.to_yaml } + let(:settings_yaml) { load_fixture('settings-valid.yml') } it 'is valid `status.merged`' do expect(settings.dictionary.status.merged).to eq '**merged!**' @@ -64,23 +41,19 @@ expect(settings.dictionary.status.closed).to eq '**closed!**' end end + + context 'given invalid settings' do + let(:settings_yaml) { load_fixture('settings-invalid.yml') } + + it 'outputs YAML syntax error message' do + expect { settings.dictionary }.to raise_error Psych::SyntaxError + end + end end describe '#yaml' do context 'given valid settings' do - let(:settings_yaml) do - <<~VALID_YAML - format: - subject: "### %{subject}" - line: "* [%{title}](%{url}) by %{user} %{status}" - dictionary: - status: - merged: "**merged!**" - closed: "**closed!**" - VALID_YAML - end - - before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_yaml } + let(:settings_yaml) { load_fixture('settings-valid.yml') } it 'is valid yaml' do expect(settings.yaml).to eq <<~VALID_YAML @@ -95,5 +68,13 @@ VALID_YAML end end + + context 'given invalid settings' do + let(:settings_yaml) { load_fixture('settings-invalid.yml') } + + it 'outputs YAML syntax error message' do + expect { settings.yaml }.to raise_error Psych::SyntaxError + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 687b3ae..627992e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,8 +14,17 @@ require 'github/nippou' +Dir[File.expand_path('support/**/*.rb', __dir__)].each do |f| + require f +end + # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| + config.include LoadFixtureHelper + + config.before(:all) { silence_stdout } + config.after(:all){ enable_stdout } + # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer. @@ -46,6 +55,12 @@ # triggering implicit auto-inclusion in groups with matching metadata. config.shared_context_metadata_behavior = :apply_to_host_groups + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + # The settings below are suggested to provide a good initial experience # with RSpec, but feel free to customize to your heart's content. =begin @@ -87,12 +102,6 @@ # particularly slow. config.profile_examples = 10 - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - config.order = :random - # Seed global randomization in this process using the `--seed` CLI option. # Setting this allows you to use `--seed` to deterministically reproduce # test failures related to randomization by passing the same `--seed` value @@ -100,3 +109,15 @@ Kernel.srand config.seed =end end + +# Redirects stdout to /dev/null. +def silence_stdout + @orig_stdout = $stdout + $stdout = File.new('/dev/null', 'w') +end + +# Replace stdout so anything else is output correctly. +def enable_stdout + $stdout = @orig_stdout + @orig_stdout = nil +end diff --git a/spec/support/load_fixture.rb b/spec/support/load_fixture.rb new file mode 100644 index 0000000..b38fd33 --- /dev/null +++ b/spec/support/load_fixture.rb @@ -0,0 +1,5 @@ +module LoadFixtureHelper + def load_fixture(name) + File.read(File.expand_path("../fixtures/#{name}", __dir__)) + end +end