6868 end
6969 end
7070
71+ describe "cookbook_versions" do
72+ before ( :each ) do
73+ @cookbook_versions = {
74+ "apt" => "1.0.0" ,
75+ "god" => "2.0.0" ,
76+ "apache2" => "4.2.0"
77+ }
78+ end
79+
80+ it "should let you set the cookbook versions in a hash" do
81+ @environment . cookbook_versions ( @cookbook_versions ) . should == @cookbook_versions
82+ end
83+
84+ it "should return the cookbook versions" do
85+ @environment . cookbook_versions ( @cookbook_versions )
86+ @environment . cookbook_versions . should == @cookbook_versions
87+ end
88+
89+ it "should not accept anything but a hash" do
90+ lambda { @environment . cookbook_versions ( "I am a string!" ) } . should raise_error ( ArgumentError )
91+ lambda { @environment . cookbook_versions ( Array . new ) } . should raise_error ( ArgumentError )
92+ lambda { @environment . cookbook_versions ( 42 ) } . should raise_error ( ArgumentError )
93+ end
94+
95+ it "should validate the hash" do
96+ Chef ::Environment . should_receive ( :validate_cookbook_versions ) . with ( @cookbook_versions ) . and_return true
97+ @environment . cookbook_versions ( @cookbook_versions )
98+ end
99+ end
100+
101+ describe "to_hash" do
102+ before ( :each ) do
103+ @environment . name ( "spec" )
104+ @environment . description ( "Where we run the spec tests" )
105+ @environment . cookbook_versions ( { :apt => "1.2.3" } )
106+ @hash = @environment . to_hash
107+ end
108+
109+ %w{ name description cookbook_versions } . each do |t |
110+ it "should include '#{ t } '" do
111+ @hash [ t ] . should == @environment . send ( t . to_sym )
112+ end
113+ end
114+
115+ it "should include 'json_class'" do
116+ @hash [ "json_class" ] . should == "Chef::Environment"
117+ end
118+
119+ it "should include 'chef_type'" do
120+ @hash [ "chef_type" ] . should == "environment"
121+ end
122+ end
123+
71124 describe "to_json" do
72125 before ( :each ) do
73126 @environment . name ( "spec" )
74127 @environment . description ( "Where we run the spec tests" )
128+ @environment . cookbook_versions ( { :apt => "1.2.3" } )
75129 @json = @environment . to_json
76130 end
77131
78- %w{ name description } . each do |t |
132+ %w{ name description cookbook_versions } . each do |t |
79133 it "should include '#{ t } '" do
80- @json . should =~ /"#{ t } ":" #{ @environment . send ( t . to_sym ) } " /
134+ @json . should =~ /"#{ t } ":#{ Regexp . escape ( @environment . send ( t . to_sym ) . to_json ) } /
81135 end
82136 end
83137
93147 describe "from_json" do
94148 before ( :each ) do
95149 @data = {
96- :name => "production" ,
97- :description => "We are productive" ,
98- :json_class => "Chef::Environment" ,
99- :chef_type => "environment"
150+ "name" => "production" ,
151+ "description" => "We are productive" ,
152+ "cookbook_versions" => {
153+ "apt" => "1.2.3" ,
154+ "god" => "4.2.0" ,
155+ "apache2" => "2.0.0"
156+ } ,
157+ "json_class" => "Chef::Environment" ,
158+ "chef_type" => "environment"
100159 }
101160 @environment = JSON . parse ( @data . to_json )
102161 end
105164 @environment . should be_a_kind_of ( Chef ::Environment )
106165 end
107166
108- %w{ name description } . each do |t |
167+ %w{ name description cookbook_versions } . each do |t |
109168 it "should match '#{ t } '" do
110- @environment . send ( t . to_sym ) . should == @data [ t . to_sym ]
169+ @environment . send ( t . to_sym ) . should == @data [ t ]
111170 end
112171 end
113172 end
173+
174+ describe "self.validate_cookbook_versions" do
175+ before ( :each ) do
176+ @cookbook_versions = {
177+ "apt" => "1.0.0" ,
178+ "god" => "2.0.0" ,
179+ "apache2" => "4.2.0"
180+ }
181+ end
182+
183+ it "should return true when all versions are strings" do
184+ Chef ::Environment . validate_cookbook_versions ( @cookbook_versions ) . should == true
185+ end
186+
187+ it "should return false when anything other than a string is passed as a version" do
188+ a = @cookbook_versions . dup . merge :number => 2
189+ Chef ::Environment . validate_cookbook_versions ( a ) . should == false
190+
191+ b = @cookbook_versions . dup . merge :hash => { :foo => 'bar' }
192+ Chef ::Environment . validate_cookbook_versions ( b ) . should == false
193+
194+ c = @cookbook_versions . dup . merge :array => [ 'this' , 'is' , 'a' , 'bad' , 'version' ]
195+ Chef ::Environment . validate_cookbook_versions ( c ) . should == false
196+
197+ d = @cookbook_versions . dup . merge :cookbook_versions => Chef ::CookbookVersion . new ( "meta" )
198+ Chef ::Environment . validate_cookbook_versions ( d ) . should == false
199+ end
200+ end
114201end
0 commit comments