fmt: update tests to run all fmt also in a vmodules context (#20995)

This commit is contained in:
Turiiya 2024-03-12 16:11:27 +01:00 committed by GitHub
parent a373bee98b
commit 365bd18542
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 82 additions and 108 deletions

7
.gitignore vendored
View File

@ -39,9 +39,10 @@ a.out
fns.txt fns.txt
.noprefix.vrepl_temp.v .noprefix.vrepl_temp.v
# ignore temp directories # ignore temp and cache directories
/temp temp/
/tmp tmp/
cache/
# unignore special files without extension # unignore special files without extension
!.github/PULL_REQUEST_TEMPLATE !.github/PULL_REQUEST_TEMPLATE

View File

@ -11,43 +11,32 @@ import v.pref
import v.util.diff import v.util.diff
import v.util.vtest import v.util.vtest
const error_missing_vexe = 1 const vroot = @VEXEROOT
const error_failed_tests = 2
const fpref = &pref.Preferences{ const fpref = &pref.Preferences{
is_fmt: true is_fmt: true
} }
const vexe = os.getenv('VEXE')
fn test_fmt() { fn run_fmt(mut input_files []string) {
fmt_message := 'checking that v fmt keeps already formatted files *unchanged*' fmt_message := 'checking that v fmt keeps already formatted files *unchanged*'
eprintln(term.header(fmt_message, '-')) eprintln(term.header(fmt_message, '-'))
if vexe.len == 0 || !os.exists(vexe) { assert input_files.len > 0
eprintln('VEXE must be set') input_files = vtest.filter_vtest_only(input_files)
exit(error_missing_vexe) if input_files.len == 0 {
// No need to produce a failing test here.
eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY'))
exit(0)
} }
vroot := os.dir(vexe) input_files.sort()
os.chdir(vroot) or {} mut fmt_bench := benchmark.new_benchmark()
basepath := vroot + '/' fmt_bench.set_total_expected_steps(input_files.len + 1)
tmpfolder := os.temp_dir() tmpfolder := os.temp_dir()
diff_cmd := diff.find_working_diff_command() or { '' } diff_cmd := diff.find_working_diff_command() or { '' }
mut fmt_bench := benchmark.new_benchmark()
keep_input_files := os.walk_ext('vlib/v/fmt/tests', '_keep.vv')
expected_input_files := os.walk_ext('vlib/v/fmt/tests', '_expected.vv')
mut input_files := []string{}
input_files << keep_input_files
input_files << expected_input_files
input_files = vtest.filter_vtest_only(input_files, basepath: vroot)
input_files.sort()
fmt_bench.set_total_expected_steps(input_files.len + 1)
for istep, ipath in input_files { for istep, ipath in input_files {
fmt_bench.cstep = istep + 1 fmt_bench.cstep = istep + 1
fmt_bench.step() fmt_bench.step()
ifilename := os.file_name(ipath) expected_ocontent := os.read_file(ipath) or {
vrelpath := ipath.replace(basepath, '')
opath := ipath
expected_ocontent := os.read_file(opath) or {
fmt_bench.fail() fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('cannot read from ${vrelpath}')) eprintln(fmt_bench.step_message_fail('cannot read from ${ipath}'))
continue continue
} }
mut table := ast.new_table() mut table := ast.new_table()
@ -55,23 +44,44 @@ fn test_fmt() {
result_ocontent := fmt.fmt(file_ast, mut table, fpref, false) result_ocontent := fmt.fmt(file_ast, mut table, fpref, false)
if expected_ocontent != result_ocontent { if expected_ocontent != result_ocontent {
fmt_bench.fail() fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('file ${vrelpath} after formatting, does not look as expected.')) eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.'))
if diff_cmd == '' { if diff_cmd == '' {
eprintln('>> sorry, but no working "diff" CLI command can be found') eprintln('>> sorry, but no working "diff" CLI command can be found')
continue continue
} }
vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}') vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}')
os.write_file(vfmt_result_file, result_ocontent) or { panic(err) } os.write_file(vfmt_result_file, result_ocontent) or { panic(err) }
eprintln(diff.color_compare_files(diff_cmd, opath, vfmt_result_file)) eprintln(diff.color_compare_files(diff_cmd, ipath, vfmt_result_file))
continue continue
} }
fmt_bench.ok() fmt_bench.ok()
eprintln(fmt_bench.step_message_ok(vrelpath)) eprintln(fmt_bench.step_message_ok(ipath))
} }
fmt_bench.stop() fmt_bench.stop()
eprintln(term.h_divider('-')) eprintln(term.h_divider('-'))
eprintln(fmt_bench.total_message(fmt_message)) eprintln(fmt_bench.total_message(fmt_message))
if fmt_bench.nfail > 0 { assert fmt_bench.nfail == 0
exit(error_failed_tests)
} }
fn get_test_files(path string) []string {
mut files := []string{}
mut ref := &files
os.walk(path, fn [mut ref] (p string) {
if p.ends_with('_keep.vv') || p.ends_with('_expected.vv') {
ref << p
}
})
return files
}
fn test_fmt() {
mut input_files := get_test_files(os.join_path(vroot, 'vlib', 'v', 'fmt', 'tests'))
run_fmt(mut input_files)
}
fn test_fmt_vmodules() {
vmodules_tdir := os.join_path(vroot, 'vlib', 'v', 'fmt', 'testdata', 'vmodules')
os.setenv('VMODULES', vmodules_tdir, true)
mut input_files := get_test_files(vmodules_tdir)
run_fmt(mut input_files)
} }

