Skip to content

Commit 90e1711

Browse files
koronchrisbra
authored andcommitted
patch 9.1.2013: tests: Test_terminal_shell_option fails with conpty
Problem: tests: When opening a conpty terminal, if process startup fails, it will silently exit. As a result, the Test_terminal_shell_option in test_terminal3.vim failed in conpty. In a winpty terminal, the winpty-provided error message "CreateProcess failed" was displayed. The test is designed to catch this error as an exception. Solution: Make conpty fail with an error messages in the same way as winpty. (Muraoka Taro) In addition, since the GetWin32Error() function can obtain more detailed error messages, the format has been changed to "CreateProcess failed: {localized message from the OS}" for conpty. Also, since the GetWin32Error() function returns errors in ACP (Active Code Page) encoding, these have been converted to Vim's internal encoding, enc. This will prevent messages from being garbled in Japanese environments, etc. The output of this function was basically used by the semsg() function in other places, so this change also fixes potential similar garbled characters. The test now errors out immediately in places where it is expected not to be reached, and comments have been added about the expected content of the winpty and conpty error messages. closes: #18998 Signed-off-by: Muraoka Taro <koron.kaoriya@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent cbcc5ba commit 90e1711

4 files changed

Lines changed: 39 additions & 12 deletions

File tree

src/os_win32.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9074,22 +9074,38 @@ resize_console_buf(void)
90749074
char *
90759075
GetWin32Error(void)
90769076
{
9077-
static char *oldmsg = NULL;
9078-
char *msg = NULL;
9077+
static char *oldmsg = NULL;
9078+
char *acp_msg = NULL;
9079+
DWORD acp_len;
9080+
char_u *enc_msg = NULL;
9081+
int enc_len = 0;
9082+
9083+
// get formatted message from OS
9084+
acp_len = FormatMessage(
9085+
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
9086+
NULL, GetLastError(), 0, (LPSTR)&acp_msg, 0, NULL);
9087+
if (acp_len == 0 || acp_msg == NULL)
9088+
return NULL;
90799089

9080-
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
9081-
NULL, GetLastError(), 0, (LPSTR)&msg, 0, NULL);
9090+
// clean oldmsg if remained.
90829091
if (oldmsg != NULL)
9083-
LocalFree(oldmsg);
9084-
if (msg == NULL)
9092+
{
9093+
vim_free(oldmsg);
9094+
oldmsg = NULL;
9095+
}
9096+
9097+
acp_to_enc(acp_msg, (int)acp_len, &enc_msg, &enc_len);
9098+
LocalFree(acp_msg);
9099+
if (enc_msg == NULL)
90859100
return NULL;
90869101

90879102
// remove trailing \r\n
9088-
char *pcrlf = strstr(msg, "\r\n");
9103+
char *pcrlf = strstr(enc_msg, "\r\n");
90899104
if (pcrlf != NULL)
90909105
*pcrlf = NUL;
9091-
oldmsg = msg;
9092-
return msg;
9106+
9107+
oldmsg = enc_msg;
9108+
return enc_msg;
90939109
}
90949110

90959111
#if defined(FEAT_RELTIME)

src/terminal.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7048,6 +7048,7 @@ conpty_term_and_job_init(
70487048
HANDLE o_theirs = NULL;
70497049
HANDLE i_ours = NULL;
70507050
HANDLE o_ours = NULL;
7051+
char *errmsg = NULL;
70517052

70527053
ga_init2(&ga_cmd, sizeof(char*), 20);
70537054
ga_init2(&ga_env, sizeof(char*), 20);
@@ -7149,7 +7150,10 @@ conpty_term_and_job_init(
71497150
| CREATE_SUSPENDED | CREATE_DEFAULT_ERROR_MODE,
71507151
env_wchar, cwd_wchar,
71517152
&term->tl_siex.StartupInfo, &proc_info))
7153+
{
7154+
errmsg = GetWin32Error();
71527155
goto failed;
7156+
}
71537157

71547158
CloseHandle(i_theirs);
71557159
CloseHandle(o_theirs);
@@ -7257,6 +7261,9 @@ conpty_term_and_job_init(
72577261
if (term->tl_conpty != NULL)
72587262
pClosePseudoConsole(term->tl_conpty);
72597263
term->tl_conpty = NULL;
7264+
// Propagate errors that occur in CreateProcess
7265+
if (errmsg)
7266+
semsg("CreateProcess failed: %s", errmsg);
72607267
return FAIL;
72617268
}
72627269

src/testdir/test_terminal3.vim

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ func Test_terminal_shell_option()
4646
" the %PATH%, "term dir" succeeds unintentionally. Use dir.com instead.
4747
try
4848
term dir.com /b runtest.vim
49-
call WaitForAssert({-> assert_match('job failed', term_getline(bufnr(), 1))})
50-
catch /CreateProcess/
51-
" ignore
49+
throw 'dir.com without a shell must fail, so you will never get here'
50+
catch /CreateProcess failed/
51+
" ignore:
52+
" winpty simply fails with "CreateProcess failed".
53+
" compty fails with "CreateProcess failed: {localized failure reason}".
5254
endtry
5355
bwipe!
5456

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,8 @@ static char *(features[]) =
734734

735735
static int included_patches[] =
736736
{ /* Add new patch number below this line */
737+
/**/
738+
2013,
737739
/**/
738740
2012,
739741
/**/

0 commit comments

Comments
 (0)