Skip to content

Commit b17ef68

Browse files
authored
Merge pull request #6 from zev/move-jruby-ant-specs
Move JRuby's Ant integration specs here as minitest tests
2 parents 9e99fc8 + 061cedf commit b17ef68

8 files changed

Lines changed: 554 additions & 7 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
ruby: ['jruby', 'jruby-head']
17+
fail-fast: false
18+
19+
name: rake test (${{ matrix.ruby }})
20+
21+
steps:
22+
- name: checkout
23+
uses: actions/checkout@v6
24+
- name: set up java 21
25+
uses: actions/setup-java@v5
26+
with:
27+
distribution: 'temurin'
28+
java-version: 21
29+
- name: set up ant
30+
uses: cedx/SetupAnt@v7
31+
- name: set up ruby
32+
uses: ruby/setup-ruby@v1
33+
with:
34+
ruby-version: ${{ matrix.ruby }}
35+
bundler-cache: true
36+
- name: rake test
37+
run: bundle exec rake test

‎rake-ant.gemspec‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
1717
spec.files = Dir["lib/**/*", "LICENSE", "README.md"]
1818
spec.require_paths = ["lib"]
1919

20-
spec.add_development_dependency "bundler", "~> 2.1"
20+
spec.add_development_dependency "bundler", ">= 2.1"
2121
spec.add_development_dependency "rake", "~> 13.0"
22+
spec.add_development_dependency "minitest", ">= 5.0"
23+
spec.add_development_dependency "minitest-mock"
2224
end

‎test/ant_test_helper.rb‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
require 'minitest/autorun'
2+
require 'minitest/mock'
3+
require 'rake'
4+
require 'ant'
5+
require 'tmpdir'
6+
7+
class Rake::Ant
8+
module TestHelper
9+
def after_teardown
10+
super
11+
Ant.instance_eval do
12+
instance_variable_set "@ant", nil
13+
end
14+
end
15+
16+
# Allows matching task structure with a nested hash as follows.
17+
#
18+
# { :_name => "jar", :destfile => "spec-test.jar", :compress => "true", :index => "true",
19+
# :_children => [
20+
# { :_name => "fileset", :dir => "build" }
21+
# ]}
22+
#
23+
# would match the following:
24+
#
25+
# jar :destfile => "spec-test.jar", :compress => "true", :index => "true" do
26+
# fileset :dir => "build"
27+
# end
28+
#
29+
class TaskStructureMatcher
30+
def initialize(hash, configure = false)
31+
@expected = hash
32+
@configure = configure
33+
end
34+
35+
def matches?(actual)
36+
@actual = actual
37+
result = true
38+
tasks = actual.defined_tasks
39+
if Hash === @expected && tasks.length != 1 || tasks.length != @expected.length
40+
@message = "task list length different"
41+
return false
42+
end
43+
@expected.each_with_index do |h,i|
44+
tasks[i].maybe_configure if @configure
45+
result &&= match_wrapper(h, tasks[i].wrapper)
46+
end
47+
result
48+
end
49+
50+
def match_wrapper(hash, wrapper, name = nil)
51+
hname = hash[:_name] ? hash[:_name] : '<?>'
52+
if name
53+
name = "#{name} => #{hname}"
54+
else
55+
name = hname
56+
end
57+
58+
# name
59+
if hash[:_name] && hash[:_name] != wrapper.element_tag
60+
@message = "name different: expected #{hash[:_name].inspect} but was #{wrapper.element_tag.inspect}"
61+
return false
62+
end
63+
64+
# proxy class name
65+
if hash[:_type] && (wrapper.proxy.nil? || hash[:_type] != wrapper.proxy.java_class.name)
66+
@message = "type different: expected #{hash[:_type]} but was #{wrapper.proxy.java_class.name}"
67+
return false
68+
end
69+
70+
# attributes
71+
hash.keys.select {|k| k.to_s !~ /^_/}.each do |k|
72+
unless wrapper.attribute_map.containsKey(k.to_s)
73+
@message = "'#{name} => #{k}' attribute missing"
74+
return false
75+
end
76+
77+
if hash[k] != wrapper.attribute_map[k.to_s]
78+
@message = "'#{name} => #{k}' attribute different: expected #{hash[k].inspect} but was #{wrapper.attribute_map[k.to_s].inspect}"
79+
return false
80+
end
81+
end
82+
83+
# children, recursively
84+
children = wrapper.children.to_a
85+
(hash[:_children] || []).each_with_index do |h,i|
86+
return false unless match_wrapper(h, children[i], name)
87+
end
88+
true
89+
end
90+
91+
def failure_message
92+
require 'pp'
93+
"expected #{@actual.name} to have structure:\n#{@expected.pretty_inspect}\n#{@message}"
94+
end
95+
end
96+
97+
def assert_structure(target, hash)
98+
matcher = TaskStructureMatcher.new(hash)
99+
assert matcher.matches?(target), -> { matcher.failure_message }
100+
end
101+
102+
def assert_configured_structure(target, hash)
103+
matcher = TaskStructureMatcher.new(hash, true)
104+
assert matcher.matches?(target), -> { matcher.failure_message }
105+
end
106+
107+
def example_ant(options = {}, &block)
108+
Ant.new({:basedir => Dir::tmpdir, :run => false, :output_level => 0}.merge(options),&block)
109+
end
110+
111+
def with_hidden_ant_path
112+
original_env_path = ENV['PATH']
113+
hidden_path = ENV['PATH'].split(File::PATH_SEPARATOR).reject { |dir|
114+
File.executable?(File.join(dir, 'ant'))
115+
}.join(File::PATH_SEPARATOR)
116+
ENV['PATH'] = hidden_path
117+
118+
yield
119+
ensure
120+
ENV['PATH'] = original_env_path
121+
end
122+
end
123+
end

