2021-06-22 18:52:34 +03:00
|
|
|
import os
|
|
|
|
import rand
|
|
|
|
import term
|
|
|
|
import v.util.vtest
|
|
|
|
import v.util.diff
|
|
|
|
|
|
|
|
const vexe = @VEXE
|
|
|
|
|
|
|
|
const vroot = @VMODROOT
|
|
|
|
|
|
|
|
const diff_cmd = find_diff_cmd()
|
|
|
|
|
|
|
|
fn find_diff_cmd() string {
|
|
|
|
return diff.find_working_diff_command() or { '' }
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_vet() {
|
2021-06-22 18:52:34 +03:00
|
|
|
os.setenv('VCOLORS', 'never', true)
|
2022-10-20 22:14:33 +03:00
|
|
|
os.chdir(vroot)!
|
2021-06-22 18:52:34 +03:00
|
|
|
test_dir := 'cmd/tools/vdoc/tests/testdata'
|
|
|
|
main_files := get_main_files_in_dir(test_dir)
|
|
|
|
fails := check_path(vexe, test_dir, main_files)
|
|
|
|
assert fails == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_main_files_in_dir(dir string) []string {
|
|
|
|
mut mfiles := os.walk_ext(dir, '.v')
|
|
|
|
mfiles.sort()
|
|
|
|
return mfiles
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_path(vexe string, dir string, tests []string) int {
|
2023-03-30 16:48:21 +03:00
|
|
|
mut total_fails := 0
|
2021-06-22 18:52:34 +03:00
|
|
|
paths := vtest.filter_vtest_only(tests, basepath: vroot)
|
|
|
|
for path in paths {
|
2023-03-30 16:48:21 +03:00
|
|
|
mut fails := 0
|
2021-06-22 18:52:34 +03:00
|
|
|
program := path
|
|
|
|
print(path + ' ')
|
2023-03-30 16:48:21 +03:00
|
|
|
fails += check_output(
|
|
|
|
program: program
|
|
|
|
cmd: '${os.quoted_path(vexe)} doc ${os.quoted_path(program)}'
|
|
|
|
out_filename: 'main.out'
|
|
|
|
)
|
|
|
|
fails += check_output(
|
|
|
|
program: program
|
|
|
|
cmd: '${os.quoted_path(vexe)} doc -comments ${os.quoted_path(program)}'
|
|
|
|
out_filename: 'main.unsorted.out'
|
|
|
|
should_sort: false
|
|
|
|
)
|
|
|
|
fails += check_output(
|
|
|
|
program: program
|
|
|
|
cmd: '${os.quoted_path(vexe)} doc -comments ${os.quoted_path(program)}'
|
|
|
|
out_filename: 'main.comments.out'
|
|
|
|
)
|
2023-07-30 00:00:18 +03:00
|
|
|
fails += check_output(
|
|
|
|
program: program
|
|
|
|
cmd: '${os.quoted_path(vexe)} doc -readme -comments ${os.quoted_path(program)}'
|
|
|
|
out_filename: 'main.readme.comments.out'
|
|
|
|
)
|
2023-03-30 16:48:21 +03:00
|
|
|
total_fails += fails
|
|
|
|
if fails == 0 {
|
2021-06-22 18:52:34 +03:00
|
|
|
println(term.green('OK'))
|
|
|
|
}
|
2023-03-30 16:48:21 +03:00
|
|
|
flush_stdout()
|
2021-06-22 18:52:34 +03:00
|
|
|
}
|
2023-03-30 16:48:21 +03:00
|
|
|
return total_fails
|
2021-06-22 18:52:34 +03:00
|
|
|
}
|
|
|
|
|
2021-08-19 10:20:43 +03:00
|
|
|
fn print_compare(expected string, found string) {
|
|
|
|
println(term.red('FAIL'))
|
|
|
|
println('============')
|
|
|
|
println('expected:')
|
|
|
|
println(expected)
|
|
|
|
println('============')
|
|
|
|
println('found:')
|
|
|
|
println(found)
|
|
|
|
println('============\n')
|
|
|
|
println('diff:')
|
2023-07-29 16:41:02 +03:00
|
|
|
println(diff.color_compare_strings(diff_cmd, rand.ulid(), expected, found))
|
2021-08-19 10:20:43 +03:00
|
|
|
println('============\n')
|
|
|
|
}
|
|
|
|
|
2021-06-22 18:52:34 +03:00
|
|
|
fn clean_line_endings(s string) string {
|
|
|
|
mut res := s.trim_space()
|
|
|
|
res = res.replace(' \n', '\n')
|
|
|
|
res = res.replace(' \r\n', '\n')
|
|
|
|
res = res.replace('\r\n', '\n')
|
|
|
|
res = res.trim('\n')
|
|
|
|
return res
|
|
|
|
}
|
2023-03-30 16:48:21 +03:00
|
|
|
|
|
|
|
[params]
|
|
|
|
struct CheckOutputParams {
|
|
|
|
program string = 'some/dir/main.v'
|
|
|
|
cmd string = 'v doc'
|
|
|
|
main_filename string = 'main.v'
|
|
|
|
out_filename string = 'main.out'
|
|
|
|
should_sort bool = true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_output(params CheckOutputParams) int {
|
|
|
|
out_file_path := params.program.replace(params.main_filename, params.out_filename)
|
|
|
|
if !os.exists(out_file_path) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
mut fails := 0
|
|
|
|
mut expected := os.read_file(out_file_path) or { panic(err) }
|
|
|
|
expected = clean_line_endings(expected)
|
|
|
|
|
|
|
|
os.setenv('VDOC_SORT', params.should_sort.str(), true)
|
|
|
|
res := os.execute(params.cmd)
|
|
|
|
|
|
|
|
if res.exit_code < 0 {
|
|
|
|
panic(res.output)
|
|
|
|
}
|
|
|
|
found := clean_line_endings(res.output)
|
|
|
|
if expected != found {
|
|
|
|
print_compare(expected, found)
|
|
|
|
eprintln('>>> out_file_path: ${out_file_path}')
|
|
|
|
eprintln('>>> cmd: VDOC_SORT=${params.should_sort} ${params.cmd}')
|
|
|
|
fails++
|
|
|
|
}
|
|
|
|
return fails
|
|
|
|
}
|