View File

@ -9,32 +9,30 @@ import v.fmt
import v.parser import v.parser
import v.pref import v.pref
import v.util.diff import v.util.diff
import v.util.vtest
const error_missing_vexe = 1 const vroot = @VEXEROOT
const error_failed_tests = 2
const fpref = &pref.Preferences{ const fpref = &pref.Preferences{
is_fmt: true is_fmt: true
} }
fn test_fmt() { fn run_fmt(mut input_files []string) {
fmt_message := 'vfmt tests' fmt_message := 'vfmt tests'
eprintln(term.header(fmt_message, '-')) eprintln(term.header(fmt_message, '-'))
vexe := os.getenv('VEXE')
if vexe.len == 0 || !os.exists(vexe) {
eprintln('VEXE must be set')
exit(error_missing_vexe)
}
vroot := os.dir(vexe)
tmpfolder := os.temp_dir() tmpfolder := os.temp_dir()
diff_cmd := diff.find_working_diff_command() or { '' } diff_cmd := diff.find_working_diff_command() or { '' }
assert input_files.len > 0
input_files = vtest.filter_vtest_only(input_files)
if input_files.len == 0 {
// No need to produce a failing test here.
eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY'))
exit(0)
}
mut fmt_bench := benchmark.new_benchmark() mut fmt_bench := benchmark.new_benchmark()
// Lookup the existing test _input.vv files:
input_files := os.walk_ext('${vroot}/vlib/v/fmt/tests', '_input.vv')
fmt_bench.set_total_expected_steps(input_files.len) fmt_bench.set_total_expected_steps(input_files.len)
for istep, ipath in input_files { for istep, ipath in input_files {
fmt_bench.cstep = istep fmt_bench.cstep = istep
fmt_bench.step() fmt_bench.step()
ifilename := os.file_name(ipath)
opath := ipath.replace('_input.vv', '_expected.vv') opath := ipath.replace('_input.vv', '_expected.vv')
if !os.exists(opath) { if !os.exists(opath) {
fmt_bench.fail() fmt_bench.fail()
@ -56,18 +54,29 @@ fn test_fmt() {
eprintln('>> sorry, but no working "diff" CLI command can be found') eprintln('>> sorry, but no working "diff" CLI command can be found')
continue continue
} }
vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}') vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}')
os.write_file(vfmt_result_file, result_ocontent) or { panic(err) } os.write_file(vfmt_result_file, result_ocontent) or { panic(err) }
eprintln(diff.color_compare_files(diff_cmd, opath, vfmt_result_file)) eprintln(diff.color_compare_files(diff_cmd, opath, vfmt_result_file))
continue continue
} }
fmt_bench.ok() fmt_bench.ok()
eprintln(fmt_bench.step_message_ok('${ipath}')) eprintln(fmt_bench.step_message_ok(ipath))
} }
fmt_bench.stop() fmt_bench.stop()
eprintln(term.h_divider('-')) eprintln(term.h_divider('-'))
eprintln(fmt_bench.total_message(fmt_message)) eprintln(fmt_bench.total_message(fmt_message))
if fmt_bench.nfail > 0 { assert fmt_bench.nfail == 0
exit(error_failed_tests)
} }
fn test_fmt() {
mut input_files := os.walk_ext(os.join_path(vroot, 'vlib', 'v', 'fmt', 'tests'), '_input.vv')
run_fmt(mut input_files)
} }
// NOTE: enable with upcommming `__input.vv` / `__expected.vv` files for vmodules
/* fn test_fmt_vmodules() {
vmodules_tdir := os.join_path(vroot, 'vlib', 'v', 'fmt', 'testdata', 'vmodules')
os.setenv('VMODULES', vmodules_tdir, true)
mut input_files := os.walk_ext(vmodules_tdir, '_input.vv')
run_fmt(mut input_files)
} */

