Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Commit

Permalink
Fix repo config request
Browse files Browse the repository at this point in the history
  • Loading branch information
volmer committed Dec 21, 2014
1 parent ce07802 commit 2c8f3e1
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 61 deletions.
2 changes: 1 addition & 1 deletion lib/policial/commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(repo, sha)
def file_content(filename)
contents = Octokit.contents(@repo, path: filename, ref: @sha)

if contents.try(:content)
if contents && contents.content
Base64.decode64(contents.content).force_encoding('UTF-8')
else
''
Expand Down
8 changes: 4 additions & 4 deletions lib/policial/repo_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ def initialize(commit)
@commit = commit
end

def enabled_for?(style_guide)
Policial::STYLE_GUIDES.include?(style_guide.class)
def enabled_for?(style_guide_class)
Policial::STYLE_GUIDES.include?(style_guide_class)
end

def for(style_guide)
config_file = style_guide.class.config_file
def for(style_guide_class)
config_file = style_guide_class.config_file

if config_file
load_file(config_file)
Expand Down
2 changes: 1 addition & 1 deletion lib/policial/style_guides/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(repo_config)
end

def enabled?
@repo_config.enabled_for?(self)
@repo_config.enabled_for?(self.class)
end

def violations_in_file(_file)
Expand Down
14 changes: 9 additions & 5 deletions lib/policial/style_guides/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ def config
end

def custom_config
RuboCop::Config.new(@repo_config.for(self), '').tap do |config|
config.add_missing_namespaces
config.make_excludes_absolute
custom = @repo_config.for(self.class)

if custom.is_a?(Hash)
RuboCop::Config.new(custom, '').tap do |config|
config.add_missing_namespaces
config.make_excludes_absolute
end
else
RuboCop::Config.new
end
rescue NoMethodError
RuboCop::Config.new
end

def rubocop_options
Expand Down
14 changes: 7 additions & 7 deletions spec/commit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
subject { described_class.new('volmer/cerberus', 'commitsha') }

describe '#file_content' do
let(:body) { nil }
let(:content) { '' }

