Skip to content

Commit

Permalink
vcreate: add tests for module creation with v new (#19519)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored Oct 9, 2023
1 parent 04c8d45 commit 4dee1e0
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/tools/vcreate/tests/new_with_model_arg.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/expect

set timeout 1

# 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_name [lindex $argv 1]
set model [lindex $argv 2]

spawn $v_root/v new $project_name $model

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 eof
17 changes: 17 additions & 0 deletions cmd/tools/vcreate/tests/new_with_name_arg.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/expect

set timeout 1

# 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_name [lindex $argv 1]

spawn $v_root/v new $project_name

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 eof
18 changes: 18 additions & 0 deletions cmd/tools/vcreate/tests/new_with_no_arg.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/expect

set timeout 1

# 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_name [lindex $argv 1]

spawn $v_root/v new

expect "Input your project name: " { send "$project_name\r" } timeout { exit 1 }
expect "Input your project description: " { send "My Awesome V Project.\r" } timeout { exit 1 }
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 eof
91 changes: 91 additions & 0 deletions cmd/tools/vcreate/vcreate_input_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import os
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 (
// Expect has to be installed for the test.
expect_exe = 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.
expect_tests_path = os.join_path(@VMODROOT, 'cmd', 'tools', 'vcreate', 'tests')
)

fn prepare_test_path() ! {
os.rmdir_all(test_module_path) or {}
os.mkdir_all(test_module_path) or {}
os.chdir(test_module_path)!
}

fn test_new_with_no_arg_input() {
prepare_test_path()!
project_name := 'my_project'
res := os.execute('${os.join_path(expect_tests_path, 'new_with_no_arg.expect')} ${@VMODROOT} ${project_name}')
if res.exit_code != 0 {
assert false, res.output
}
// 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 {
assert false, 'Failed reading v.mod of ${project_name}'
return
}) or {
assert false, err.str()
return
}
assert mod.name == project_name
assert mod.description == 'My Awesome V Project.'
assert mod.version == '0.1.0'
assert mod.license == 'GPL'
}

fn test_new_with_name_arg_input() {
prepare_test_path()!
project_name := 'my_other_project'
res := os.execute('${os.join_path(expect_tests_path, 'new_with_name_arg.expect')} ${@VMODROOT} ${project_name}')
if res.exit_code != 0 {
assert false, res.output
}
// 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 {
assert false, 'Failed reading v.mod of ${project_name}'
return
}) or {
assert false, err.str()
return
}
assert mod.name == project_name
assert mod.description == ''
assert mod.version == '0.0.0'
assert mod.license == 'MIT'
}

fn test_new_with_model_arg_input() {
prepare_test_path()!
project_name := 'my_lib'
model := 'lib'
res := os.execute('${os.join_path(expect_tests_path, 'new_with_model_arg.expect')} ${@VMODROOT} ${project_name} ${model}')
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(test_module_path, project_name, 'v.mod')) or {
assert false, 'Failed reading v.mod of ${project_name}'
return
}) or {
assert false, err.str()
return
}
assert mod.name == project_name
assert mod.description == model
assert mod.version == '0.0.1'
assert mod.license == 'MIT'
}

fn testsuite_end() {
os.rmdir_all(test_module_path) or {}
}

0 comments on commit 4dee1e0

Please sign in to comment.