File: testbase.rb

package info (click to toggle)
ruby-rubymail 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,056 kB
  • sloc: ruby: 6,061; makefile: 7
file content (214 lines) | stat: -rw-r--r-- 5,502 bytes parent folder | download | duplicates (2)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env ruby
#--
#   Copyright (C) 2001, 2002, 2003, 2007 Matt Armstrong.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
# NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Base for all the test cases, providing a default setup and teardown

require 'test/unit'
require 'rbconfig.rb'
require 'tempfile'
require 'find'
require 'fileutils'

begin
  require 'pp'
rescue LoadError
end

class TestBase < Test::Unit::TestCase
  include Config

  attr_reader :scratch_dir

  def test_nothing
    assert(true)  # Appease Test::Unit
  end

  # Print a string to a temporary file and return the file opened.
  # This lets you have some test data in a string, but access it with
  # a file.
  def string_as_file(string, strip_whitespace = true)
    if strip_whitespace
      temp = ""
      string.each_line { |line|
	temp += line.sub(/^[ \t]+/, '')
      }
      string = temp
    end
    file = Tempfile.new("ruby.string_as_file.")
    begin
      file.print(string)
      file.close()
      file.open()
      yield file
    ensure
      file.close(true)
    end
  end

  # Return true if the given file contains a line matching regexp
  def file_contains(filename, regexp)
    unless regexp.kind_of?(Regexp)
      regexp = Regexp.new(regexp)
    end
    detected = nil
    File.open(filename) { |f|
      detected = f.detect { |line|
	line =~ regexp
      }
    }
    ! detected.nil?
  end

  # Deletes everything in directory +dir+, including any
  # subdirectories
  def cleandir(dir)
    if FileTest.directory?(dir)
      files = []
      Find.find(dir) { |f|
	files.push(f)
      }
      files.shift		# get rid of 'dir'
      files.reverse_each { |f|
	if FileTest.directory?(f)
	  Dir.delete(f)
	else
	  File.delete(f)
	end
      }
    end
  end

  # In RubyUnit 1.9 the method #name was removed. Implement wrapper here for
  # compatibility with both RubyUnit 1.8 and 1.9.
  def get_test_name
    if (RUBY_VERSION.to_f < 1.9)
      name
    else
      "#{self.__name__}(#{self.class})"
    end
  end

  def setup
    @scratch_dir = File.join(Dir.getwd, "_scratch_" + get_test_name)
    @data_dir = File.join(Dir.getwd, "test", "data")
    @scratch_hash = {}

    cleandir(@scratch_dir)
    Dir.rmdir(@scratch_dir) if FileTest.directory?(@scratch_dir)
    Dir.mkdir(@scratch_dir) unless FileTest.directory?(@scratch_dir)
  end

  def ruby_program
    File.join(CONFIG['bindir'], CONFIG['ruby_install_name'])
  end

  def data_filename(name)
    File.join(@data_dir, name)
  end

  def data_as_file(name)
    unless name =~ %r{^/}
      name = data_filename(name)
    end
    File.open(name) { |f|
      yield f
    }
  rescue Errno::ENOENT
    flunk("data file #{name.inspect} does not exist")
  end

  def data_as_string(name)
    data_as_file(name) { |f|
      f.read
    }
  end

  def scratch_filename(name)
    if @scratch_hash.key?(name)
      temp = @scratch_hash[name]
      temp = temp.succ
      @scratch_hash[name] = name = temp
    else
      temp = name.dup
      temp << '.0' unless temp =~ /\.\d+$/
      @scratch_hash[name] = temp
    end
    File.join(@scratch_dir, name)
  end

  def scratch_file_write(name)
    name = scratch_filename(name)
    File.open(name, 'w') { |f|
      yield f
    }
  end

  def teardown
    unless $! || ((defined? passed?) && !passed?)
      cleandir(@scratch_dir)
      Dir.rmdir(@scratch_dir) if FileTest.directory?(@scratch_dir)
    end
  end

  def call_fails(arg, &block)
    begin
      yield arg
    rescue Exception
      return true
    end
    return false
  end

  # if a random string failes, run it through this function to find the
  # shortest fail case
  def find_shortest_failure(str, &block)
    unless call_fails(str, &block)
      raise "hey, the input didn't fail!"
    else
      # Chop off stuff from the beginning and then the end
      # until it stops failing
      bad = str
      0.upto(bad.length) {|index|
	bad.length.downto(1) {|length|
	  begin
	    loop {
	      s = bad.dup
	      s[index,length] = ''
	      break if bad == s
	      break unless call_fails(s, &block)
	      bad = s
	    }
	  rescue IndexError
	    break
	  end
	}
      }
      raise "shortest failure is #{bad.inspect}"
    end
  end

end