Browse files

added specs for pom_magic and cli

  • Loading branch information...
1 parent 62def8d commit cc6325517389900e7c173f4b0ec40477c73b57c9 @mkristian committed Aug 12, 2012
View
1 .gitignore
@@ -7,3 +7,4 @@ bin/mvn*
lib/*jar
lib/ext
bin/m2*
+.pom.xml
View
2 bin/rmvn
@@ -8,4 +8,4 @@ magic = Maven::Ruby::PomMagic.new
args = ARGV.dup
ARGV.clear # clean up in case another script gets executed it gets clear ARGV
-mvn.exec(magic.generate_pom(*args))
+mvn.exec(*args)
View
1 it/project/Gemfile
@@ -0,0 +1 @@
+source :rubygems
View
2 it/project/Gemfile2
@@ -0,0 +1,2 @@
+source :rubygems
+gemspec
View
10 it/project/minimal.gemspec
@@ -0,0 +1,10 @@
+Gem::Specification.new do |s|
+ s.name = 'minimal'
+ s.version = 0
+
+ s.authors = ["nobody"]
+ s.description = 'some project'
+ s.email = ["nobody@example.com"]
+
+end
+
View
10 it/project_no_gemfile/minimal.gemspec
@@ -0,0 +1,10 @@
+Gem::Specification.new do |s|
+ s.name = 'minimal'
+ s.version = 0
+
+ s.authors = ["nobody"]
+ s.description = 'some project'
+ s.email = ["nobody@example.com"]
+
+end
+
View
1 it/rails/Gemfile
@@ -0,0 +1 @@
+source :rubygems
View
0 it/rails/config/application.rb
No changes.
View
16 lib/ruby/maven/ruby/cli.rb
@@ -1,3 +1,4 @@
+require 'maven/ruby/maven'
module Maven
module Ruby
class Cli
@@ -104,20 +105,29 @@ def command_line(args)
end
def setup(*args)
+ args = magic_pom(args)
log(args)
command_line(args.dup.flatten)
end
+ protected
+
+ def magic_pom(*args)
+ file = PomMagic.new.generate_pom(args)
+ args += ['-f', file] if file && !(args.member?("-f") || args.member?("--file"))
+ args
+ end
+
public
def exec(*args)
- mvn = RubyMaven.new
+ mvn = Maven.new
mvn.exec(setup(args))
end
def exec_in(launchdirectory, *args)
- mvn = RubyMaven.new
- mvn.exec(launchdirectory, setup(args))
+ mvn = Maven.new
+ mvn.exec_in(launchdirectory, setup(args))
end
end
end
View
2 lib/ruby/maven/ruby/maven.rb
@@ -79,7 +79,7 @@ def property(key, value = nil)
def verbose
if @verbose.nil?
- options.delete('--verbose').to_s == 'true'
+ options.delete('-Dverbose').to_s == 'true'
else
@verbose
end
View
57 lib/ruby/maven/ruby/pom_magic.rb
@@ -1,57 +1,68 @@
require 'fileutils'
+require 'maven/tools/rails_project'
module Maven
module Ruby
class PomMagic
+ def initialize(pom = '.pom.xml')
+ @pom = pom
+ end
+
def new_rails_project
- Maven::Tools::RailsProject.new
+ ::Maven::Tools::RailsProject.new
end
- def pom_xml
- ".pom.xml"
+ def pom_xml(dir = '.')
+ File.join(dir, @pom)
end
- def generate_pom(*args)
- unless args.member?("-f") || args.member?("--file")
- gemfiles = Dir["*Gemfile"]
+ def generate_pom(dir = '.', *args)
+ if index = (args.index("-f") || args.index("--file"))
+ filename = args[index + 1]
+ if filename =~ /.gemspec$/
+ proj = ::Maven::Tools::GemProject.new
+ proj.load_gemspec(filename)
+ elsif filename =~ /Gemfile/
+ proj = ::Maven::Tools::GemProject.new
+ proj.load_gemfile(filename)
+ end
+ else
+ gemfiles = Dir[File.join(dir, "*Gemfile")]
gemfiles.delete_if {|g| g =~ /.pom/}
if gemfiles.size > 0
proj =
- if File.exists? File.join( 'config', 'application.rb' )
+ if File.exists? File.join( dir, 'config', 'application.rb' )
new_rails_project
else
- Maven::Tools::GemProject.new
+ ::Maven::Tools::GemProject.new
end
filename = gemfiles[0]
proj.load_gemfile(filename)
else
- gemspecs = Dir["*.gemspec"]
+ gemspecs = Dir[File.join(dir, "*.gemspec")]
gemspecs.delete_if {|g| g =~ /.pom/}
if gemspecs.size > 0
- proj = Maven::Tools::GemProject.new
+ proj = ::Maven::Tools::GemProject.new
filename = gemspecs[0]
proj.load_gemspec(filename)
end
end
- if proc
- proj.load_jarfile(File.join(File.dirname(filename), 'Jarfile'))
- proj.load_gemfile(File.join(File.dirname(filename), 'Mavenfile'))
- proj.add_defaults
- File.open(pom_xml, 'w') do |f|
- f.puts proj.to_xml
- end
- args << '-f'
- args << pom_xml
+ end
+ if proj
+ proj.load_jarfile(File.join(File.dirname(filename), 'Jarfile'))
+ proj.load_gemfile(File.join(File.dirname(filename), 'Mavenfile'))
+ proj.add_defaults
+ File.open(pom_xml(dir), 'w') do |f|
+ f.puts proj.to_xml
end
+ pom_xml(dir)
end
- args
end
- def dump_pom(force = false, file = 'pom.xml')
+ def dump_pom(dir = '.', force = false, file = 'pom.xml')
if force || !File.exists?(file)
- generate_pom
- FileUtils.cp(pom_xml, file)
+ FileUtils.cp(generate_pom(dir), file)
end
end
end
View
27 spec/cli_spec.rb
@@ -0,0 +1,27 @@
+require 'maven/ruby/cli'
+require 'stringio'
+
+describe Maven::Ruby::Cli do
+
+ subject { Maven::Ruby::Cli.new }
+ let(:log){ File.expand_path(File.join('target', 'output.log')) }
+ let(:out){ StringIO.new }
+
+ it 'should launch maven with pom.xml' do
+ subject.exec('-l', log, 'validate')
+ File.read(log).must_match /BUILD SUCCESS/
+ File.read(log).must_match /Building ruby wrapper/
+ end
+
+ it 'should launch maven without pom.xml' do
+ subject.exec_in('it', '-l', log)
+ File.read(log).must_match /BUILD FAILURE/
+ File.read(log).wont_match /Building /
+ end
+
+ it 'should launch maven within rails application' do
+ subject.exec_in('it/rails', '-l', log)
+ File.read(log).must_match /BUILD FAILURE/
+ File.read(log).wont_match /Building /
+ end
+end
View
61 spec/pom_magic_spec.rb
@@ -0,0 +1,61 @@
+require 'maven/ruby/pom_magic'
+require 'fileutils'
+
+describe Maven::Ruby::PomMagic do
+
+ describe 'given name' do
+
+ subject { Maven::Ruby::PomMagic.new('_pom.xml') }
+
+ it 'should generate project specific pom.xml with given name' do
+ pom = 'it/project/_pom.xml'
+ subject.generate_pom(File.join('it', 'project')).must_equal pom
+ File.read(pom).must_match /<name><..CDATA.ruby-maven - gem..><.name>/
+ end
+
+ end
+
+ subject { Maven::Ruby::PomMagic.new }
+
+ it 'should skip generation of pom.xml' do
+ subject.generate_pom(File.join('it', 'rails'),
+ '-f',
+ 'pom.xml').must_be_nil
+ subject.generate_pom(File.join('it', 'rails'),
+ '--file',
+ 'pom.xml').must_be_nil
+ end
+
+ it 'should generate rails specific pom.xml' do
+ pom = 'it/rails/.pom.xml'
+ subject.generate_pom(File.join('it', 'rails')).must_equal pom
+ File.read(pom).must_match /<name><..CDATA.ruby-maven - rails application..><.name>/
+ end
+
+ it 'should generate project specific pom.xml' do
+ pom = 'it/project/.pom.xml'
+ subject.generate_pom(File.join('it', 'project')).must_equal pom
+ File.read(pom).must_match /<name><..CDATA.ruby-maven - gem..><.name>/
+ end
+
+ it 'should generate project specific pom.xml using gemspec' do
+ pom = 'it/project/.pom.xml'
+ subject.generate_pom(File.join('it', 'project'),
+ '-f',
+ 'it/project/minimal.gemspec').must_equal pom
+ File.read(pom).must_match /<name><..CDATA.minimal - gem..><.name>/
+ end
+
+ it 'should generate project specific pom.xml using Gemfile with gemspec' do
+ pom = 'it/project/.pom.xml'
+ subject.generate_pom(File.join('it', 'project'),
+ '-f',
+ 'it/project/Gemfile2').must_equal pom
+ File.read(pom).must_match /<name><..CDATA.minimal - gem..><.name>/
+ end
+
+ it 'should generate project specific pom.xml without Gemfile' do
+ subject.generate_pom(File.join('it', 'project_no_gemfile')).must_equal 'it/project_no_gemfile/.pom.xml'
+ end
+
+end

0 comments on commit cc63255

Please sign in to comment.