Skip to content

Commit

Permalink
(PUP-1691) (WIP) chocolateyversion fact
Browse files Browse the repository at this point in the history
  • Loading branch information
ferventcoder committed Sep 8, 2015
1 parent 72e67c3 commit 125491f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
20 changes: 20 additions & 0 deletions lib/facter/chocolateyversion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'facter/util/resolution'

Facter.add('chocolateyversion') do
setcode do
value = nil

choco_path = "#{Facter.value(:choco_install_path)}\\bin\\choco.exe"
if Puppet::Util::Platform.windows? && File.exist?(choco_path)
begin
old_choco_message = 'Please run chocolatey /? or chocolatey help - chocolatey v'
#Facter::Core::Execution.exec is 2.0.1 forward
value = Facter::Util::Resolution.exec("#{choco_path} -v").gsub(old_choco_message,'').strip
rescue StandardError => e
value = '0'
end
end

value || '0'
end
end
24 changes: 11 additions & 13 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
class chocolatey::config {
assert_private()

#todo: check choco version from custom choco_version fact
#if versioncmp("#{::choco_version}", '0.9.9.0') >= 0 {
#
$_choco_exe_path = "${chocolatey::choco_install_location}\\bin\\choco.exe"
if versioncmp($::chocolateyversion, '0.9.9.0') >= 0 {
$_choco_exe_path = "${chocolatey::choco_install_location}\\bin\\choco.exe"

$_enable_autouninstaller = $chocolatey::enable_autouninstaller ? {
false => 'disable',
default => 'enable'
}
$_enable_autouninstaller = $chocolatey::enable_autouninstaller ? {
false => 'disable',
default => 'enable'
}

exec { "chocolatey_autouninstaller_${_enable_autouninstaller}":
path => $::path,
command => "${_choco_exe_path} feature -r ${_enable_autouninstaller} -n autoUninstaller",
unless => "${_choco_exe_path} feature list -r | findstr /X /I /C:'autoUninstaller - [${_enable_autouninstaller}d]'",
exec { "chocolatey_autouninstaller_${_enable_autouninstaller}":
path => $::path,
command => "${_choco_exe_path} feature -r ${_enable_autouninstaller} -n autoUninstaller",
unless => "${_choco_exe_path} feature list -r | findstr /X /I /C:'autoUninstaller - [${_enable_autouninstaller}d]'",
}
}
#}
}
2 changes: 1 addition & 1 deletion spec/classes/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

let(:facts) {
{
:choco_version => '0.9.9.8',
:chocolateyversion => '0.9.9.8',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe 'chocolatey' do
let(:facts) {
{
:choco_version => '0.9.9.8',
:chocolateyversion => '0.9.9.8',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

let(:facts) {
{
:choco_version => '0.9.9.8',
:chocolateyversion => '0.9.9.8',
:choco_install_path => 'C:\ProgramData\chocolatey',
}
}
Expand Down
38 changes: 38 additions & 0 deletions spec/unit/facter/chocolateyversion_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'facter'
require 'rspec/its'

describe 'chocolateyversion fact' do
subject(:fact) { Facter.fact(:chocolateyversion) }

context 'on Windows', :if => Puppet::Util::Platform.windows? do
it "should return the value from running choco -v" do
expected_value = '1.2.3'
Facter::Util::Resolution.expects(:exec).returns(expected_value)

subject.value.must == expected_value
end

it "should handle cleaning up spaces" do
expected_value = '1.2.3'
Facter::Util::Resolution.expects(:exec).returns(' ' + expected_value + ' ')

subject.value.must == expected_value
end

it "should handle older versions of choco" do
expected_value = '1.2.3'
Facter::Util::Resolution.expects(:exec).returns('Please run chocolatey /? or chocolatey help - chocolatey v' + expected_value)

subject.value.must == expected_value
end
end

context 'on Linux', :if => Puppet.features.posix? do
its(:value) { should eql('0') }
end

after :each do
Facter.clear
Facter.clear_messages
end
end

0 comments on commit 125491f

Please sign in to comment.