Error message to detect unspecified transitive dependencies. This seem to be a probl...
[buildr.git] / spec / groovy / compiler_spec.rb
blobe61ccdd7c9642e52e2a6a38e1591eb2f38d69701
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with this
3 # work for additional information regarding copyright ownership.  The ASF
4 # licenses this file to you under the Apache License, Version 2.0 (the
5 # "License"); you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 #    http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13 # License for the specific language governing permissions and limitations under
14 # the License.
17 require File.join(File.dirname(__FILE__), '../spec_helpers')
19 describe 'groovyc compiler' do 
20   
21   it 'should identify itself from groovy source directories' do
22     write 'src/main/groovy/some/Hello.groovy', 'println "Hello Groovy"'
23     write 'src/test/groovy/some/Hello.groovy', 'println "Hello Groovy"'
24     define('foo') do 
25       compile.compiler.should eql(:groovyc)
26       test.compile.compiler.should eql(:groovyc)
27     end
28   end
30   it 'should identify if groovy sources are found on java directories' do
31     write 'src/main/java/some/Hello.groovy', 'println "Hello Groovy"'
32     write 'src/test/java/some/Hello.groovy', 'println "Hello Groovy"'
33     define('foo') do 
34       compile.compiler.should eql(:groovyc)
35       test.compile.compiler.should eql(:groovyc)
36     end
37   end
39   it 'should identify itself even if groovy and java sources are found' do
40     write 'src/main/java/some/Empty.java', 'package some; public interface Empty {}'
41     write 'src/main/groovy/some/Hello.groovy', 'println "Hello Groovy"'
42     write 'src/test/java/some/Empty.java', 'package some; public interface Empty {}'
43     write 'src/test/groovy/some/Hello.groovy', 'println "Hello Groovy"'
44     define('foo') do 
45       compile.compiler.should eql(:groovyc)
46       test.compile.compiler.should eql(:groovyc)
47     end
48   end
50   it 'should identify from custom layout' do 
51     write 'groovy/Hello.groovy', 'println "Hello world"'
52     write 'testing/Hello.groovy', 'println "Hello world"'
53     custom = Layout.new
54     custom[:source, :main, :groovy] = 'groovy'
55     custom[:source, :test, :groovy] = 'testing'
56     define 'foo', :layout=>custom do
57       compile.compiler.should eql(:groovyc)
58       test.compile.compiler.should eql(:groovyc)
59     end
60   end
61   
62   it 'should identify from compile source directories' do
63     write 'src/com/example/Code.groovy', 'println "monkey code"' 
64     write 'testing/com/example/Test.groovy', 'println "some test"' 
65     define 'foo' do
66       lambda { compile.from 'src' }.should change { compile.compiler }.to(:groovyc)
67       lambda { test.compile.from 'testing' }.should change { test.compile.compiler }.to(:groovyc)
68     end
69   end
71   it 'should report the multi-language as :groovy, :java' do
72     define('foo').compile.using(:groovyc).language.should == :groovy
73   end
75   it 'should set the target directory to target/classes' do
76     define 'foo' do
77       lambda { compile.using(:groovyc) }.should change { compile.target.to_s }.to(File.expand_path('target/classes'))
78     end
79   end
81   it 'should not override existing target directory' do
82     define 'foo' do
83       compile.into('classes')
84       lambda { compile.using(:groovyc) }.should_not change { compile.target }
85     end
86   end
88   it 'should not change existing list of sources' do
89     define 'foo' do
90       compile.from('sources')
91       lambda { compile.using(:groovyc) }.should_not change { compile.sources }
92     end
93   end
94   
95   it 'should compile groovy sources' do
96     write 'src/main/groovy/some/Example.groovy', 'package some; class Example { static main(args) { println "Hello" } }'
97     define('foo').compile.invoke
98     file('target/classes/some/Example.class').should exist
99   end
101   it 'should include as classpath dependency' do
102     write 'src/bar/groovy/some/Foo.groovy', 'package some; interface Foo {}'
103     write 'src/main/groovy/some/Example.groovy', 'package some; class Example implements Foo { }'
104     define('bar', :version => '1.0') do 
105       compile.from('src/bar/groovy').into('target/bar')
106       package(:jar)
107     end
108     lambda { define('foo').compile.with(project('bar').package(:jar)).invoke }.should run_task('foo:compile')
109     file('target/classes/some/Example.class').should exist
110   end
111      
112   it 'should cross compile java sources' do 
113     write 'src/main/java/some/Foo.java', 'package some; public interface Foo { public void hello(); }'
114     write 'src/main/java/some/Baz.java', 'package some; public class Baz extends Bar { }'
115     write 'src/main/groovy/some/Bar.groovy', 'package some; class Bar implements Foo { def void hello() { } }'
116     define('foo').compile.invoke
117     %w{Foo Bar Baz}.each { |f| file("target/classes/some/#{f}.class").should exist }
118   end
120   it 'should cross compile test java sources' do 
121     write 'src/test/java/some/Foo.java', 'package some; public interface Foo { public void hello(); }'
122     write 'src/test/java/some/Baz.java', 'package some; public class Baz extends Bar { }'
123     write 'src/test/groovy/some/Bar.groovy', 'package some; class Bar implements Foo { def void hello() { } }'
124     define('foo').test.compile.invoke
125     %w{Foo Bar Baz}.each { |f| file("target/test/classes/some/#{f}.class").should exist }
126   end
128   it 'should package classes into a jar file' do
129     write 'src/main/groovy/some/Example.groovy', 'package some; class Example { }'
130     define('foo', :version => '1.0').package.invoke
131     file('target/foo-1.0.jar').should exist
132     Zip::ZipFile.open(project('foo').package(:jar).to_s) do |jar|
133       jar.file.exist?('some/Example.class').should be_true
134     end
135   end
139 describe 'groovyc compiler options' do
140   
141   def groovyc(&prc)
142     define('foo') do
143       compile.using(:groovyc)
144       @compiler = compile.instance_eval { @compiler }
145       class << @compiler
146         public :groovyc_options, :javac_options
147       end
148       if block_given?
149         instance_eval(&prc)
150       else
151         return compile
152       end
153     end
154   end
155   
156   it 'should set warning option to false by default' do 
157     groovyc do
158       compile.options.warnings.should be_false
159       @compiler.javac_options[:nowarn].should be_true
160     end
161   end
163   it 'should set warning option to true when running with --verbose option' do
164     verbose true
165     groovyc do
166       compile.options.warnings.should be_true
167       @compiler.javac_options[:nowarn].should be_false
168     end
169   end
171   it 'should not set verbose option by default' do
172     groovyc.options.verbose.should be_false
173   end
175   it 'should set verbose option when running with --trace option' do
176     trace true
177     groovyc.options.verbose.should be_true
178   end
179   
180   it 'should set debug option to false based on Buildr.options' do
181     Buildr.options.debug = false
182     groovyc.options.debug.should be_false
183   end
185   it 'should set debug option to false based on debug environment variable' do
186     ENV['debug'] = 'no'
187     groovyc.options.debug.should be_false
188   end
190   it 'should set debug option to false based on DEBUG environment variable' do
191     ENV['DEBUG'] = 'no'
192     groovyc.options.debug.should be_false
193   end
195   it 'should set deprecation option to false by default' do
196     groovyc.options.deprecation.should be_false
197   end
199   it 'should use deprecation argument when deprecation is true' do
200     groovyc do
201       compile.using(:deprecation=>true)
202       compile.options.deprecation.should be_true
203       @compiler.javac_options[:deprecation].should be_true
204     end
205   end
207   it 'should not use deprecation argument when deprecation is false' do
208     groovyc do
209       compile.using(:deprecation=>false)
210       compile.options.deprecation.should be_false
211       @compiler.javac_options[:deprecation].should_not be_true
212     end
213   end
215   it 'should set optimise option to false by default' do
216     groovyc.options.optimise.should be_false
217   end
219   it 'should use optimize argument when deprecation is true' do
220     groovyc do
221       compile.using(:optimise=>true)
222       @compiler.javac_options[:optimize].should be_true
223     end
224   end
226   it 'should not use optimize argument when deprecation is false' do
227     groovyc do
228       compile.using(:optimise=>false)
229       @compiler.javac_options[:optimize].should be_false
230     end
231   end
233   after do
234     Buildr.options.debug = nil
235     ENV.delete "debug"
236     ENV.delete "DEBUG"
237   end