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

testing: allow tests to import the same module with no custom options

This commit is contained in:
Delyan Angelov
2019-12-13 18:28:39 +02:00
committed by Alexander Medvednikov
parent ec36755407
commit fc64238a39
4 changed files with 66 additions and 21 deletions

View File

@ -150,27 +150,45 @@ pub fn(graph &DepGraph) imports() []string {
// 'strings' => 'VROOT/vlib/strings'
// 'installed_mod' => '~/.vmodules/installed_mod'
// 'local_mod' => '/path/to/current/dir/local_mod'
fn (v &V) find_module_path(mod string) ?string {
fn (v mut V) set_module_lookup_paths(){
mlookup_path := if v.pref.vpath.len>0{ v.pref.vpath }else{ v_modules_path }
// Module search order:
// 0) V test files are very commonly located right inside the folder of the
// module, which they test. Adding the parent folder of the module folder
// with the _test.v files, *guarantees* that the tested module can be found
// without needing to set custom options/flags.
// 1) search in the *same* directory, as the compiled final v program source
// (i.e. the . in `v .` or file.v in `v file.v`)
// 2) search in the modules/ in the same directory.
// 3) search in vlib/
// 4.1) search in -vpath (if given)
// 4.2) search in ~/.vmodules/ (i.e. modules installed with vpm) (no -vpath)
modules_lookup_path := if v.pref.vpath.len > 0 { v.pref.vpath } else { v_modules_path }
mod_path := v.module_path(mod)
mut tried_paths := []string
tried_paths << filepath.join(v.compiled_dir, mod_path)
tried_paths << filepath.join(v.compiled_dir, 'modules', mod_path)
tried_paths << filepath.join(v.pref.vlib_path, mod_path)
tried_paths << filepath.join(modules_lookup_path, mod_path)
if v.pref.user_mod_path.len > 0 {
tried_paths << filepath.join(v.pref.user_mod_path, mod_path)
v.module_lookup_paths = []
if v.pref.is_test {
v.module_lookup_paths << os.basedir(v.compiled_dir) // pdir of _test.v
}
for try_path in tried_paths {
v.module_lookup_paths << v.compiled_dir
v.module_lookup_paths << filepath.join(v.compiled_dir, 'modules')
v.module_lookup_paths << v.pref.vlib_path
v.module_lookup_paths << mlookup_path
if v.pref.user_mod_path.len > 0 {
v.module_lookup_paths << v.pref.user_mod_path
}
if v.pref.is_verbose {
v.log('v.module_lookup_paths: $v.module_lookup_paths')
}
}
fn (v &V) find_module_path(mod string) ?string {
mod_path := v.module_path(mod)
for lookup_path in v.module_lookup_paths {
try_path := filepath.join(lookup_path, mod_path)
if v.pref.is_verbose { println(' >> trying to find $mod in $try_path ...') }
if os.is_dir(try_path) {
if os.is_dir(try_path) {
if v.pref.is_verbose { println(' << found $try_path .') }
return try_path
}
}