tools: cleanup vcreate tests (#19666)

This commit is contained in:
Turiiya 2023-10-29 03:36:16 +01:00 committed by GitHub
parent fd04c9d9aa
commit b9d0aedb09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 38 deletions

View File

@ -1,19 +1,19 @@
import os import os
import v.vmod import v.vmod
// Note: the following uses `test_vcreate` and NOT `vcreate_input_test` deliberately,
// to both avoid confusions with the name of the current test itself, and to
// avoid clashes with the postfix `_test.v`, that V uses for its own test files.
const ( const (
// Expect has to be installed for the test. // Expect has to be installed for the test.
expect_exe = os.quoted_path(os.find_abs_path_of_executable('expect') or { expect_exe = os.quoted_path(os.find_abs_path_of_executable('expect') or {
eprintln('skipping test, since expect is missing') eprintln('skipping test, since expect is missing')
exit(0) exit(0)
}) })
// Directory where the Expect scripts will create projects.
test_module_path = os.join_path(os.vtmp_dir(), 'v', 'test_vcreate_input')
// Directory that contains the Expect scripts used in the test. // Directory that contains the Expect scripts used in the test.
expect_tests_path = os.join_path(@VMODROOT, 'cmd', 'tools', 'vcreate', 'tests') expect_tests_path = os.join_path(@VMODROOT, 'cmd', 'tools', 'vcreate', 'tests')
// Running tests appends a tsession path to VTMP, which is automatically cleaned up after the test.
// The following will result in e.g. `$VTMP/tsession_7fe8e93bd740_1612958707536/test_vcreate_input/`.
// Note: The following uses `test_vcreate_input` deliberately and NOT `vcreate_input_test`.
// This avoids clashes with the `_test` postfix, which V also uses for test file binaries.
test_module_path = os.join_path(os.vtmp_dir(), 'test_vcreate_input')
) )
fn testsuite_begin() { fn testsuite_begin() {
@ -22,6 +22,10 @@ fn testsuite_begin() {
dump(expect_tests_path) dump(expect_tests_path)
} }
fn testsuite_end() {
os.rmdir_all(test_module_path) or {}
}
fn prepare_test_path() ! { fn prepare_test_path() ! {
os.rmdir_all(test_module_path) or {} os.rmdir_all(test_module_path) or {}
os.mkdir_all(test_module_path) or {} os.mkdir_all(test_module_path) or {}
@ -36,10 +40,7 @@ fn test_new_with_no_arg_input() {
assert false, res.output assert false, res.output
} }
// Assert mod data set in `new_no_arg.expect`. // Assert mod data set in `new_no_arg.expect`.
mod := vmod.decode(os.read_file(os.join_path(test_module_path, project_name, 'v.mod')) or { mod := vmod.from_file(os.join_path(test_module_path, project_name, 'v.mod')) or {
assert false, 'Failed reading v.mod of ${project_name}'
return
}) or {
assert false, err.str() assert false, err.str()
return return
} }
@ -57,10 +58,7 @@ fn test_new_with_name_arg_input() {
assert false, res.output assert false, res.output
} }
// Assert mod data set in `new_with_name_arg.expect`. // Assert mod data set in `new_with_name_arg.expect`.
mod := vmod.decode(os.read_file(os.join_path(test_module_path, project_name, 'v.mod')) or { mod := vmod.from_file(os.join_path(test_module_path, project_name, 'v.mod')) or {
assert false, 'Failed reading v.mod of ${project_name}'
return
}) or {
assert false, err.str() assert false, err.str()
return return
} }
@ -79,10 +77,7 @@ fn test_new_with_model_arg_input() {
assert false, res.output assert false, res.output
} }
// Assert mod data set in `new_with_model_arg.expect`. // Assert mod data set in `new_with_model_arg.expect`.
mod := vmod.decode(os.read_file(os.join_path(test_module_path, project_name, 'v.mod')) or { mod := vmod.from_file(os.join_path(test_module_path, project_name, 'v.mod')) or {
assert false, 'Failed reading v.mod of ${project_name}'
return
}) or {
assert false, err.str() assert false, err.str()
return return
} }
@ -96,7 +91,7 @@ fn test_v_init_in_dir_with_invalid_mod_name() {
// A project with a directory name with hyphens, which is invalid for a module name. // A project with a directory name with hyphens, which is invalid for a module name.
dir_name_with_invalid_mod_name := 'my-proj' dir_name_with_invalid_mod_name := 'my-proj'
corrected_mod_name := 'my_proj' corrected_mod_name := 'my_proj'
proj_path := os.join_path(os.vtmp_dir(), 'v', dir_name_with_invalid_mod_name) proj_path := os.join_path(os.vtmp_dir(), dir_name_with_invalid_mod_name)
os.mkdir_all(proj_path) or {} os.mkdir_all(proj_path) or {}
os.chdir(proj_path)! os.chdir(proj_path)!
res := os.execute('${expect_exe} ${os.join_path(expect_tests_path, 'init_in_dir_with_invalid_mod_name.expect')} ${@VMODROOT} ${dir_name_with_invalid_mod_name} ${corrected_mod_name}') res := os.execute('${expect_exe} ${os.join_path(expect_tests_path, 'init_in_dir_with_invalid_mod_name.expect')} ${@VMODROOT} ${dir_name_with_invalid_mod_name} ${corrected_mod_name}')
@ -104,17 +99,9 @@ fn test_v_init_in_dir_with_invalid_mod_name() {
assert false, res.output assert false, res.output
} }
// Assert mod data set in `new_with_model_arg.expect`. // Assert mod data set in `new_with_model_arg.expect`.
mod := vmod.decode(os.read_file(os.join_path(proj_path, 'v.mod')) or { mod := vmod.from_file(os.join_path(proj_path, 'v.mod')) or {
assert false, 'Failed reading v.mod of ${proj_path}'
return
}) or {
assert false, err.str() assert false, err.str()
return return
} }
assert mod.name == corrected_mod_name assert mod.name == corrected_mod_name
os.rmdir_all(proj_path) or {}
}
fn testsuite_end() {
os.rmdir_all(test_module_path) or {}
} }

