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_escaped := fname.replace_each(['.', '_', '-', '_'])
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
}
@ -108,7 +108,7 @@ fn main() {
exit(0)
}
files := fp.finalize() or {
eprintln('Error: $err')
eprintln('Error: $err.msg')
exit(1)
}
real_files := files.filter(it != 'bin2v')
@ -123,8 +123,8 @@ fn main() {
mut file_byte_map := map[string][]byte{}
for file in real_files {
bname, fbytes := context.bname_and_bytes(file) or {
eprintln(err)
continue
eprintln(err.msg)
exit(1)
}
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 term
import benchmark
@ -16,12 +19,12 @@ const (
fpref = &pref.Preferences{
is_fmt: true
}
vexe = os.getenv('VEXE')
)
fn test_fmt() {
fmt_message := 'checking that v fmt keeps already formatted files *unchanged*'
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)
@ -32,16 +35,16 @@ fn test_fmt() {
tmpfolder := os.temp_dir()
diff_cmd := util.find_working_diff_command() or { '' }
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')
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)
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 {
fmt_bench.cstep = istep
fmt_bench.cstep = istep + 1
fmt_bench.step()
ifilename := os.file_name(ipath)
vrelpath := ipath.replace(basepath, '')
@ -67,14 +70,16 @@ fn test_fmt() {
continue
}
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))
continue
}
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()
eprintln(term.h_divider('-'))
eprintln(fmt_bench.total_message(fmt_message))
@ -83,13 +88,26 @@ fn test_fmt() {
}
}
fn fill_bin2v_keep() ? {
img0 := os.join_path('tutorials', 'img', 'hello.png')
img1 := os.join_path('tutorials', 'img', 'time.png')
fn prepare_bin2v_file(mut fmt_bench benchmark.Benchmark) {
fmt_bench.cstep = 0
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) ?
res := os.execute('v bin2v -w $b2v_keep_path $img0 $img1')
if res.exit_code < 0 {
return error_with_code(res.output, res.exit_code)
res := os.execute('$vexe bin2v -w $b2v_keep_path $img0 $img1')
if res.exit_code != 0 {
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 term
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 term
import benchmark