|
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 |
6 | 5 |
|
7 | | -class TestAnt < Test::Unit::TestCase |
8 | 6 | def test_ant_import |
9 | 7 | Dir.chdir(File.dirname(__FILE__)) do |
10 | 8 | ant_import 'ant_example.xml' |
11 | 9 | assert_equal 'surely', ant.properties['set_from_ant'] |
12 | 10 | end |
13 | 11 | end |
14 | 12 | 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 |
0 commit comments