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

tests: add cmd/tools/vdoc/tests/vdoc_file_test.v, extract a v.util.diff module, cleanup

This commit is contained in:
Delyan Angelov
2021-06-22 18:52:34 +03:00
parent 0aef92b613
commit b6bd6d1d35
17 changed files with 212 additions and 128 deletions

View File

@@ -2,6 +2,7 @@ module main
import os
import v.util
import v.util.diff
import v.pref
import v.builder
import v.ast
@@ -13,6 +14,7 @@ const (
os_names = ['linux', 'macos', 'windows']
skip_modules = [
'builtin.bare',
'builtin.linux_bare.old',
'builtin.js',
'strconv',
'strconv.ftoa',
@@ -41,7 +43,7 @@ fn main() {
vroot := os.dir(vexe)
util.set_vroot_folder(vroot)
os.chdir(vroot)
cmd := util.find_working_diff_command() or { '' }
cmd := diff.find_working_diff_command() or { '' }
mut app := App{
diff_cmd: cmd
is_verbose: os.getenv('VERBOSE').len > 0
@@ -117,7 +119,7 @@ fn (app App) gen_api_for_module_in_os(mod_name string, os_name string) string {
}
fn (mut app App) compare_api(api_base string, api_os string, mod_name string, os_base string, os_target string) {
res := util.color_compare_strings(app.diff_cmd, rand.ulid(), api_base, api_os)
res := diff.color_compare_strings(app.diff_cmd, rand.ulid(), api_base, api_os)
if res.len > 0 {
summary := 'Different APIs found for module: `$mod_name`, between OS base: `$os_base` and OS: `$os_target`'
eprintln(term.header(summary, '-'))

View File

@@ -3,7 +3,7 @@ import os
const test_path = 'vcreate_test'
fn init_and_check() ? {
vexe := os.getenv('VEXE')
vexe := @VEXE
os.execute_or_panic('$vexe init')
assert os.read_file('vcreate_test.v') ? == [
@@ -53,12 +53,12 @@ fn test_v_init() ? {
fn test_v_init_in_git_dir() ? {
dir := os.join_path(os.temp_dir(), test_path)
os.rmdir_all(dir) or {}
os.execute_or_panic('git init $dir')
os.mkdir(dir) ?
defer {
os.rmdir_all(dir) or {}
}
os.chdir(dir)
os.execute_or_panic('git init .')
init_and_check() ?
}
@@ -72,7 +72,7 @@ fn test_v_init_no_overwrite_gitignore() ? {
}
os.chdir(dir)
vexe := os.getenv('VEXE')
vexe := @VEXE
os.execute_or_panic('$vexe init')
assert os.read_file('.gitignore') ? == 'blah'

View File

@@ -0,0 +1,3 @@
cmd/tools/vdoc/tests/testdata/project1/main.v:0:1: error: unexpected unknown, expecting `const`
1 | const (
2 | source_root = 'temp'

View File

@@ -0,0 +1,8 @@
const (
source_root = 'temp'
)
// funky - comment for function below
fn funky() {
println('hi')
}

View File

@@ -0,0 +1,72 @@
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 { '' }
}
fn test_vet() {
os.setenv('VCOLORS', 'never', true)
os.chdir(vroot)
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 {
mut nb_fail := 0
paths := vtest.filter_vtest_only(tests, basepath: vroot)
for path in paths {
program := path
print(path + ' ')
res := os.execute('$vexe doc $program')
if res.exit_code < 0 {
panic(res.output)
}
mut expected := os.read_file(program.replace('main.v', 'main.out')) or { panic(err) }
expected = clean_line_endings(expected)
found := clean_line_endings(res.output)
if expected != found {
println(term.red('FAIL'))
println('============')
println('expected:')
println(expected)
println('============')
println('found:')
println(found)
println('============\n')
println('diff:')
println(diff.color_compare_strings(diff_cmd, rand.ulid(), found, expected))
println('============\n')
nb_fail++
} else {
println(term.green('OK'))
}
}
return nb_fail
}
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
}

View File

@@ -11,6 +11,7 @@ import v.ast
import v.pref
import v.fmt
import v.util
import v.util.diff
import v.parser
import vhelp
@@ -203,25 +204,25 @@ fn (foptions &FormatOptions) post_process_file(file string, formatted_file_path
return
}
if foptions.is_diff {
diff_cmd := util.find_working_diff_command() or {
diff_cmd := diff.find_working_diff_command() or {
eprintln(err)
return
}
if foptions.is_verbose {
eprintln('Using diff command: $diff_cmd')
}
diff := util.color_compare_files(diff_cmd, file, formatted_file_path)
diff := diff.color_compare_files(diff_cmd, file, formatted_file_path)
if diff.len > 0 {
println(diff)
}
return
}
if foptions.is_verify {
diff_cmd := util.find_working_diff_command() or {
diff_cmd := diff.find_working_diff_command() or {
eprintln(err)
return
}
x := util.color_compare_files(diff_cmd, file, formatted_file_path)
x := diff.color_compare_files(diff_cmd, file, formatted_file_path)
if x.len != 0 {
println("$file is not vfmt'ed")
return error('')

View File

@@ -2,12 +2,12 @@ import os
import rand
import term
import v.util.vtest
import v.util
import v.util.diff
const diff_cmd = find_diff_cmd()
fn find_diff_cmd() string {
res := util.find_working_diff_command() or { '' }
res := diff.find_working_diff_command() or { '' }
return res
}
@@ -52,7 +52,7 @@ fn check_path(vexe string, dir string, tests []string) int {
println(found)
println('============\n')
println('diff:')
println(util.color_compare_strings(diff_cmd, rand.ulid(), found, expected))
println(diff.color_compare_strings(diff_cmd, rand.ulid(), found, expected))
println('============\n')
nb_fail++
} else {