diff --git a/lib/capsulecd/ruby/ruby_engine.rb b/lib/capsulecd/ruby/ruby_engine.rb index 30a1ff9..eca2f00 100644 --- a/lib/capsulecd/ruby/ruby_engine.rb +++ b/lib/capsulecd/ruby/ruby_engine.rb @@ -11,10 +11,10 @@ def build_step super gemspec_path = CapsuleCD::Ruby::RubyHelper.get_gemspec_path(@source_git_local_path) - # check for/create required VERSION file - gemspec_data = CapsuleCD::Ruby::RubyHelper.load_gemspec_data(gemspec_path) + # check for required VERSION file + gemspec_data = CapsuleCD::Ruby::RubyHelper.get_gemspec_data(@source_git_local_path) - if !gemspec_data || !File.exist?(CapsuleCD::Ruby::RubyHelper.version_filepath(@source_git_local_path, gemspec_data.name)) + if !File.exist?(CapsuleCD::Ruby::RubyHelper.version_filepath(@source_git_local_path, gemspec_data.name)) fail CapsuleCD::Error::BuildPackageInvalid, 'version.rb file is required to process Ruby gem' end diff --git a/lib/capsulecd/ruby/ruby_helper.rb b/lib/capsulecd/ruby/ruby_helper.rb index d3a340e..f7ab35a 100644 --- a/lib/capsulecd/ruby/ruby_helper.rb +++ b/lib/capsulecd/ruby/ruby_helper.rb @@ -27,6 +27,9 @@ def self.get_gemspec_data(repo_path) self.load_gemspec_data(self.get_gemspec_path(repo_path)) end + ################################################################################################################## + # protected/private methods. + # since the Gem::Specification class is basically eval'ing the gemspec file, and the gemspec file is doing a require # to load the version.rb file, the version.rb file is cached in memory. We're going to try to get around that issue # by parsing the gemspec file in a forked process. @@ -49,9 +52,13 @@ def self.execute_in_child end def self.load_gemspec_data(gemspec_path) - return self.execute_in_child do + gemspec_data = self.execute_in_child do Gem::Specification::load(gemspec_path) end + if !gemspec_data + fail CapsuleCD::Error::BuildPackageInvalid, '*.gemspec could not be parsed.' + end + return gemspec_data end diff --git a/spec/lib/capsulecd/ruby/ruby_helper_spec.rb b/spec/lib/capsulecd/ruby/ruby_helper_spec.rb new file mode 100644 index 0000000..1eee3d8 --- /dev/null +++ b/spec/lib/capsulecd/ruby/ruby_helper_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe 'CapsuleCD::Ruby::RubyHelper', :ruby do + subject{ + CapsuleCD::Ruby::RubyHelper + } + describe '#version_filepath' do + it 'default should generate the correct path to version.rb' do + expect(subject.version_filepath('/tmp','capsulecd')).to eql('/tmp/lib/capsulecd/version.rb') + end + it 'with custom version filename should generate the correct path' do + expect(subject.version_filepath('/tmp','capsulecd', 'VERSION.rb')).to eql('/tmp/lib/capsulecd/VERSION.rb') + end + end + + describe '#get_gemspec_path' do + describe 'without a gemspec file' do + it 'should raise an error' do + expect{subject.get_gemspec_path(test_directory)}.to raise_error(CapsuleCD::Error::BuildPackageInvalid) + end + end + + describe 'with a valid gemspec file' do + it 'should generate correct gemspec path' do + FileUtils.copy_entry('spec/fixtures/ruby/gem_analogj_test', test_directory) + + expect(subject.get_gemspec_path(test_directory)).to eql(test_directory + '/gem_analogj_test.gemspec') + end + end + end + + describe '#get_gemspec_data' do + it 'should parse gemspec data' do + FileUtils.copy_entry('spec/fixtures/ruby/gem_analogj_test', test_directory) + gemspec_data = subject.get_gemspec_data(test_directory) + expect(gemspec_data.name).to eql('gem_analogj_test') + expect(gemspec_data.version.to_s).to eql('0.1.3') + end + + describe 'with an invalid gemspec file' do + it 'should raise an error' do + FileUtils.copy_entry('spec/fixtures/ruby/gem_analogj_test', test_directory) + FileUtils.rm(test_directory + '/lib/gem_analogj_test/version.rb') + + expect{subject.get_gemspec_data(test_directory)}.to raise_error(CapsuleCD::Error::BuildPackageInvalid) + end + end + + end + +end