before do
stub_contents_request(
stub_contents_request_with_content(
'volmer/cerberus',
sha: 'commitsha',
file: 'test.rb',
body: body
content: content
)
end

context 'when content is returned from GitHub' do
let(:body) { { content: Base64.encode64('some content') }.to_json }
let(:content) { 'some content' }

it 'returns content' do
expect(subject.file_content('test.rb')).to eq('some content')
end
end

context 'when file contains special characters' do
let(:body) { { content: Base64.encode64('€25.00') }.to_json }
let(:content) { '€25.00' }

it 'does not error when linters try writing to disk' do
tmp_file = Tempfile.new('foo', encoding: 'utf-8')
Expand All @@ -41,9 +41,9 @@
end

context 'when content is nil' do
let(:body) { { content: nil }.to_json }

it 'returns blank string' do
expect(Octokit).to receive(:contents).and_return(nil)

expect(subject.file_content('test.rb')).to eq('')
end
end
Expand Down
9 changes: 2 additions & 7 deletions spec/investigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
describe '#run' do
it 'finds all violations present in the pull request' do
stub_pull_request_files_request('volmer/cerberus', 2)
stub_contents_request(
stub_contents_request_with_fixture(
'volmer/cerberus',
sha: '498b81cd038f8a3ac02f035a8537b7ddcff38a81',
file: '.rubocop.yml',
fixture: 'config_contents.json'
)
stub_contents_request(
stub_contents_request_with_fixture(
'volmer/cerberus',
sha: '498b81cd038f8a3ac02f035a8537b7ddcff38a81',
file: 'config/unicorn.rb',
Expand All @@ -40,11 +40,6 @@
describe '#accuse' do
it 'add comments to the pull request regarding all current violations' do
stub_pull_request_files_request('volmer/cerberus', 2)
stub_contents_request(
'volmer/cerberus',
sha: '498b81cd038f8a3ac02f035a8537b7ddcff38a81',
file: 'config/unicorn.rb'
)
stub_pull_request_comments_request('volmer/cerberus', 2)
comment_request_1 = stub_comment_request(
'violation1',
Expand Down
50 changes: 26 additions & 24 deletions spec/repo_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@

describe Policial::RepoConfig do
subject { described_class.new(commit) }
let(:commit) { double('Commit') }
let(:commit) { Policial::Commit.new('volmer/cerberus', 'commitsha') }
let(:guide) { double('guide', config_file: '.policial.yml') }

describe '#enabled_for?' do
it 'returns true for StyleGuides::Ruby' do
expect(subject).to be_enabled_for(
Policial::StyleGuides::Ruby.new(subject)
)
expect(subject).to be_enabled_for(Policial::StyleGuides::Ruby)
end
end

describe '#for' do
context 'when Ruby config file is specified' do
it 'returns parsed config' do
config_for_file('.rubocop.yml', <<-EOS.strip_heredoc)
StringLiterals:
EnforcedStyle: double_quotes
LineLength:
Max: 90
EOS

result = subject.for(Policial::StyleGuides::Ruby.new(subject))

expect(result).to eq(
'StringLiterals' => { 'EnforcedStyle' => 'double_quotes' },
'LineLength' => { 'Max' => 90 }
)
end
it 'is a Hash with the config from the file found on the repo' do
content_request_returns('DoubleQuotes: enabled')

expect(subject.for(guide)).to eq('DoubleQuotes' => 'enabled')
end

def config_for_file(file_path, content)
allow(commit).to receive(:file_content).with(file_path)
.and_return(content)
it 'is empty if style guide class does not use a config file' do
guide = double('guide', config_file: nil)

expect(subject.for(guide)).to eq({})
end

it 'is empty if the retrieved config file is invalid' do
content_request_returns('###')

expect(subject.for(guide)).to eq({})
end
end

def content_request_returns(content)
stub_contents_request_with_content(
'volmer/cerberus',
sha: 'commitsha',
file: '.policial.yml',
content: content
)
end
end
21 changes: 21 additions & 0 deletions spec/style_guides/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe Policial::StyleGuides::Base do
subject { described_class.new(repo_config) }
let(:repo_config) { Policial::RepoConfig.new('commit') }

describe '#enabled?' do
it 'asks the repo config if it is enabled or not' do
expect(subject.enabled?).to be false
end
end

describe '#violations_in_file' do
it 'raises NotImplementedError' do
expect { subject.violations_in_file('file') }
.to raise_error(
NotImplementedError, 'must implement #violations_in_file'
)
end
end
end
21 changes: 18 additions & 3 deletions spec/style_guides/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@

describe Policial::StyleGuides::Ruby do
subject do
described_class.new(
double('RepoConfig', enabled_for?: true, for: custom_config)
)
described_class.new(Policial::RepoConfig.new(
Policial::Commit.new('volmer/cerberus', 'commitsha')
))
end
let(:custom_config) { nil }

describe '#enabled?' do
it 'is true' do
expect(subject.enabled?).to be true
end
end

describe '#violations_in_file' do
before do
stub_contents_request_with_content(
'volmer/cerberus',
sha: 'commitsha',
file: '.rubocop.yml',
content: custom_config.to_yaml
)
end

it 'detects offenses to the Ruby community Style Guide' do
file = build_file('test.rb', "\"I am naughty\"\n")
violations = subject.violations_in_file(file)
Expand Down
30 changes: 21 additions & 9 deletions spec/support/helpers/github_api_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ def stub_pull_request_files_request(repo, pull_request)
)
end

def stub_contents_request(repo, sha:, file:, body: nil, fixture: nil)
body ||= File.read("spec/support/fixtures/#{fixture}") if fixture

stub_request(
:get, url(repo, "/contents/#{file}", ref: sha)
).with(headers: request_headers).to_return(
status: 200,
body: body,
headers: { 'Content-Type' => 'application/json; charset=utf-8' }
def stub_contents_request_with_content(repo, sha:, file:, content:)
body = JSON.generate(
content: Base64.encode64(content)
)

stub_contents_request(repo, sha, file, body)
end

def stub_contents_request_with_fixture(repo, sha:, file:, fixture:)
body = File.read("spec/support/fixtures/#{fixture}")

stub_contents_request(repo, sha, file, body)
end

# rubocop:disable Metrics/ParameterLists
Expand Down Expand Up @@ -75,4 +77,14 @@ def url(repo, path, query = {})
query: query.to_query.presence
)
end

def stub_contents_request(repo, sha, file, body)
stub_request(
:get, url(repo, "/contents/#{file}", ref: sha)
).with(headers: request_headers).to_return(
status: 200,
body: body,
headers: { 'Content-Type' => 'application/json; charset=utf-8' }
)
end
end

0 comments on commit 2c8f3e1

Please sign in to comment.