‎test/test_ant.rb‎

Lines changed: 151 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,159 @@
1-
require 'test/unit'
2-
require 'rubygems'
3-
require 'rake'
4-
require 'rake/task'
5-
require 'rake/ant'
1+
require 'ant_test_helper'
2+
3+
class TestAnt < Minitest::Test
4+
include Ant::TestHelper
65

7-
class TestAnt < Test::Unit::TestCase
86
def test_ant_import
97
Dir.chdir(File.dirname(__FILE__)) do
108
ant_import 'ant_example.xml'
119
assert_equal 'surely', ant.properties['set_from_ant']
1210
end
1311
end
1412
end
13+
14+
class TestAntLoad < Minitest::Test
15+
include Ant::TestHelper
16+
17+
def setup
18+
@previous_java_home = ENV['JAVA_HOME'] || ENV_JAVA['java.home']
19+
20+
if Gem.win_platform?
21+
ENV['JAVA_HOME'] = '/C:/java6'
22+
else
23+
ENV['JAVA_HOME'] = '/System/Library/Frameworks/JavaVM.framework/Home'
24+
end
25+
@tools_jar = "#{ENV['JAVA_HOME']}/lib/tools.jar"
26+
@classes_zip = "#{ENV['JAVA_HOME']}/lib/classes.zip"
27+
28+
Ant.instance_eval do
29+
remove_const(:JAVA_HOME) rescue nil
30+
end
31+
end
32+
33+
def teardown
34+
ENV['JAVA_HOME'] = @previous_java_home
35+
Ant.instance_eval do
36+
begin
37+
remove_const(:JAVA_HOME)
38+
const_set(:JAVA_HOME, @previous_java_home)
39+
rescue NameError
40+
# ignore, JAVA_HOME constant is not necessarily set now
41+
end
42+
end
43+
end
44+
45+
def test_adds_tools_jar_to_classpath_when_java_home_is_set_and_it_exists
46+
stub_file_exist { Ant.load }
47+
assert_includes $CLASSPATH, "file:#{@tools_jar}"
48+
end
49+
50+
def test_adds_classes_zip_to_classpath_when_java_home_is_set_and_it_exists
51+
stub_file_exist { Ant.load }
52+
assert_includes $CLASSPATH, "file:#{@classes_zip}"
53+
end
54+
55+
# Only JAVA_HOME, tools.jar and classes.zip exist, and each must be checked.
56+
def stub_file_exist(&block)
57+
existing = [ENV['JAVA_HOME'], @tools_jar, @classes_zip]
58+
checked = []
59+
File.stub(:exist?, ->(path) { checked << path; existing.include?(path) }, &block)
60+
assert_empty existing - checked
61+
end
62+
end
63+
64+
class TestAntNew < Minitest::Test
65+
include Ant::TestHelper
66+
67+
def test_can_be_instantiated_with_a_block
68+
klass = nil
69+
70+
Ant.new do
71+
klass = self.class
72+
end
73+
74+
assert_equal Ant, klass
75+
end
76+
77+
def test_can_be_instantiated_with_a_block_whose_single_argument_receives_the_ant_instance
78+
klass = nil
79+
ant_klass = nil
80+
81+
Ant.new do |ant|
82+
klass = self.class
83+
ant_klass = ant.class
84+
end
85+
86+
refute_equal Ant, klass
87+
assert_equal Ant, ant_klass
88+
end
89+
90+
def test_executes_top_level_tasks_as_it_encounters_them
91+
Ant.new do |ant|
92+
refute_equal "bar", ant.properties["foo"]
93+
ant.property :name => "foo", :value => "bar"
94+
assert_equal "bar", ant.properties["foo"]
95+
end
96+
end
97+
98+
def test_has_a_valid_location
99+
assert File.exist?(Ant.new.location.file_name)
100+
end
101+
end
102+
103+
class TestAntInstance < Minitest::Test
104+
include Ant::TestHelper
105+
106+
def setup
107+
@ant = example_ant
108+
end
109+
110+
def test_defines_methods_corresponding_to_ant_tasks
111+
[:java, :antcall, :property, :import, :path, :patternset].each do |task|
112+
assert_includes @ant.methods, task
113+
end
114+
end
115+
116+
def test_executes_the_default_target
117+
@ant.target("default") { property :name => "spec", :value => "example" }
118+
@ant.project.default = "default"
119+
@ant.execute_default
120+
assert_equal "example", @ant.properties["spec"]
121+
end
122+
123+
def test_executes_the_specified_target
124+
@ant.target("a") { property :name => "a", :value => "true" }
125+
@ant.target("b") { property :name => "b", :value => "true" }
126+
@ant.execute_target("a")
127+
assert_equal "true", @ant.properties["a"]
128+
@ant["b"].execute
129+
assert_equal "true", @ant.properties["b"]
130+
end
131+
132+
def test_raises_when_a_bogus_target_is_executed
133+
assert_raises(RuntimeError) { @ant["bogus"].execute }
134+
end
135+
136+
def test_handles_key_value_arguments_from_the_command_line
137+
@ant.project.default = "help"
138+
@ant.process_arguments(["-Dcommand.line.msg=hello", "help"], false)
139+
@ant.define_tasks do
140+
target :help do
141+
property :name => "msg", :value => "${command.line.msg}"
142+
end
143+
end
144+
@ant.run
145+
assert_equal "hello", @ant.properties["msg"]
146+
end
147+
end
148+
149+
class TestAntDotAnt < Minitest::Test
150+
include Ant::TestHelper
151+
152+
def test_prefers_ant_home_to_path
153+
skip '$ANT_HOME is not set' unless ENV['ANT_HOME']
154+
155+
with_hidden_ant_path do
156+
Ant.ant(:basedir => File.expand_path('..', __dir__))
157+
end
158+
end
159+
end

‎test/test_project.rb‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'ant_test_helper'
2+
3+
class TestProject < Minitest::Test
4+
include Ant::TestHelper
5+
6+
def setup
7+
@ant = example_ant :name => "spec project", :description => "spec description"
8+
end
9+
10+
def test_has_the_basedir_set
11+
# expand_path is used to avoid / and \\ mismatch on Windows
12+
assert_equal Dir::tmpdir, File.expand_path(@ant.project.base_dir.path)
13+
end
14+
15+
def test_has_a_project_helper_created
16+
assert_kind_of Ant::ProjectHelper, @ant.project.get_reference(Ant::ProjectHelper::PROJECTHELPER_REFERENCE)
17+
end
18+
19+
def test_has_a_logger_set
20+
refute_empty @ant.project.build_listeners
21+
end
22+
23+
def test_has_a_name_and_description
24+
assert_equal "spec project", @ant.project.name
25+
assert_equal "spec description", @ant.project.description
26+
end
27+
end

0 commit comments

Comments
 (0)