v doc: fix an infinite recursion in `v doc .` executed in /tmp. Add tests for v.doc.get_parent_mod/1 .

pull/11039/head
Delyan Angelov 2021-08-03 09:03:15 +03:00
parent fd58e9f819
commit 0ebad47d2a
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 59 additions and 9 deletions

View File

@ -0,0 +1,47 @@
module doc
import os
fn testsuite_begin() {
os.chdir(@VMODROOT)
eprintln('>> @VMODROOT: ' + @VMODROOT)
}
fn test_get_parent_mod_on_root_folder() {
// TODO: add an equivalent windows check for c:\
$if !windows {
assert '---' == get_parent_mod('/') or {
assert err.msg == 'root folder reached'
'---'
}
}
}
fn test_get_parent_mod_current_folder() {
// TODO: this should may be return '' reliably on windows too:
// assert '' == get_parent_mod('.') or {
// assert err.msg == 'No V files found.'
// '---'
// }
}
fn test_get_parent_mod_on_temp_dir() ? {
// TODO: fix this on windows
$if !windows {
assert get_parent_mod(os.temp_dir()) ? == ''
}
}
fn test_get_parent_mod_normal_cases() ? {
assert '---' == get_parent_mod(os.join_path(@VMODROOT, 'vlib/v')) or {
assert err.msg == 'No V files found.'
'---'
}
// TODO: WTF?
// assert get_parent_mod(os.join_path(@VMODROOT, 'vlib', 'v', 'doc', 'doc.v')) ? == 'v.v.doc'
assert get_parent_mod(os.join_path(@VMODROOT, 'vlib', 'v', 'doc')) ? == 'v'
assert get_parent_mod(os.join_path(@VMODROOT, 'vlib', 'os', 'os.v')) ? == 'os'
assert get_parent_mod(os.join_path(@VMODROOT, 'cmd')) ? == ''
assert get_parent_mod(os.join_path(@VMODROOT, 'cmd', 'tools', 'modules', 'testing',
'common.v')) ? == 'testing'
}

View File

@ -11,16 +11,19 @@ import v.pref
// For example, given something like /languages/v/vlib/x/websocket/tests/autobahn
// it returns `x.websocket.tests`, because /languages/v/ has v.mod file in it.
// NB: calling this is expensive, so keep the result, instead of recomputing it.
// TODO: turn this to a Doc method, so that the new_vdoc_preferences call here can
// be removed.
fn get_parent_mod(input_dir string) ?string {
$if windows {
// windows root path is C: or D:
if input_dir.len <= 2 {
return error('root folder reached')
}
} $else {
if input_dir.len == 0 {
return error('root folder reached')
}
// windows root path is C: or D:
if input_dir.len == 2 && input_dir[1] == `:` {
return error('root folder reached')
}
// unix systems have / at the top:
if input_dir == '/' {
return error('root folder reached')
}
if input_dir == '' {
return error('no input folder')
}
base_dir := os.dir(input_dir)
input_dir_name := os.file_name(base_dir)