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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
#
# test/unit/bio/test_command.rb - Functional test for external command execution methods in Bio::Command
#
# Copyright:: Copyright (C) 2008
# Naohisa Goto <ng@bioruby.org>
# License:: The Ruby License
#
# $Id:$
#
# loading helper routine for testing bioruby
require 'pathname'
load Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 2,
'bioruby_test_helper.rb')).cleanpath.to_s
# libraries needed for the tests
require 'test/unit'
require 'tempfile'
require 'bio/command'
module Bio
module FuncTestCommandCallCommon
def windows_platform?
Bio::Command.module_eval { windows_platform? }
end
module_function :windows_platform?
def setup_cmd
if windows_platform? then
[ File.expand_path(File.join(BioRubyTestDataPath, 'command', 'echoarg2.bat')) ]
else
[ "/bin/sh", "/bin/echo" ].each do |cmd|
unless FileTest.executable?(cmd) then
raise "Unsupported environment: #{cmd} not found"
end
end
[ "/bin/sh", File.expand_path(File.join(BioRubyTestDataPath, 'command', 'echoarg2.sh')) ]
end
end
private :setup_cmd
def test_call_command
ret = Bio::Command.call_command(@arg) do |io|
io.close_write
io.read
end
assert_equal(@expected, ret.to_s.strip)
end
def test_call_command_popen
ret = Bio::Command.call_command_popen(@arg) do |io|
io.close_write
io.read
end
assert_equal(@expected, ret.to_s.strip)
end
def test_call_command_fork
return unless Thread.respond_to?(:critical)
begin
ret = Bio::Command.call_command_fork(@arg) do |io|
io.close_write
io.read
end
rescue Errno::ENOENT, NotImplementedError
# fork() not supported
return
end
assert_equal(@expected, ret.to_s.strip)
end
def test_call_command_open3
begin
ret = Bio::Command.call_command_open3(@arg) do |pin, pout, perr|
t = Thread.start { perr.read }
begin
pin.close
output = pout.read
ensure
t.join
end
output
end
rescue NotImplementedError
# fork() not supported
return
end
assert_equal(@expected, ret.to_s.strip)
end
end #module FuncTestCommandCallCommon
class FuncTestCommandCallSimple < Test::Unit::TestCase
include FuncTestCommandCallCommon
def setup
@arg = setup_cmd
@arg.concat [ "first", "second", "third" ]
@expected = "second"
end
end #class FuncTestCommandCallSimple
class FuncTestCommandCallWithSpace < Test::Unit::TestCase
include FuncTestCommandCallCommon
def setup
@arg = setup_cmd
@arg.concat [ "this is", "a test for", "escape of space" ]
if windows_platform? then
@expected = '"a test for"'
else
@expected = "a test for"
end
end
end #class FuncTestCommandCallWithSpace
class FuncTestCommandCallMisc1 < Test::Unit::TestCase
include FuncTestCommandCallCommon
def setup
@arg = setup_cmd
@arg.concat [ 'test (a) *.* \'argument 1\'',
'\'test\' (b) *.* argument 2', 'arg3' ]
if windows_platform? then
@expected = '"\'test\' (b) *.* argument 2"'
else
@expected = '\'test\' (b) *.* argument 2'
end
end
end #class FuncTestCommandCallMisc1
class FuncTestCommandQuery < Test::Unit::TestCase
def setup
@data = [ "987", "123", "567", "456", "345" ]
@sorted = @data.sort
if Bio::Command.module_eval { windows_platform? } then
@sort = "sort"
@data = @data.join("\r\n") + "\r\n"
else
@sort = `which sort`.chomp
if @sort.empty? or !FileTest.executable?(@sort) then
raise "Unsupported environment: sort not found in PATH"
end
@data = @data.join("\n") + "\n"
end
end
def test_query_command
ary = [ @sort ]
assert_equal('', Bio::Command.query_command(ary).to_s.strip)
str = Bio::Command.query_command(ary, @data).to_s
assert_equal(@sorted, str.strip.split(/\s+/))
end
def test_query_command_popen
ary = [ @sort ]
assert_equal('', Bio::Command.query_command_popen(ary).to_s.strip)
str = Bio::Command.query_command_popen(ary, @data).to_s
assert_equal(@sorted, str.strip.split(/\s+/))
end
def test_query_command_fork
return unless Thread.respond_to?(:critical)
ary = [ @sort ]
begin
str = Bio::Command.query_command_fork(ary).to_s
rescue Errno::ENOENT, NotImplementedError
# fork() not supported
return
end
assert_equal('', str.strip)
str = Bio::Command.query_command_fork(ary, @data).to_s
assert_equal(@sorted, str.strip.split(/\s+/))
end
def test_query_command_open3
ary = [ @sort ]
begin
str, err = Bio::Command.query_command_open3(ary)
rescue NotImplementedError
# fork() not supported
return
end
assert_equal('', str.to_s.strip)
assert_equal('', err.to_s.strip)
str, err = Bio::Command.query_command_open3(ary, @data)
assert_equal(@sorted, str.to_s.strip.split(/\s+/))
assert_equal('', err.to_s.strip)
end
end #class FuncTestCommandQuery
class FuncTestCommandChdir < Test::Unit::TestCase
def setup
if Bio::Command.module_eval { windows_platform? } then
@arg = [ 'dir', '/B', '/-P' ]
else
cmd = '/bin/ls'
@arg = [ cmd ]
unless FileTest.executable?(cmd) then
raise "Unsupported environment: #{cmd} not found"
end
end
@tempfile = Tempfile.new('chdir')
@tempfile.close(false)
@filename = File.basename(@tempfile.path)
@dirname = File.dirname(@tempfile.path)
end
def teardown
@tempfile.close(true)
end
def test_call_command_chdir
str = nil
Bio::Command.call_command(@arg, { :chdir => @dirname }) do |io|
io.close_write
str = io.read
end
assert(str.index(@filename))
end
def test_call_command_popen_chdir
str = nil
Bio::Command.call_command_popen(@arg,
{ :chdir => @dirname }) do |io|
io.close_write
str = io.read
end
assert(str.index(@filename))
end
def test_call_command_fork_chdir
return unless Thread.respond_to?(:critical)
str = nil
begin
Bio::Command.call_command_fork(@arg,
{ :chdir => @dirname }) do |io|
io.close_write
str = io.read
end
rescue Errno::ENOENT, NotImplementedError
# fork() not supported
return
end
assert(str.index(@filename))
end
def test_query_command_chdir
str = Bio::Command.query_command(@arg, nil,
{ :chdir => @dirname }).to_s
assert(str.index(@filename))
end
def test_query_command_popen_chdir
str = Bio::Command.query_command_popen(@arg, nil,
{ :chdir => @dirname }).to_s
assert(str.index(@filename))
end
def test_query_command_fork_chdir
return unless Thread.respond_to?(:critical)
begin
str = Bio::Command.query_command_fork(@arg, nil,
{ :chdir => @dirname }).to_s
rescue Errno::ENOENT, NotImplementedError
# fork() not supported
return
end
assert(str.index(@filename))
end
end #class FuncTestCommandChdir
class FuncTestCommandBackports < Test::Unit::TestCase
def setup
if RUBY_VERSION < "1.8.3"
@notest = true
else
@notest = false
end
end
def test_remove_entry_secure
return if @notest
begin
tempfile = Tempfile.new('removed')
tempfile.close(false)
assert(File.exist?(tempfile.path))
Bio::Command.remove_entry_secure(tempfile.path)
assert_equal(false, File.exist?(tempfile.path))
ensure
tempfile.close(true) if tempfile
end
end
def test_mktmpdir_with_block
return if @notest
tmpdirpath = nil
Bio::Command.mktmpdir('bioruby') do |path|
tmpdirpath = path
assert(File.directory?(path))
assert_nothing_raised {
File.open(File.join(path, 'test'), 'w') do |w|
w.print "This is test."
end
}
end
assert_equal(false, File.directory?(tmpdirpath))
end
def test_mktmpdir_without_block
return if @notest
path = nil
begin
assert_nothing_raised {
path = Bio::Command.mktmpdir('bioruby')
}
assert(File.directory?(path))
assert_nothing_raised {
File.open(File.join(path, 'test'), 'w') do |w|
w.print "This is test."
end
}
ensure
Bio::Command.remove_entry_secure(path) if path
end
end
end #class FuncTestCommandBackports
class FuncTestCommandTmpdir < Test::Unit::TestCase
def setup
if RUBY_VERSION < "1.8.3"
@notest = true
else
@notest = false
end
end
def test_initialize
return if @notest
tmpdir = Bio::Command::Tmpdir.new('bioruby')
assert_instance_of(Bio::Command::Tmpdir, tmpdir)
assert(File.directory?(tmpdir.path))
assert_nothing_raised {
# creates a dummy file
File.open(File.join(tmpdir.path, 'test'), 'w') do |w|
w.print "This is test."
end
}
end
def test_path
return if @notest
tmpdir = Bio::Command::Tmpdir.new('bioruby')
assert_kind_of(String, tmpdir.path)
assert(File.directory?(tmpdir.path))
end
def test_close!
return if @notest
tmpdir = Bio::Command::Tmpdir.new('bioruby')
path = tmpdir.path
# creates a dummy file
File.open(File.join(tmpdir.path, 'test'), 'w') do |w|
w.print "This is test."
end
assert_nothing_raised { tmpdir.close! }
assert_equal(false, File.directory?(path))
end
def test_path_after_close
return if @notest
tmpdir = Bio::Command::Tmpdir.new('bioruby')
tmpdir.close!
assert_raise(IOError) { tmpdir.path }
end
end #class FuncTestCommandTmpdir
end #module Bio
|