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

os: fix exec() and get_raw_line(); fix Windows tests and examples

This commit is contained in:
Delyan Angelov 2019-08-26 13:51:48 +03:00 committed by Alexander Medvednikov
parent fe50aeb130
commit aa438c7c3f
10 changed files with 106 additions and 54 deletions

View File

@ -38,10 +38,15 @@ script:
fi fi
- | - |
if [[ "${TRAVIS_OS_NAME}" == "windows" ]]; then if [[ "${TRAVIS_OS_NAME}" == "windows" ]]; then
git clone --depth=1 https://github.com/ubawurinna/freetype-windows-binaries thirdparty/freetype/
##echo "Just running ./make.bat to produce v.exe"
##./make.bat
##echo "Running only the repl tests directly"
##./v.exe ./compiler/tests/repl/repl_test.v
echo "Testing msvc bootstrapping" echo "Testing msvc bootstrapping"
#./make_msvc.bat ./make_msvc.bat
echo "Running make_tests.bat..." echo "Running make_tests.bat..."
#./make_tests.bat ./make_tests.bat
fi fi
- | - |
if [[ "${TRAVIS_OS_NAME}" != "windows" ]]; then if [[ "${TRAVIS_OS_NAME}" != "windows" ]]; then

View File

@ -76,6 +76,7 @@ fn test_mut_array() {
mut nums := [1, 2, 3] mut nums := [1, 2, 3]
modify_array(mut nums) modify_array(mut nums)
//assert nums.len == 4 //assert nums.len == 4
println(nums)
assert nums[0] == 20 assert nums[0] == 20
assert nums[1] == 4 assert nums[1] == 4
assert nums[2] == 6 assert nums[2] == 6

2
compiler/tests/repl/.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
*.repl text=auto eol=lf

View File

@ -1,30 +1,67 @@
import os import os
fn test_repl() { fn full_path_to_v() string {
test_files := os.walk_ext('.', '.repl') vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
wd := os.getwd() + '/' vexec := os.dir(os.dir(os.dir(os.dir( os.executable() )))) + os.PathSeparator + vname
for file in test_files { return vexec
content := os.read_file(file) or { }
assert false
break fn test_the_v_compiler_can_be_invoked() {
} vexec := full_path_to_v()
input_temporary_filename := 'input_temporary_filename.txt' println('vexecutable: $vexec')
input := content.all_before('===output===\n') /*
output := content.all_after('===output===\n') args := os.args
os.write_file(input_temporary_filename, input) vreal := os.realpath('v')
defer { myself := os.realpath( os.executable() )
os.rm(input_temporary_filename) wd := os.getwd() + os.PathSeparator
} println('args are: $args')
r := os.exec('./v < $input_temporary_filename') or { println('vreal : $vreal')
assert false println('myself : $myself')
break println('wd : $wd')
} */
result := r.output.replace('>>> ', '').replace('>>>', '').replace('... ', '').all_after('Use Ctrl-C or `exit` to exit\n').replace( wd, '' ) assert vexec != ''
assert result == output
if result != output { vcmd := '$vexec --version'
println(file) r := os.exec(vcmd) or { panic(err) }
println('Got : $result') //println('"$vcmd" exit_code: $r.exit_code | output: $r.output')
println('Expected : $output') assert r.exit_code == 0
}
} vcmd_error := '$vexec nonexisting.v'
r_error := os.exec(vcmd_error) or { panic(err) }
//println('"$vcmd_error" exit_code: $r_error.exit_code | output: $r_error.output')
assert r_error.exit_code == 1
assert r_error.output == '`nonexisting.v` does not exist'
}
fn test_the_v_repl() {
test_files := os.walk_ext('.', '.repl')
wd := os.getwd() + os.PathSeparator
vexec := full_path_to_v()
for file in test_files {
fcontent := os.read_file(file) or {
assert false
break
}
content := fcontent.replace('\r', '')
input := content.all_before('===output===\n')
output := content.all_after('===output===\n')
input_temporary_filename := 'input_temporary_filename.txt'
os.write_file(input_temporary_filename, input)
defer { os.rm(input_temporary_filename) }
r := os.exec('$vexec < $input_temporary_filename') or {
assert false
break
}
result := r.output.replace('\r','').replace('>>> ', '').replace('>>>', '').replace('... ', '').all_after('Use Ctrl-C or `exit` to exit\n').replace( wd, '' )
assert result == output
if result != output {
println(file)
println('Got : |$result|')
println('Expected : |$output|')
} else {
println('Repl file $file is OK')
}
}
} }

View File

@ -1,3 +1,5 @@
/*
module main module main
import pg import pg
@ -52,5 +54,6 @@ fn main() {
db.insert(nc) db.insert(nc)
*/ */
} }
*/

