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

vvet: move to own subdir, prepare richer suggestions (#7989)

This commit is contained in:
Larpon
2021-01-09 15:11:49 +01:00
committed by GitHub
parent 7545ed4121
commit 8f315d226b
20 changed files with 76 additions and 30 deletions

View File

@@ -0,0 +1,2 @@
cmd/tools/vvet/tests/array_init_one_val.vv:2: Use `var == value` instead of `var in [value]`
NB: You can run `v fmt -w file.v` to fix these automatically

View File

@@ -0,0 +1,5 @@
fn main() {
if 1 in [1] {
println('hello world')
}
}

View File

@@ -0,0 +1,2 @@
cmd/tools/vvet/tests/indent_with_space.vv:2: Looks like you are using spaces for indentation.
NB: You can run `v fmt -w file.v` to fix these automatically

View File

@@ -0,0 +1,3 @@
fn main() {
_ = 1 == 2
}

View File

@@ -0,0 +1,2 @@
cmd/tools/vvet/tests/parens_space_a.vv:1: Looks like you are adding a space after `(`
NB: You can run `v fmt -w file.v` to fix these automatically

View File

@@ -0,0 +1,4 @@
fn main() {
_ = 1 + ( 1 + 2)
}

View File

@@ -0,0 +1,2 @@
cmd/tools/vvet/tests/parens_space_b.vv:1: Looks like you are adding a space before `)`
NB: You can run `v fmt -w file.v` to fix these automatically

View File

@@ -0,0 +1,4 @@
fn main() {
_ = 1 + (1 + 2 )
}

56
cmd/tools/vvet/vet_test.v Normal file
View File

@@ -0,0 +1,56 @@
import os
import term
import v.util.vtest
fn test_vet() {
vexe := os.getenv('VEXE')
vroot := os.dir(vexe)
os.chdir(vroot)
test_dir := 'cmd/tools/vvet/tests'
tests := get_tests_in_dir(test_dir)
fails := check_path(vexe, test_dir, tests)
assert fails == 0
}
fn get_tests_in_dir(dir string) []string {
files := os.ls(dir) or { panic(err) }
mut tests := files.filter(it.ends_with('.vv'))
tests.sort()
return tests
}
fn check_path(vexe string, dir string, tests []string) int {
mut nb_fail := 0
paths := vtest.filter_vtest_only(tests, basepath: dir)
for path in paths {
program := path
print(path + ' ')
res := os.exec('$vexe vet $program') or { panic(err) }
mut expected := os.read_file(program.replace('.vv', '') + '.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')
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
}

81
cmd/tools/vvet/vvet.v Normal file
View File

@@ -0,0 +1,81 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module main
import v.vet
import v.pref
import v.parser
import v.util
import v.table
import os
import os.cmdline
struct VetOptions {
is_verbose bool
mut:
errors []vet.Error
}
fn (vet_options &VetOptions) vprintln(s string) {
if !vet_options.is_verbose {
return
}
println(s)
}
fn main() {
args := util.join_env_vflags_and_os_args()
paths := cmdline.only_non_options(cmdline.options_after(args, ['vet']))
mut vet_options := VetOptions{
is_verbose: '-verbose' in args || '-v' in args
}
for path in paths {
if !os.exists(path) {
eprintln('File/folder $path does not exist')
continue
}
if path.ends_with('_test.v') ||
(path.contains('/tests/') && !path.contains('cmd/tools/vvet/tests/')) {
eprintln('skipping $path')
continue
}
if path.ends_with('.v') || path.ends_with('.vv') {
vet_options.vet_file(path)
} else if os.is_dir(path) {
vet_options.vprintln("vetting folder '$path'...")
vfiles := os.walk_ext(path, '.v')
vvfiles := os.walk_ext(path, '.vv')
mut files := []string{}
files << vfiles
files << vvfiles
for file in files {
if file.ends_with('_test.v') || file.contains('/tests/') { // TODO copy pasta
continue
}
vet_options.vet_file(file)
}
}
}
if vet_options.errors.len > 0 {
for err in vet_options.errors.filter(it.kind == .error) {
eprintln('$err.file_path:$err.pos.line_nr: $err.message')
}
eprintln('NB: You can run `v fmt -w file.v` to fix these automatically')
/*
for err in vet_options.errors.filter(it.kind == .warning) {
eprintln('$err.file_path:$err.pos.line_nr: err.message')
}
*/
exit(1)
}
}
fn (mut vet_options VetOptions) vet_file(path string) {
mut prefs := pref.new_preferences()
prefs.is_vet = true
table := table.new_table()
vet_options.vprintln("vetting file '$path'...")
file_ast, errors := parser.parse_vet_file(path, table, prefs)
// Transfer errors from scanner and parser
vet_options.errors << errors
}