Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to inject settings_gist_id instead of the settings #63

Merged
merged 4 commits into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions lib/github/nippou/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/settings-invalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
format:
**!!invalid!!**
line: '* [%{title}](%{url}) by %{user} %{status}'
dictionary:
status:
merged: '**merged!**'
closed: '**closed!**'
7 changes: 7 additions & 0 deletions spec/fixtures/settings-valid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
format:
subject: '### %{subject}'
line: '* [%{title}](%{url}) by %{user} %{status}'
dictionary:
status:
merged: '**merged!**'
closed: '**closed!**'
69 changes: 25 additions & 44 deletions spec/github/nippou/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } } )
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebMock gem だとどうしてもうまくいかなかったので、この辺で妥協しておく。

WebMock.stub_request に JSON(つまり String)を渡すと、Octokit::Client#gist が Hash ライクなオブジェクト(Sawyer::Resource)ではなく、JSON を返してしまう。

https://stackoverflow.com/questions/37621783/how-to-correctly-replicate-response-body-of-an-octokit-requests-response-for-we

WebMock::Response::InvalidBody: must be one of: [Proc, IO, Pathname, String, Array]. 'Hash' given

ならばと Hash を渡すと上記のエラーに遭遇。

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}'
Expand All @@ -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
Expand All @@ -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!**'
Expand All @@ -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
Expand All @@ -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
33 changes: 27 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Github::Nippou::Settings#data で puts しており、rspec の結果が Noisy になるため。YAML syntax error は発生しやすいと思うので、親切に教えてあげたい。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いわゆる printf デバッグできないのでリバートしました...。→ 7a75ef5


# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -87,16 +102,22 @@
# 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
# as the one that triggered the failure.
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
5 changes: 5 additions & 0 deletions spec/support/load_fixture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module LoadFixtureHelper
def load_fixture(name)
File.read(File.expand_path("../fixtures/#{name}", __dir__))
end
end