diff --git a/cmd/tools/vcreate/tests/init_in_dir_with_invalid_mod_name.expect b/cmd/tools/vcreate/tests/init_in_dir_with_invalid_mod_name.expect new file mode 100755 index 0000000000..60851901f5 --- /dev/null +++ b/cmd/tools/vcreate/tests/init_in_dir_with_invalid_mod_name.expect @@ -0,0 +1,16 @@ +#!/usr/bin/env expect + +set timeout 3 + +# Pass v_root as arg, since we chdir into a temp directory during testing and create a project there. +set v_root [lindex $argv 0] +set project_dir_name [lindex $argv 1] +set corrected_mod_name [lindex $argv 2] + +spawn $v_root/v init + +expect "The directory name `$project_dir_name` is invalid as a module name. The module name in `v.mod` was set to `$corrected_mod_name`" {} timeout { exit 1 } +expect "Change the description of your project in `v.mod`" {} timeout { exit 1 } +expect "Complete!" {} timeout {} timeout { exit 1 } + +expect eof diff --git a/cmd/tools/vcreate/tests/new_with_model_arg.expect b/cmd/tools/vcreate/tests/new_with_model_arg.expect index 96d1280ae4..67c06c72ab 100755 --- a/cmd/tools/vcreate/tests/new_with_model_arg.expect +++ b/cmd/tools/vcreate/tests/new_with_model_arg.expect @@ -13,6 +13,6 @@ expect "Input your project description: " { send "My Awesome V Project.\r" } tim expect "Input your project version: (0.0.0) " { send "0.0.1\r" } timeout { exit 1 } expect "Input your project license: (MIT) " { send "\r" } timeout { exit 1 } expect "Initialising ..." {} timeout { exit 1 } -expect "Complete!" {} timeout { exit 1 } +expect "Complete!" {} timeout { exit 1 } expect eof diff --git a/cmd/tools/vcreate/tests/new_with_name_arg.expect b/cmd/tools/vcreate/tests/new_with_name_arg.expect index f033f04a46..09fa0cdec4 100755 --- a/cmd/tools/vcreate/tests/new_with_name_arg.expect +++ b/cmd/tools/vcreate/tests/new_with_name_arg.expect @@ -12,6 +12,6 @@ expect "Input your project description: " { send "\r" } timeout { exit 1 } expect "Input your project version: (0.0.0) " { send "\r" } timeout { exit 1 } expect "Input your project license: (MIT) " { send "\r" } timeout { exit 1 } expect "Initialising ..." {} timeout { exit 1 } -expect "Complete!" {} timeout { exit 1 } +expect "Complete!" {} timeout { exit 1 } expect eof diff --git a/cmd/tools/vcreate/tests/new_with_no_arg.expect b/cmd/tools/vcreate/tests/new_with_no_arg.expect index c975229ded..61c4b0f506 100755 --- a/cmd/tools/vcreate/tests/new_with_no_arg.expect +++ b/cmd/tools/vcreate/tests/new_with_no_arg.expect @@ -13,6 +13,6 @@ expect "Input your project description: " { send "My Awesome V Project.\r" } tim expect "Input your project version: (0.0.0) " { send "0.1.0\r" } timeout { exit 1 } expect "Input your project license: (MIT) " { send "GPL\r" } timeout { exit 1 } expect "Initialising ..." {} timeout { exit 1 } -expect "Complete!" {} timeout { exit 1 } +expect "Complete!" {} timeout { exit 1 } expect eof diff --git a/cmd/tools/vcreate/vcreate.v b/cmd/tools/vcreate/vcreate.v index f31c68ab3f..b324d78899 100644 --- a/cmd/tools/vcreate/vcreate.v +++ b/cmd/tools/vcreate/vcreate.v @@ -129,10 +129,15 @@ fn new_project(args []string) { fn init_project() { mut c := Create{} - c.name = check_name(os.file_name(os.getwd())) + dir_name := check_name(os.file_name(os.getwd())) if !os.exists('v.mod') { c.description = '' + mod_dir_has_hyphens := dir_name.contains('-') + c.name = if mod_dir_has_hyphens { dir_name.replace('-', '_') } else { dir_name } c.write_vmod(false) + if mod_dir_has_hyphens { + println('The directory name `${dir_name}` is invalid as a module name. The module name in `v.mod` was set to `${c.name}`') + } println('Change the description of your project in `v.mod`') } if !os.exists('src/main.v') { diff --git a/cmd/tools/vcreate/vcreate_input_test.v b/cmd/tools/vcreate/vcreate_input_test.v index 5be6fc3f61..23fd9e3bbb 100644 --- a/cmd/tools/vcreate/vcreate_input_test.v +++ b/cmd/tools/vcreate/vcreate_input_test.v @@ -6,10 +6,10 @@ import v.vmod // avoid clashes with the postfix `_test.v`, that V uses for its own test files. const ( // Expect has to be installed for the test. - expect_exe = 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') 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. @@ -31,8 +31,7 @@ fn prepare_test_path() ! { fn test_new_with_no_arg_input() { prepare_test_path()! project_name := 'my_project' - res := os.execute('${os.quoted_path(expect_exe)} ${os.join_path(expect_tests_path, - 'new_with_no_arg.expect')} ${@VMODROOT} ${project_name}') + res := os.execute('${expect_exe} ${os.join_path(expect_tests_path, 'new_with_no_arg.expect')} ${@VMODROOT} ${project_name}') if res.exit_code != 0 { assert false, res.output } @@ -53,8 +52,7 @@ fn test_new_with_no_arg_input() { fn test_new_with_name_arg_input() { prepare_test_path()! project_name := 'my_other_project' - res := os.execute('${os.quoted_path(expect_exe)} ${os.join_path(expect_tests_path, - 'new_with_name_arg.expect')} ${@VMODROOT} ${project_name}') + res := os.execute('${expect_exe} ${os.join_path(expect_tests_path, 'new_with_name_arg.expect')} ${@VMODROOT} ${project_name}') if res.exit_code != 0 { assert false, res.output } @@ -76,8 +74,7 @@ fn test_new_with_model_arg_input() { prepare_test_path()! project_name := 'my_lib' model := 'lib' - res := os.execute('${os.quoted_path(expect_exe)} ${os.join_path(expect_tests_path, - 'new_with_model_arg.expect')} ${@VMODROOT} ${project_name} ${model}') + res := os.execute('${expect_exe} ${os.join_path(expect_tests_path, 'new_with_model_arg.expect')} ${@VMODROOT} ${project_name} ${model}') if res.exit_code != 0 { assert false, res.output } @@ -95,6 +92,29 @@ fn test_new_with_model_arg_input() { assert mod.license == 'MIT' } +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. + dir_name_with_invalid_mod_name := 'my-proj' + corrected_mod_name := 'my_proj' + proj_path := os.join_path(os.vtmp_dir(), 'v', dir_name_with_invalid_mod_name) + os.mkdir_all(proj_path) or {} + 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}') + if res.exit_code != 0 { + assert false, res.output + } + // Assert mod data set in `new_with_model_arg.expect`. + mod := vmod.decode(os.read_file(os.join_path(proj_path, 'v.mod')) or { + assert false, 'Failed reading v.mod of ${proj_path}' + return + }) or { + assert false, err.str() + return + } + assert mod.name == corrected_mod_name + os.rmdir_all(proj_path) or {} +} + fn testsuite_end() { os.rmdir_all(test_module_path) or {} } diff --git a/cmd/tools/vcreate/vcreate_test.v b/cmd/tools/vcreate/vcreate_test.v index dc3e3557e4..132cd38ae3 100644 --- a/cmd/tools/vcreate/vcreate_test.v +++ b/cmd/tools/vcreate/vcreate_test.v @@ -1,4 +1,5 @@ import os +import v.vmod // Note: the following uses `test_vcreate` and NOT `vcreate_test` deliberately, // to both avoid confusions with the name of the current test itself, and to