2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2018-07-19 22:24:59 -04:00
|
|
|
require 'test/unit'
|
2013-09-17 08:35:49 -04:00
|
|
|
require 'shell'
|
|
|
|
require 'tmpdir'
|
|
|
|
|
|
|
|
class TestShell < Test::Unit::TestCase
|
|
|
|
end
|
|
|
|
class TestShell::CommandProcessor < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@tmpdir = Dir.mktmpdir("test_shell")
|
|
|
|
@shell = Shell.new
|
|
|
|
@shell.system_path = [@tmpdir]
|
|
|
|
end
|
|
|
|
|
2013-11-05 03:10:32 -05:00
|
|
|
def teardown
|
|
|
|
Dir.rmdir(@tmpdir)
|
|
|
|
end
|
|
|
|
|
2013-09-17 08:35:49 -04:00
|
|
|
def catch_command_start(tc = Object.new)
|
|
|
|
@shell.process_controller.singleton_class.class_eval do
|
|
|
|
define_method(:add_schedule) {|cmd| throw tc, cmd}
|
|
|
|
end
|
|
|
|
tc
|
|
|
|
end
|
|
|
|
|
2013-09-25 03:46:20 -04:00
|
|
|
def exeext
|
|
|
|
RbConfig::CONFIG["EXECUTABLE_EXTS"][/\S+\z/]
|
|
|
|
end
|
|
|
|
|
2013-09-17 08:35:49 -04:00
|
|
|
def test_system_external
|
2013-09-25 03:46:20 -04:00
|
|
|
name = "foo#{exeext}"
|
|
|
|
path = File.join(@tmpdir, name)
|
2013-09-17 08:35:49 -04:00
|
|
|
open(path, "w", 0755) {}
|
|
|
|
|
2013-09-25 03:46:20 -04:00
|
|
|
cmd = assert_throw(catch_command_start) {@shell.system(name)}
|
2013-09-17 08:35:49 -04:00
|
|
|
assert_equal(path, cmd.command)
|
|
|
|
ensure
|
|
|
|
File.unlink(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_system_not_found
|
|
|
|
bug8918 = '[ruby-core:57235] [Bug #8918]'
|
|
|
|
|
2013-09-25 03:46:20 -04:00
|
|
|
name = "foo"
|
|
|
|
path = File.join(@tmpdir, name)
|
2013-09-17 08:35:49 -04:00
|
|
|
open(path, "w", 0644) {}
|
|
|
|
|
|
|
|
assert_raise(Shell::Error::CommandNotFound, bug8918) {
|
2013-09-25 03:46:20 -04:00
|
|
|
catch(catch_command_start) {@shell.system(name)}
|
2013-09-17 08:35:49 -04:00
|
|
|
}
|
|
|
|
ensure
|
|
|
|
Process.waitall
|
|
|
|
File.unlink(path)
|
|
|
|
end
|
2013-09-25 03:46:20 -04:00
|
|
|
|
|
|
|
def test_system_directory
|
|
|
|
bug8918 = '[ruby-core:57235] [Bug #8918]'
|
|
|
|
|
|
|
|
name = "foo#{exeext}"
|
|
|
|
path = File.join(@tmpdir, name)
|
|
|
|
Dir.mkdir(path)
|
|
|
|
|
|
|
|
assert_raise(Shell::Error::CommandNotFound, bug8918) {
|
|
|
|
catch(catch_command_start) {@shell.system(name)}
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
Process.waitall
|
|
|
|
Dir.rmdir(path)
|
|
|
|
end
|
2017-06-22 06:56:49 -04:00
|
|
|
|
|
|
|
def test_option_type
|
2017-06-23 11:08:50 -04:00
|
|
|
name = 'foo.cmd'
|
2017-06-22 06:56:49 -04:00
|
|
|
path = File.join(@tmpdir, name)
|
|
|
|
|
|
|
|
open(path, 'w', 0755) {}
|
|
|
|
assert_raise(TypeError) {
|
|
|
|
catch(catch_command_start) {@shell.system(name, 42)}
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
Process.waitall
|
|
|
|
File.unlink(path)
|
|
|
|
end
|
2013-09-17 08:35:49 -04:00
|
|
|
end
|