From b6152a4d45fcfd6e5bbe49b0bd881e4e13849874 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 20:04:43 +0900 Subject: [PATCH 1/4] Enable to inject settings_gist_id instead of the settings --- lib/github/nippou/settings.rb | 33 +++++++----- spec/fixtures/settings-invalid.yml | 7 +++ spec/fixtures/settings-valid.yml | 7 +++ spec/github/nippou/settings_spec.rb | 79 +++++++++++++---------------- spec/spec_helper.rb | 6 +++ spec/support/load_fixture.rb | 5 ++ 6 files changed, 79 insertions(+), 58 deletions(-) create mode 100644 spec/fixtures/settings-invalid.yml create mode 100644 spec/fixtures/settings-valid.yml create mode 100644 spec/support/load_fixture.rb 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..c3cb11f 100644 --- a/spec/github/nippou/settings_spec.rb +++ b/spec/github/nippou/settings_spec.rb @@ -3,17 +3,13 @@ let(:settings) { described_class.new(client: client) } describe '#format' do - context 'given valid settings' do - let(:settings_format) do - { - format: { - subject: '### %{subject}', - line: '* [%{title}](%{url}) by %{user} %{status}', - }, - } - end + before do + ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] = '12345' + allow(client).to receive(:gist).and_return( files: { 'settings.yml': { content: settings_yaml } } ) + end - before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format.to_yaml } + context 'given valid settings' do + 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 @@ -42,19 +30,13 @@ end describe '#dictionary' do - context 'given valid settings' do - let(:settings_dictionary) do - { - dictionary: { - status: { - merged: '**merged!**', - closed: '**closed!**', - }, - }, - } - end + before do + ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] = '12345' + allow(client).to receive(:gist).and_return( files: { 'settings.yml': { content: settings_yaml } } ) + end - before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_dictionary.to_yaml } + context 'given valid settings' do + let(:settings_yaml) { load_fixture('settings-valid.yml') } it 'is valid `status.merged`' do expect(settings.dictionary.status.merged).to eq '**merged!**' @@ -64,23 +46,24 @@ 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 do + ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] = '12345' + allow(client).to receive(:gist).and_return( files: { 'settings.yml': { content: settings_yaml } } ) + end - before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_yaml } + context 'given valid settings' do + let(:settings_yaml) { load_fixture('settings-valid.yml') } it 'is valid yaml' do expect(settings.yaml).to eq <<~VALID_YAML @@ -95,5 +78,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..471cd39 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,8 +14,14 @@ 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 + # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer. 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 From 66324a71bd9103a09242874ceabe471646c08e75 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 22:12:29 +0900 Subject: [PATCH 2/4] Silence stdout Referred to https://gist.github.com/adamstegman/926858 --- spec/spec_helper.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 471cd39..f21f3cd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,6 +22,9 @@ 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. @@ -106,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 From 713c4d7b7b6b43d60c58d030690b765f0ea74a5a Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 22:13:06 +0900 Subject: [PATCH 3/4] Enable random test --- spec/spec_helper.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f21f3cd..627992e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -55,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 @@ -96,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 From 92af40487dc001b133187175024fa3d936b32249 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 22:15:13 +0900 Subject: [PATCH 4/4] Refactor spec --- spec/github/nippou/settings_spec.rb | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/spec/github/nippou/settings_spec.rb b/spec/github/nippou/settings_spec.rb index c3cb11f..3637759 100644 --- a/spec/github/nippou/settings_spec.rb +++ b/spec/github/nippou/settings_spec.rb @@ -2,12 +2,12 @@ let(:client) { Octokit::Client.new(login: 'taro', access_token: '1234abcd') } let(:settings) { described_class.new(client: client) } - describe '#format' do - before do - ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] = '12345' - allow(client).to receive(:gist).and_return( files: { 'settings.yml': { content: settings_yaml } } ) - end + 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_yaml) { load_fixture('settings-valid.yml') } @@ -30,11 +30,6 @@ end describe '#dictionary' do - before do - ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] = '12345' - allow(client).to receive(:gist).and_return( files: { 'settings.yml': { content: settings_yaml } } ) - end - context 'given valid settings' do let(:settings_yaml) { load_fixture('settings-valid.yml') } @@ -57,11 +52,6 @@ end describe '#yaml' do - before do - ENV['GITHUB_NIPPOU_SETTINGS_GIST_ID'] = '12345' - allow(client).to receive(:gist).and_return( files: { 'settings.yml': { content: settings_yaml } } ) - end - context 'given valid settings' do let(:settings_yaml) { load_fixture('settings-valid.yml') }