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

Fix undefined method error in facts #555

Merged
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
23 changes: 20 additions & 3 deletions lib/facter/pip_version.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
# Make pip version available as a fact

def get_pip_version(executable)
if Facter::Util::Resolution.which(executable) # rubocop:disable Style/GuardClause
results = Facter::Util::Resolution.exec("#{executable} --version 2>&1").match(%r{^pip (\d+\.\d+\.?\d*).*$})
results[1] if results
end
end

Facter.add('pip_version') do
setcode do
if Facter::Util::Resolution.which('pip')
Facter::Util::Resolution.exec('pip --version 2>&1').match(%r{^pip (\d+\.\d+\.?\d*).*$})[1]
end
get_pip_version 'pip'
end
end

Facter.add('pip2_version') do
setcode do
get_pip_version 'pip2'
end
end

Facter.add('pip3_version') do
setcode do
get_pip_version 'pip3'
end
end
3 changes: 2 additions & 1 deletion lib/facter/virtualenv_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Facter.add('virtualenv_version') do
setcode do
if Facter::Util::Resolution.which('virtualenv')
Facter::Util::Resolution.exec('virtualenv --version 2>&1').match(%r{(\d+\.\d+\.?\d*).*$})[1]
results = Facter::Util::Resolution.exec('virtualenv --version 2>&1').match(%r{(\d+\.\d+\.?\d*).*$})
results[1] if results
end
end
end
50 changes: 50 additions & 0 deletions spec/unit/facter/pip_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
EOS
end

let(:pip2_version_output) do
<<-EOS
pip 9.0.1 from /usr/lib/python2.7/dist-packages/pip (python 2.7)
EOS
end

let(:pip3_version_output) do
<<-EOS
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
EOS
end

describe 'pip_version' do
context 'returns pip version when pip present' do
it do
Expand All @@ -29,4 +41,42 @@
end
end
end

describe 'pip2_version' do
context 'returns pip2 version when pip2 present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with('pip2').returns(true)
Facter::Util::Resolution.expects(:exec).with('pip2 --version 2>&1').returns(pip2_version_output)
expect(Facter.value(:pip2_version)).to eq('9.0.1')
end
end

context 'returns nil when pip2 not present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with('pip2').returns(false)
expect(Facter.value(:pip2_version)).to eq(nil)
end
end
end

describe 'pip3_version' do
context 'returns pip3 version when pip3 present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with('pip3').returns(true)
Facter::Util::Resolution.expects(:exec).with('pip3 --version 2>&1').returns(pip3_version_output)
expect(Facter.value(:pip3_version)).to eq('18.1')
end
end

context 'returns nil when pip3 not present' do
it do
Facter::Util::Resolution.stubs(:exec)
Facter::Util::Resolution.expects(:which).with('pip3').returns(false)
expect(Facter.value(:pip3_version)).to eq(nil)
end
end
end
end