test: cleanup

This commit is contained in:
Justin M. Keyes 2017-08-06 14:26:17 +02:00
parent 8642f05fd9
commit 6a90f53862
3 changed files with 39 additions and 50 deletions

View File

@ -4,10 +4,11 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#ifdef _WIN32
#include <windows.h>
# include <windows.h>
#else
# include <unistd.h>
#endif
// -V:STRUCT_CAST:641
@ -15,39 +16,31 @@
#define is_terminal(stream) (uv_guess_handle(fileno(stream)) == UV_TTY)
#define BUF_SIZE 0xfff
#define CTRL_C 0x03
#ifdef _WIN32
#define CTRL_Q 0x11
#endif
uv_tty_t tty;
#ifdef _WIN32
typedef struct screen_size {
int width;
int height;
} ScreenSize;
#endif
uv_tty_t tty;
#ifdef _WIN32
ScreenSize screen_rect;
#endif
#ifdef _WIN32
bool owns_tty(void)
{
#ifdef _WIN32
// XXX: We need to make proper detect owns tty
// HWND consoleWnd = GetConsoleWindow();
// DWORD dwProcessId;
// GetWindowThreadProcessId(consoleWnd, &dwProcessId);
// return GetCurrentProcessId() == dwProcessId;
return true;
}
#else
#include <unistd.h>
bool owns_tty(void)
{
return getsid(0) == getpid();
}
#endif
}
static void walk_cb(uv_handle_t *handle, void *arg)
{
@ -56,7 +49,6 @@ static void walk_cb(uv_handle_t *handle, void *arg)
}
}
#ifndef WIN32
static void sig_handler(int signum)
{
switch (signum) {
@ -73,17 +65,6 @@ static void sig_handler(int signum)
return;
}
}
#else
// static void sigwinch_cb(uv_signal_t *handle, int signum)
// {
// int width, height;
// uv_tty_t out;
// uv_tty_init(uv_default_loop(), &out, fileno(stdout), 0);
// uv_tty_get_winsize(&out, &width, &height);
// fprintf(stderr, "rows: %d, cols: %d\n", height, width);
// uv_close((uv_handle_t *)&out, NULL);
// }
#endif
static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf)
{
@ -100,9 +81,9 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
int *interrupted = stream->data;
#ifdef _WIN32
bool prsz = false;
int width;
int height;
// HACK: Special-case to avoid relying on SIGWINCH on Windows.
// See note at Screen:try_resize().
bool invoke_sigwinch_handler = false;
#endif
for (int i = 0; i < cnt; i++) {
@ -110,7 +91,7 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
(*interrupted)++;
#ifdef _WIN32
} else if (buf->base[i] == CTRL_Q) {
prsz = true;
invoke_sigwinch_handler = true;
#endif
}
}
@ -121,12 +102,15 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
uv_tty_init(&write_loop, &out, fileno(stdout), 0);
#ifdef _WIN32
if (prsz) {
if (invoke_sigwinch_handler) {
int width, height;
uv_tty_get_winsize(&out, &width, &height);
if (screen_rect.width != width || screen_rect.height != height) {
screen_rect.width = width;
screen_rect.height = height;
fprintf(stderr, "rows: %d, cols: %d\n", height, width);
// HACK: Invoke directly. See note at Screen:try_resize().
sig_handler(SIGWINCH);
uv_run(&write_loop, UV_RUN_NOWAIT);
}
} else {
#endif

View File

@ -141,7 +141,8 @@ describe('terminal scrollback', function()
describe('and the height is decreased by 1', function()
local function will_hide_top_line()
screen:try_resize(screen._width, screen._height - 1)
screen:expect_after_resize([[
retry(nil, 100000, function()
screen:expect([[
line2 |
line3 |
line4 |
@ -149,6 +150,7 @@ describe('terminal scrollback', function()
{1: } |
{3:-- TERMINAL --} |
]])
end)
end
it('will hide top line', will_hide_top_line)
@ -160,7 +162,8 @@ describe('terminal scrollback', function()
end)
it('will hide the top 3 lines', function()
screen:expect_after_resize([[
retry(nil, 100000, function()
screen:expect([[
rows: 5, cols: 30 |
rows: 3, cols: 30 |
{1: } |
@ -174,19 +177,21 @@ describe('terminal scrollback', function()
rows: 3, cols: 30 |
|
]])
end)
end)
end)
end)
end)
describe('with empty lines after the cursor', function()
if helpers.pending_win32(pending) then return end
describe('and the height is decreased by 2', function()
before_each(function()
screen:try_resize(screen._width, screen._height - 2)
end)
local function will_delete_last_two_lines()
if helpers.pending_win32(pending) then return end
screen:expect([[
tty ready |
rows: 4, cols: 30 |
@ -200,7 +205,6 @@ describe('terminal scrollback', function()
it('will delete the last two empty lines', will_delete_last_two_lines)
describe('and then decreased by 1', function()
if helpers.pending_win32(pending) then return end
before_each(function()
will_delete_last_two_lines()
screen:try_resize(screen._width, screen._height - 1)
@ -246,19 +250,21 @@ describe('terminal scrollback', function()
{3:-- TERMINAL --} |
]])
screen:try_resize(screen._width, screen._height - 3)
screen:expect_after_resize([[
retry(nil, 100000, function()
screen:expect([[
line4 |
rows: 3, cols: 30 |
{1: } |
{3:-- TERMINAL --} |
]])
eq(7, curbuf('line_count'))
end)
end)
describe('and the height is increased by 1', function()
local function pop_then_push()
screen:try_resize(screen._width, screen._height + 1)
screen:expect_after_resize([[
screen:expect([[
line4 |
rows: 3, cols: 30 |
rows: 4, cols: 30 |
@ -277,7 +283,7 @@ describe('terminal scrollback', function()
end)
local function pop3_then_push1()
screen:expect_after_resize([[
screen:expect([[
line2 |
line3 |
line4 |

View File

@ -176,6 +176,16 @@ function Screen:try_resize(columns, rows)
-- Give ourselves a chance to _handle_resize, which requires using
-- self.sleep() (for the resize notification) rather than run()
self:sleep(0.1)
-- XXX: On Windows we don't bother to handle SIGWINCH.
-- CTRL-Q invokes the handler in tty-test.c directly.
-- uv_tty_update_virtual_window() _does_ emit SIGWINCH, but:
-- "SIGWINCH may not always be delivered in a timely manner; libuv
-- will only detect size changes when the cursor is being moved."
-- http://docs.libuv.org/en/v1.x/signal.html
if iswin() and 0 ~= nvim('eval', "exists('b:terminal_job_id')") then
nvim('command', [[call jobsend(b:terminal_job_id, "\<C-Q>")]])
end
end
-- Asserts that `expected` eventually matches the screen state.
@ -261,17 +271,6 @@ screen:redraw_debug() to show all intermediate screen states. ]])
end)
end
function Screen:expect_after_resize(expected)
if iswin() then
retry(nil, nil, function()
nvim('command', 'call jobsend(b:terminal_job_id, "\\<C-q>")')
self:expect(expected)
end)
else
self:expect(expected)
end
end
function Screen:wait(check, timeout)
local err, checked = false
local success_seen = false