@@ -62,20 +62,15 @@ def mock_build_message(name, version)
6262 mock_confirm_message message
6363 end
6464
65- def mock_checksum_message ( name , version )
66- message = "#{ name } #{ version } checksum written to checksums/#{ name } -#{ version } .gem.sha512 ."
65+ def mock_checksum_message ( name , version , extension )
66+ message = "#{ name } #{ version } checksum written to checksums/#{ name } -#{ version } .gem.#{ extension } ."
6767 mock_confirm_message message
6868 end
6969
70- def sha512_hexdigest ( path )
71- Digest ::SHA512 . file ( path ) . hexdigest
72- end
73-
7470 subject! { Bundler ::GemHelper . new ( app_path ) }
7571 let ( :app_version ) { "0.1.0" }
7672 let ( :app_gem_dir ) { app_path . join ( "pkg" ) }
7773 let ( :app_gem_path ) { app_gem_dir . join ( "#{ app_name } -#{ app_version } .gem" ) }
78- let ( :app_sha_path ) { app_path . join ( "checksums" , "#{ app_name } -#{ app_version } .gem.sha512" ) }
7974 let ( :app_gemspec_content ) { File . read ( app_gemspec_path ) }
8075
8176 before ( :each ) do
@@ -173,44 +168,52 @@ def sha512_hexdigest(path)
173168 end
174169 end
175170
176- describe "#build_checksum" do
177- it "calculates SHA512 of the content" do
178- FileUtils . mkdir_p ( app_gem_dir )
179- File . write ( app_gem_path , "" )
180- mock_checksum_message app_name , app_version
181- subject . build_checksum ( app_gem_path )
182- expect ( File . read ( app_sha_path ) . chomp ) . to eql ( Digest ::SHA512 . hexdigest ( "" ) )
183- end
184-
185- context "when build was successful" do
186- it "creates .sha512 file" do
187- mock_build_message app_name , app_version
188- mock_checksum_message app_name , app_version
189- subject . build_checksum
190- expect ( app_sha_path ) . to exist
191- expect ( File . read ( app_sha_path ) . chomp ) . to eql ( sha512_hexdigest ( app_gem_path ) )
171+ describe "#build_checksums" do
172+ %w( SHA256 SHA512 ) . each do |sha_bits |
173+ let ( :digest ) { Object . const_get ( "Digest::#{ sha_bits } " ) }
174+ let ( :hex_digest ) { digest . file ( app_gem_path ) . hexdigest }
175+ let ( :app_sha_path ) { app_path . join ( "checksums" , "#{ app_name } -#{ app_version } .gem.#{ sha_bits . downcase } " ) }
176+
177+ it "calculates #{ sha_bits } of the content" do
178+ FileUtils . mkdir_p ( app_gem_dir )
179+ File . write ( app_gem_path , "" )
180+ subject . build_checksums ( app_gem_path )
181+ expect ( File . read ( app_sha_path ) . chomp ) . to eql ( digest . hexdigest ( "" ) )
192182 end
193- end
194- context "when building in the current working directory " do
195- it "creates a .sha512 file" do
196- mock_build_message app_name , app_version
197- mock_checksum_message app_name , app_version
198- Dir . chdir app_path do
199- Bundler :: GemHelper . new . build_checksum
183+
184+ context "with messages " do
185+ before do
186+ mock_build_message app_name , app_version
187+ %w( sha256 sha512 ) . each do | extension |
188+ mock_checksum_message app_name , app_version , extension
189+ end
200190 end
201- expect ( app_sha_path ) . to exist
202- expect ( File . read ( app_sha_path ) . chomp ) . to eql ( sha512_hexdigest ( app_gem_path ) )
203- end
204- end
205- context "when building in a location relative to the current working directory" do
206- it "creates a .sha512 file" do
207- mock_build_message app_name , app_version
208- mock_checksum_message app_name , app_version
209- Dir . chdir File . dirname ( app_path ) do
210- Bundler ::GemHelper . new ( File . basename ( app_path ) ) . build_checksum
191+
192+ context "when build was successful" do
193+ it "creates .#{ sha_bits } file" do
194+ subject . build_checksums
195+ expect ( app_sha_path ) . to exist
196+ expect ( File . read ( app_sha_path ) . chomp ) . to eql ( hex_digest )
197+ end
198+ end
199+ context "when building in the current working directory" do
200+ it "creates a .#{ sha_bits } file" do
201+ Dir . chdir app_path do
202+ Bundler ::GemHelper . new . build_checksums
203+ end
204+ expect ( app_sha_path ) . to exist
205+ expect ( File . read ( app_sha_path ) . chomp ) . to eql ( hex_digest )
206+ end
207+ end
208+ context "when building in a location relative to the current working directory" do
209+ it "creates a .#{ sha_bits } file" do
210+ Dir . chdir File . dirname ( app_path ) do
211+ Bundler ::GemHelper . new ( File . basename ( app_path ) ) . build_checksums
212+ end
213+ expect ( app_sha_path ) . to exist
214+ expect ( File . read ( app_sha_path ) . chomp ) . to eql ( hex_digest )
215+ end
211216 end
212- expect ( app_sha_path ) . to exist
213- expect ( File . read ( app_sha_path ) . chomp ) . to eql ( sha512_hexdigest ( app_gem_path ) )
214217 end
215218 end
216219 end
0 commit comments