Skip to content

Commit a2b4f08

Browse files
committed
tools: cleanup vtest-all.v, using option values instead of comparison to <nothing>
Also allow for passing `rmfile: ['hw.linux', 'hw.linux.o']`
1 parent e104a5f commit a2b4f08

1 file changed

Lines changed: 49 additions & 35 deletions

File tree

‎cmd/tools/vtest-all.v‎

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import os
44
import term
55
import time
66

7+
type FnCheck = fn () !
8+
9+
type OneOrManyStrings = string | []string
10+
711
const vexe_path = os.getenv('VEXE')
812

913
const vroot = os.dir(vexe_path)
@@ -27,7 +31,7 @@ fn main() {
2731
// summary
2832
sw := time.new_stopwatch()
2933
for mut cmd in commands {
30-
cmd.run()
34+
cmd.run()?
3135
}
3236
spent := sw.elapsed().milliseconds()
3337
oks := commands.filter(it.ecode == 0)
@@ -55,29 +59,19 @@ enum RunCommandKind {
5559
execute
5660
}
5761

58-
const expect_nothing = '<nothing>'
59-
60-
const starts_with_nothing = '<nothing>'
61-
62-
const ends_with_nothing = '<nothing>'
63-
64-
const contains_nothing = '<nothing>'
65-
66-
type FnCheck = fn () !
67-
6862
struct Command {
6963
mut:
7064
line string
7165
label string // when set, the label will be printed *before* cmd.line is executed
7266
ecode int
7367
okmsg string
7468
errmsg string
75-
rmfile string
69+
rmfile ?OneOrManyStrings
7670
runcmd RunCommandKind = .system
77-
expect string = expect_nothing
78-
starts_with string = starts_with_nothing
79-
ends_with string = ends_with_nothing
80-
contains string = contains_nothing
71+
expect ?string
72+
starts_with ?string
73+
ends_with ?string
74+
contains ?string
8175
output string
8276
before_cb FnCheck = unsafe { nil }
8377
after_cb FnCheck = unsafe { nil }
@@ -228,18 +222,18 @@ fn get_all_commands() []Command {
228222
res << Command{
229223
line: '${vexe} -os linux -experimental -b native -o hw.linux examples/hello_world.v'
230224
okmsg: 'V compiles hello_world.v on the native backend for linux'
231-
rmfile: 'hw.linux'
225+
rmfile: ['hw.linux', 'hw.linux.o']
232226
}
233227
res << Command{
234228
line: '${vexe} -os macos -experimental -b native -o hw.macos examples/hello_world.v'
235229
okmsg: 'V compiles hello_world.v on the native backend for macos'
236-
rmfile: 'hw.macos'
230+
rmfile: ['hw.macos', 'hw.macos.o']
237231
}
238232
$if windows {
239233
res << Command{
240234
line: '${vexe} -os windows -experimental -b native -o hw.exe examples/hello_world.v'
241235
okmsg: 'V compiles hello_world.v on the native backend for windows'
242-
rmfile: 'hw.exe'
236+
rmfile: ['hw.exe', 'hw.o']
243237
}
244238
}
245239
//
@@ -447,7 +441,7 @@ fn get_all_commands() []Command {
447441
return res
448442
}
449443

450-
fn (mut cmd Command) run() {
444+
fn (mut cmd Command) run() ? {
451445
// Changing the current directory is needed for some of the compiler tests,
452446
// vlib/v/tests/local_test.v and vlib/v/tests/repl/repl_test.v
453447
os.chdir(vroot) or {}
@@ -490,25 +484,25 @@ fn (mut cmd Command) run() {
490484
if cmd.ecode != 0 {
491485
is_failed = true
492486
}
493-
if cmd.expect != expect_nothing {
487+
if cmd.expect != none {
494488
if cmd.output != cmd.expect {
495489
is_failed = true
496490
is_failed_expected = true
497491
}
498492
}
499-
if cmd.starts_with != starts_with_nothing {
493+
if cmd.starts_with != none {
500494
if !cmd.output.starts_with(cmd.starts_with) {
501495
is_failed = true
502496
is_failed_starts_with = true
503497
}
504498
}
505-
if cmd.ends_with != ends_with_nothing {
499+
if cmd.ends_with != none {
506500
if !cmd.output.ends_with(cmd.ends_with) {
507501
is_failed = true
508502
is_failed_ends_with = true
509503
}
510504
}
511-
if cmd.contains != contains_nothing {
505+
if cmd.contains != none {
512506
if !cmd.output.contains(cmd.contains) {
513507
is_failed = true
514508
is_failed_contains = true
@@ -523,36 +517,56 @@ fn (mut cmd Command) run() {
523517
eprintln('> output:\n${cmd.output}')
524518
}
525519
if is_failed && is_failed_starts_with {
526-
eprintln('> expected to start with:\n${cmd.starts_with}')
527-
eprintln('> output:\n${cmd.output#[..cmd.starts_with.len]}')
520+
eprintln('> expected to start with:\n${cmd.starts_with?}')
521+
eprintln('> output:\n${cmd.output#[..cmd.starts_with?.len]}')
528522
}
529523
if is_failed && is_failed_ends_with {
530-
eprintln('> expected to end with:\n${cmd.ends_with}')
531-
eprintln('> output:\n${cmd.output#[-cmd.starts_with.len..]}')
524+
eprintln('> expected to end with:\n${cmd.ends_with?}')
525+
eprintln('> output:\n${cmd.output#[-cmd.starts_with?.len..]}')
532526
}
533527
if is_failed && is_failed_contains {
534-
eprintln('> expected to contain:\n${cmd.contains}')
528+
eprintln('> expected to contain:\n${cmd.contains?}')
535529
eprintln('> output:\n${cmd.output}')
536530
}
537531
if vtest_nocleanup {
538532
return
539533
}
540-
if cmd.rmfile != '' {
534+
if cmd.rmfile != none {
541535
mut file_existed := rm_existing(cmd.rmfile)
542-
if os.user_os() == 'windows' {
543-
file_existed = file_existed || rm_existing(cmd.rmfile + '.exe')
544-
}
545536
if !file_existed {
546537
eprintln('Expected file did not exist: ${cmd.rmfile}')
547538
cmd.ecode = 999
548539
}
549540
}
550541
}
551542

543+
fn rm_existing(paths OneOrManyStrings) bool {
544+
match paths {
545+
string {
546+
return rm_existing_file(paths)
547+
}
548+
[]string {
549+
mut existing := false
550+
for path in paths {
551+
existing ||= rm_existing_file(path)
552+
}
553+
return existing
554+
}
555+
}
556+
return false
557+
}
558+
552559
// try to remove a file, return if it existed before the removal attempt
553-
fn rm_existing(path string) bool {
554-
existed := os.exists(path)
560+
fn rm_existing_file(path string) bool {
561+
mut existed := os.exists(path)
555562
os.rm(path) or {}
563+
564+
win_path := path + '.exe'
565+
if os.exists(win_path) {
566+
existed = true
567+
os.rm(win_path) or {}
568+
}
569+
556570
return existed
557571
}
558572

0 commit comments

Comments
 (0)