View File

@ -38,8 +38,8 @@ del v_win.c
del v2.exe del v2.exe
echo Success echo Success
goto :done
exit
:nomsvc :nomsvc
echo Cannot find an msvc installation echo Cannot find an msvc installation
@ -49,6 +49,11 @@ goto :error
echo Failed to compile - Create an issue at 'https://github.com/vlang' and tag '@emily33901'! echo Failed to compile - Create an issue at 'https://github.com/vlang' and tag '@emily33901'!
goto :error goto :error
:error :error
echo Exiting from error echo fail
exit /b 1 exit /b 1
:done
echo pass

View File

@ -18,18 +18,19 @@ echo build v using vc
vc.exe -o v.exe compiler vc.exe -o v.exe compiler
if %ERRORLEVEL% NEQ 0 goto :fail if %ERRORLEVEL% NEQ 0 goto :fail
echo build vc.msvc using vc
vc.exe -os msvc -o v.msvc.exe compiler
if %ERRORLEVEL% NEQ 0 goto :fail
setlocal EnableDelayedExpansion setlocal EnableDelayedExpansion
echo testing v echo testing v
v test v v test v
if %ERRORLEVEL% NEQ 0 goto :fail if %ERRORLEVEL% NEQ 0 goto :fail
echo testing v.msvc -os msvc echo skipping build vc.msvc using vc
v.msvc.exe -os msvc test v REM vc.exe -os msvc -o v.msvc.exe compiler
if %ERRORLEVEL% NEQ 0 goto :fail REM if %ERRORLEVEL% NEQ 0 goto :fail
echo skipping testing v.msvc -os msvc
REM v.msvc.exe -os msvc test v
REM if %ERRORLEVEL% NEQ 0 goto :fail
goto :done goto :done

View File

@ -13,6 +13,9 @@ import (
gl gl
) )
#flag windows -I @VROOT/thirdparty/freetype/include
#flag windows -L @VROOT/thirdparty/freetype/win64
#flag darwin -I/usr/local/include/freetype2 #flag darwin -I/usr/local/include/freetype2
#flag darwin -I/opt/local/include/freetype2 #flag darwin -I/opt/local/include/freetype2
#flag -lfreetype #flag -lfreetype

View File

@ -325,7 +325,7 @@ fn pclose(f *FILE) int {
return C._pclose(f) return C._pclose(f)
} }
$else { $else {
return C.pclose(f) return C.pclose(f) / 256 // WEXITSTATUS()
} }
} }
@ -349,7 +349,7 @@ pub fn exec(cmd string) ?Result {
res += tos(buf, strlen(buf)) res += tos(buf, strlen(buf))
} }
res = res.trim_space() res = res.trim_space()
exit_code := pclose(f)/256 exit_code := pclose(f)
//if exit_code != 0 { //if exit_code != 0 {
//return error(res) //return error(res)
//} //}
@ -509,19 +509,13 @@ pub fn get_line() string {
// get_raw_line returns a one-line string from stdin along with '\n' if there is any // get_raw_line returns a one-line string from stdin along with '\n' if there is any
pub fn get_raw_line() string { pub fn get_raw_line() string {
$if windows { $if windows {
max := 512 // MAX_PATH * sizeof(wchar_t) maxlinechars := 256
buf := &u16(malloc(max*2)) buf := &u16(malloc(maxlinechars*2))
h_input := C.GetStdHandle(STD_INPUT_HANDLE) res := int( C.fgetws(buf, maxlinechars, C.stdin ) )
if h_input == INVALID_HANDLE_VALUE { len := int( C.wcslen(buf) )
panic('get_raw_line() error getting input handle.') if 0 != res { return string_from_wide2( buf, len ) }
} return ''
mut nr_chars := 0 }
C.ReadConsole(h_input, buf, max, &nr_chars, 0)
if nr_chars == 0 {
return ''
}
return string_from_wide2(buf, nr_chars)
}
$else { $else {
//u64 is used because C.getline needs a size_t as second argument //u64 is used because C.getline needs a size_t as second argument
//Otherwise, it would cause a valgrind warning and may be dangerous //Otherwise, it would cause a valgrind warning and may be dangerous

View File

@ -15,7 +15,8 @@ const (
$if !windows { $if !windows {
#include <sys/time.h> #include <sys/time.h>
#include <sys/wait.h> //#include <sys/wait.h>
/// ^^^^ including this makes the windows build fail.
} }
struct Time { struct Time {