File: ant_spec_helper.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (131 lines) | stat: -rw-r--r-- 4,034 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require File.expand_path('../spec_helper', __FILE__)
require 'ant'
require 'tmpdir'

class Rake::Ant
  module RSpec
    module AntExampleGroup
      def self.included(example_group)
        example_group.after :each do
          Ant.instance_eval do
            instance_variable_set "@ant", nil
          end
        end
      end

      # Allows matching task structure with a nested hash as follows.
      #
      # { :_name => "jar", :destfile => "spec-test.jar", :compress => "true", :index => "true",
      #   :_children => [
      #     { :_name => "fileset", :dir => "build" }
      #   ]}
      #
      # would match the following:
      #
      # jar :destfile => "spec-test.jar", :compress => "true", :index => "true" do
      #   fileset :dir => "build"
      # end
      #
      class TaskStructureMatcher
        def initialize(hash, configure = false)
          @expected = hash
          @configure = configure
        end

        def description
          "have the specified #{(@configure ? 'configured' : 'element')} structure"
        end

        def matches?(actual)
          @actual = actual
          result = true
          tasks = actual.defined_tasks
          if Hash === @expected && tasks.length != 1 || tasks.length != @expected.length
            @message = "task list length different"
            return false
          end
          @expected.each_with_index do |h,i|
            tasks[i].maybe_configure if @configure
            result &&= match_wrapper(h, tasks[i].wrapper)
          end
          result
        end

        def match_wrapper(hash, wrapper, name = nil)
          hname = hash[:_name] ? hash[:_name] : '<?>'
          if name
            name = "#{name} => #{hname}"
          else
            name = hname
          end

          # name
          if hash[:_name] && hash[:_name] != wrapper.element_tag
            @message = "name different: expected #{hash[:_name].inspect} but was #{wrapper.element_tag.inspect}"
            return false
          end

          # proxy class name
          if hash[:_type] && (wrapper.proxy.nil? || hash[:_type] != wrapper.proxy.java_class.name)
            @message = "type different: expected #{hash[:_type]} but was #{wrapper.proxy.java_class.name}"
            return false
          end

          # attributes
          hash.keys.select {|k| k.to_s !~ /^_/}.each do |k|
            unless wrapper.attribute_map.containsKey(k.to_s)
              @message = "'#{name} => #{k}' attribute missing"
              return false
            end

            if hash[k] != wrapper.attribute_map[k.to_s]
              @message = "'#{name} => #{k}' attribute different: expected #{hash[k].inspect} but was #{wrapper.attribute_map[k.to_s].inspect}"
              return false
            end
          end

          # children, recursively
          children = wrapper.children.to_a
          (hash[:_children] || []).each_with_index do |h,i|
            return false unless match_wrapper(h, children[i], name)
          end
          true
        end

        def failure_message
          require 'pp'
          "expected #{@actual.name} to have structure:\n#{@expected.pretty_inspect}\n#{@message}"
        end

        def failure_message_when_negated
          require 'pp'
          "expected #{@actual.name} to not have structure:\n#{@expected.pretty_inspect}\n#{@message}"
        end
      end

      def have_structure(hash)
        TaskStructureMatcher.new(hash)
      end

      def have_configured_structure(hash)
        TaskStructureMatcher.new(hash, true)
      end

      def example_ant(options = {}, &block)
        Ant.new({:basedir => Dir::tmpdir, :run => false, :output_level => 0}.merge(options),&block)
      end
    end
  end
end

def with_hidden_ant_path
  original_env_path = ENV['PATH']
  hidden_path = ENV['PATH'].split(File::PATH_SEPARATOR).reject { |dir|
    File.executable?(File.join(dir, 'ant'))
  }.join(File::PATH_SEPARATOR)
  ENV['PATH'] = hidden_path

  yield
ensure
  ENV['PATH'] = original_env_path
end