View File

@ -1,55 +0,0 @@
import os
const vexe = os.quoted_path(@VEXE)
const vmodules_tdir = os.join_path(os.vtmp_dir(), 'fmt_vmodules_test')
fn testsuite_begin() {
os.mkdir_all(vmodules_tdir) or {}
os.setenv('VMODULES', vmodules_tdir, true)
}
fn testsuite_end() {
os.rmdir_all(vmodules_tdir) or {}
}
fn test_fmt_imports() {
mod_tdir := os.join_path(vmodules_tdir, @FN)
os.mkdir_all(mod_tdir)!
tfile_content := [
'import x.json2 as json',
'import datatypes { Stack }',
'',
'const foo = Stack[string]{}',
'',
].join_lines()
os.write_file(os.join_path(mod_tdir, 'main.v'), tfile_content)!
os.execute_opt('${vexe} fmt -c ${mod_tdir}') or { assert false, err.msg() }
}
fn test_fmt_submod_type_alias() {
mod_tdir := os.join_path(vmodules_tdir, @FN)
mod_src_tdir := os.join_path(mod_tdir, 'src')
submod_tdir := os.join_path(mod_tdir, 'bar', 'baz')
os.mkdir_all(mod_src_tdir)!
os.mkdir_all(submod_tdir)!
tfile_content := [
'module ${@FN}',
'',
'import bar.baz',
'',
'type MyAlias = baz.Baz',
'',
].join_lines()
submod_tfile_content := [
'module baz',
'',
'enum BarBaz {',
' bar',
' baz',
'}',
'',
].join_lines()
os.write_file(os.join_path(mod_src_tdir, 'foo.v'), tfile_content)!
os.write_file(os.join_path(submod_tdir, 'baz.v'), submod_tfile_content)!
os.execute_opt('${vexe} fmt -c ${mod_tdir}') or { assert false, err.msg() }
}

View File

@ -0,0 +1 @@
module baz

View File

@ -0,0 +1,7 @@
// The module name has to match the modules dir name.
module submod_type_alias
// The submod dir structure that is imported has to be existent.
import bar.baz
type MyAlias = baz.Baz

View File

@ -2,6 +2,7 @@ module vtest
import os import os
@[params]
pub struct FilterVTestConfig { pub struct FilterVTestConfig {
basepath string basepath string
fix_slashes bool = true fix_slashes bool = true
@ -13,13 +14,13 @@ pub fn filter_vtest_only(paths []string, config FilterVTestConfig) []string {
patterns := os.getenv('VTEST_ONLY').split(',') patterns := os.getenv('VTEST_ONLY').split(',')
for relative_path in paths { for relative_path in paths {
mut file := relative_path mut file := relative_path
if config.basepath.len > 0 { if config.basepath != '' {
file = os.join_path_single(config.basepath, file) file = os.join_path_single(config.basepath, file)
} }
if config.fix_slashes { if config.fix_slashes {
file = file.replace('\\', '/') file = file.replace('\\', '/')
} }
if patterns.len != 0 { if patterns.len > 0 {
mut found := 0 mut found := 0
for okpat in patterns { for okpat in patterns {
if file.contains(okpat) { if file.contains(okpat) {