1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

ci: fix failing tests on windows too

This commit is contained in:
Delyan Angelov 2020-10-15 15:54:44 +03:00
parent 5d4cce3e95
commit 50a2b033b7
2 changed files with 113 additions and 118 deletions

View File

@ -5,7 +5,6 @@ import strings
#flag -lws2_32 #flag -lws2_32
#include <winsock2.h> #include <winsock2.h>
#include <process.h> #include <process.h>
pub const ( pub const (
path_separator = '\\' path_separator = '\\'
path_delimiter = ';' path_delimiter = ';'
@ -18,69 +17,69 @@ pub type HANDLE = voidptr
// win: FILETIME // win: FILETIME
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime // https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
struct Filetime { struct Filetime {
dw_low_date_time u32 dw_low_date_time u32
dw_high_date_time u32 dw_high_date_time u32
} }
// win: WIN32_FIND_DATA // win: WIN32_FIND_DATA
// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataw // https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataw
struct Win32finddata { struct Win32finddata {
mut: mut:
dw_file_attributes u32 dw_file_attributes u32
ft_creation_time Filetime ft_creation_time Filetime
ft_last_access_time Filetime ft_last_access_time Filetime
ft_last_write_time Filetime ft_last_write_time Filetime
n_file_size_high u32 n_file_size_high u32
n_file_size_low u32 n_file_size_low u32
dw_reserved0 u32 dw_reserved0 u32
dw_reserved1 u32 dw_reserved1 u32
c_file_name [260]u16 // max_path_len = 260 c_file_name [260]u16 // max_path_len = 260
c_alternate_file_name [14]u16 // 14 c_alternate_file_name [14]u16 // 14
dw_file_type u32 dw_file_type u32
dw_creator_type u32 dw_creator_type u32
w_finder_flags u16 w_finder_flags u16
} }
struct ProcessInformation { struct ProcessInformation {
mut: mut:
h_process voidptr h_process voidptr
h_thread voidptr h_thread voidptr
dw_process_id u32 dw_process_id u32
dw_thread_id u32 dw_thread_id u32
} }
struct StartupInfo { struct StartupInfo {
mut: mut:
cb u32 cb u32
lp_reserved &u16 lp_reserved &u16
lp_desktop &u16 lp_desktop &u16
lp_title &u16 lp_title &u16
dw_x u32 dw_x u32
dw_y u32 dw_y u32
dw_x_size u32 dw_x_size u32
dw_y_size u32 dw_y_size u32
dw_x_count_chars u32 dw_x_count_chars u32
dw_y_count_chars u32 dw_y_count_chars u32
dw_fill_attributes u32 dw_fill_attributes u32
dw_flags u32 dw_flags u32
w_show_window u16 w_show_window u16
cb_reserved2 u16 cb_reserved2 u16
lp_reserved2 byteptr lp_reserved2 byteptr
h_std_input voidptr h_std_input voidptr
h_std_output voidptr h_std_output voidptr
h_std_error voidptr h_std_error voidptr
} }
struct SecurityAttributes { struct SecurityAttributes {
mut: mut:
n_length u32 n_length u32
lp_security_descriptor voidptr lp_security_descriptor voidptr
b_inherit_handle bool b_inherit_handle bool
} }
fn init_os_args_wide(argc int, argv &byteptr) []string { fn init_os_args_wide(argc int, argv &byteptr) []string {
mut args := []string{} mut args := []string{}
for i in 0..argc { for i in 0 .. argc {
args << string_from_wide(unsafe {&u16(argv[i])}) args << string_from_wide(unsafe {&u16(argv[i])})
} }
return args return args
@ -92,7 +91,7 @@ pub fn ls(path string) ?[]string {
// We can also check if the handle is valid. but using is_dir instead // We can also check if the handle is valid. but using is_dir instead
// h_find_dir := C.FindFirstFile(path.str, &find_file_data) // h_find_dir := C.FindFirstFile(path.str, &find_file_data)
// if (invalid_handle_value == h_find_dir) { // if (invalid_handle_value == h_find_dir) {
// return dir_files // return dir_files
// } // }
// C.FindClose(h_find_dir) // C.FindClose(h_find_dir)
if !is_dir(path) { if !is_dir(path) {
@ -131,12 +130,12 @@ pub fn is_dir(path string) bool {
return false return false
} }
*/ */
// mkdir creates a new directory with the specified path. // mkdir creates a new directory with the specified path.
pub fn mkdir(path string) ?bool { pub fn mkdir(path string) ?bool {
if path == '.' { return true } if path == '.' {
apath := os.real_path( path ) return true
}
apath := real_path(path)
if !C.CreateDirectory(apath.to_wide(), 0) { if !C.CreateDirectory(apath.to_wide(), 0) {
return error('mkdir failed for "$apath", because CreateDirectory returned ' + get_error_msg(int(C.GetLastError()))) return error('mkdir failed for "$apath", because CreateDirectory returned ' + get_error_msg(int(C.GetLastError())))
} }
@ -146,9 +145,11 @@ pub fn mkdir(path string) ?bool {
// Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019 // Ref - https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle?view=vs-2019
// get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor. // get_file_handle retrieves the operating-system file handle that is associated with the specified file descriptor.
pub fn get_file_handle(path string) HANDLE { pub fn get_file_handle(path string) HANDLE {
cfile := vfopen(path, 'rb') or { return HANDLE(invalid_handle_value) } cfile := vfopen(path, 'rb') or {
handle := HANDLE(C._get_osfhandle(fileno(cfile))) // CreateFile? - hah, no -_- return HANDLE(invalid_handle_value)
return handle }
handle := HANDLE(C._get_osfhandle(fileno(cfile))) // CreateFile? - hah, no -_-
return handle
} }
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea // Ref - https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamea
@ -171,57 +172,54 @@ pub fn get_module_filename(handle HANDLE) ?string {
} }
} }
} }
panic('this should be unreachable') // TODO remove unreachable after loop panic('this should be unreachable') // TODO remove unreachable after loop
} }
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea#parameters // Ref - https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-formatmessagea#parameters
const ( const (
format_message_allocate_buffer = 0x00000100 format_message_allocate_buffer = 0x00000100
format_message_argument_array = 0x00002000 format_message_argument_array = 0x00002000
format_message_from_hmodule = 0x00000800 format_message_from_hmodule = 0x00000800
format_message_from_string = 0x00000400 format_message_from_string = 0x00000400
format_message_from_system = 0x00001000 format_message_from_system = 0x00001000
format_message_ignore_inserts = 0x00000200 format_message_ignore_inserts = 0x00000200
) )
// Ref - winnt.h // Ref - winnt.h
const ( const (
sublang_neutral = 0x00 sublang_neutral = 0x00
sublang_default = 0x01 sublang_default = 0x01
lang_neutral = (sublang_neutral) lang_neutral = (sublang_neutral)
) )
// Ref - https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--12000-15999- // Ref - https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--12000-15999-
const ( const (
max_error_code = 15841 // ERROR_API_UNAVAILABLE max_error_code = 15841 // ERROR_API_UNAVAILABLE
) )
// ptr_win_get_error_msg return string (voidptr) // ptr_win_get_error_msg return string (voidptr)
// representation of error, only for windows. // representation of error, only for windows.
fn ptr_win_get_error_msg(code u32) voidptr { fn ptr_win_get_error_msg(code u32) voidptr {
mut buf := voidptr(0) mut buf := voidptr(0)
// Check for code overflow // Check for code overflow
if code > u32(max_error_code) { if code > u32(max_error_code) {
return buf return buf
} }
C.FormatMessage( C.FormatMessage(format_message_allocate_buffer | format_message_from_system | format_message_ignore_inserts,
format_message_allocate_buffer 0, code, C.MAKELANGID(lang_neutral, sublang_default), voidptr(&buf), 0, 0)
| format_message_from_system return buf
| format_message_ignore_inserts,
0, code, C.MAKELANGID(lang_neutral, sublang_default), voidptr(&buf), 0, 0)
return buf
} }
// get_error_msg return error code representation in string. // get_error_msg return error code representation in string.
pub fn get_error_msg(code int) string { pub fn get_error_msg(code int) string {
if code < 0 { // skip negative if code < 0 { // skip negative
return '' return ''
} }
ptr_text := ptr_win_get_error_msg(u32(code)) ptr_text := ptr_win_get_error_msg(u32(code))
if ptr_text == 0 { // compare with null if ptr_text == 0 { // compare with null
return '' return ''
} }
return string_from_wide(ptr_text) return string_from_wide(ptr_text)
} }
// exec starts the specified command, waits for it to complete, and returns its output. // exec starts the specified command, waits for it to complete, and returns its output.
@ -232,24 +230,23 @@ pub fn exec(cmd string) ?Result {
mut child_stdin := &u32(0) mut child_stdin := &u32(0)
mut child_stdout_read := &u32(0) mut child_stdout_read := &u32(0)
mut child_stdout_write := &u32(0) mut child_stdout_write := &u32(0)
mut sa := SecurityAttributes {} mut sa := SecurityAttributes{}
sa.n_length = sizeof(C.SECURITY_ATTRIBUTES) sa.n_length = sizeof(C.SECURITY_ATTRIBUTES)
sa.b_inherit_handle = true sa.b_inherit_handle = true
create_pipe_ok := C.CreatePipe(voidptr(&child_stdout_read), voidptr(&child_stdout_write),
create_pipe_ok := C.CreatePipe(voidptr(&child_stdout_read), voidptr(&sa), 0)
voidptr(&child_stdout_write), voidptr(&sa), 0)
if !create_pipe_ok { if !create_pipe_ok {
error_num := int(C.GetLastError()) error_num := int(C.GetLastError())
error_msg := get_error_msg(error_num) error_msg := get_error_msg(error_num)
return error_with_code('exec failed (CreatePipe): $error_msg', error_num) return error_with_code('exec failed (CreatePipe): $error_msg', error_num)
} }
set_handle_info_ok := C.SetHandleInformation(child_stdout_read, C.HANDLE_FLAG_INHERIT, 0) set_handle_info_ok := C.SetHandleInformation(child_stdout_read, C.HANDLE_FLAG_INHERIT,
0)
if !set_handle_info_ok { if !set_handle_info_ok {
error_num := int(C.GetLastError()) error_num := int(C.GetLastError())
error_msg := get_error_msg(error_num) error_msg := get_error_msg(error_num)
return error_with_code('exec failed (SetHandleInformation): $error_msg', error_num) return error_with_code('exec failed (SetHandleInformation): $error_msg', error_num)
} }
proc_info := ProcessInformation{} proc_info := ProcessInformation{}
start_info := StartupInfo{ start_info := StartupInfo{
lp_reserved: 0 lp_reserved: 0
@ -263,11 +260,13 @@ pub fn exec(cmd string) ?Result {
} }
command_line := [32768]u16{} command_line := [32768]u16{}
C.ExpandEnvironmentStringsW(cmd.to_wide(), voidptr(&command_line), 32768) C.ExpandEnvironmentStringsW(cmd.to_wide(), voidptr(&command_line), 32768)
create_process_ok := C.CreateProcessW(0, command_line, 0, 0, C.TRUE, 0, 0, 0, voidptr(&start_info), voidptr(&proc_info)) create_process_ok := C.CreateProcessW(0, command_line, 0, 0, C.TRUE, 0, 0, 0, voidptr(&start_info),
voidptr(&proc_info))
if !create_process_ok { if !create_process_ok {
error_num := int(C.GetLastError()) error_num := int(C.GetLastError())
error_msg := get_error_msg(error_num) error_msg := get_error_msg(error_num)
return error_with_code('exec failed (CreateProcess) with code $error_num: $error_msg cmd: $cmd', error_num) return error_with_code('exec failed (CreateProcess) with code $error_num: $error_msg cmd: $cmd',
error_num)
} }
C.CloseHandle(child_stdin) C.CloseHandle(child_stdin)
C.CloseHandle(child_stdout_write) C.CloseHandle(child_stdout_write)
@ -275,7 +274,8 @@ pub fn exec(cmd string) ?Result {
mut bytes_read := u32(0) mut bytes_read := u32(0)
mut read_data := strings.new_builder(1024) mut read_data := strings.new_builder(1024)
for { for {
readfile_result := C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read), 0) readfile_result := C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read),
0)
read_data.write_bytes(buf, int(bytes_read)) read_data.write_bytes(buf, int(bytes_read))
if readfile_result == false || int(bytes_read) == 0 { if readfile_result == false || int(bytes_read) == 0 {
break break
@ -288,7 +288,7 @@ pub fn exec(cmd string) ?Result {
C.GetExitCodeProcess(proc_info.h_process, voidptr(&exit_code)) C.GetExitCodeProcess(proc_info.h_process, voidptr(&exit_code))
C.CloseHandle(proc_info.h_process) C.CloseHandle(proc_info.h_process)
C.CloseHandle(proc_info.h_thread) C.CloseHandle(proc_info.h_thread)
return Result { return Result{
output: soutput output: soutput
exit_code: int(exit_code) exit_code: int(exit_code)
} }
@ -296,8 +296,8 @@ pub fn exec(cmd string) ?Result {
fn C.CreateSymbolicLinkW(&u16, &u16, u32) int fn C.CreateSymbolicLinkW(&u16, &u16, u32) int
pub fn symlink(origin, target string) ?bool { pub fn symlink(origin string, target string) ?bool {
flags := if os.is_dir(origin) { 1 } else { 0 } flags := if is_dir(origin) { 1 } else { 0 }
if C.CreateSymbolicLinkW(origin.to_wide(), target.to_wide(), u32(flags)) != 0 { if C.CreateSymbolicLinkW(origin.to_wide(), target.to_wide(), u32(flags)) != 0 {
return true return true
} }
@ -316,11 +316,10 @@ pub fn (mut f File) close() {
pub struct ExceptionRecord { pub struct ExceptionRecord {
pub: pub:
// status_ constants // status_ constants
code u32 code u32
flags u32 flags u32
record &ExceptionRecord
record &ExceptionRecord address voidptr
address voidptr
param_count u32 param_count u32
// params []voidptr // params []voidptr
} }
@ -332,25 +331,22 @@ pub struct ContextRecord {
pub struct ExceptionPointers { pub struct ExceptionPointers {
pub: pub:
exception_record &ExceptionRecord exception_record &ExceptionRecord
context_record &ContextRecord context_record &ContextRecord
} }
pub type VectoredExceptionHandler = fn (&ExceptionPointers) u32 pub type VectoredExceptionHandler = fn ( &ExceptionPointers) u32
// This is defined in builtin because we use vectored exception handling // This is defined in builtin because we use vectored exception handling
// for our unhandled exception handler on windows // for our unhandled exception handler on windows
// As a result this definition is commented out to prevent // As a result this definition is commented out to prevent
// duplicate definitions from displeasing the compiler // duplicate definitions from displeasing the compiler
// fn C.AddVectoredExceptionHandler(u32, VectoredExceptionHandler) // fn C.AddVectoredExceptionHandler(u32, VectoredExceptionHandler)
pub fn add_vectored_exception_handler(first bool, handler VectoredExceptionHandler) { pub fn add_vectored_exception_handler(first bool, handler VectoredExceptionHandler) {
C.AddVectoredExceptionHandler(u32(first), C.PVECTORED_EXCEPTION_HANDLER(handler)) C.AddVectoredExceptionHandler(u32(first), C.PVECTORED_EXCEPTION_HANDLER(handler))
} }
// this is defined in builtin_windows.c.v in builtin // this is defined in builtin_windows.c.v in builtin
// fn C.IsDebuggerPresent() bool // fn C.IsDebuggerPresent() bool
pub fn debugger_present() bool { pub fn debugger_present() bool {
return C.IsDebuggerPresent() return C.IsDebuggerPresent()
} }
@ -367,25 +363,25 @@ pub fn uname() Uname {
} }
} }
// `is_writable_folder` - `folder` exists and is writable to the process // `is_writable_folder` - `folder` exists and is writable to the process
pub fn is_writable_folder(folder string) ?bool { pub fn is_writable_folder(folder string) ?bool {
if !os.exists(folder) { if !exists(folder) {
return error('`$folder` does not exist') return error('`$folder` does not exist')
} }
if !os.is_dir(folder) { if !is_dir(folder) {
return error('`folder` is not a folder') return error('`folder` is not a folder')
} }
tmp_perm_check := os.join_path(folder, 'tmp_perm_check_pid_' + getpid().str()) tmp_perm_check := join_path(folder, 'tmp_perm_check_pid_' + getpid().str())
mut f := os.open_file(tmp_perm_check, 'w+', 0o700) or { mut f := open_file(tmp_perm_check, 'w+', 0o700) or {
return error('cannot write to folder $folder: $err') return error('cannot write to folder $folder: $err')
} }
f.close() f.close()
os.rm(tmp_perm_check) rm(tmp_perm_check)
return true return true
} }
fn C._getpid() int fn C._getpid() int
[inline] [inline]
pub fn getpid() int { pub fn getpid() int {
return C._getpid() return C._getpid()

View File

@ -9,8 +9,8 @@ import benchmark
const ( const (
skip_files = [ skip_files = [
'vlib/v/checker/tests/return_missing_comp_if.vv' 'vlib/v/checker/tests/return_missing_comp_if.vv',
'vlib/v/checker/tests/return_missing_comp_if_nested.vv' 'vlib/v/checker/tests/return_missing_comp_if_nested.vv',
] ]
) )
@ -23,7 +23,7 @@ struct TaskDescription {
mut: mut:
is_error bool is_error bool
is_skipped bool is_skipped bool
is_module bool is_module bool
expected string expected string
found___ string found___ string
took time.Duration took time.Duration
@ -38,7 +38,7 @@ fn test_all() {
global_dir := '$classic_dir/globals' global_dir := '$classic_dir/globals'
global_tests := get_tests_in_dir(global_dir, false) global_tests := get_tests_in_dir(global_dir, false)
module_dir := '$classic_dir/modules' module_dir := '$classic_dir/modules'
module_tests := get_tests_in_dir(module_dir, true) module_tests := get_tests_in_dir(module_dir, true)
run_dir := '$classic_dir/run' run_dir := '$classic_dir/run'
run_tests := get_tests_in_dir(run_dir, false) run_tests := get_tests_in_dir(run_dir, false)
parser_dir := 'vlib/v/parser/tests' parser_dir := 'vlib/v/parser/tests'
@ -46,7 +46,8 @@ fn test_all() {
// -prod so that warns are errors // -prod so that warns are errors
mut tasks := []TaskDescription{} mut tasks := []TaskDescription{}
tasks << new_tasks(vexe, classic_dir, '-prod', '.out', classic_tests, false) tasks << new_tasks(vexe, classic_dir, '-prod', '.out', classic_tests, false)
tasks << new_tasks(vexe, global_dir, '--enable-globals', '.out', global_tests, false) tasks <<
new_tasks(vexe, global_dir, '--enable-globals', '.out', global_tests, false)
tasks << tasks <<
new_tasks(vexe, classic_dir, '--enable-globals run', '.run.out', ['globals_error.vv'], false) new_tasks(vexe, classic_dir, '--enable-globals run', '.run.out', ['globals_error.vv'], false)
tasks << new_tasks(vexe, module_dir, '-prod run', '.out', module_tests, true) tasks << new_tasks(vexe, module_dir, '-prod run', '.out', module_tests, true)
@ -55,7 +56,7 @@ fn test_all() {
tasks.run() tasks.run()
} }
fn new_tasks(vexe, dir, voptions, result_extension string, tests []string, is_module bool) []TaskDescription { fn new_tasks(vexe string, dir string, voptions string, result_extension string, tests []string, is_module bool) []TaskDescription {
paths := vtest.filter_vtest_only(tests, { paths := vtest.filter_vtest_only(tests, {
basepath: dir basepath: dir
}) })
@ -88,9 +89,7 @@ fn (mut tasks []TaskDescription) run() {
if tasks[i].path in m_skip_files { if tasks[i].path in m_skip_files {
tasks[i].is_skipped = true tasks[i].is_skipped = true
} }
unsafe { unsafe {work.push(&tasks[i])}
work.push(&tasks[i])
}
} }
work.close() work.close()
for _ in 0 .. vjobs { for _ in 0 .. vjobs {
@ -106,7 +105,7 @@ fn (mut tasks []TaskDescription) run() {
eprintln(bench.step_message_with_label_and_duration(benchmark.b_skip, task.path, eprintln(bench.step_message_with_label_and_duration(benchmark.b_skip, task.path,
task.took)) task.took))
continue continue
} }
if task.is_error { if task.is_error {
total_errors++ total_errors++
bench.fail() bench.fail()
@ -150,10 +149,10 @@ fn work_processor(mut work sync.Channel, mut results sync.Channel) {
// actual processing; NB: no output is done here at all // actual processing; NB: no output is done here at all
fn (mut task TaskDescription) execute() { fn (mut task TaskDescription) execute() {
if task.is_skipped { if task.is_skipped {
return return
} }
program := task.path program := task.path
cli_cmd := '$task.vexe $task.voptions $program' cli_cmd := '$task.vexe $task.voptions $program'
res := os.exec(cli_cmd) or { res := os.exec(cli_cmd) or {
panic(err) panic(err)
} }
@ -181,7 +180,7 @@ fn clean_line_endings(s string) string {
return res return res
} }
fn diff_content(s1, s2 string) { fn diff_content(s1 string, s2 string) {
diff_cmd := util.find_working_diff_command() or { diff_cmd := util.find_working_diff_command() or {
return return
} }