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

fmt: fail keep test when bin2v_keep preparation step fails (#9361)

This commit is contained in:
Lukas Neubert 2021-03-19 12:13:03 +01:00 committed by GitHub
parent 125236694f
commit 82c4daa4b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 17 deletions

View File

@ -77,7 +77,7 @@ fn (context Context) bname_and_bytes(file string) ?(string, []byte) {
fname := os.file_name(file) fname := os.file_name(file)
fname_escaped := fname.replace_each(['.', '_', '-', '_']) fname_escaped := fname.replace_each(['.', '_', '-', '_'])
byte_name := '$context.prefix$fname_escaped'.to_lower() byte_name := '$context.prefix$fname_escaped'.to_lower()
fbytes := os.read_bytes(file) or { return error('Error: $err') } fbytes := os.read_bytes(file) or { return error('Error: $err.msg') }
return byte_name, fbytes return byte_name, fbytes
} }
@ -108,7 +108,7 @@ fn main() {
exit(0) exit(0)
} }
files := fp.finalize() or { files := fp.finalize() or {
eprintln('Error: $err') eprintln('Error: $err.msg')
exit(1) exit(1)
} }
real_files := files.filter(it != 'bin2v') real_files := files.filter(it != 'bin2v')
@ -123,8 +123,8 @@ fn main() {
mut file_byte_map := map[string][]byte{} mut file_byte_map := map[string][]byte{}
for file in real_files { for file in real_files {
bname, fbytes := context.bname_and_bytes(file) or { bname, fbytes := context.bname_and_bytes(file) or {
eprintln(err) eprintln(err.msg)
continue exit(1)
} }
file_byte_map[bname] = fbytes file_byte_map[bname] = fbytes
} }

View File

@ -1,3 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import os import os
import term import term
import benchmark import benchmark
@ -16,12 +19,12 @@ const (
fpref = &pref.Preferences{ fpref = &pref.Preferences{
is_fmt: true is_fmt: true
} }
vexe = os.getenv('VEXE')
) )
fn test_fmt() { fn test_fmt() {
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, '-'))
vexe := os.getenv('VEXE')
if vexe.len == 0 || !os.exists(vexe) { if vexe.len == 0 || !os.exists(vexe) {
eprintln('VEXE must be set') eprintln('VEXE must be set')
exit(error_missing_vexe) exit(error_missing_vexe)
@ -32,16 +35,16 @@ fn test_fmt() {
tmpfolder := os.temp_dir() tmpfolder := os.temp_dir()
diff_cmd := util.find_working_diff_command() or { '' } diff_cmd := util.find_working_diff_command() or { '' }
mut fmt_bench := benchmark.new_benchmark() mut fmt_bench := benchmark.new_benchmark()
fill_bin2v_keep() or { eprintln('failed creating vbin2v_keep.vv: $err') }
keep_input_files := os.walk_ext('vlib/v/fmt/tests', '_keep.vv') keep_input_files := os.walk_ext('vlib/v/fmt/tests', '_keep.vv')
expected_input_files := os.walk_ext('vlib/v/fmt/tests', '_expected.vv') expected_input_files := os.walk_ext('vlib/v/fmt/tests', '_expected.vv')
mut input_files := []string{} mut input_files := []string{}
input_files << keep_input_files input_files << keep_input_files
input_files << expected_input_files input_files << expected_input_files
input_files = vtest.filter_vtest_only(input_files, basepath: vroot) input_files = vtest.filter_vtest_only(input_files, basepath: vroot)
fmt_bench.set_total_expected_steps(input_files.len) fmt_bench.set_total_expected_steps(input_files.len + 1)
prepare_bin2v_file(mut fmt_bench)
for istep, ipath in input_files { for istep, ipath in input_files {
fmt_bench.cstep = istep fmt_bench.cstep = istep + 1
fmt_bench.step() fmt_bench.step()
ifilename := os.file_name(ipath) ifilename := os.file_name(ipath)
vrelpath := ipath.replace(basepath, '') vrelpath := ipath.replace(basepath, '')
@ -67,14 +70,16 @@ fn test_fmt() {
continue continue
} }
vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_$ifilename') vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_$ifilename')
os.write_file(vfmt_result_file, result_ocontent) or { panic(err) } os.write_file(vfmt_result_file, result_ocontent) or { panic(err.msg) }
eprintln(util.color_compare_files(diff_cmd, opath, vfmt_result_file)) eprintln(util.color_compare_files(diff_cmd, opath, vfmt_result_file))
continue continue
} }
fmt_bench.ok() fmt_bench.ok()
eprintln(fmt_bench.step_message_ok('$vrelpath')) eprintln(fmt_bench.step_message_ok(vrelpath))
}
restore_bin2v_placeholder() or {
eprintln('failed restoring vbin2v_keep.vv placeholder: $err.msg')
} }
restore_bin2v_placeholder() or { eprintln('failed restoring vbin2v_keep.vv placeholder: $err') }
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))
@ -83,13 +88,26 @@ fn test_fmt() {
} }
} }
fn fill_bin2v_keep() ? { fn prepare_bin2v_file(mut fmt_bench benchmark.Benchmark) {
img0 := os.join_path('tutorials', 'img', 'hello.png') fmt_bench.cstep = 0
img1 := os.join_path('tutorials', 'img', 'time.png') fmt_bench.step()
write_bin2v_keep_content() or {
fmt_bench.fail()
eprintln(fmt_bench.step_message_fail('Failed preparing bin2v_keep.vv: $err.msg'))
return
}
fmt_bench.ok()
eprintln(fmt_bench.step_message_ok('Prepared bin2v_keep.vv'))
}
fn write_bin2v_keep_content() ? {
img0 := os.join_path('vlib', 'v', 'embed_file', 'v.png')
img1 := os.join_path('tutorials', 'building_a_simple_web_blog_with_vweb', 'img', 'time.png')
os.rm(b2v_keep_path) ? os.rm(b2v_keep_path) ?
res := os.execute('v bin2v -w $b2v_keep_path $img0 $img1') res := os.execute('$vexe bin2v -w $b2v_keep_path $img0 $img1')
if res.exit_code < 0 { if res.exit_code != 0 {
return error_with_code(res.output, res.exit_code) restore_bin2v_placeholder() or {}
return error_with_code(res.output.trim_space(), res.exit_code)
} }
} }

View File

@ -1,3 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import os import os
import term import term
import benchmark import benchmark

View File

@ -1,3 +1,6 @@
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import os import os
import term import term
import benchmark import benchmark