Skip to content

Commit

Permalink
vdoc: fix out path if -o is not passed but input has a path, add te…
Browse files Browse the repository at this point in the history
…st (#21246)
  • Loading branch information
ttytm authored Apr 11, 2024
1 parent 6dc417e commit 39de87c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
38 changes: 38 additions & 0 deletions cmd/tools/vdoc/tests/vdoc_file_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ fn test_run_examples_bad() {
assert res.output.contains('Example: assert 5 * 5 == 77'), res.output
}

fn test_out_path() {
// Work around CI issues covering v doc generation for relative input paths in tmp dir.
// Instead just generate documentation in the v source dir.
if os.getenv('CI') == 'true' {
mod := 'coroutines'
// Default output path.
os.execute_opt('${vexe} doc -f html -m vlib/${mod}')!
assert os.exists(os.join_path(vroot, 'vlib', 'coroutines', '_docs', '${mod}.html'))

// Custom out path (no `_docs` subdir).
out_dir := os.join_path(vroot, 'vlib', 'coroutines', 'docs')
os.execute_opt('${vexe} doc -f html -m -o ${out_dir} ${mod}')!
assert os.exists(os.join_path(out_dir, '${mod}.html'))

return
}

test_path := os.join_path(os.vtmp_dir(), 'vdoc_test_${rand.ulid()}')
os.mkdir_all(test_path)!
os.chdir(test_path)!
defer {
os.rmdir_all(test_path) or {}
}

// Copy a *small* vlib module for the test.
mod := 'coroutines'
os.cp_all(os.join_path(vroot, 'vlib', mod), os.join_path(test_path, mod), true) or {}

// Relative input with default output path.
os.execute_opt('${vexe} doc -f html -m ${mod}')!
assert os.exists(os.join_path(test_path, mod, '_docs', '${mod}.html'))

// Custom out path (no `_docs` subdir).
out_dir := os.join_path(os.vtmp_dir(), 'docs_test')
os.execute_opt('${vexe} doc -f html -m -o ${out_dir} ${mod}')!
assert os.exists(os.join_path(out_dir, '${mod}.html'))
}

fn get_main_files_in_dir(dir string) []string {
mut mfiles := os.walk_ext(dir, '.v')
mfiles.sort()
Expand Down
6 changes: 5 additions & 1 deletion cmd/tools/vdoc/vdoc.v
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,11 @@ fn (mut vd VDoc) generate_docs_from_file() {
out.path = os.real_path('.')
}
if cfg.is_multi {
out.path = if out.path == '.' { '_docs' } else { out.path }
out.path = if cfg.input_path == out.path {
os.join_path(out.path, '_docs')
} else {
out.path
}
if !os.exists(out.path) {
os.mkdir(out.path) or { panic(err) }
} else {
Expand Down

0 comments on commit 39de87c

Please sign in to comment.