Skip to content

Commit bde1ad2

Browse files
author
peppyheppy
committed
fixed bug with 'defined?' being mistakenly used instad of 'const_defined?' which caused the defined to always return truthy becuase defined returned a string; irony alert: added missing test coverage
1 parent 5e9373d commit bde1ad2

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/code_climate/test_reporter/git.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def configured_git_dir
5858
end
5959

6060
def rails_git_dir_present?
61-
defined?(Rails) && !Rails.root.nil? &&
61+
const_defined?(:Rails) && Rails.respond_to?(:root) && !Rails.root.nil? &&
6262
File.directory?(File.expand_path('.git', Rails.root))
6363
end
6464
end

spec/lib/git_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,40 @@ module CodeClimate::TestReporter
2222
expect(Git).to receive(:`).once.with "git --git-dir=\"#{path}/.git\" help"
2323

2424
Git.send :git, 'help'
25+
end
26+
27+
context 'ensure logic that replies on Rails is robust in non-rails environments' do
28+
before :all do
29+
module ::Rails; end
30+
end
31+
32+
after :all do
33+
Object.send(:remove_const, :Rails)
2534
end
35+
36+
after :each do
37+
Git.send :git, 'help'
38+
end
39+
40+
it 'will check if constant Rails is defined' do
41+
expect(Git).to receive(:configured_git_dir).once.and_return(nil)
42+
end
43+
44+
it 'will not rails root if constant Rails is defined but does not respond to root' do
45+
expect(Git).to receive(:configured_git_dir).once.and_return(nil)
46+
expect(Rails).to receive(:root).twice.and_return('/path')
47+
end
48+
49+
it 'will call rails root if constant Rails is defined and root method is defined' do
50+
module ::Rails
51+
def self.root
52+
'/path'
53+
end
54+
end
55+
expect(Git).to receive(:configured_git_dir).once.and_return(nil)
56+
expect(Rails).to receive(:root).twice.and_return('/path')
57+
end
58+
end
2659
end
2760

2861
describe 'branch_from_git_or_ci' do

0 commit comments

Comments
 (0)