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

v test v => v test-compiler

This commit is contained in:
Delyan Angelov
2019-12-01 11:50:13 +02:00
committed by Alexander Medvednikov
parent 854309a7d8
commit ec15bfb7d1
29 changed files with 431 additions and 280 deletions

9
tools/.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
gen_vc
performance_compare
vcreate
vnames
vpm
vrepl
vtest
vtest-compiler
vup

View File

@@ -39,7 +39,7 @@ fn main() {
commit_date := exec('git log -n1 --pretty="format:%at"')
message := exec('git log -n1 --pretty="format:%s"')
date := time.unix(commit_date.int())
out := os.create('table.html') or { panic(err) }
mut out := os.create('table.html') or { panic(err) }
// Place the new row on top
table =
'<tr>
@@ -57,7 +57,7 @@ fn main() {
// Regenerate index.html
header := os.read_file('header.html') or { panic(err) }
footer := os.read_file('footer.html') or { panic(err) }
res := os.create('index.html') or { panic(err) }
mut res := os.create('index.html') or { panic(err) }
res.writeln(header)
res.writeln(table)
res.writeln(footer)

View File

@@ -45,7 +45,7 @@ const(
// name
app_name = 'gen_vc'
// version
app_version = '0.1.0'
app_version = '0.1.1'
// description
app_description = 'This tool regenerates V\'s bootstrap .c files every time the V master branch is updated.'
// assume something went wrong if file size less than this
@@ -114,17 +114,22 @@ fn main() {
fp.version(app_version)
fp.description(app_description)
fp.skip_executable()
show_help:=fp.bool('help', false, 'Show this help screen\n')
flag_options := parse_flags(mut fp)
_ = fp.finalize() or {
if( show_help ){ println( fp.usage() ) exit(0) }
fp.finalize() or {
eprintln(err)
println(fp.usage())
return
}
// webhook server mode
if flag_options.serve {
vweb.run<WebhookServer>(flag_options.port)
app := WebhookServer{ gen_vc: new_gen_vc(flag_options) }
vweb.run(mut app, flag_options.port)
}
// cmd mode
else {
@@ -136,15 +141,14 @@ fn main() {
// new GenVC
fn new_gen_vc(flag_options FlagOptions) &GenVC {
mut logger := &log.Log{}
logger.set_level(log.DEBUG)
if flag_options.log_to == 'file' {
logger.set_full_logpath( flag_options.log_file )
}
return &GenVC{
// options
options: flag_options
// logger
logger: if flag_options.log_to == 'file' {
&log.Log{log.DEBUG, flag_options.log_file}
} else {
&log.Log{log.DEBUG, 'terminal'}
}
logger: logger
}
}
@@ -175,7 +179,7 @@ fn parse_flags(fp mut flag.FlagParser) FlagOptions {
purge : fp.bool('purge', false, 'force purge the local repositories')
port : fp.int('port', int(server_port), 'port for web server to listen on')
log_to : fp.string('log-to', log_to, 'log to is \'file\' or \'terminal\'')
log_file : fp.string('log_file', log_file, 'log file to use when log-to is \'file\'')
log_file : fp.string('log-file', log_file, 'log file to use when log-to is \'file\'')
dry_run : fp.bool('dry-run', dry_run, 'when specified dont push anything to remote repo')
}
}

View File

@@ -0,0 +1,126 @@
module testing
import (
os
term
benchmark
filepath
)
pub struct TestSession {
pub mut:
files []string
vexe string
vargs string
failed bool
benchmark benchmark.Benchmark
ok string
fail string
}
pub fn new_test_sesion(vargs string) TestSession {
return TestSession{
vexe: vexe_path()
vargs: vargs
}
}
pub fn vexe_path() string {
// NB: tools extracted from v require that the first
// argument to them to be the v executable location.
// They are usually launched by vlib/compiler/vtools.v,
// launch_tool/1 , which provides it.
return os.args[1]
}
pub fn (ts mut TestSession) init() {
ts.ok = term.ok_message('OK')
ts.fail = term.fail_message('FAIL')
ts.benchmark = benchmark.new_benchmark()
}
pub fn (ts mut TestSession) test() {
ts.init()
show_stats := '-stats' in ts.vargs.split(' ')
for dot_relative_file in ts.files {
relative_file := dot_relative_file.replace('./', '')
file := os.realpath( relative_file )
$if windows {
if file.contains('sqlite') { continue }
}
$if msvc {
if file.contains('asm') { continue }
}
$if tinyc {
if file.contains('asm') { continue }
}
tmpc_filepath := file.replace('.v', '.tmp.c')
cmd := '"$ts.vexe" $ts.vargs "$file"'
ts.benchmark.step()
if show_stats {
println('-------------------------------------------------')
status := os.system(cmd)
if status == 0 {
ts.benchmark.ok()
}else{
ts.benchmark.fail()
ts.failed = true
continue
}
}else{
r := os.exec(cmd) or {
ts.benchmark.fail()
ts.failed = true
println(ts.benchmark.step_message('$relative_file ${ts.fail}'))
continue
}
if r.exit_code != 0 {
ts.benchmark.fail()
ts.failed = true
println(ts.benchmark.step_message('$relative_file ${ts.fail}\n`$file`\n (\n$r.output\n)'))
} else {
ts.benchmark.ok()
println(ts.benchmark.step_message('$relative_file ${ts.ok}'))
}
}
os.rm( tmpc_filepath )
}
ts.benchmark.stop()
}
pub fn vlib_should_be_present( parent_dir string ) {
vlib_dir := filepath.join( parent_dir, 'vlib' )
if !os.dir_exists( vlib_dir ){
println('$vlib_dir is missing, it must be next to the V executable')
exit(1)
}
}
pub fn v_build_failing(vargs string, folder string) bool {
main_label := 'Building $folder ...'
finish_label := 'building $folder'
vexe := vexe_path()
parent_dir := os.dir(vexe)
vlib_should_be_present( parent_dir )
println(main_label)
mut session := new_test_sesion( vargs )
files := os.walk_ext(filepath.join(parent_dir, folder),'.v')
mains := files.filter(!it.contains('modules'))
mut rebuildable_mains := mains
if os.user_os() == 'windows' {
// on windows, an executable can not be rebuilt, while it is running
myself := os.executable().replace('.exe', '') + '.v'
mains_without_myself := mains.filter(!it.contains(myself))
rebuildable_mains = mains_without_myself // workaround a bug in it.contains generation
}
session.files << rebuildable_mains
session.test()
println( session.benchmark.total_message( finish_label ) )
return session.failed
}

14
tools/vbuild-examples.v Normal file
View File

@@ -0,0 +1,14 @@
module main
import (
os
testing
)
fn main() {
args := os.args
args_string := args[1..].join(' ')
if testing.v_build_failing(args_string.all_before('build-examples'), 'examples') {
exit(1)
}
}

14
tools/vbuild-tools.v Normal file
View File

@@ -0,0 +1,14 @@
module main
import (
os
testing
)
fn main() {
args := os.args
args_string := args[1..].join(' ')
if testing.v_build_failing(args_string.all_before('build-tools'), 'tools') {
exit(1)
}
}

View File

@@ -19,7 +19,7 @@ fn cerror(e string){
}
fn (c Create)write_vmod() {
vmod := os.create('${c.name}/v.mod') or { cerror(err) exit(1) }
mut vmod := os.create('${c.name}/v.mod') or { cerror(err) exit(1) }
mut vmod_content := []string
vmod_content << '#V Project#\n'
vmod_content << 'Module {'
@@ -31,7 +31,7 @@ fn (c Create)write_vmod() {
}
fn (c Create)write_main() {
main := os.create('${c.name}/${c.name}.v') or { cerror(err) exit(2) }
mut main := os.create('${c.name}/${c.name}.v') or { cerror(err) exit(2) }
mut main_content := []string
main_content << 'module main\n'
main_content << 'fn main() {'

77
tools/vtest-compiler.v Normal file
View File

@@ -0,0 +1,77 @@
module main
import (
os
testing
benchmark
)
pub const (
v_modules_path = os.home_dir() + '.vmodules'
)
fn main() {
args := os.args
args_string := args[1..].join(' ')
v_test_compiler(args_string.all_before('test-compiler'))
}
fn v_test_compiler(vargs string){
vexe := testing.vexe_path()
parent_dir := os.dir(vexe)
testing.vlib_should_be_present( parent_dir )
// Changing the current directory is needed for some of the compiler tests,
// compiler/tests/local_test.v and compiler/tests/repl/repl_test.v
os.chdir( parent_dir )
/*
if !os.file_exists(parent_dir + '/v.v') {
println('v.v is missing, it must be next to the V executable')
exit(1)
}
*/
// Make sure v.c can be compiled without warnings
$if mac {
if os.file_exists('/v.v') {
os.system('$vexe -o v.c v.v')
if os.system('cc -Werror v.c') != 0 {
println('cc failed to build v.c without warnings')
exit(1)
}
println('v.c can be compiled without warnings. This is good :)')
}
}
building_tools_failed := testing.v_build_failing(vargs, 'tools')
println('\nTesting all _test.v files...')
mut compiler_test_session := testing.new_test_sesion( vargs )
compiler_test_session.files << os.walk_ext(parent_dir, '_test.v')
compiler_test_session.test()
println( compiler_test_session.benchmark.total_message('running V tests') )
println('')
building_examples_failed := testing.v_build_failing(vargs, 'examples')
v_module_install_cmd := '$vexe install nedpals.args'
println('\nInstalling a v module with: $v_module_install_cmd ')
mut vmark := benchmark.new_benchmark()
ret := os.system(v_module_install_cmd)
if ret != 0 {
println('failed to run v install')
exit(1)
}
if !os.file_exists(v_modules_path + '/nedpals/args') {
println('v failed to install a test module')
exit(1)
}
vmark.stop()
println( 'Installing a v module took: ' + vmark.total_duration().str() + 'ms')
if building_tools_failed || compiler_test_session.failed || building_examples_failed {
exit(1)
}
}

View File

@@ -2,44 +2,17 @@ module main
import (
os
term
benchmark
testing
)
struct TestSession {
mut:
files []string
vexe string
vargs string
failed bool
benchmark benchmark.Benchmark
}
pub fn new_test_sesion(vargs string) TestSession {
return TestSession{
vexe: vexe_path()
vargs: vargs
}
}
fn vexe_path() string {
// NB: tools extracted from v require that the first
// argument to them to be the v executable location.
// They are usually launched by vlib/compiler/vtools.v,
// launch_tool/1 , which provides it.
return os.args[1]
}
pub fn main() {
args := os.args
if args.last() == 'test' {
println('Usage:')
println(' A)')
println(' v test v : run all v tests and build all the examples')
println(' B)')
println(' v test folder/ : run all v tests in the given folder.')
println(' v -stats test folder/ : the same, but print more stats.')
println(' C)')
println(' B)')
println(' v test file_test.v : run test functions in a given test file.')
println(' v -stats test file_test.v : as above, but with more stats.')
println(' NB: you can also give many and mixed folder/ file_test.v arguments after test.')
@@ -52,11 +25,12 @@ pub fn main() {
args_after := args_string.all_after('test ')
if args_after == 'v' {
v_test_v(args_before)
return
eprintln('`v test v` has been deprecated.')
eprintln('Use `v test-compiler` instead.')
exit(1)
}
mut ts := new_test_sesion(args_before)
mut ts := testing.new_test_sesion(args_before)
for targ in args_after.split(' ') {
if os.file_exists(targ) && targ.ends_with('_test.v') {
ts.files << targ
@@ -79,119 +53,3 @@ pub fn main() {
}
}
pub fn (ts mut TestSession) test() {
ok := term.ok_message('OK')
fail := term.fail_message('FAIL')
show_stats := '-stats' in ts.vargs.split(' ')
ts.benchmark = benchmark.new_benchmark()
for dot_relative_file in ts.files {
relative_file := dot_relative_file.replace('./', '')
file := os.realpath( relative_file )
$if windows {
if file.contains('sqlite') { continue }
}
$if msvc {
if file.contains('asm') { continue }
}
$if tinyc {
if file.contains('asm') { continue }
}
tmpc_filepath := file.replace('.v', '.tmp.c')
cmd := '"$ts.vexe" $ts.vargs "$file"'
ts.benchmark.step()
if show_stats {
println('-------------------------------------------------')
status := os.system(cmd)
if status == 0 {
ts.benchmark.ok()
}else{
ts.benchmark.fail()
ts.failed = true
continue
}
}else{
r := os.exec(cmd) or {
ts.benchmark.fail()
ts.failed = true
println(ts.benchmark.step_message('$relative_file $fail'))
continue
}
if r.exit_code != 0 {
ts.benchmark.fail()
ts.failed = true
println(ts.benchmark.step_message('$relative_file $fail\n`$file`\n (\n$r.output\n)'))
} else {
ts.benchmark.ok()
println(ts.benchmark.step_message('$relative_file $ok'))
}
}
os.rm( tmpc_filepath )
}
ts.benchmark.stop()
}
pub fn v_test_v(args_before_test string){
vexe := vexe_path()
parent_dir := os.dir(vexe)
// Changing the current directory is needed for some of the compiler tests,
// compiler/tests/local_test.v and compiler/tests/repl/repl_test.v
os.chdir( parent_dir )
if !os.dir_exists(parent_dir + '/vlib') {
println('vlib/ is missing, it must be next to the V executable')
exit(1)
}
/*
if !os.file_exists(parent_dir + '/v.v') {
println('v.v is missing, it must be next to the V executable')
exit(1)
}
*/
// Make sure v.c can be compiled without warnings
$if mac {
if os.file_exists('/v.v') {
os.system('$vexe -o v.c v.v')
if os.system('cc -Werror v.c') != 0 {
println('cc failed to build v.c without warnings')
exit(1)
}
println('v.c can be compiled without warnings. This is good :)')
}
}
//
println('Testing...')
mut ts := new_test_sesion( args_before_test )
ts.files << os.walk_ext(parent_dir, '_test.v')
ts.test()
println( ts.benchmark.total_message('running V tests') )
//
println('\nBuilding examples...')
mut es := new_test_sesion( args_before_test )
files := os.walk_ext(parent_dir+'/examples','.v')
stable := files.filter(!it.contains('automaton.v') && !it.contains('some_module.v'))
es.files << stable
es.test()
println( es.benchmark.total_message('building examples') )
//
test_vget()
if ts.failed || es.failed {
exit(1)
}
}
pub fn test_vget() {
/*
vexe := vexe_path()
ret := os.system('$vexe install nedpals.args')
if ret != 0 {
println('failed to run v install')
exit(1)
}
if !os.file_exists(v_modules_path + '/nedpals/args') {
println('v failed to install a test module')
exit(1)
}
println('vget is OK')
*/
}