From dfdf7f9868573e03b464497479e36d94d2e39aea Mon Sep 17 00:00:00 2001 From: Philip Jones <53430719+pjonesIDBS@users.noreply.github.com> Date: Thu, 27 Feb 2020 14:51:24 +0000 Subject: [PATCH] Fixes version check for virtualenv_version This should fix issue #534 I've tested locally with success. I've tested against Virtualenv 20.0.4 and 16.7.9. The output from the fact is only the version number as was previously the case. --- lib/facter/virtualenv_version.rb | 2 +- spec/unit/facter/virtualenv_version_spec.rb | 31 +++++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/facter/virtualenv_version.rb b/lib/facter/virtualenv_version.rb index cf717b91..5cf2e019 100644 --- a/lib/facter/virtualenv_version.rb +++ b/lib/facter/virtualenv_version.rb @@ -3,7 +3,7 @@ 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*).*$})[0] + Facter::Util::Resolution.exec('virtualenv --version 2>&1').match(%r{(\d+\.\d+\.?\d*).*$})[1] end end end diff --git a/spec/unit/facter/virtualenv_version_spec.rb b/spec/unit/facter/virtualenv_version_spec.rb index 1d625859..3774991f 100644 --- a/spec/unit/facter/virtualenv_version_spec.rb +++ b/spec/unit/facter/virtualenv_version_spec.rb @@ -5,18 +5,24 @@ Facter.clear end - let(:virtualenv_version_output) do + let(:virtualenv_old_version_output) do <<-EOS 12.0.7 EOS end - describe 'virtualenv_version' do + let(:virtualenv_new_version_output) do + <<-EOS +virtualenv 20.0.17 from /opt/python/lib/python3.5/site-packages/virtualenv/__init__.py +EOS + end + + describe 'virtualenv_version old' do context 'returns virtualenv version when virtualenv present' do it do Facter::Util::Resolution.stubs(:exec) Facter::Util::Resolution.expects(:which).with('virtualenv').returns(true) - Facter::Util::Resolution.expects(:exec).with('virtualenv --version 2>&1').returns(virtualenv_version_output) + Facter::Util::Resolution.expects(:exec).with('virtualenv --version 2>&1').returns(virtualenv_old_version_output) expect(Facter.value(:virtualenv_version)).to eq('12.0.7') end end @@ -29,4 +35,23 @@ end end end + + describe 'virtualenv_version new' do + context 'returns virtualenv version when virtualenv present' do + it do + Facter::Util::Resolution.stubs(:exec) + Facter::Util::Resolution.expects(:which).with('virtualenv').returns(true) + Facter::Util::Resolution.expects(:exec).with('virtualenv --version 2>&1').returns(virtualenv_new_version_output) + expect(Facter.value(:virtualenv_version)).to eq('20.0.17') + end + end + + context 'returns nil when virtualenv not present' do + it do + Facter::Util::Resolution.stubs(:exec) + Facter::Util::Resolution.expects(:which).with('virtualenv').returns(false) + expect(Facter.value(:virtualenv_version)).to eq(nil) + end + end + end end