2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2021-03-19 12:13:03 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-05-18 18:43:04 +02:00
|
|
|
import os
|
|
|
|
import term
|
|
|
|
import benchmark
|
|
|
|
import v.ast
|
|
|
|
import v.fmt
|
|
|
|
import v.parser
|
|
|
|
import v.pref
|
2021-06-22 17:52:34 +02:00
|
|
|
import v.util.diff
|
2020-05-18 18:43:04 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
error_missing_vexe = 1
|
|
|
|
error_failed_tests = 2
|
2021-02-02 11:06:54 +01:00
|
|
|
fpref = &pref.Preferences{
|
|
|
|
is_fmt: true
|
|
|
|
}
|
2020-05-18 18:43:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
fn test_vlib_fmt() {
|
2020-08-25 18:09:47 +02:00
|
|
|
$if !vfmt_everything ? {
|
2020-05-18 18:43:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt_message := "checking that all V source files are vfmt'ed"
|
|
|
|
eprintln(term.header(fmt_message, '-'))
|
|
|
|
vexe := os.getenv('VEXE')
|
|
|
|
if vexe.len == 0 || !os.exists(vexe) {
|
|
|
|
eprintln('VEXE must be set')
|
2021-01-25 12:08:43 +01:00
|
|
|
exit(error_missing_vexe)
|
2020-05-18 18:43:04 +02:00
|
|
|
}
|
|
|
|
vroot := os.dir(vexe)
|
|
|
|
tmpfolder := os.temp_dir()
|
2021-06-22 17:52:34 +02:00
|
|
|
diff_cmd := diff.find_working_diff_command() or { '' }
|
2020-05-18 18:43:04 +02:00
|
|
|
mut fmt_bench := benchmark.new_benchmark()
|
2021-08-28 11:51:37 +02:00
|
|
|
os.chdir(vroot) or {}
|
2020-08-25 18:09:47 +02:00
|
|
|
input_files := os.walk_ext('vlib/v/', '.v').filter(!it.contains('/tests/'))
|
2020-05-18 18:43:04 +02:00
|
|
|
fmt_bench.set_total_expected_steps(input_files.len)
|
|
|
|
for istep, ipath in input_files {
|
|
|
|
fmt_bench.cstep = istep
|
|
|
|
fmt_bench.step()
|
|
|
|
ifilename := os.file_name(ipath)
|
|
|
|
opath := ipath
|
|
|
|
expected_ocontent := os.read_file(opath) or {
|
|
|
|
fmt_bench.fail()
|
2020-10-15 22:12:59 +02:00
|
|
|
eprintln(fmt_bench.step_message_fail('cannot read from $opath'))
|
2020-05-18 18:43:04 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-04-02 00:57:09 +02:00
|
|
|
table := ast.new_table()
|
2021-07-18 14:29:34 +02:00
|
|
|
file_ast := parser.parse_file(ipath, table, .parse_comments, fpref)
|
2021-02-02 11:06:54 +01:00
|
|
|
result_ocontent := fmt.fmt(file_ast, table, fpref, false)
|
2020-05-18 18:43:04 +02:00
|
|
|
if expected_ocontent != result_ocontent {
|
|
|
|
fmt_bench.fail()
|
2020-10-15 22:12:59 +02:00
|
|
|
eprintln(fmt_bench.step_message_fail('file $ipath after formatting, does not look as expected.'))
|
2020-05-18 18:43:04 +02:00
|
|
|
if diff_cmd == '' {
|
|
|
|
eprintln('>> sorry, but no working "diff" CLI command can be found')
|
|
|
|
continue
|
|
|
|
}
|
2020-10-15 22:12:59 +02:00
|
|
|
vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_$ifilename')
|
2021-03-01 00:18:14 +01:00
|
|
|
os.write_file(vfmt_result_file, result_ocontent) or { panic(err) }
|
2021-06-22 17:52:34 +02:00
|
|
|
eprintln(diff.color_compare_files(diff_cmd, opath, vfmt_result_file))
|
2020-05-18 18:43:04 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt_bench.ok()
|
2020-10-15 22:12:59 +02:00
|
|
|
eprintln(fmt_bench.step_message_ok('$ipath'))
|
2020-05-18 18:43:04 +02:00
|
|
|
}
|
|
|
|
fmt_bench.stop()
|
|
|
|
eprintln(term.h_divider('-'))
|
|
|
|
eprintln(fmt_bench.total_message(fmt_message))
|
|
|
|
if fmt_bench.nfail > 0 {
|
2021-01-25 12:08:43 +01:00
|
|
|
exit(error_failed_tests)
|
2020-05-18 18:43:04 +02:00
|
|
|
}
|
|
|
|
}
|