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
17 require File.join(File.dirname(__FILE__), '../spec_helpers')
19 describe 'groovyc compiler' do
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"'
25 compile.compiler.should eql(:groovyc)
26 test.compile.compiler.should eql(:groovyc)
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"'
34 compile.compiler.should eql(:groovyc)
35 test.compile.compiler.should eql(:groovyc)
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"'
45 compile.compiler.should eql(:groovyc)
46 test.compile.compiler.should eql(:groovyc)
50 it 'should identify from custom layout' do
51 write 'groovy/Hello.groovy', 'println "Hello world"'
52 write 'testing/Hello.groovy', 'println "Hello world"'
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)
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"'
66 lambda { compile.from 'src' }.should change { compile.compiler }.to(:groovyc)
67 lambda { test.compile.from 'testing' }.should change { test.compile.compiler }.to(:groovyc)
71 it 'should report the multi-language as :groovy, :java' do
72 define('foo').compile.using(:groovyc).language.should == :groovy
75 it 'should set the target directory to target/classes' do
77 lambda { compile.using(:groovyc) }.should change { compile.target.to_s }.to(File.expand_path('target/classes'))
81 it 'should not override existing target directory' do
83 compile.into('classes')
84 lambda { compile.using(:groovyc) }.should_not change { compile.target }
88 it 'should not change existing list of sources' do
90 compile.from('sources')
91 lambda { compile.using(:groovyc) }.should_not change { compile.sources }
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
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')
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
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 }
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 }
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
139 describe 'groovyc compiler options' do
143 compile.using(:groovyc)
144 @compiler = compile.instance_eval { @compiler }
146 public :groovyc_options, :javac_options
156 it 'should set warning option to false by default' do
158 compile.options.warnings.should be_false
159 @compiler.javac_options[:nowarn].should be_true
163 it 'should set warning option to true when running with --verbose option' do
166 compile.options.warnings.should be_true
167 @compiler.javac_options[:nowarn].should be_false
171 it 'should not set verbose option by default' do
172 groovyc.options.verbose.should be_false
175 it 'should set verbose option when running with --trace option' do
177 groovyc.options.verbose.should be_true
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
185 it 'should set debug option to false based on debug environment variable' do
187 groovyc.options.debug.should be_false
190 it 'should set debug option to false based on DEBUG environment variable' do
192 groovyc.options.debug.should be_false
195 it 'should set deprecation option to false by default' do
196 groovyc.options.deprecation.should be_false
199 it 'should use deprecation argument when deprecation is true' do
201 compile.using(:deprecation=>true)
202 compile.options.deprecation.should be_true
203 @compiler.javac_options[:deprecation].should be_true
207 it 'should not use deprecation argument when deprecation is false' do
209 compile.using(:deprecation=>false)
210 compile.options.deprecation.should be_false
211 @compiler.javac_options[:deprecation].should_not be_true
215 it 'should set optimise option to false by default' do
216 groovyc.options.optimise.should be_false
219 it 'should use optimize argument when deprecation is true' do
221 compile.using(:optimise=>true)
222 @compiler.javac_options[:optimize].should be_true
226 it 'should not use optimize argument when deprecation is false' do
228 compile.using(:optimise=>false)
229 @compiler.javac_options[:optimize].should be_false
234 Buildr.options.debug = nil