View File

@ -1,10 +1,17 @@
import os import os
import v.vmod
// Note: the following uses `test_vcreate` and NOT `vcreate_test` deliberately, const (
// to both avoid confusions with the name of the current test itself, and to test_project_dir_name = 'test_project'
// avoid clashes with the postfix `_test.v`, that V uses for its own test files. // Running tests appends a tsession path to VTMP, which is automatically cleaned up after the test.
const test_path = os.join_path(os.vtmp_dir(), 'v', 'test_vcreate') // The following will result in e.g. `$VTMP/tsession_7fe8e93bd740_1612958707536/test_project/`.
// Note: The following uses `test_vcreate` deliberately and NOT `vcreate_test`.
// This avoids clashes with the `_test` postfix, which V also uses for test file binaries.
test_path = os.join_path(os.vtmp_dir(), test_project_dir_name)
)
fn testsuite_end() {
os.rmdir_all(test_path) or {}
}
fn init_and_check() ! { fn init_and_check() ! {
os.chdir(test_path)! os.chdir(test_path)!
@ -35,7 +42,7 @@ fn init_and_check() ! {
assert os.read_file('v.mod')! == [ assert os.read_file('v.mod')! == [
'Module {', 'Module {',
" name: 'test_vcreate'", " name: '${test_project_dir_name}'",
" description: ''", " description: ''",
" version: ''", " version: ''",
" license: ''", " license: ''",
@ -47,7 +54,7 @@ fn init_and_check() ! {
assert os.read_file('.gitignore')! == [ assert os.read_file('.gitignore')! == [
'# Binaries for programs and plugins', '# Binaries for programs and plugins',
'main', 'main',
'test_vcreate', '${test_project_dir_name}',
'*.exe', '*.exe',
'*.exe~', '*.exe~',
'*.so', '*.so',
@ -139,7 +146,3 @@ indent_style = tab
assert os.read_file('.gitattributes')! == git_attributes_content assert os.read_file('.gitattributes')! == git_attributes_content
assert os.read_file('.editorconfig')! == editor_config_content assert os.read_file('.editorconfig')! == editor_config_content
} }
fn testsuite_end() {
os.rmdir_all(test_path) or {}
}