Skip to content

Commit 969a165

Browse files
author
David Balatero
committed
Updated the spec and library to be more robust / correct.
1 parent 5579ad2 commit 969a165

2 files changed

Lines changed: 69 additions & 21 deletions

File tree

chef/lib/chef/provider/package/macports.rb

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,31 @@ def load_current_resource
1010
Chef::Log.debug("Current version is #{@current_resource.version}") if @current_resource.version
1111

1212
@candidate_version = macports_candidate_version
13+
14+
if !@new_resource.version and !@candidate_version
15+
raise Chef::Exceptions::Package, "Could not get a candidate version for this package -- #{@new_resource.name} does not seem to be a valid package!"
16+
end
17+
1318
Chef::Log.debug("MacPorts candidate version is #{@candidate_version}") if @candidate_version
1419

1520
@current_resource
1621
end
1722

1823
def current_installed_version
19-
command = "port installed #{@new_resource.package_name} | grep \"(active)\""
20-
output = get_line_from_command(command)
21-
22-
if output.empty?
23-
nil
24-
else
25-
match = output.match(/^.+ @([^\s]+) \(active\)$/)
26-
match[1]
24+
command = "port installed #{@new_resource.package_name}"
25+
output = get_response_from_command(command)
26+
27+
response = nil
28+
output.each_line do |line|
29+
match = line.match(/^.+ @([^\s]+) \(active\)$/)
30+
response = match[1] if match
2731
end
32+
response
2833
end
2934

3035
def macports_candidate_version
3136
command = "port info --version #{@new_resource.package_name}"
32-
output = get_line_from_command(command)
37+
output = get_response_from_command(command)
3338

3439
match = output.match(/^version: (.+)$/)
3540

@@ -38,37 +43,56 @@ def macports_candidate_version
3843

3944
def install_package(name, version)
4045
unless @current_resource.version == version
46+
command = "port install #{name}"
47+
command << " @#{version}" if version and !version.empty?
4148
run_command(
42-
:command => "port install #{name} @#{version}"
49+
:command => command
4350
)
4451
end
4552
end
4653

4754
def purge_package(name, version)
55+
command = "port uninstall #{name}"
56+
command << " @#{version}" if version and !version.empty?
4857
run_command(
49-
:command => "port uninstall #{name} @#{version}"
58+
:command => command
5059
)
5160
end
5261

5362
def remove_package(name, version)
63+
command = "port deactivate #{name}"
64+
command << " @#{version}" if version and !version.empty?
65+
5466
run_command(
55-
:command => "port deactivate #{name} @#{version}"
67+
:command => command
5668
)
5769
end
5870

5971
def upgrade_package(name, version)
60-
unless @current_resource.version == version
72+
# Saving this to a variable -- weird rSpec behavior
73+
# happens otherwise...
74+
current_version = @current_resource.version
75+
76+
if current_version.nil? or current_version.empty?
77+
# Macports doesn't like when you upgrade a package
78+
# that hasn't been installed.
79+
install_package(name, version)
80+
elsif current_version != version
6181
run_command(
6282
:command => "port upgrade #{name} @#{version}"
6383
)
6484
end
6585
end
6686

6787
private
68-
def get_line_from_command(command)
88+
def get_response_from_command(command)
6989
output = nil
7090
status = popen4(command) do |pid, stdin, stdout, stderr|
71-
output = stdout.readline.strip
91+
begin
92+
output = stdout.read
93+
rescue Exception
94+
raise Chef::Exceptions::Package, "Could not read from STDOUT on command: #{command}"
95+
end
7296
end
7397
unless status.exitstatus == 0 || status.exitstatus == 1
7498
raise Chef::Exceptions::Package, "#{command} failed - #{status.insect}!"

chef/spec/unit/provider/package/macports_spec.rb

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,32 @@
7878

7979
describe "current_installed_version" do
8080
it "should return the current version if the package is installed" do
81-
@stdout.should_receive(:readline).and_return(" openssl @0.9.8k_0 (active)\n")
81+
@stdout.should_receive(:read).and_return(<<EOF
82+
The following ports are currently installed:
83+
openssl @0.9.8k_0 (active)
84+
EOF
85+
)
86+
8287
@provider.should_receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
8388
@provider.current_installed_version.should == "0.9.8k_0"
8489
end
8590

8691
it "should return nil if a package is not currently installed" do
87-
@stdout.should_receive(:readline).and_return(" \n")
92+
@stdout.should_receive(:read).and_return(" \n")
8893
@provider.should_receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
8994
@provider.current_installed_version.should be_nil
9095
end
9196
end
9297

9398
describe "macports_candidate_version" do
9499
it "should return the latest available version of a given package" do
95-
@stdout.should_receive(:readline).and_return("version: 4.2.7\n")
100+
@stdout.should_receive(:read).and_return("version: 4.2.7\n")
96101
@provider.should_receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
97102
@provider.macports_candidate_version.should == "4.2.7"
98103
end
99104

100105
it "should return nil if there is no version for a given package" do
101-
@stdout.should_receive(:readline).and_return("Error: port fadsfadsfads not found\n")
106+
@stdout.should_receive(:read).and_return("Error: port fadsfadsfads not found\n")
102107
@provider.should_receive(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
103108
@provider.macports_candidate_version.should be_nil
104109
end
@@ -127,30 +132,49 @@
127132
@provider.should_receive(:run_command).with(:command => "port uninstall zsh @4.2.7")
128133
@provider.purge_package("zsh", "4.2.7")
129134
end
135+
136+
it "should purge the currently active version if no explicit version is passed in" do
137+
@provider.should_receive(:run_command).with(:command => "port uninstall zsh")
138+
@provider.purge_package("zsh", nil)
139+
end
130140
end
131141

132142
describe "remove_package" do
133143
it "should run the port deactivate command with the correct version" do
134144
@provider.should_receive(:run_command).with(:command => "port deactivate zsh @4.2.7")
135145
@provider.remove_package("zsh", "4.2.7")
136146
end
147+
148+
it "should remove the currently active version if no explicit version is passed in" do
149+
@provider.should_receive(:run_command).with(:command => "port deactivate zsh")
150+
@provider.remove_package("zsh", nil)
151+
end
137152
end
138153

139154
describe "upgrade_package" do
140155
it "should run the port upgrade command with the correct version" do
141-
@current_resource.should_receive(:version).and_return("4.1.6")
156+
@current_resource.should_receive(:version).at_least(:once).and_return("4.1.6")
142157
@provider.current_resource = @current_resource
158+
143159
@provider.should_receive(:run_command).with(:command => "port upgrade zsh @4.2.7")
144160

145161
@provider.upgrade_package("zsh", "4.2.7")
146162
end
147163

148164
it "should not run the port upgrade command if the version is already installed" do
149-
@current_resource.should_receive(:version).and_return("4.2.7")
165+
@current_resource.should_receive(:version).at_least(:once).and_return("4.2.7")
150166
@provider.current_resource = @current_resource
151167
@provider.should_not_receive(:run_command)
152168

153169
@provider.upgrade_package("zsh", "4.2.7")
154170
end
171+
172+
it "should call install_package if the package isn't currently installed" do
173+
@current_resource.should_receive(:version).at_least(:once).and_return(nil)
174+
@provider.current_resource = @current_resource
175+
@provider.should_receive(:install_package).and_return(true)
176+
177+
@provider.upgrade_package("zsh", "4.2.7")
178+
end
155179
end
156180
end

0 commit comments

Comments
 (0)