2021-07-18 14:15:08 +02:00
|
|
|
extends "res://addons/gut/test.gd"
|
2021-07-03 15:54:34 +02:00
|
|
|
|
2021-07-22 07:17:23 +02:00
|
|
|
const LibuvUtils := preload("res://addons/godot_xterm/nodes/pty/libuv_utils.gd")
|
2021-07-25 19:31:23 +02:00
|
|
|
var PTY = load("res://addons/godot_xterm/pty.gd")
|
2021-07-22 07:17:23 +02:00
|
|
|
|
2021-07-25 19:31:23 +02:00
|
|
|
var pty
|
2021-07-20 02:27:07 +02:00
|
|
|
var helper: Helper
|
|
|
|
|
|
|
|
|
|
|
|
func before_all():
|
|
|
|
if OS.get_name() == "OSX":
|
|
|
|
helper = MacOSHelper.new()
|
|
|
|
else:
|
|
|
|
helper = LinuxHelper.new()
|
2021-07-03 15:54:34 +02:00
|
|
|
|
|
|
|
|
2021-07-18 14:15:08 +02:00
|
|
|
func before_each():
|
2021-07-25 19:31:23 +02:00
|
|
|
pty = PTY.new()
|
2021-07-18 14:15:08 +02:00
|
|
|
add_child_autofree(pty)
|
2021-07-03 15:54:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
func test_fork_succeeds():
|
|
|
|
var err = pty.fork("sh")
|
2021-07-18 14:15:08 +02:00
|
|
|
assert_eq(err, OK)
|
2021-07-03 15:54:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
func test_open_succeeds():
|
|
|
|
var result = pty.open()
|
2021-07-18 14:15:08 +02:00
|
|
|
assert_eq(result[0], OK)
|
2021-07-03 15:54:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
func test_open_creates_a_new_pty():
|
2021-07-20 02:27:07 +02:00
|
|
|
var num_pts = helper._get_pts().size()
|
2021-07-03 15:54:34 +02:00
|
|
|
pty.open()
|
2021-07-20 02:27:07 +02:00
|
|
|
var new_num_pts = helper._get_pts().size()
|
2021-07-18 14:15:08 +02:00
|
|
|
assert_eq(new_num_pts, num_pts + 1)
|
2021-07-03 15:54:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
func test_open_pty_has_correct_name():
|
2021-07-20 02:27:07 +02:00
|
|
|
var original_pts = helper._get_pts()
|
2021-07-03 15:54:34 +02:00
|
|
|
var result = pty.open()
|
2021-07-20 02:27:07 +02:00
|
|
|
var new_pts = helper._get_pts()
|
2021-07-03 15:54:34 +02:00
|
|
|
for pt in original_pts:
|
|
|
|
new_pts.erase(pt)
|
2021-07-18 14:15:08 +02:00
|
|
|
assert_eq(result[1].pty, new_pts[0])
|
2021-07-03 15:54:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
func test_open_pty_has_correct_win_size():
|
|
|
|
var cols = 7684
|
|
|
|
var rows = 9314
|
|
|
|
var result = pty.open(cols, rows)
|
2021-07-20 02:27:07 +02:00
|
|
|
var winsize = helper._get_winsize(result[1].master)
|
2021-07-18 14:15:08 +02:00
|
|
|
assert_eq(winsize.cols, cols)
|
|
|
|
assert_eq(winsize.rows, rows)
|
2021-07-03 15:54:34 +02:00
|
|
|
|
|
|
|
|
2022-06-26 10:00:36 +02:00
|
|
|
func test_win_size_supports_max_unsigned_short_value():
|
|
|
|
var cols = 65535
|
|
|
|
var rows = 65535
|
|
|
|
var result = pty.open(cols, rows)
|
|
|
|
var winsize = helper._get_winsize(result[1].master)
|
|
|
|
assert_eq(winsize.cols, cols)
|
|
|
|
assert_eq(winsize.cols, rows)
|
|
|
|
|
|
|
|
|
2021-07-22 07:17:23 +02:00
|
|
|
func test_closes_pty_on_exit():
|
|
|
|
var num_pts = helper._get_pts().size()
|
|
|
|
pty.fork("sleep", ["1000"])
|
|
|
|
remove_child(pty)
|
|
|
|
pty.free()
|
|
|
|
yield(yield_for(1), YIELD)
|
|
|
|
var new_num_pts = helper._get_pts().size()
|
|
|
|
assert_eq(new_num_pts, num_pts)
|
|
|
|
|
|
|
|
|
2021-07-03 15:54:34 +02:00
|
|
|
class Helper:
|
|
|
|
static func _get_pts() -> Array:
|
2021-07-20 02:27:07 +02:00
|
|
|
assert(false, "Abstract method")
|
|
|
|
return []
|
2021-07-03 15:54:34 +02:00
|
|
|
|
|
|
|
static func _get_winsize(fd: int) -> Dictionary:
|
|
|
|
var output = []
|
|
|
|
|
|
|
|
assert(
|
|
|
|
OS.execute("command", ["-v", "python"], true, output) == 0,
|
|
|
|
"Python must be installed to run this test."
|
|
|
|
)
|
|
|
|
var python_path = output[0].strip_edges()
|
|
|
|
|
|
|
|
var exit_code = OS.execute(
|
|
|
|
python_path,
|
|
|
|
[
|
|
|
|
"-c",
|
|
|
|
(
|
2022-06-26 10:00:36 +02:00
|
|
|
"import struct, fcntl, termios; print(struct.unpack('HH', fcntl.ioctl(%d, termios.TIOCGWINSZ, '1234')))"
|
2021-07-03 15:54:34 +02:00
|
|
|
% fd
|
|
|
|
)
|
|
|
|
],
|
|
|
|
true,
|
|
|
|
output
|
|
|
|
)
|
|
|
|
assert(exit_code == 0, "Failed to run python command for this test.")
|
|
|
|
|
|
|
|
var size = str2var("Vector2" + output[0].strip_edges())
|
|
|
|
return {rows = int(size.x), cols = int(size.y)}
|
2021-07-20 02:27:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LinuxHelper:
|
|
|
|
extends Helper
|
2022-06-04 06:27:46 +02:00
|
|
|
|
2021-07-20 02:27:07 +02:00
|
|
|
static func _get_pts() -> Array:
|
|
|
|
var dir := Directory.new()
|
|
|
|
|
|
|
|
if dir.open("/dev/pts") != OK or dir.list_dir_begin(true, true) != OK:
|
|
|
|
assert(false, "Could not open /dev/pts.")
|
|
|
|
|
|
|
|
var pts := []
|
|
|
|
var file_name: String = dir.get_next()
|
|
|
|
|
|
|
|
while file_name != "":
|
|
|
|
if file_name.is_valid_integer():
|
|
|
|
pts.append("/dev/pts/%s" % file_name)
|
|
|
|
file_name = dir.get_next()
|
|
|
|
|
|
|
|
return pts
|
|
|
|
|
|
|
|
|
|
|
|
class MacOSHelper:
|
|
|
|
extends Helper
|
2022-06-04 06:27:46 +02:00
|
|
|
|
2021-07-20 02:27:07 +02:00
|
|
|
static func _get_pts() -> Array:
|
|
|
|
# TODO: Implement for macOS.
|
|
|
|
# On macOS there is no /dev/pts directory, rather new ptys are created
|
|
|
|
# under /dev/ttysXYZ.
|
|
|
|
assert(false, "Not implemented")
|
|
|
|
return []
|